Skip to content

Commit 83aef6f

Browse files
committed
C++: Write formulas instead of 'non-strictly upper bounded by'.
1 parent 108cd7f commit 83aef6f

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

cpp/ql/lib/semmle/code/cpp/security/InvalidPointerDereference/AllocationToInvalidPointer.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ predicate hasSize(HeuristicAllocationExpr alloc, DataFlow::Node n, int state) {
4242
* ```
4343
* In this case, the sink pair identified by the product flow library (without any additional barriers)
4444
* would be `(p, n)` (where `n` is the `n` in `p[n]`), because there exists a pointer-arithmetic
45-
* instruction `pai` such that:
46-
* 1. The left-hand of `pai` flows from the allocation, and
47-
* 2. The right-hand of `pai` is non-strictly upper bounded by `n` (where `n` is the `n` in `p[n]`)
45+
* instruction `pai = a + b` such that:
46+
* 1. the allocation flows to `a`, and
47+
* 2. `b <= n` where `n` is the `n` in `p[n]`
4848
* but because there's a strict comparison that compares `n` against the size of the allocation this
4949
* snippet is fine.
5050
*/
@@ -147,8 +147,8 @@ private module InterestingPointerAddInstruction {
147147
}
148148

149149
/**
150-
* A product-flow configuration for flow from an (allocation, size) pair to a
151-
* pointer-arithmetic operation that is non-strictly upper-bounded by `allocation + size`.
150+
* A product-flow configuration for flow from an `(allocation, size)` pair to a
151+
* pointer-arithmetic operation `pai` such that `pai <= allocation + size`.
152152
*
153153
* The goal of this query is to find patterns such as:
154154
* ```cpp
@@ -207,7 +207,7 @@ private module Config implements ProductFlow::StateConfigSig {
207207
private module AllocToInvalidPointerFlow = ProductFlow::GlobalWithState<Config>;
208208

209209
/**
210-
* Holds if `pai` is non-strictly upper bounded by `sink2 + delta` and `sink1` is the
210+
* Holds if `pai` is non-strictly upper bounded by `sizeSink + delta` and `allocSink` is the
211211
* left operand of the pointer-arithmetic operation.
212212
*
213213
* For example in,
@@ -216,8 +216,8 @@ private module AllocToInvalidPointerFlow = ProductFlow::GlobalWithState<Config>;
216216
* ```
217217
* We will have:
218218
* - `pai` is `p + (size + 1)`,
219-
* - `sink1` is `p`
220-
* - `sink2` is `size`
219+
* - `allocSink` is `p`
220+
* - `sizeSink` is `size`
221221
* - `delta` is `1`.
222222
*/
223223
pragma[nomagic]
@@ -237,9 +237,9 @@ private predicate pointerAddInstructionHasBounds0(
237237
}
238238

239239
/**
240-
* Holds if `allocation` flows to `sink1` and `sink1` represents the left-hand
241-
* side of the pointer-arithmetic instruction `pai`, and the right-hand side of `pai`
242-
* is non-strictly upper bounded by the size of `alllocation` + `delta`.
240+
* Holds if `allocation` flows to `allocSink` and `allocSink` represents the left operand
241+
* of the pointer-arithmetic instruction `pai = a + b` (i.e., `allocSink = a`), and
242+
* `b <= allocation + delta`.
243243
*/
244244
pragma[nomagic]
245245
predicate pointerAddInstructionHasBounds(

cpp/ql/lib/semmle/code/cpp/security/InvalidPointerDereference/InvalidPointerToDereference.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ private module InvalidPointerToDerefConfig implements DataFlow::ConfigSig {
8585
private import DataFlow::Global<InvalidPointerToDerefConfig>
8686

8787
/**
88-
* Holds if `source1` is dataflow node that represents an allocation that flows to the
89-
* left-hand side of the pointer-arithmetic `pai`, and `derefSource` is a dataflow node with
90-
* a pointer-value that is non-strictly upper bounded by `pai + delta`.
88+
* Holds if `allocSource` is dataflow node that represents an allocation that flows to the
89+
* left-hand side of the pointer-arithmetic `pai`, and `derefSource <= pai + derefSourcePaiDelta`.
9190
*
9291
* For example, if `pai` is a pointer-arithmetic operation `p + size` in an expression such
9392
* as `(p + size) + 1` and `derefSource` is the node representing `(p + size) + 1`. In this
@@ -114,8 +113,8 @@ private predicate invalidPointerToDerefSource(
114113

115114
/**
116115
* Holds if `sink` is a sink for `InvalidPointerToDerefConfig` and `i` is a `StoreInstruction` that
117-
* writes to an address that non-strictly upper-bounds `sink`, or `i` is a `LoadInstruction` that
118-
* reads from an address that non-strictly upper-bounds `sink`.
116+
* writes to an address `addr` such that `addr <= sink`, or `i` is a `LoadInstruction` that
117+
* reads from an address `addr` such that `addr <= sink`.
119118
*/
120119
pragma[inline]
121120
private predicate isInvalidPointerDerefSink(

0 commit comments

Comments
 (0)