Skip to content

Commit a5632a2

Browse files
committed
Merge branch 'main' into precompute-states-in-overrun-write
2 parents 650e9e1 + 9954542 commit a5632a2

File tree

136 files changed

+2088
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+2088
-678
lines changed

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

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

255255
predicate isBarrier2(DataFlow::Node node, FlowState2 state) { none() }
256256

257+
predicate isBarrierOut2(DataFlow::Node node) {
258+
node = any(DataFlow::SsaPhiNode phi).getAnInput(true)
259+
}
260+
257261
predicate isAdditionalFlowStep1(
258262
DataFlow::Node node1, FlowState1 state1, DataFlow::Node node2, FlowState1 state2
259263
) {

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
@@ -83,6 +83,7 @@ edges
8383
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:16:243:21 | string indirection |
8484
| test.cpp:243:16:243:21 | string indirection | test.cpp:243:12:243:21 | string |
8585
| test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p |
86+
| test.cpp:256:17:256:22 | call to malloc | test.cpp:257:12:257:12 | p |
8687
nodes
8788
| test.cpp:16:11:16:21 | mk_string_t indirection [string] | semmle.label | mk_string_t indirection [string] |
8889
| test.cpp:18:5:18:30 | ... = ... | semmle.label | ... = ... |
@@ -159,6 +160,8 @@ nodes
159160
| test.cpp:243:16:243:21 | string indirection | semmle.label | string indirection |
160161
| test.cpp:249:20:249:27 | call to my_alloc | semmle.label | call to my_alloc |
161162
| test.cpp:250:12:250:12 | p | semmle.label | p |
163+
| test.cpp:256:17:256:22 | call to malloc | semmle.label | call to malloc |
164+
| test.cpp:257:12:257:12 | p | semmle.label | p |
162165
subpaths
163166
| 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] |
164167
#select
@@ -177,6 +180,5 @@ subpaths
177180
| 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 |
178181
| 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 |
179182
| 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 |
180-
| 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 2 elements. | test.cpp:232:10:232:15 | buffer | buffer |
181183
| 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 |
182184
| 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
}

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/InvalidPointerDeref.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ edges
649649
| test.cpp:280:13:280:24 | new[] | test.cpp:281:14:281:15 | xs |
650650
| test.cpp:290:13:290:24 | new[] | test.cpp:291:14:291:15 | xs |
651651
| test.cpp:290:13:290:24 | new[] | test.cpp:292:30:292:30 | x |
652+
| test.cpp:304:15:304:26 | new[] | test.cpp:307:5:307:6 | xs |
653+
| test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:6 | xs |
654+
| test.cpp:308:5:308:6 | xs | test.cpp:308:5:308:11 | access to array |
655+
| test.cpp:308:5:308:11 | access to array | test.cpp:308:5:308:29 | Store: ... = ... |
652656
#select
653657
| test.cpp:6:14:6:15 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
654658
| test.cpp:8:14:8:21 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:8:14:8:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
@@ -672,3 +676,4 @@ edges
672676
| test.cpp:254:9:254:16 | Store: ... = ... | test.cpp:248:24:248:30 | call to realloc | test.cpp:254:9:254:16 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:248:24:248:30 | call to realloc | call to realloc | test.cpp:254:11:254:11 | i | i |
673677
| test.cpp:264:13:264:14 | Load: * ... | test.cpp:260:13:260:24 | new[] | test.cpp:264:13:264:14 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:260:13:260:24 | new[] | new[] | test.cpp:261:19:261:21 | len | len |
674678
| test.cpp:274:5:274:10 | Store: ... = ... | test.cpp:270:13:270:24 | new[] | test.cpp:274:5:274:10 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:270:13:270:24 | new[] | new[] | test.cpp:271:19:271:21 | len | len |
679+
| test.cpp:308:5:308:29 | Store: ... = ... | test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:29 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:304:15:304:26 | new[] | new[] | test.cpp:308:8:308:10 | ... + ... | ... + ... |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/test.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,18 @@ void test20(unsigned len)
293293
{
294294
*x = 0; // GOOD
295295
}
296-
}
296+
}
297+
298+
void* test21_get(int n);
299+
300+
void test21() {
301+
int n = 0;
302+
while (test21_get(n)) n+=2;
303+
304+
void** xs = new void*[n];
305+
306+
for (int i = 0; i < n; i += 2) {
307+
xs[i] = test21_get(i);
308+
xs[i+1] = test21_get(i+1);
309+
}
310+
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-consistency.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ postWithInFlow
125125
| test.cpp:681:3:681:3 | s [post update] | PostUpdateNode should not be the target of local flow. |
126126
| test.cpp:689:3:689:3 | s [post update] | PostUpdateNode should not be the target of local flow. |
127127
| test.cpp:690:3:690:3 | s [post update] | PostUpdateNode should not be the target of local flow. |
128+
| test.cpp:694:4:694:6 | buf [inner post update] | PostUpdateNode should not be the target of local flow. |
129+
| test.cpp:704:23:704:25 | buf [inner post update] | PostUpdateNode should not be the target of local flow. |
128130
viableImplInCallContextTooLarge
129131
uniqueParameterNodeAtPosition
130132
uniqueParameterNodePosition

cpp/ql/test/library-tests/dataflow/dataflow-tests/test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,16 @@ void test_static_local_9() {
690690
s = 0;
691691
}
692692

693+
void increment_buf(int** buf) { // $ ast-def=buf ir-def=*buf ir-def=**buf
694+
*buf += 10;
695+
sink(buf); // $ SPURIOUS: ast,ir // should only be flow to the indirect argument, but there's also flow to the non-indirect argument
696+
}
697+
698+
void call_increment_buf(int** buf) { // $ ast-def=buf
699+
increment_buf(buf);
700+
}
701+
702+
void test_conflation_regression(int* source) { // $ ast-def=source
703+
int* buf = source;
704+
call_increment_buf(&buf);
705+
}

cpp/ql/test/library-tests/ir/range-analysis/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@
4949
return 0;
5050
}
5151

