Skip to content

Commit 5490809

Browse files
committed
C++: Expand tests.
1 parent 55e69d4 commit 5490809

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
| tests2.cpp:27:12:27:12 | call to operator<< | This operation exposes system data from $@. | tests2.cpp:27:15:27:20 | call to getenv | call to getenv |
2-
| tests2.cpp:28:25:28:25 | call to operator<< | This operation exposes system data from $@. | tests2.cpp:28:28:28:33 | call to getenv | call to getenv |
1+
| tests2.cpp:58:12:58:12 | call to operator<< | This operation exposes system data from $@. | tests2.cpp:58:15:58:20 | call to getenv | call to getenv |
2+
| tests2.cpp:59:25:59:25 | call to operator<< | This operation exposes system data from $@. | tests2.cpp:59:28:59:33 | call to getenv | call to getenv |
3+
| tests2.cpp:63:2:63:5 | call to send | This operation exposes system data from $@. | tests2.cpp:63:13:63:18 | call to getenv | call to getenv |
4+
| tests2.cpp:64:2:64:5 | call to send | This operation exposes system data from $@. | tests2.cpp:64:13:64:18 | call to getenv | call to getenv |
5+
| tests2.cpp:65:2:65:5 | call to send | This operation exposes system data from $@. | tests2.cpp:65:13:65:18 | call to getenv | call to getenv |
6+
| tests2.cpp:66:2:66:5 | call to send | This operation exposes system data from $@. | tests2.cpp:66:13:66:18 | call to getenv | call to getenv |
7+
| tests2.cpp:78:3:78:6 | call to send | This operation exposes system data from $@. | tests2.cpp:78:14:78:34 | call to mysql_get_client_info | call to mysql_get_client_info |
8+
| tests2.cpp:80:3:80:6 | call to send | This operation exposes system data from $@. | tests2.cpp:50:23:50:43 | call to mysql_get_client_info | call to mysql_get_client_info |
9+
| tests2.cpp:91:3:91:6 | call to send | This operation exposes system data from $@. | tests2.cpp:89:3:89:20 | call to mysql_real_connect | call to mysql_real_connect |
10+
| tests2.cpp:100:3:100:6 | call to send | This operation exposes system data from $@. | tests2.cpp:99:8:99:15 | call to getpwuid | call to getpwuid |
11+
| tests2.cpp:109:3:109:6 | call to send | This operation exposes system data from $@. | tests2.cpp:107:12:107:17 | call to getenv | call to getenv |
12+
| tests2.cpp:110:3:110:6 | call to send | This operation exposes system data from $@. | tests2.cpp:107:12:107:17 | call to getenv | call to getenv |
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| tests2.cpp:27:12:27:12 | call to operator<< | tests2.cpp:27:15:27:20 | call to getenv |
2-
| tests2.cpp:28:12:28:12 | call to operator<< | tests2.cpp:28:15:28:23 | PATH = |
3-
| tests2.cpp:28:25:28:25 | call to operator<< | tests2.cpp:28:28:28:33 | call to getenv |
4-
| tests2.cpp:28:43:28:43 | call to operator<< | tests2.cpp:28:46:28:48 | . |
5-
| tests2.cpp:29:12:29:12 | call to operator<< | tests2.cpp:29:15:29:28 | PATHPATHPATH |
1+
| tests2.cpp:58:12:58:12 | call to operator<< | tests2.cpp:58:15:58:20 | call to getenv |
2+
| tests2.cpp:59:12:59:12 | call to operator<< | tests2.cpp:59:15:59:23 | PATH = |
3+
| tests2.cpp:59:25:59:25 | call to operator<< | tests2.cpp:59:28:59:33 | call to getenv |
4+
| tests2.cpp:59:43:59:43 | call to operator<< | tests2.cpp:59:46:59:48 | . |
5+
| tests2.cpp:60:12:60:12 | call to operator<< | tests2.cpp:60:15:60:28 | PATHPATHPATH |

cpp/ql/test/query-tests/Security/CWE/CWE-497/semmle/tests/tests2.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// library functions etc
44

55
char *getenv(const char *name);
6+
char *strcpy(char *s1, const char *s2);
67

78
namespace std
89
{
@@ -20,11 +21,92 @@ namespace std
2021
extern ostream cout;
2122
}
2223

24+
int socket(int p1, int p2, int p3);
25+
void send(int sock, const char *buffer, int p3, int p4);
26+
27+
const char *mysql_get_client_info();
28+
void mysql_real_connect(int p1, int p2, int p3, const char *password, int p5, int p6, int p7, int p8);
29+
30+
struct container
31+
{
32+
char *ptr;
33+
};
34+
35+
struct passwd
36+
{
37+
// ...
38+
39+
char *pw_passwd;
40+
41+
// ...
42+
};
43+
44+
passwd *getpwuid(int uid);
45+
46+
int val();
47+
2348
// test cases
2449

50+
const char *global1 = mysql_get_client_info();
51+
const char *global2 = "abc";
52+
2553
void test1()
2654
{
55+
int sock = socket(val(), val(), val());
56+
57+
// tests for a strict implementation of CWE-497
2758
std::cout << getenv("HOME"); // BAD: outputs HOME environment variable
2859
std::cout << "PATH = " << getenv("PATH") << "."; // BAD: outputs PATH environment variable
2960
std::cout << "PATHPATHPATH"; // GOOD: not system data
61+
62+
// tests for a more pragmatic implementation of CWE-497
63+
send(sock, getenv("HOME"), val(), val()); // BAD
64+
send(sock, getenv("PATH"), val(), val()); // BAD
65+
send(sock, getenv("USERNAME"), val(), val()); // BAD
66+
send(sock, getenv("HARMLESS"), val(), val()); // GOOD: harmless information [FALSE POSITIVE]
67+
send(sock, "HOME", val(), val()); // GOOD: not system data
68+
send(sock, "PATH", val(), val()); // GOOD: not system data
69+
send(sock, "USERNAME", val(), val()); // GOOD: not system data
70+
send(sock, "HARMLESS", val(), val()); // GOOD: not system data
71+
72+
// tests for `mysql_get_client_info`, including via a global
73+
{
74+
char buffer[256];
75+
76+
strcpy(buffer, mysql_get_client_info());
77+
78+
send(sock, mysql_get_client_info(), val(), val()); // BAD
79+
send(sock, buffer, val(), val()); // BAD [NOT DETECTED]
80+
send(sock, global1, val(), val()); // BAD
81+
send(sock, global2, val(), val()); // GOOD: not system data
82+
}
83+
84+
// tests for `mysql_real_connect`
85+
{
86+
const char *str1 = "123456";
87+
const char *str2 = "abcdef";
88+
89+
mysql_real_connect(sock, val(), val(), str1, val(), val(), val(), val());
90+
91+
send(sock, str1, val(), val()); // BAD
92+
send(sock, str2, val(), val()); // GOOD: not system data
93+
}
94+
95+
// tests for `getpwuid`
96+
{
97+
passwd *pw;
98+
99+
pw = getpwuid(val());
100+
send(sock, pw->pw_passwd, val(), val()); // BAD
101+
}
102+
103+
// tests for containers
104+
{
105+
container c1, c2;
106+
107+
c1.ptr = getenv("MY_SECRET_TOKEN");
108+
c2.ptr = "";
109+
send(sock, c1.ptr, val(), val()); // BAD
110+
send(sock, c2.ptr, val(), val()); // GOOD: not system data [FALSE POSITIVE]
111+
}
30112
}

0 commit comments

Comments
 (0)