Skip to content

Commit 8b4c42d

Browse files
committed
C++: Add cpp/command-line-injection test using a wrapper macro
1 parent f8198c3 commit 8b4c42d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ edges
6565
| test.cpp:196:26:196:33 | filename indirection | test.cpp:186:47:186:54 | *filename |
6666
| test.cpp:196:26:196:33 | filename indirection | test.cpp:196:10:196:16 | command [post update] |
6767
| test.cpp:196:26:196:33 | filename indirection | test.cpp:196:10:196:16 | command [post update] |
68+
| test.cpp:218:9:218:16 | fread output argument | test.cpp:220:19:220:26 | filename indirection |
69+
| test.cpp:218:9:218:16 | fread output argument | test.cpp:220:19:220:26 | filename indirection |
70+
| test.cpp:220:10:220:16 | strncat output argument | test.cpp:222:32:222:38 | command indirection |
71+
| test.cpp:220:10:220:16 | strncat output argument | test.cpp:222:32:222:38 | command indirection |
72+
| test.cpp:220:19:220:26 | filename indirection | test.cpp:220:10:220:16 | strncat output argument |
73+
| test.cpp:220:19:220:26 | filename indirection | test.cpp:220:10:220:16 | strncat output argument |
74+
| test.cpp:220:19:220:26 | filename indirection | test.cpp:220:10:220:16 | strncat output argument |
75+
| test.cpp:220:19:220:26 | filename indirection | test.cpp:220:10:220:16 | strncat output argument |
6876
nodes
6977
| test.cpp:16:20:16:23 | argv | semmle.label | argv |
7078
| test.cpp:22:13:22:20 | sprintf output argument | semmle.label | sprintf output argument |
@@ -137,6 +145,12 @@ nodes
137145
| test.cpp:196:26:196:33 | filename indirection | semmle.label | filename indirection |
138146
| test.cpp:198:32:198:38 | command indirection | semmle.label | command indirection |
139147
| test.cpp:198:32:198:38 | command indirection | semmle.label | command indirection |
148+
| test.cpp:218:9:218:16 | fread output argument | semmle.label | fread output argument |
149+
| test.cpp:220:10:220:16 | strncat output argument | semmle.label | strncat output argument |
150+
| test.cpp:220:10:220:16 | strncat output argument | semmle.label | strncat output argument |
151+
| test.cpp:220:19:220:26 | filename indirection | semmle.label | filename indirection |
152+
| test.cpp:220:19:220:26 | filename indirection | semmle.label | filename indirection |
153+
| test.cpp:222:32:222:38 | command indirection | semmle.label | command indirection |
140154
subpaths
141155
| test.cpp:196:26:196:33 | filename | test.cpp:186:47:186:54 | filename | test.cpp:188:11:188:17 | command [post update] | test.cpp:196:10:196:16 | command [post update] |
142156
| test.cpp:196:26:196:33 | filename | test.cpp:186:47:186:54 | filename | test.cpp:188:11:188:17 | command [post update] | test.cpp:196:10:196:16 | command [post update] |
@@ -161,3 +175,5 @@ subpaths
161175
| 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 |
162176
| 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 |
163177
| 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 |
178+
| test.cpp:222:32:222:38 | command | test.cpp:218:9:218:16 | fread output argument | test.cpp:222:32:222:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:218:9:218:16 | fread output argument | user input (String read by fread) | test.cpp:220:10:220:16 | strncat output argument | strncat output argument |
179+
| test.cpp:222:32:222:38 | command | test.cpp:218:9:218:16 | fread output argument | test.cpp:222:32:222:38 | command indirection | This argument to an OS command is derived from $@, dangerously concatenated into $@, and then passed to execl | test.cpp:218:9:218:16 | fread output argument | user input (String read by fread) | test.cpp:220:10:220:16 | strncat output argument | strncat output argument |

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,19 @@ void test18() {
207207
execl("/bin/sh", "sh", "-c", command);
208208
}
209209

210+
#define CONCAT(COMMAND, FILENAME) \
211+
strncat(COMMAND, FILENAME, 1000); \
212+
strncat(COMMAND, " ", 1000); \
213+
strncat(COMMAND, FILENAME, 1000);
214+
215+
void test19(FILE *f) {
216+
// BAD: the user string is injected directly into a command
217+
char command[1000] = "mv ", filename[1000];
218+
fread(filename, 1, 1000, f);
219+
220+
CONCAT(command, filename)
221+
222+
execl("/bin/sh", "sh", "-c", command);
223+
}
224+
210225
// open question: do we want to report certain sources even when they're the start of the string?

0 commit comments

Comments
 (0)