Skip to content

Commit bbd80ec

Browse files
committed
C++: Add some more test cases.
1 parent 290b0fc commit bbd80ec

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
edges
22
| test.c:8:27:8:30 | **argv | test.c:9:23:9:29 | *access to array | provenance | |
33
| test.c:8:27:8:30 | **argv | test.c:31:22:31:28 | *access to array | provenance | |
4-
| test.c:8:27:8:30 | **argv | test.c:57:10:57:16 | *access to array | provenance | |
4+
| test.c:8:27:8:30 | **argv | test.c:69:14:69:20 | *access to array | provenance | |
55
| test.c:9:23:9:29 | *access to array | test.c:17:11:17:18 | *fileName | provenance | TaintFunction |
66
| test.c:31:22:31:28 | *access to array | test.c:32:11:32:18 | *fileName | provenance | |
77
| test.c:37:17:37:24 | scanf output argument | test.c:38:11:38:18 | *fileName | provenance | |
88
| test.c:43:17:43:24 | scanf output argument | test.c:44:11:44:18 | *fileName | provenance | |
9+
| test.c:48:21:48:26 | *call to getenv | test.c:48:21:48:26 | *call to getenv | provenance | |
10+
| test.c:48:21:48:26 | *call to getenv | test.c:49:11:49:17 | *tainted | provenance | |
11+
| test.c:54:21:54:26 | *call to getenv | test.c:55:11:55:16 | *buffer | provenance | TaintFunction |
12+
| test.c:54:21:54:26 | *call to getenv | test.c:56:11:56:16 | *buffer | provenance | TaintFunction |
13+
| test.c:74:13:74:18 | read output argument | test.c:76:11:76:16 | *buffer | provenance | |
14+
| test.c:75:13:75:18 | read output argument | test.c:76:11:76:16 | *buffer | provenance | |
915
nodes
1016
| test.c:8:27:8:30 | **argv | semmle.label | **argv |
1117
| test.c:9:23:9:29 | *access to array | semmle.label | *access to array |
@@ -16,11 +22,25 @@ nodes
1622
| test.c:38:11:38:18 | *fileName | semmle.label | *fileName |
1723
| test.c:43:17:43:24 | scanf output argument | semmle.label | scanf output argument |
1824
| test.c:44:11:44:18 | *fileName | semmle.label | *fileName |
19-
| test.c:57:10:57:16 | *access to array | semmle.label | *access to array |
25+
| test.c:48:21:48:26 | *call to getenv | semmle.label | *call to getenv |
26+
| test.c:48:21:48:26 | *call to getenv | semmle.label | *call to getenv |
27+
| test.c:49:11:49:17 | *tainted | semmle.label | *tainted |
28+
| test.c:54:21:54:26 | *call to getenv | semmle.label | *call to getenv |
29+
| test.c:55:11:55:16 | *buffer | semmle.label | *buffer |
30+
| test.c:56:11:56:16 | *buffer | semmle.label | *buffer |
31+
| test.c:69:14:69:20 | *access to array | semmle.label | *access to array |
32+
| test.c:74:13:74:18 | read output argument | semmle.label | read output argument |
33+
| test.c:75:13:75:18 | read output argument | semmle.label | read output argument |
34+
| test.c:76:11:76:16 | *buffer | semmle.label | *buffer |
2035
subpaths
2136
#select
2237
| test.c:17:11:17:18 | fileName | test.c:8:27:8:30 | **argv | test.c:17:11:17:18 | *fileName | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:8:27:8:30 | **argv | user input (a command-line argument) |
2338
| test.c:32:11:32:18 | fileName | test.c:8:27:8:30 | **argv | test.c:32:11:32:18 | *fileName | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:8:27:8:30 | **argv | user input (a command-line argument) |
2439
| test.c:38:11:38:18 | fileName | test.c:37:17:37:24 | scanf output argument | test.c:38:11:38:18 | *fileName | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:37:17:37:24 | scanf output argument | user input (value read by scanf) |
2540
| test.c:44:11:44:18 | fileName | test.c:43:17:43:24 | scanf output argument | test.c:44:11:44:18 | *fileName | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:43:17:43:24 | scanf output argument | user input (value read by scanf) |
26-
| test.c:57:10:57:16 | access to array | test.c:8:27:8:30 | **argv | test.c:57:10:57:16 | *access to array | This argument to a file access function is derived from $@ and then passed to read(fileName), which calls fopen(filename). | test.c:8:27:8:30 | **argv | user input (a command-line argument) |
41+
| test.c:49:11:49:17 | tainted | test.c:48:21:48:26 | *call to getenv | test.c:49:11:49:17 | *tainted | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:48:21:48:26 | *call to getenv | user input (an environment variable) |
42+
| test.c:55:11:55:16 | buffer | test.c:54:21:54:26 | *call to getenv | test.c:55:11:55:16 | *buffer | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:54:21:54:26 | *call to getenv | user input (an environment variable) |
43+
| test.c:56:11:56:16 | buffer | test.c:54:21:54:26 | *call to getenv | test.c:56:11:56:16 | *buffer | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:54:21:54:26 | *call to getenv | user input (an environment variable) |
44+
| test.c:69:14:69:20 | access to array | test.c:8:27:8:30 | **argv | test.c:69:14:69:20 | *access to array | This argument to a file access function is derived from $@ and then passed to readFile(fileName), which calls fopen(filename). | test.c:8:27:8:30 | **argv | user input (a command-line argument) |
45+
| test.c:76:11:76:16 | buffer | test.c:74:13:74:18 | read output argument | test.c:76:11:76:16 | *buffer | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:74:13:74:18 | read output argument | user input (buffer read by read) |
46+
| test.c:76:11:76:16 | buffer | test.c:75:13:75:18 | read output argument | test.c:76:11:76:16 | *buffer | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:75:13:75:18 | read output argument | user input (buffer read by read) |

