Skip to content

Commit d37ef1b

Browse files
committed
C++: Add command line injection test that currently results in a false positive
1 parent 459870a commit d37ef1b

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-078/semmle/ExecTainted/ExecTainted.expected

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,29 @@ edges
4747
| test.cpp:180:13:180:19 | strncat output argument | test.cpp:183:32:183:38 | command indirection |
4848
| test.cpp:180:22:180:29 | filename indirection | test.cpp:180:13:180:19 | strncat output argument |
4949
| test.cpp:180:22:180:29 | filename indirection | test.cpp:180:13:180:19 | strncat output argument |
50-
| test.cpp:186:53:186:60 | *filename | test.cpp:187:18:187:25 | filename indirection |
51-
| test.cpp:186:53:186:60 | *filename | test.cpp:188:20:188:24 | flags indirection |
52-
| test.cpp:186:53:186:60 | filename | test.cpp:187:18:187:25 | filename indirection |
53-
| test.cpp:186:53:186:60 | filename | test.cpp:188:20:188:24 | flags indirection |
50+
| test.cpp:186:47:186:54 | *filename | test.cpp:187:18:187:25 | filename indirection |
51+
| test.cpp:186:47:186:54 | *filename | test.cpp:188:20:188:24 | flags indirection |
52+
| test.cpp:186:47:186:54 | filename | test.cpp:187:18:187:25 | filename indirection |
53+
| test.cpp:186:47:186:54 | filename | test.cpp:188:20:188:24 | flags indirection |
5454
| test.cpp:187:11:187:15 | strncat output argument | test.cpp:188:11:188:17 | command [post update] |
5555
| test.cpp:187:11:187:15 | strncat output argument | test.cpp:188:11:188:17 | command [post update] |
5656
| test.cpp:187:18:187:25 | filename indirection | test.cpp:187:11:187:15 | strncat output argument |
5757
| test.cpp:187:18:187:25 | filename indirection | test.cpp:187:11:187:15 | strncat output argument |
5858
| test.cpp:188:11:188:17 | command [post update] | test.cpp:188:11:188:17 | command [post update] |
59-
| test.cpp:188:11:188:17 | command [post update] | test.cpp:196:16:196:22 | command [post update] |
60-
| test.cpp:188:11:188:17 | command [post update] | test.cpp:196:16:196:22 | command [post update] |
59+
| test.cpp:188:11:188:17 | command [post update] | test.cpp:196:10:196:16 | command [post update] |
60+
| test.cpp:188:11:188:17 | command [post update] | test.cpp:196:10:196:16 | command [post update] |
61+
| test.cpp:188:11:188:17 | command [post update] | test.cpp:205:10:205:16 | command [post update] |
62+
| test.cpp:188:11:188:17 | command [post update] | test.cpp:205:10:205:16 | command [post update] |
6163
| test.cpp:188:11:188:17 | strncat output argument | test.cpp:188:11:188:17 | command [post update] |
6264
| test.cpp:188:11:188:17 | strncat output argument | test.cpp:188:11:188:17 | command [post update] |
6365
| test.cpp:188:20:188:24 | flags indirection | test.cpp:188:11:188:17 | strncat output argument |
6466
| test.cpp:188:20:188:24 | flags indirection | test.cpp:188:11:188:17 | strncat output argument |
65-
| test.cpp:194:9:194:16 | fread output argument | test.cpp:196:32:196:39 | filename |
66-
| test.cpp:194:9:194:16 | fread output argument | test.cpp:196:32:196:39 | filename indirection |
67-
| test.cpp:196:16:196:22 | command [post update] | test.cpp:198:32:198:38 | command indirection |
68-
| test.cpp:196:32:196:39 | filename | test.cpp:186:53:186:60 | filename |
69-
| test.cpp:196:32:196:39 | filename indirection | test.cpp:186:53:186:60 | *filename |
67+
| test.cpp:194:9:194:16 | fread output argument | test.cpp:196:26:196:33 | filename |
68+
| test.cpp:194:9:194:16 | fread output argument | test.cpp:196:26:196:33 | filename indirection |
69+
| test.cpp:196:10:196:16 | command [post update] | test.cpp:198:32:198:38 | command indirection |
70+
| test.cpp:196:26:196:33 | filename | test.cpp:186:47:186:54 | filename |
71+
| test.cpp:196:26:196:33 | filename indirection | test.cpp:186:47:186:54 | *filename |
72+
| test.cpp:205:10:205:16 | command [post update] | test.cpp:207:32:207:38 | command indirection |
7073
nodes
7174
| test.cpp:16:20:16:23 | argv | semmle.label | argv |
7275
| test.cpp:22:13:22:20 | sprintf output argument | semmle.label | sprintf output argument |
@@ -112,19 +115,21 @@ nodes
112115
| test.cpp:180:13:180:19 | strncat output argument | semmle.label | strncat output argument |
113116
| test.cpp:180:22:180:29 | filename indirection | semmle.label | filename indirection |
114117
| test.cpp:183:32:183:38 | command indirection | semmle.label | command indirection |
115-
| test.cpp:186:53:186:60 | *filename | semmle.label | *filename |
116-
| test.cpp:186:53:186:60 | filename | semmle.label | filename |
118+
| test.cpp:186:47:186:54 | *filename | semmle.label | *filename |
119+
| test.cpp:186:47:186:54 | filename | semmle.label | filename |
117120
| test.cpp:187:11:187:15 | strncat output argument | semmle.label | strncat output argument |
118121
| test.cpp:187:18:187:25 | filename indirection | semmle.label | filename indirection |
119122
| test.cpp:188:11:188:17 | command [post update] | semmle.label | command [post update] |
120123
| test.cpp:188:11:188:17 | command [post update] | semmle.label | command [post update] |
121124
| test.cpp:188:11:188:17 | strncat output argument | semmle.label | strncat output argument |
122125
| test.cpp:188:20:188:24 | flags indirection | semmle.label | flags indirection |
123126
| test.cpp:194:9:194:16 | fread output argument | semmle.label | fread output argument |
124-
| test.cpp:196:16:196:22 | command [post update] | semmle.label | command [post update] |
125-
| test.cpp:196:32:196:39 | filename | semmle.label | filename |
126-
| test.cpp:196:32:196:39 | filename indirection | semmle.label | filename indirection |
127+
| test.cpp:196:10:196:16 | command [post update] | semmle.label | command [post update] |
128+
| test.cpp:196:26:196:33 | filename | semmle.label | filename |
129+
| test.cpp:196:26:196:33 | filename indirection | semmle.label | filename indirection |
127130
| test.cpp:198:32:198:38 | command indirection | semmle.label | command indirection |
131+
| test.cpp:205:10:205:16 | command [post update] | semmle.label | command [post update] |
132+
| test.cpp:207:32:207:38 | command indirection | semmle.label | command indirection |
128133
subpaths
129134
#select
130135
| test.cpp:23:12:23:19 | command1 | test.cpp:16:20:16:23 | argv | test.cpp:23:12:23:19 | command1 indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to system(string) | test.cpp:16:20:16:23 | argv | user input (a command-line argument) | test.cpp:22:13:22:20 | sprintf output argument | sprintf output argument |
@@ -141,3 +146,5 @@ subpaths
141146
| test.cpp:183:32:183:38 | command | test.cpp:174:9:174:16 | fread output argument | test.cpp:183:32:183:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:174:9:174:16 | fread output argument | user input (String read by fread) | test.cpp:180:13:180:19 | strncat output argument | strncat output argument |
142147
| test.cpp:198:32:198:38 | command | test.cpp:194:9:194:16 | fread output argument | test.cpp:198:32:198:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:194:9:194:16 | fread output argument | user input (String read by fread) | test.cpp:187:11:187:15 | strncat output argument | strncat output argument |
143148
| test.cpp:198:32:198:38 | command | test.cpp:194:9:194:16 | fread output argument | test.cpp:198:32:198:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:194:9:194:16 | fread output argument | user input (String read by fread) | test.cpp:188:11:188:17 | strncat output argument | strncat output argument |
149+
| test.cpp:207:32:207:38 | command | test.cpp:194:9:194:16 | fread output argument | test.cpp:207:32:207:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:194:9:194:16 | fread output argument | user input (String read by fread) | test.cpp:187:11:187:15 | strncat output argument | strncat output argument |
150+
| test.cpp:207:32:207:38 | command | test.cpp:194:9:194:16 | fread output argument | test.cpp:207:32:207:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:194:9:194:16 | fread output argument | user input (String read by fread) | test.cpp:188:11:188:17 | strncat output argument | strncat output argument |

