Skip to content

Commit 922f4d5

Browse files
committed
C++: Add more documentation to the 'cpp/invalid-pointer-deref' query.
1 parent 0a0e9bb commit 922f4d5

3 files changed

Lines changed: 162 additions & 18 deletions

File tree

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

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
11
/**
22
* This file provides the first phase of the `cpp/invalid-pointer-deref` query that identifies flow
33
* from an allocation to a pointer-arithmetic instruction that constructs a pointer that is out of bounds.
4+
*
5+
* Consider the following snippet:
6+
* ```cpp
7+
* 1. char* begin = (char*)malloc(size);
8+
* 2. char* end = begin + size;
9+
* 3. for(int *p = begin; p <= end; p++) {
10+
* 4. use(*p);
11+
* 5. }
12+
* ```
13+
* this file identifies the flow from `new int[size]` to `base + size`.
14+
*
15+
* This is done using the product-flow library. The configuration tracks flow from the pair `(allocation, size of allocation)`
16+
* to a pair `(a, b)` where there exists a pointer-arithmetic instruction `pai` such that:
17+
* 1. `a` is equal to the left-hand side of `pai`, and
18+
* 2. `b` is a dataflow node that represents an operand that _non-strictly_ upper bounds the right-hand side of `pai`.
19+
* See `pointerAddInstructionHasBounds` for the implementation of this.
20+
*
21+
* In the above example, the pair `(a, b)` is `(base, size)` from the expression `base + size` on line 2. However, it could
22+
* also be something more complex like `(base, size)` where `base` is from line 3 and `size` is from line 2, and the
23+
* pointer-arithmetic instruction is `base + n` on line 3 in the following example:
24+
* ```cpp
25+
* 1. int* base = new int[size];
26+
* 2. if(n <= size) {
27+
* 3. int* end = base + n;
28+
* 4. for(int* p = base; p <= end; ++p) {
29+
* 5. *p = 0; // BUG: Should have been bounded by `p < end`.
30+
* 6. }
31+
* 7. }
32+
* ```
33+
*
34+
* Reducing the size of `pointerAddInstructionHasBounds`:
35+
* The `pointerAddInstructionHasBounds` can be very large since the `sink2` column is defined as anything that non-strictly
36+
* upper bounds the right-hand side of a pointer-arithmetic instruction. In order to reduce the size of this predicate we prune
37+
* the set of pointer-arithmetic instructions to only those instructions where the left-hand side flows from an allocation.
38+
*
39+
* Handling false positives:
40+
*
41+
* Consider a snippet such as:
42+
* ```cpp
43+
* 1. int* base = new int[size];
44+
* 2. int n = condition() ? size : 0;
45+
* 3. if(n >= size) return;
46+
* 4. int* end = base + n;
47+
* 5. for(int* p = base; p <= end; ++p) {
48+
* 6. *p = 0; // This is fine since `end < base + size`
49+
* 7. }
50+
* ```
51+
* In order to remove this false positive we define a barrier (see `Barrier2::BarrierConfig2`) that finds the possible guards
52+
* that compares a value to the size of the allocation. In the above example, that's the `(n >= size)` guard on line 3.
53+
* `Barrier2::getABarrierNode` then defines any node that's guarded by such a guard as a barrier in the dataflow configuration.
454
*/
555

656
private import cpp
@@ -151,24 +201,8 @@ private module InterestingPointerAddInstruction {
151201
}
152202

