Skip to content

Commit 7d9f78b

Browse files
authored
Merge pull request github#16328 from geoffw0/pathinj
C++: Reduce duplication from cpp/path-injection
2 parents 7e83979 + 5538716 commit 7d9f78b

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ module TaintedPathConfig implements DataFlow::ConfigSig {
8888
hasUpperBoundsCheck(checkedVar)
8989
)
9090
}
91+
92+
predicate isBarrierOut(DataFlow::Node node) {
93+
// make sinks barriers so that we only report the closest instance
94+
isSink(node)
95+
}
9196
}
9297

9398
module TaintedPath = TaintTracking::Global<TaintedPathConfig>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The "Uncontrolled data used in path expression" query (`cpp/path-injection`) query produces fewer near-duplicate results.
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
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:74:13:74:18 | read output argument | test.c:76:11:76:16 | *buffer | provenance | |
13+
| test.c:75:13:75:18 | read output argument | test.c:76:11:76:16 | *buffer | provenance | |
914
nodes
1015
| test.c:8:27:8:30 | **argv | semmle.label | **argv |
1116
| test.c:9:23:9:29 | *access to array | semmle.label | *access to array |
@@ -16,11 +21,23 @@ nodes
1621
| test.c:38:11:38:18 | *fileName | semmle.label | *fileName |
1722
| test.c:43:17:43:24 | scanf output argument | semmle.label | scanf output argument |
1823
| 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 |
24+
| test.c:48:21:48:26 | *call to getenv | semmle.label | *call to getenv |
25+
| test.c:48:21:48:26 | *call to getenv | semmle.label | *call to getenv |
26+
| test.c:49:11:49:17 | *tainted | semmle.label | *tainted |
27+
| test.c:54:21:54:26 | *call to getenv | semmle.label | *call to getenv |
28+
| test.c:55:11:55:16 | *buffer | semmle.label | *buffer |
29+
| test.c:69:14:69:20 | *access to array | semmle.label | *access to array |
30+
| test.c:74:13:74:18 | read output argument | semmle.label | read output argument |
31+
| test.c:75:13:75:18 | read output argument | semmle.label | read output argument |
32+
| test.c:76:11:76:16 | *buffer | semmle.label | *buffer |
2033
subpaths
2134
#select
2235
| 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) |
2336
| 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) |
2437
| 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) |
2538
| 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) |
39+
| 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) |
40+
| 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) |
41+
| 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) |
42+
| 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) |
43+
| 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)