cpp/ql/test/query-tests/Security/CWE/CWE-078/semmle/ExecTainted/test.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void test16(FILE *f, bool use_flags) {
183183
execl("/bin/sh", "sh", "-c", command);
184184
}
185185

186-
void test17_inner(char *command, char *flags, char *filename) {
186+
void concat(char *command, char *flags, char *filename) {
187187
strncat(flags, filename, 1000);
188188
strncat(command, flags, 1000);
189189
}
@@ -193,11 +193,18 @@ void test17(FILE *f) {
193193
char command[1000] = "mv ", flags[1000] = "-R", filename[1000];
194194
fread(filename, 1, 1000, f);
195195

196-
test17_inner(command, flags, filename);
196+
concat(command, flags, filename);
197197

198198
execl("/bin/sh", "sh", "-c", command);
199199
}
200200

201-
// TODO: test for call context sensitivity at concatenation site
201+
void test18() {
202+
// GOOD [FALSE POSITIVE]
203+
char command[1000] = "ls ", flags[1000] = "-l", filename[1000] = ".";
204+
205+
concat(command, flags, filename);
206+
207+
execl("/bin/sh", "sh", "-c", command);
208+
}
202209

203210
// open question: do we want to report certain sources even when they're the start of the string?

0 commit comments

Comments
 (0)