Skip to content

Commit ba51b65

Browse files
committed
C++: Fix hasRemoteFlowSource for fgets
Also add the test that exposed this. Note that the test would only have started failing after `cpp/uncontrolled-process-operation` with the rewrite of the query away from default taint tracking, which has not happened yet.
1 parent 4ae35d1 commit ba51b65

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Gets.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ private class FgetsFunction extends DataFlowFunction, TaintFunction, ArrayFuncti
4949
}
5050

5151
override predicate hasRemoteFlowSource(FunctionOutput output, string description) {
52-
output.isParameterDeref(0) and
53-
description = "string read by " + this.getName()
54-
or
55-
output.isReturnValue() and
52+
(
53+
output.isParameterDeref(0) or
54+
output.isReturnValue() or
55+
output.isReturnValueDeref()
56+
) and
5657
description = "string read by " + this.getName()
5758
}
5859

cpp/ql/test/query-tests/Security/CWE/CWE-114/semmle/UncontrolledProcessOperation/UncontrolledProcessOperation.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ edges
5252
| test.cpp:106:17:106:22 | buffer | test.cpp:107:15:107:20 | buffer |
5353
| test.cpp:106:17:106:22 | recv output argument | test.cpp:107:15:107:20 | buffer |
5454
| test.cpp:106:17:106:22 | recv output argument | test.cpp:107:15:107:20 | buffer |
55+
| test.cpp:113:8:113:12 | call to fgets | test.cpp:114:9:114:11 | ptr |
56+
| test.cpp:113:8:113:12 | call to fgets | test.cpp:114:9:114:11 | ptr |
57+
| test.cpp:113:8:113:12 | call to fgets | test.cpp:114:9:114:11 | ptr |
58+
| test.cpp:113:8:113:12 | call to fgets | test.cpp:114:9:114:11 | ptr |
5559
subpaths
5660
nodes
5761
| test.cpp:24:30:24:36 | command | semmle.label | command |
@@ -91,6 +95,10 @@ nodes
9195
| test.cpp:106:17:106:22 | recv output argument | semmle.label | recv output argument |
9296
| test.cpp:107:15:107:20 | buffer | semmle.label | buffer |
9397
| test.cpp:107:15:107:20 | buffer | semmle.label | buffer |
98+
| test.cpp:113:8:113:12 | call to fgets | semmle.label | call to fgets |
99+
| test.cpp:113:8:113:12 | call to fgets | semmle.label | call to fgets |
100+
| test.cpp:114:9:114:11 | ptr | semmle.label | ptr |
101+
| test.cpp:114:9:114:11 | ptr | semmle.label | ptr |
94102
#select
95103
| test.cpp:26:10:26:16 | command | test.cpp:42:18:42:23 | call to getenv | test.cpp:26:10:26:16 | command | The value of this argument may come from $@ and is being passed to system. | test.cpp:42:18:42:23 | call to getenv | call to getenv |
96104
| test.cpp:31:10:31:16 | command | test.cpp:43:18:43:23 | call to getenv | test.cpp:31:10:31:16 | command | The value of this argument may come from $@ and is being passed to system. | test.cpp:43:18:43:23 | call to getenv | call to getenv |
@@ -101,3 +109,4 @@ nodes
101109
| test.cpp:78:10:78:15 | buffer | test.cpp:76:12:76:17 | buffer | test.cpp:78:10:78:15 | buffer | The value of this argument may come from $@ and is being passed to system. | test.cpp:76:12:76:17 | buffer | buffer |
102110
| test.cpp:99:15:99:20 | buffer | test.cpp:98:17:98:22 | buffer | test.cpp:99:15:99:20 | buffer | The value of this argument may come from $@ and is being passed to LoadLibrary. | test.cpp:98:17:98:22 | buffer | buffer |
103111
| test.cpp:107:15:107:20 | buffer | test.cpp:106:17:106:22 | buffer | test.cpp:107:15:107:20 | buffer | The value of this argument may come from $@ and is being passed to LoadLibrary. | test.cpp:106:17:106:22 | buffer | buffer |
112+
| test.cpp:114:9:114:11 | ptr | test.cpp:113:8:113:12 | call to fgets | test.cpp:114:9:114:11 | ptr | The value of this argument may come from $@ and is being passed to system. | test.cpp:113:8:113:12 | call to fgets | call to fgets |

cpp/ql/test/query-tests/Security/CWE/CWE-114/semmle/UncontrolledProcessOperation/test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ void testAcceptRecv(int socket1, int socket2)
107107
LoadLibrary(buffer); // BAD: using data from recv
108108
}
109109
}
110+
111+
void argumentUse(char *ptr, FILE *stream) {
112+
char buffer[80];
113+
ptr = fgets(buffer, sizeof(buffer), stream);
114+
system(ptr); // BAD
115+
}

0 commit comments

Comments
 (0)