Skip to content

Commit f5ed02a

Browse files
committed
C++: Take into account the delta at the final sink in cpp/invalid-pointer-deref
1 parent de974cc commit f5ed02a

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-193/InvalidPointerDeref.ql

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ predicate isSinkImpl(
185185
* reads from an address that non-strictly upper-bounds `sink`.
186186
*/
187187
pragma[inline]
188-
predicate isInvalidPointerDerefSink(DataFlow::Node sink, Instruction i, string operation) {
189-
exists(AddressOperand addr, int delta |
188+
predicate isInvalidPointerDerefSink(DataFlow::Node sink, Instruction i, string operation, int delta) {
189+
exists(AddressOperand addr |
190190
bounded1(addr.getDef(), sink.asInstruction(), delta) and
191191
delta >= 0 and
192192
i.getAnOperand() = addr
@@ -207,7 +207,7 @@ module InvalidPointerToDerefConfig implements DataFlow::ConfigSig {
207207
predicate isSource(DataFlow::Node source) { invalidPointerToDerefSource(_, source, _) }
208208

209209
pragma[inline]
210-
predicate isSink(DataFlow::Node sink) { isInvalidPointerDerefSink(sink, _, _) }
210+
predicate isSink(DataFlow::Node sink) { isInvalidPointerDerefSink(sink, _, _, _) }
211211

212212
predicate isBarrier(DataFlow::Node node) {
213213
node = any(DataFlow::SsaPhiNode phi | not phi.isPhiRead()).getAnInput(true)
@@ -247,7 +247,7 @@ newtype TMergedPathNode =
247247
TPathNodeSink(Instruction i) {
248248
exists(DataFlow::Node n |
249249
InvalidPointerToDerefFlow::flowTo(n) and
250-
isInvalidPointerDerefSink(n, i, _)
250+
isInvalidPointerDerefSink(n, i, _, _)
251251
)
252252
}
253253

@@ -321,7 +321,7 @@ query predicate edges(MergedPathNode node1, MergedPathNode node2) {
321321
or
322322
node1.asPathNode3().getASuccessor() = node2.asPathNode3()
323323
or
324-
joinOn2(node1.asPathNode3(), node2.asSinkNode(), _)
324+
joinOn2(node1.asPathNode3(), node2.asSinkNode(), _, _)
325325
}
326326

327327
query predicate subpaths(
@@ -352,32 +352,32 @@ predicate joinOn1(
352352
* a `StoreInstruction` or `LoadInstruction`.
353353
*/
354354
pragma[inline]
355-
predicate joinOn2(InvalidPointerToDerefFlow::PathNode p1, Instruction i, string operation) {
356-
isInvalidPointerDerefSink(p1.getNode(), i, operation)
355+
predicate joinOn2(InvalidPointerToDerefFlow::PathNode p1, Instruction i, string operation, int delta) {
356+
isInvalidPointerDerefSink(p1.getNode(), i, operation, delta)
357357
}
358358

359359
predicate hasFlowPath(
360360
MergedPathNode source1, MergedPathNode sink, InvalidPointerToDerefFlow::PathNode source3,
361-
PointerArithmeticInstruction pai, string operation
361+
PointerArithmeticInstruction pai, string operation, int delta
362362
) {
363363
exists(InvalidPointerToDerefFlow::PathNode sink3, AllocToInvalidPointerFlow::PathNode1 sink1 |
364364
AllocToInvalidPointerFlow::flowPath(source1.asPathNode1(), _, sink1, _) and
365365
joinOn1(pai, sink1, source3) and
366366
InvalidPointerToDerefFlow::flowPath(source3, sink3) and
367-
joinOn2(sink3, sink.asSinkNode(), operation)
367+
joinOn2(sink3, sink.asSinkNode(), operation, delta)
368368
)
369369
}
370370

371371
from
372-
MergedPathNode source, MergedPathNode sink, int k, string kstr,
372+
MergedPathNode source, MergedPathNode sink, int k2, int k3, string kstr,
373373
InvalidPointerToDerefFlow::PathNode source3, PointerArithmeticInstruction pai, string operation,
374374
Expr offset, DataFlow::Node n
375375
where
376-
hasFlowPath(source, sink, source3, pai, operation) and
377-
invalidPointerToDerefSource(pai, source3.getNode(), k) and
376+
hasFlowPath(source, sink, source3, pai, operation, k3) and
377+
invalidPointerToDerefSource(pai, source3.getNode(), k2) and
378378
offset = pai.getRight().getUnconvertedResultExpression() and
379379
n = source.asPathNode1().getNode() and
380-
if k = 0 then kstr = "" else kstr = " + " + k
380+
if (k2 + k3) = 0 then kstr = "" else kstr = " + " + (k2 + k3)
381381
select sink, source, sink,
382382
"This " + operation + " might be out of bounds, as the pointer might be equal to $@ + $@" + kstr +
383383
".", n, n.toString(), offset, offset.toString()

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,11 @@ subpaths
727727
#select
728728
| 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 |
729729
| 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 |
730-
| 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 $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
731730
| test.cpp:20:14:20:21 | Load: * ... | test.cpp:16:15:16:20 | call to malloc | test.cpp:20:14:20:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:16:15:16:20 | call to malloc | call to malloc | test.cpp:17:19:17:22 | size | size |
732731
| test.cpp:30:14:30:15 | Load: * ... | test.cpp:28:15:28:20 | call to malloc | test.cpp:30:14:30:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:28:15:28:20 | call to malloc | call to malloc | test.cpp:29:20:29:27 | ... + ... | ... + ... |
733732
| test.cpp:32:14:32:21 | Load: * ... | test.cpp:28:15:28:20 | call to malloc | test.cpp:32:14:32:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:28:15:28:20 | call to malloc | call to malloc | test.cpp:29:20:29:27 | ... + ... | ... + ... |
734-
| test.cpp:32:14:32:21 | Load: * ... | test.cpp:28:15:28:20 | call to malloc | test.cpp:32:14:32:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:28:15:28:20 | call to malloc | call to malloc | test.cpp:29:20:29:27 | ... + ... | ... + ... |
735733
| test.cpp:42:14:42:15 | Load: * ... | test.cpp:40:15:40:20 | call to malloc | test.cpp:42:14:42:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:40:15:40:20 | call to malloc | call to malloc | test.cpp:41:20:41:27 | ... - ... | ... - ... |
736734
| test.cpp:44:14:44:21 | Load: * ... | test.cpp:40:15:40:20 | call to malloc | test.cpp:44:14:44:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:40:15:40:20 | call to malloc | call to malloc | test.cpp:41:20:41:27 | ... - ... | ... - ... |
737-
| test.cpp:44:14:44:21 | Load: * ... | test.cpp:40:15:40:20 | call to malloc | test.cpp:44:14:44:21 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:40:15:40:20 | call to malloc | call to malloc | test.cpp:41:20:41:27 | ... - ... | ... - ... |
738735
| test.cpp:67:9:67:14 | Store: ... = ... | test.cpp:52:19:52:24 | call to malloc | test.cpp:67:9:67:14 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:52:19:52:24 | call to malloc | call to malloc | test.cpp:53:20:53:23 | size | size |
739736
| test.cpp:96:9:96:14 | Store: ... = ... | test.cpp:82:17:82:22 | call to malloc | test.cpp:96:9:96:14 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:82:17:82:22 | call to malloc | call to malloc | test.cpp:83:27:83:30 | size | size |
740737
| test.cpp:110:9:110:14 | Store: ... = ... | test.cpp:82:17:82:22 | call to malloc | test.cpp:110:9:110:14 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:82:17:82:22 | call to malloc | call to malloc | test.cpp:83:27:83:30 | size | size |
@@ -752,7 +749,4 @@ subpaths
752749
| test.cpp:341:5:341:21 | Store: ... = ... | test.cpp:325:14:325:27 | new[] | test.cpp:341:5:341:21 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:325:14:325:27 | new[] | new[] | test.cpp:326:20:326:23 | size | size |
753750
| test.cpp:350:15:350:19 | Load: * ... | test.cpp:347:14:347:27 | new[] | test.cpp:350:15:350:19 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:347:14:347:27 | new[] | new[] | test.cpp:348:20:348:23 | size | size |
754751
| test.cpp:358:14:358:26 | Load: * ... | test.cpp:355:14:355:27 | new[] | test.cpp:358:14:358:26 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
755-
| test.cpp:358:14:358:26 | Load: * ... | test.cpp:355:14:355:27 | new[] | test.cpp:358:14:358:26 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
756-
| test.cpp:359:14:359:32 | Load: * ... | test.cpp:355:14:355:27 | new[] | test.cpp:359:14:359:32 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
757752
| test.cpp:359:14:359:32 | Load: * ... | test.cpp:355:14:355:27 | new[] | test.cpp:359:14:359:32 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 2. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
758-
| test.cpp:359:14:359:32 | Load: * ... | test.cpp:355:14:355:27 | new[] | test.cpp:359:14:359:32 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |

0 commit comments

Comments
 (0)