52+
void* f3_get(int n);
53+
54+
void f3() {
55+
int n = 0;
56+
while (f3_get(n)) n+=2;
57+
58+
for (int i = 0; i < n; i += 2) {
59+
range(i); // $ range=>=0 SPURIOUS: range="<=call to f3_get-1" range="<=call to f3_get-2"
60+
}
61+
}

csharp/ql/consistency-queries/DataFlowConsistency.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ private class MyConsistencyConfiguration extends ConsistencyConfiguration {
7272

7373
override predicate reverseReadExclude(Node n) { n.asExpr() = any(AwaitExpr ae).getExpr() }
7474

75-
override predicate identityLocalStepExclude(Node n) { n.getLocation().getFile().fromLibrary() }
75+
override predicate identityLocalStepExclude(Node n) { none() }
7676
}

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ module LocalFlow {
335335
exists(ControlFlow::BasicBlock bb, int i |
336336
SsaImpl::lastRefBeforeRedefExt(def, bb, i, next.getDefinitionExt()) and
337337
def.definesAt(_, bb, i, _) and
338-
def = getSsaDefinitionExt(nodeFrom)
338+
def = getSsaDefinitionExt(nodeFrom) and
339+
nodeFrom != next
339340
)
340341
}
341342

@@ -414,7 +415,8 @@ module LocalFlow {
414415
) {
415416
exists(CIL::BasicBlock bb, int i | CilSsaImpl::lastRefBeforeRedefExt(def, bb, i, next) |
416417
def.definesAt(_, bb, i, _) and
417-
def = nodeFrom.(CilSsaDefinitionExtNode).getDefinition()
418+
def = nodeFrom.(CilSsaDefinitionExtNode).getDefinition() and
419+
def != next
418420
or
419421
nodeFrom = TCilExprNode(bb.getNode(i).(CIL::ReadAccess))
420422
)
@@ -440,7 +442,8 @@ module LocalFlow {
440442
exists(CIL::ReadAccess readFrom, CIL::ReadAccess readTo |
441443
CilSsaImpl::hasAdjacentReadsExt(def, readFrom, readTo) and
442444
nodeTo = TCilExprNode(readTo) and
443-
nodeFrom = TCilExprNode(readFrom)
445+
nodeFrom = TCilExprNode(readFrom) and
446+
nodeFrom != nodeTo
444447
)
445448
or
446449
// Flow into phi (read) node
@@ -483,7 +486,8 @@ module LocalFlow {
483486
or
484487
hasNodePath(any(LocalExprStepConfiguration x), nodeFrom, nodeTo)
485488
or
486-
ThisFlow::adjacentThisRefs(nodeFrom, nodeTo)
489+
ThisFlow::adjacentThisRefs(nodeFrom, nodeTo) and
490+
nodeFrom != nodeTo
487491
or
488492
ThisFlow::adjacentThisRefs(nodeFrom.(PostUpdateNode).getPreUpdateNode(), nodeTo)
489493
or
@@ -541,7 +545,8 @@ predicate simpleLocalFlowStep(Node nodeFrom, Node nodeTo) {
541545
exists(SsaImpl::DefinitionExt def |
542546
LocalFlow::localSsaFlowStepUseUse(def, nodeFrom, nodeTo) and
543547
not FlowSummaryImpl::Private::Steps::prohibitsUseUseFlow(nodeFrom, _) and
544-
not LocalFlow::usesInstanceField(def)
548+
not LocalFlow::usesInstanceField(def) and
549+
nodeFrom != nodeTo
545550
)
546551
or
547552
// Flow into phi (read)/uncertain SSA definition node from read
@@ -880,7 +885,8 @@ private module Cached {
880885
predicate localFlowStepImpl(Node nodeFrom, Node nodeTo) {
881886
LocalFlow::localFlowStepCommon(nodeFrom, nodeTo)
882887
or
883-
LocalFlow::localSsaFlowStepUseUse(_, nodeFrom, nodeTo)
888+
LocalFlow::localSsaFlowStepUseUse(_, nodeFrom, nodeTo) and
889+
nodeFrom != nodeTo
884890
or
885891
exists(SsaImpl::DefinitionExt def |
886892
LocalFlow::localSsaFlowStep(def, nodeFrom, nodeTo) and

0 commit comments

Comments
 (0)