153203
/**
154-
* A product-flow configuration for flow from an (allocation, size) pair to a
155-
* pointer-arithmetic operation that is non-strictly upper-bounded by `allocation + size`.
156-
*
157-
* The goal of this query is to find patterns such as:
158-
* ```cpp
159-
* 1. char* begin = (char*)malloc(size);
160-
* 2. char* end = begin + size;
161-
* 3. for(int *p = begin; p <= end; p++) {
162-
* 4. use(*p);
163-
* 5. }
164-
* ```
165-
*
166-
* We do this by splitting the task up into two configurations:
167-
* 1. `AllocToInvalidPointerConfig` find flow from `malloc(size)` to `begin + size`, and
168-
* 2. `InvalidPointerToDerefConfig` finds flow from `begin + size` to an `end` (on line 3).
169-
*
170-
* Finally, the range-analysis library will find a load from (or store to) an address that
171-
* is non-strictly upper-bounded by `end` (which in this case is `*p`).
204+
* A product-flow configuration for flow from an `(allocation, size)` pair to a pointer-
205+
* arithmetic instruction that is non-strictly upper-bounded by `allocation + size`.
172206
*/
173207
private module Config implements ProductFlow::StateConfigSig {
174208
class FlowState1 = Unit;

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,71 @@
22
* This file provides the second phase of the `cpp/invalid-pointer-deref` query that identifies flow
33
* from the out-of-bounds pointer identified by the `AllocationToInvalidPointer.qll` library to
44
* a dereference of the out-of-bounds pointer.
5+
*
6+
* Consider the following snippet:
7+
* ```cpp
8+
* 1. char* begin = (char*)malloc(size);
9+
* 2. char* end = begin + size;
10+
* 3. for(int *p = begin; p <= end; p++) {
11+
* 4. use(*p);
12+
* 5. }
13+
* ```
14+
* this file identifies the flow from `base + size` to `end`. We call `base + size` the "dereference source" and `end`
15+
* the "dereference sink" (even though `end` is not actually dereferenced - it will be used to find the correct
16+
* dereference eventually).
17+
*
18+
* Merely _constructing_ a pointer that's out-of-bounds is fine if the pointer is never dereferenced (in reality, the
19+
* standard only guarentees that it's safe to move the pointer one element past the last element. But we ignore that
20+
* here). So this step is about identifying which of those out-of-bounds pointers identified from step 1 that are
21+
* actually being dereferenced. We do this using a regular dataflow configuration (see `InvalidPointerToDerefConfig`).
22+
*
23+
* This dataflow traversal defines the set of sources as any dataflow node that is non-strictly upper-bounded by the
24+
* pointer-arithmetic instruction identified by `AllocationToInvalidPointer.qll`. (TOOD: I'm pretty sure this is incorrect,
25+
* and we should define the set of sources as anything that is non-strictly _lower_ bounded by the pointer-arithmetic
26+
* instruction). That is, the set of sources is any dataflow node `source` such that `source.asInstruction <= pai + delta1`
27+
* for some `delta1 >= 0`.
28+
*
29+
* The set of sinks is defined to be any address operand `addr` that is non-strictly upper-bounded by the sink. That is,
30+
* any dataflow node `n` such that `addr <= sink.asInstruction() + delta2` for some `delta2`. We call the instruction that
31+
* consumes the address operand the "operation".
32+
*
33+
* For example, consider the flow from `begin + size` to `end` above. The sink is `end` on line 3 because that is a dataflow
34+
* node whose underlying instruction non-strictly upper bounds the address operand `p` in `use(*p)`. The load attached to `*p`
35+
* is the "operation". To ensure that the path makes intuitive sense, we only pick operations that are control-flow reachable
36+
* from the dereference sink.
37+
*
38+
* To compute the amount of the dereference is away from the final entry of the allocation, we sum the two deltas `delta1` and
39+
* `delta2`. This is done in the `operationIsOffBy` predicate (which is the only predicate exposed by this file).
40+
*
41+
* Handling false positives:
42+
*
43+
* Consider the following snippet:
44+
* ```cpp
45+
* 1. char *p = new char[size];
46+
* 2. char *end = p + size; // $ alloc=L363
47+
* 3. if (p < end) {
48+
* 4. p += 1;
49+
* 5. }
50+
* 6. if (p < end) {
51+
* 7. int val = *p; // GOOD
52+
* 8. }
53+
* ```
54+
* this is safe because `p` is guarded to be strictly less than `end` on line 6 before the dereference on line 7. However, if we
55+
* run the query on the above without further modifications we'd see an alert on line 7. This is because range analysis infers
56+
* that `p <= end` after the increment on line 4, and thus the result of `p += 1` is seen as a valid dereference source. This
57+
* node then flows to `p` on line 6 (which is a valid dereference sink since it non-strictly upper bounds an address operand), and
58+
* range analysis then infers that the address operand of `*p` (i.e., `p`) is non-strictly upper bounded by `p`, and thus reports
59+
* an alert on line 7.
60+
*
61+
* In order to handle this false positive, we define a barrier that identifies guards such as `p < end` that ensures that a value
62+
* is less than the pointer-arithmetic instruction that computed the invalid pointer. This is done in the `InvalidPointerToDerefBarrier`
63+
* module. Since the node we're tracking isn't necessarily _equal_ to the pointer-arithmetic instruction, but rather satisfies
64+
* `node.asInstruction() <= pai + delta`, we need to account for the delta when checking if a guard is sufficiently strong to infer
65+
* that a future dereference is safe. To do this, we check that the guard guarantees that a node `n` satisfies `n < node + d` where
66+
* `node` is a node we know is equal to the value of the dereference source (i.e., it satisfies `node.asInstruction() <= pai + delta`)
67+
* and `d <= delta`. Combining this we have `n < node + d <= node + delta <= pai + 2*delta` (TODO: Oops. This math doesn't quite work
68+
* out. This is because we need to redefine the `BarrierConfig` to start flow at the pointer-arithmetic instruction instead of at the
69+
* dereference source. When combined with TODO above it's easy to show that this guard ensures that the dereference is safe).
570
*/
671

772
private import cpp

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,51 @@
1515
* external/cwe/cwe-787
1616
*/
1717

18+
/*
19+
* High-level description of the query:
20+
*
21+
* The goal of this query is to identify issues such as:
22+
* ```cpp
23+
* 1. int* base = new int[size];
24+
* 2. int* end = base + size;
25+
* 3. for(int* p = base; p <= end; ++p) {
26+
* 4. *p = 0; // BUG: Should have been bounded by `p < end`.
27+
* 5. }
28+
* ```
29+
* In order to do this, we split the problem into three subtasks:
30+
* 1. First, we find flow from `new int[size]` to `base + size`.
31+
* 2. Then, we find flow from `base + size` to `end` (on line 3).
32+
* 3. Finally, we use range-analysis to find a write to (or read from) a pointer that may be equal to `end`.
33+
*
34+
* Step 1 is implemented in `AllocationToInvalidPointer.qll`, and step 2 is implemented by
35+
* `InvalidPointerToDereference.qll`. See those files for the description of these.
36+
*
37+
* This file imports both libraries and define a final dataflow configuration that constructs the full path from
38+
* the allocation to the dereference of the out-of-bounds pointer. This is done for several reasons:
39+
* 1. It means the user is able to inspect the entire path from the allocation to the dereference, which can be useful
40+
* to understand the problem highlighted.
41+
* 2. It ensures that the call-contexts line up correctly when we transition from step 1 to step 2. See the
42+
* `test_missing_call_context_1` and `test_missing_call_context_2` tests for how this may flag false positives
43+
* without this final configuration.
44+
*
45+
* The source of the final path is an allocation that's:
46+
* 1. identified as flowing to an invalid pointer (by `AllocationToInvalidPointer`), and
47+
* 2. for which the invalid pointer flows to a dereference (as identified by `InvalidPointerToDereference`).
48+
*
49+
* The path can be described in 3 "chunks":
50+
* 1. One path from the allocation to the construction of the invalid pointer
51+
* 2. Another path from the construction of the invalid pointer to the final pointer that's about to be dereferenced.
52+
* 3. Finally, there's a single step from the dataflow node that represents the final pointer to the dereference.
53+
*
54+
* Step 1 happens when the flow state is `TInitial`, and step 2 and 3 happens when the flow state is `TPointerArith(pai)`
55+
* where the pointer-arithmetic instruction `pai` tracks the instruction that generated the out-of-bounds pointer. This
56+
* instruction is used in the construction of the alert message.
57+
*
58+
* The set of pointer-arithmetic instructions that define the `TPointerArith` flow state is restricted to be the pointer-
59+
* arithmetic instructions that both receive flow from the allocation (as identified by `AllocationToInvalidPointer.qll`),
60+
* and further flows to a dereference (as identified by `InvalidPointerToDereference.qll`).
61+
*/
62+
1863
import cpp
1964
import semmle.code.cpp.dataflow.new.DataFlow
2065
import semmle.code.cpp.ir.IR

0 commit comments

Comments
 (0)