Skip to content

Commit 378206a

Browse files
committed
C++: Stop taint from flowing to arithmetic types
These are not likely to give the user much control over what can be accessed.
1 parent 7186634 commit 378206a

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class TaintedPathConfiguration extends TaintTracking::Configuration {
7575
}
7676

7777
override predicate isSanitizerIn(DataFlow::Node node) { this.isSource(node) }
78+
79+
override predicate isSanitizer(DataFlow::Node node) {
80+
node.asExpr().(Call).getTarget().getUnspecifiedType() instanceof ArithmeticType
81+
}
7882
}
7983

8084
from

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,10 +6,12 @@
66
typedef struct {} FILE;
77
#define FILENAME_MAX 1000
88
typedef unsigned long size_t;
9+
#define NULL ((void*)0)
910

1011
FILE *fopen(const char *filename, const char *mode);
1112
int sprintf(char *s, const char *format, ...);
1213
size_t strlen(const char *s);
1314
char *strncat(char *s1, const char *s2, size_t n);
1415
int scanf(const char *format, ...);
1516
void *malloc(size_t size);
17+
double strtod(const char *ptr, char **endptr);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ int main(int argc, char** argv) {
4343
scanf("%s", fileName);
4444
fopen(fileName, "wb+"); // BAD
4545
}
46+
47+
{
48+
char *aNumber = getenv("A_NUMBER");
49+
double number = strtod(aNumber, 0);
50+
char fileName[20];
51+
sprintf(fileName, "/foo/%f", number);
52+
fopen(fileName, "wb+"); // GOOD
53+
}
4654
}

0 commit comments

Comments
 (0)