cpp/ql/test/query-tests/Security/CWE/CWE-022/semmle/tests/stdlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
typedef struct {} FILE;
77
#define FILENAME_MAX 1000
88
typedef unsigned long size_t;
9+
typedef signed long ssize_t;
910

1011
FILE *fopen(const char *filename, const char *mode);
1112
int sprintf(char *s, const char *format, ...);
@@ -15,3 +16,4 @@ int scanf(const char *format, ...);
1516
void *malloc(size_t size);
1617
double strtod(const char *ptr, char **endptr);
1718
char *getenv(const char *name);
19+
ssize_t read(int fd, void *buffer, size_t count);

cpp/ql/test/query-tests/Security/CWE/CWE-022/semmle/tests/test.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
int main(int argc, char** argv) {
99
char *userAndFile = argv[2];
10-
10+
1111
{
1212
char fileBuffer[FILENAME_MAX] = "/home/";
1313
char *fileName = fileBuffer;
@@ -44,6 +44,18 @@ int main(int argc, char** argv) {
4444
fopen(fileName, "wb+"); // BAD
4545
}
4646

47+
{
48+
char *tainted = getenv("A_STRING");
49+
fopen(tainted, "wb+"); // BAD
50+
}
51+
52+
{
53+
char buffer[1024];
54+
strncpy(buffer, getenv("A_STRING"), 1024);
55+
fopen(buffer, "wb+"); // BAD
56+
fopen(buffer, "wb+"); // (we don't want a duplicate result here)
57+
}
58+
4759
{
4860
char *aNumber = getenv("A_NUMBER");
4961
double number = strtod(aNumber, 0);
@@ -53,11 +65,18 @@ int main(int argc, char** argv) {
5365
}
5466

5567
{
56-
void read(const char *fileName);
57-
read(argv[1]); // BAD
68+
void readFile(const char *fileName);
69+
readFile(argv[1]); // BAD
70+
}
71+
72+
{
73+
char buffer[1024];
74+
read(0, buffer, 1024);
75+
read(0, buffer, 1024);
76+
fopen(buffer, "wb+"); // BAD [duplicated with both sources]
5877
}
5978
}
6079

61-
void read(char *fileName) {
80+
void readFile(char *fileName) {
6281
fopen(fileName, "wb+");
6382
}

0 commit comments

Comments
 (0)