Skip to content

Commit 7186634

Browse files
committed
C++: Stop flow from going through another source
Without this we get confusing results: ``` char *userAndFile = argv[2]; char *fileName = argv[1]; fopen(fileName, "wb+"); // Both argv[1] and argv[2] marked as source without // this change. ``` While here add some more test cases.
1 parent 6333476 commit 7186634

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

cpp/ql/src/Security/CWE/CWE-022/TaintedPath.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class TaintedPathConfiguration extends TaintTracking::Configuration {
7373
fileFunction.outermostWrapperFunctionCall(asSinkExpr(node), _)
7474
)
7575
}
76+
77+
override predicate isSanitizerIn(DataFlow::Node node) { this.isSource(node) }
7678
}
7779

7880
from
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
edges
22
| test.c:9:23:9:26 | argv | test.c:17:11:17:18 | fileName |
33
| test.c:9:23:9:26 | argv | test.c:17:11:17:18 | fileName indirection |
4+
| test.c:31:22:31:25 | argv | test.c:32:11:32:18 | fileName |
5+
| test.c:31:22:31:25 | argv | test.c:32:11:32:18 | fileName indirection |
6+
| test.c:37:17:37:24 | fileName | test.c:38:11:38:18 | fileName indirection |
7+
| test.c:37:17:37:24 | scanf output argument | test.c:38:11:38:18 | fileName indirection |
8+
| test.c:43:17:43:24 | fileName | test.c:44:11:44:18 | fileName |
9+
| test.c:43:17:43:24 | fileName | test.c:44:11:44:18 | fileName indirection |
10+
| test.c:43:17:43:24 | scanf output argument | test.c:44:11:44:18 | fileName |
11+
| test.c:43:17:43:24 | scanf output argument | test.c:44:11:44:18 | fileName indirection |
412
nodes
513
| test.c:9:23:9:26 | argv | semmle.label | argv |
614
| test.c:17:11:17:18 | fileName | semmle.label | fileName |
715
| test.c:17:11:17:18 | fileName indirection | semmle.label | fileName indirection |
16+
| test.c:31:22:31:25 | argv | semmle.label | argv |
17+
| test.c:32:11:32:18 | fileName | semmle.label | fileName |
18+
| test.c:32:11:32:18 | fileName indirection | semmle.label | fileName indirection |
19+
| test.c:37:17:37:24 | fileName | semmle.label | fileName |
20+
| test.c:37:17:37:24 | scanf output argument | semmle.label | scanf output argument |
21+
| test.c:38:11:38:18 | fileName indirection | semmle.label | fileName indirection |
22+
| test.c:43:17:43:24 | fileName | semmle.label | fileName |
23+
| test.c:43:17:43:24 | scanf output argument | semmle.label | scanf output argument |
24+
| test.c:44:11:44:18 | fileName | semmle.label | fileName |
25+
| test.c:44:11:44:18 | fileName indirection | semmle.label | fileName indirection |
826
subpaths
927
#select
1028
| test.c:17:11:17:18 | fileName | test.c:9:23:9:26 | 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:9:23:9:26 | argv | user input (argv) |
1129
| test.c:17:11:17:18 | fileName | test.c:9:23:9:26 | argv | test.c:17:11:17:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:9:23:9:26 | argv | user input (argv) |
30+
| test.c:32:11:32:18 | fileName | test.c:31:22:31:25 | 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:31:22:31:25 | argv | user input (argv) |
31+
| test.c:32:11:32:18 | fileName | test.c:31:22:31:25 | argv | test.c:32:11:32:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:31:22:31:25 | argv | user input (argv) |
32+
| test.c:38:11:38:18 | fileName | test.c:37:17:37:24 | fileName | test.c:38:11:38:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:37:17:37:24 | fileName | user input (scanf) |
33+
| test.c:38:11:38:18 | fileName | test.c:37:17:37:24 | scanf output argument | test.c:38:11:38:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:37:17:37:24 | fileName | user input (scanf) |
34+
| test.c:44:11:44:18 | fileName | test.c:43:17:43:24 | fileName | 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 | fileName | user input (scanf) |
35+
| test.c:44:11:44:18 | fileName | test.c:43:17:43:24 | fileName | test.c:44:11:44:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:43:17:43:24 | fileName | user input (scanf) |
36+
| 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 | fileName | user input (scanf) |
37+
| test.c:44:11:44:18 | fileName | test.c:43:17:43:24 | scanf output argument | test.c:44:11:44:18 | fileName indirection | This argument to a file access function is derived from $@ and then passed to fopen(filename). | test.c:43:17:43:24 | fileName | user input (scanf) |

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
@@ -11,3 +11,5 @@ FILE *fopen(const char *filename, const char *mode);
1111
int sprintf(char *s, const char *format, ...);
1212
size_t strlen(const char *s);
1313
char *strncat(char *s1, const char *s2, size_t n);
14+
int scanf(const char *format, ...);
15+
void *malloc(size_t size);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,21 @@ int main(int argc, char** argv) {
2626
strncat(fileName+len, fixed, FILENAME_MAX-len-1);
2727
fopen(fileName, "wb+");
2828
}
29-
}
3029

30+
{
31+
char *fileName = argv[1];
32+
fopen(fileName, "wb+"); // BAD
33+
}
34+
35+
{
36+
char fileName[20];
37+
scanf("%s", fileName);
38+
fopen(fileName, "wb+"); // BAD
39+
}
40+
41+
{
42+
char *fileName = malloc(20 * sizeof(char));
43+
scanf("%s", fileName);
44+
fopen(fileName, "wb+"); // BAD
45+
}
46+
}

0 commit comments

Comments
 (0)