Skip to content

Commit d26cf12

Browse files
authored
Merge pull request #2688 from geoffw0/move-taint-test
C++: Add the security taint test (previously internal).
2 parents b3eada8 + af903fc commit d26cf12

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:8:24:8:25 | s1 | |
2+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:23:14:23:19 | envStr | |
3+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:23:23:23:28 | call to getenv | |
4+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:23:23:23:40 | (const char *)... | |
5+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:25:6:25:29 | ! ... | |
6+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:25:7:25:12 | call to strcmp | |
7+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:25:7:25:29 | (bool)... | |
8+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:25:14:25:19 | envStr | |
9+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:29:6:29:28 | ! ... | |
10+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:29:7:29:12 | call to strcmp | |
11+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:29:7:29:28 | (bool)... | |
12+
| test.cpp:23:23:23:28 | call to getenv | test.cpp:29:14:29:19 | envStr | |
13+
| test.cpp:38:23:38:28 | call to getenv | test.cpp:8:24:8:25 | s1 | |
14+
| test.cpp:38:23:38:28 | call to getenv | test.cpp:38:14:38:19 | envStr | |
15+
| test.cpp:38:23:38:28 | call to getenv | test.cpp:38:23:38:28 | call to getenv | |
16+
| test.cpp:38:23:38:28 | call to getenv | test.cpp:38:23:38:40 | (const char *)... | |
17+
| test.cpp:38:23:38:28 | call to getenv | test.cpp:40:14:40:19 | envStr | |
18+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:8:24:8:25 | s1 | envStrGlobal |
19+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:45:13:45:24 | envStrGlobal | |
20+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:45:13:45:24 | envStrGlobal | envStrGlobal |
21+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:49:14:49:19 | envStr | |
22+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:49:23:49:28 | call to getenv | |
23+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:49:23:49:40 | (const char *)... | |
24+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:50:15:50:24 | envStr_ptr | |
25+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:50:15:50:24 | envStr_ptr | envStrGlobal |
26+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:50:28:50:40 | & ... | envStrGlobal |
27+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:50:29:50:40 | envStrGlobal | envStrGlobal |
28+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:52:2:52:12 | * ... | |
29+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:52:3:52:12 | envStr_ptr | |
30+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:52:16:52:21 | envStr | |
31+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:54:6:54:35 | ! ... | envStrGlobal |
32+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:54:7:54:12 | call to strcmp | envStrGlobal |
33+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:54:7:54:35 | (bool)... | envStrGlobal |
34+
| test.cpp:49:23:49:28 | call to getenv | test.cpp:54:14:54:25 | envStrGlobal | envStrGlobal |
35+
| test.cpp:60:29:60:34 | call to getenv | test.cpp:10:27:10:27 | s | |
36+
| test.cpp:60:29:60:34 | call to getenv | test.cpp:60:18:60:25 | userName | |
37+
| test.cpp:60:29:60:34 | call to getenv | test.cpp:60:29:60:34 | call to getenv | |
38+
| test.cpp:60:29:60:34 | call to getenv | test.cpp:60:29:60:47 | (const char *)... | |
39+
| test.cpp:60:29:60:34 | call to getenv | test.cpp:64:25:64:32 | userName | |
40+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:11:20:11:21 | s1 | |
41+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:11:36:11:37 | s2 | |
42+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:67:7:67:13 | copying | |
43+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:68:17:68:24 | userName | |
44+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:68:28:68:33 | call to getenv | |
45+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:68:28:68:46 | (const char *)... | |
46+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:69:10:69:13 | copy | |
47+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:70:5:70:10 | call to strcpy | |
48+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:70:12:70:15 | copy | |
49+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:70:18:70:25 | userName | |
50+
| test.cpp:68:28:68:33 | call to getenv | test.cpp:71:12:71:15 | copy | |
51+
| test.cpp:75:20:75:25 | call to getenv | test.cpp:15:22:15:25 | nptr | |
52+
| test.cpp:75:20:75:25 | call to getenv | test.cpp:75:15:75:18 | call to atoi | |
53+
| test.cpp:75:20:75:25 | call to getenv | test.cpp:75:20:75:25 | call to getenv | |
54+
| test.cpp:75:20:75:25 | call to getenv | test.cpp:75:20:75:45 | (const char *)... | |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import semmle.code.cpp.security.TaintTracking
2+
3+
from Expr source, Element tainted, string globalVar
4+
where
5+
taintedIncludingGlobalVars(source, tainted, globalVar) and
6+
not tainted.getLocation().getFile().getExtension() = "h"
7+
select source, tainted, globalVar
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Test for the general-purpose taint-tracking
2+
// mechanism that is used by several of the security queries.
3+
4+
///// Library functions //////
5+
6+
typedef unsigned long size_t;
7+
8+
int strcmp(const char *s1, const char *s2);
9+
char *getenv(const char *name);
10+
size_t strlen(const char *s);
11+
char *strcpy(char *s1, const char *s2);
12+
13+
void *malloc(size_t size);
14+
15+
int atoi(const char *nptr);
16+
17+
//// Test code /////
18+
19+
bool isAdmin = false;
20+
21+
void test1()
22+
{
23+
const char *envStr = getenv("USERINFO");
24+
25+
if (!strcmp(envStr, "admin")) {
26+
isAdmin = true;
27+
}
28+
29+
if (!strcmp(envStr, "none")) {
30+
isAdmin = false;
31+
}
32+
}
33+
34+
extern const char *specialUser;
35+
36+
void test2()
37+
{
38+
const char *envStr = getenv("USERINFO");
39+
40+
if (!strcmp(envStr, specialUser)) {
41+
isAdmin = true;
42+
}
43+
}
44+
45+
const char *envStrGlobal;
46+
47+
void test3()
48+
{
49+
const char *envStr = getenv("USERINFO");
50+
const char **envStr_ptr = &envStrGlobal;
51+
52+
*envStr_ptr = envStr;
53+
54+
if (!strcmp(envStrGlobal, "admin")) {
55+
isAdmin = true;
56+
}
57+
}
58+
59+
void bugWithBinop() {
60+
const char *userName = getenv("USER_NAME");
61+
62+
// The following is tainted, but should not cause
63+
// the whole program to be considered tainted.
64+
int bytes = strlen(userName) + 1;
65+
}
66+
67+
char* copying() {
68+
const char *userName = getenv("USER_NAME");
69+
char copy[1024];
70+
strcpy(copy, userName);
71+
return copy; // copy should be tainted
72+
}
73+
74+
void guard() {
75+
int len = atoi(getenv("FOOBAZ_BRANCHING"));
76+
if (len > 1000) return;
77+
char **node = (char **) malloc(len * sizeof(char *));
78+
}

0 commit comments

Comments
 (0)