Skip to content

Commit e8423f8

Browse files
authored
Merge pull request github#13149 from MathiasVP/barrier-out-on-phi-back-edges
C++: Block flow through back-edges in `cpp/overrun-write`
2 parents 843640c + de1f81a commit e8423f8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

cpp/ql/src/experimental/Likely Bugs/OverrunWriteProductFlow.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ module StringSizeConfig implements ProductFlow::StateConfigSig {
111111

112112
predicate isBarrier2(DataFlow::Node node, FlowState2 state) { none() }
113113

114+
predicate isBarrierOut2(DataFlow::Node node) {
115+
node = any(DataFlow::SsaPhiNode phi).getAnInput(true)
116+
}
117+
114118
predicate isAdditionalFlowStep1(
115119
DataFlow::Node node1, FlowState1 state1, DataFlow::Node node2, FlowState1 state2
116120
) {

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/OverrunWriteProductFlow.expected

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ edges
223223
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:16:243:21 | string indirection |
224224
| test.cpp:243:16:243:21 | string indirection | test.cpp:243:12:243:21 | string |
225225
| test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p |
226+
| test.cpp:256:17:256:22 | call to malloc | test.cpp:257:12:257:12 | p |
226227
nodes
227228
| test.cpp:16:11:16:21 | mk_string_t indirection [string] | semmle.label | mk_string_t indirection [string] |
228229
| test.cpp:18:5:18:30 | ... = ... | semmle.label | ... = ... |
@@ -405,6 +406,8 @@ nodes
405406
| test.cpp:243:16:243:21 | string indirection | semmle.label | string indirection |
406407
| test.cpp:249:20:249:27 | call to my_alloc | semmle.label | call to my_alloc |
407408
| test.cpp:250:12:250:12 | p | semmle.label | p |
409+
| test.cpp:256:17:256:22 | call to malloc | semmle.label | call to malloc |
410+
| test.cpp:257:12:257:12 | p | semmle.label | p |
408411
subpaths
409412
| test.cpp:242:22:242:27 | buffer | test.cpp:235:40:235:45 | buffer | test.cpp:236:12:236:17 | p_str indirection [post update] [string] | test.cpp:242:16:242:19 | set_string output argument [string] |
410413
#select
@@ -423,6 +426,5 @@ subpaths
423426
| test.cpp:199:9:199:15 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:199:22:199:27 | string | This write may overflow $@ by 2 elements. | test.cpp:199:22:199:27 | string | string |
424427
| test.cpp:203:9:203:15 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:203:22:203:27 | string | This write may overflow $@ by 2 elements. | test.cpp:203:22:203:27 | string | string |
425428
| test.cpp:207:9:207:15 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:207:22:207:27 | string | This write may overflow $@ by 3 elements. | test.cpp:207:22:207:27 | string | string |
426-
| test.cpp:232:3:232:8 | call to memset | test.cpp:228:43:228:48 | call to malloc | test.cpp:232:10:232:15 | buffer | This write may overflow $@ by 32 elements. | test.cpp:232:10:232:15 | buffer | buffer |
427429
| test.cpp:243:5:243:10 | call to memset | test.cpp:241:27:241:32 | call to malloc | test.cpp:243:12:243:21 | string | This write may overflow $@ by 1 element. | test.cpp:243:16:243:21 | string | string |
428430
| test.cpp:250:5:250:10 | call to memset | test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p | This write may overflow $@ by 1 element. | test.cpp:250:12:250:12 | p | p |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void repeated_alerts(unsigned size, unsigned offset) {
229229
while(unknown()) {
230230
++size;
231231
}
232-
memset(buffer, 0, size); // BAD
232+
memset(buffer, 0, size); // BAD [NOT DETECTED]
233233
}
234234

235235
void set_string(string_t* p_str, char* buffer) {
@@ -248,4 +248,12 @@ void* my_alloc(unsigned size);
248248
void foo(unsigned size) {
249249
int* p = (int*)my_alloc(size); // BAD
250250
memset(p, 0, size + 1);
251+
}
252+
253+
void test6(unsigned long n, char *p) {
254+
while (unknown()) {
255+
n++;
256+
p = (char *)malloc(n);
257+
memset(p, 0, n); // GOOD
258+
}
251259
}

0 commit comments

Comments
 (0)