Skip to content

Commit e0eadc7

Browse files
committed
C++: Remove the ad-hoc code for keeping track of increments/decrements on pointers in the 'cpp/overrun-write' query.
1 parent a1f4246 commit e0eadc7

File tree

2 files changed

+4
-130
lines changed

2 files changed

+4
-130
lines changed

cpp/ql/src/Security/CWE/CWE-119/OverrunWriteProductFlow.ql

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -88,81 +88,14 @@ module ValidState {
8888

8989
predicate isSink(DataFlow::Node sink) { isSinkPairImpl(_, _, sink, _, _) }
9090

91-
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
92-
isAdditionalFlowStep2(node1, node2, _)
93-
}
94-
95-
predicate includeHiddenNodes() { any() }
91+
predicate isBarrierOut(DataFlow::Node node) { DataFlow::flowsToBackEdge(node) }
9692
}
9793

9894
private import DataFlow::Global<ValidStateConfig>
9995

100-
private predicate inLoop(PathNode n) { n.getASuccessor+() = n }
101-
102-
/**
103-
* Holds if `value` is a possible offset for `n`.
104-
*
105-
* To ensure termination, we limit `value` to be in the
106-
* range `[-2, 2]` if the node is part of a loop. Without
107-
* this restriction we wouldn't terminate on an example like:
108-
* ```cpp
109-
* while(unknown()) { size++; }
110-
* ```
111-
*/
112-
private predicate validStateImpl(PathNode n, int value) {
113-
// If the dataflow node depends recursively on itself we restrict the range.
114-
(inLoop(n) implies value = [-2 .. 2]) and
115-
(
116-
// For the dataflow source we have an allocation such as `malloc(size + k)`,
117-
// and the value of the flow-state is then `k`.
118-
hasSize(_, n.getNode(), value)
119-
or
120-
// For a dataflow sink any `value` that is strictly smaller than the delta
121-
// needs to be a valid flow-state. That is, for a snippet like:
122-
// ```
123-
// p = b ? new char[size] : new char[size + 1];
124-
// memset(p, 0, size + 2);
125-
// ```
126-
// the valid flow-states at the `memset` must include the set `{0, 1}` since the
127-
// flow-state at `new char[size]` is `0`, and the flow-state at `new char[size + 1]`
128-
// is `1`.
129-
//
130-
// So we find a valid flow-state at the sink's predecessor, and use the definition
131-
// of our sink predicate to compute the valid flow-states at the sink.
132-
exists(int delta, PathNode n0 |
133-
n0.getASuccessor() = n and
134-
validStateImpl(n0, value) and
135-
isSinkPairImpl(_, _, n.getNode(), delta, _) and
136-
delta > value
137-
)
138-
or
139-
// For a non-source and non-sink node there is two cases to consider.
140-
// 1. A node where we have to update the flow-state, or
141-
// 2. A node that doesn't update the flow-state.
142-
//
143-
// For case 1, we compute the new flow-state by adding the constant operand of the
144-
// `AddInstruction` to the flow-state of any predecessor node.
145-
// For case 2 we simply propagate the valid flow-states from the predecessor node to
146-
// the next one.
147-
exists(PathNode n0, DataFlow::Node node0, DataFlow::Node node, int value0 |
148-
n0.getASuccessor() = n and
149-
validStateImpl(n0, value0) and
150-
node = n.getNode() and
151-
node0 = n0.getNode()
152-
|
153-
exists(int delta |
154-
isAdditionalFlowStep2(node0, node, delta) and
155-
value0 = value + delta
156-
)
157-
or
158-
not isAdditionalFlowStep2(node0, node, _) and
159-
value = value0
160-
)
161-
)
162-
}
163-
164-
predicate validState(DataFlow::Node n, int value) {
165-
validStateImpl(any(PathNode pn | pn.getNode() = n), value)
96+
predicate validState(DataFlow::Node source, DataFlow::Node sink, int value) {
97+
hasSize(_, source, value) and
98+
flow(source, sink)
16699
}
167100
}
168101

@@ -213,16 +146,6 @@ module StringSizeConfig implements ProductFlow::StateConfigSig {
213146
}
214147

215148
predicate isBarrierOut2(DataFlow::Node node) { DataFlow::flowsToBackEdge(node) }
216-
217-
predicate isAdditionalFlowStep2(
218-
DataFlow::Node node1, FlowState2 state1, DataFlow::Node node2, FlowState2 state2
219-
) {
220-
validState(node2, state2) and
221-
exists(int delta |
222-
isAdditionalFlowStep2(node1, node2, delta) and
223-
state1 = state2 + delta
224-
)
225-
}
226149
}
227150

228151
module StringSizeFlow = ProductFlow::GlobalWithState<StringSizeConfig>;

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ edges
1212
| test.cpp:42:13:42:15 | *str [string] | test.cpp:42:18:42:23 | string | provenance | |
1313
| test.cpp:72:17:72:19 | *str [string] | test.cpp:72:22:72:27 | string | provenance | |
1414
| test.cpp:80:17:80:19 | *str [string] | test.cpp:80:22:80:27 | string | provenance | |
15-
| test.cpp:88:11:88:30 | **mk_string_t_plus_one [string] | test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | provenance | |
16-
| test.cpp:90:5:90:7 | *str [post update] [string] | test.cpp:91:5:91:7 | *str [string] | provenance | |
17-
| test.cpp:90:5:90:34 | ... = ... | test.cpp:90:5:90:7 | *str [post update] [string] | provenance | |
18-
| test.cpp:90:19:90:24 | call to malloc | test.cpp:90:5:90:34 | ... = ... | provenance | |
19-
| test.cpp:91:5:91:7 | *str [string] | test.cpp:92:12:92:14 | *str [string] | provenance | |
20-
| test.cpp:92:12:92:14 | *str [string] | test.cpp:88:11:88:30 | **mk_string_t_plus_one [string] | provenance | |
21-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | provenance | |
22-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | test.cpp:99:13:99:15 | *str [string] | provenance | |
23-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | test.cpp:129:17:129:19 | *str [string] | provenance | |
24-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | test.cpp:137:17:137:19 | *str [string] | provenance | |
25-
| test.cpp:99:13:99:15 | *str [string] | test.cpp:99:18:99:23 | string | provenance | |
26-
| test.cpp:129:17:129:19 | *str [string] | test.cpp:129:22:129:27 | string | provenance | |
27-
| test.cpp:137:17:137:19 | *str [string] | test.cpp:137:22:137:27 | string | provenance | |
2815
| test.cpp:147:5:147:7 | *str [post update] [string] | test.cpp:148:5:148:7 | *str [string] | provenance | |
2916
| test.cpp:147:5:147:34 | ... = ... | test.cpp:147:5:147:7 | *str [post update] [string] | provenance | |
3017
| test.cpp:147:19:147:24 | call to malloc | test.cpp:147:5:147:34 | ... = ... | provenance | |
@@ -46,12 +33,6 @@ edges
4633
| test.cpp:199:17:199:19 | *str [string] | test.cpp:199:22:199:27 | string | provenance | |
4734
| test.cpp:203:17:203:19 | *str [string] | test.cpp:203:22:203:27 | string | provenance | |
4835
| test.cpp:207:17:207:19 | *str [string] | test.cpp:207:22:207:27 | string | provenance | |
49-
| test.cpp:214:24:214:24 | p | test.cpp:216:10:216:10 | p | provenance | |
50-
| test.cpp:220:27:220:54 | call to malloc | test.cpp:220:27:220:54 | call to malloc | provenance | |
51-
| test.cpp:220:27:220:54 | call to malloc | test.cpp:222:15:222:20 | buffer | provenance | |
52-
| test.cpp:222:15:222:20 | buffer | test.cpp:214:24:214:24 | p | provenance | |
53-
| test.cpp:228:27:228:54 | call to malloc | test.cpp:228:27:228:54 | call to malloc | provenance | |
54-
| test.cpp:228:27:228:54 | call to malloc | test.cpp:232:10:232:15 | buffer | provenance | |
5536
| test.cpp:235:40:235:45 | buffer | test.cpp:236:5:236:26 | ... = ... | provenance | |
5637
| test.cpp:236:5:236:9 | *p_str [post update] [string] | test.cpp:235:27:235:31 | *p_str [Return] [string] | provenance | |
5738
| test.cpp:236:5:236:9 | *p_str [post update] [string] | test.cpp:235:27:235:31 | *p_str [string] | provenance | |
@@ -64,8 +45,6 @@ edges
6445
| test.cpp:243:12:243:14 | *str [string] | test.cpp:243:12:243:21 | string | provenance | |
6546
| test.cpp:249:14:249:33 | call to my_alloc | test.cpp:249:14:249:33 | call to my_alloc | provenance | |
6647
| test.cpp:249:14:249:33 | call to my_alloc | test.cpp:250:12:250:12 | p | provenance | |
67-
| test.cpp:256:5:256:25 | ... = ... | test.cpp:257:12:257:12 | p | provenance | |
68-
| test.cpp:256:9:256:25 | call to malloc | test.cpp:256:5:256:25 | ... = ... | provenance | |
6948
| test.cpp:262:15:262:30 | call to malloc | test.cpp:262:15:262:30 | call to malloc | provenance | |
7049
| test.cpp:262:15:262:30 | call to malloc | test.cpp:266:12:266:12 | p | provenance | |
7150
| test.cpp:264:9:264:30 | ... = ... | test.cpp:266:12:266:12 | p | provenance | |
@@ -87,20 +66,6 @@ nodes
8766
| test.cpp:72:22:72:27 | string | semmle.label | string |
8867
| test.cpp:80:17:80:19 | *str [string] | semmle.label | *str [string] |
8968
| test.cpp:80:22:80:27 | string | semmle.label | string |
90-
| test.cpp:88:11:88:30 | **mk_string_t_plus_one [string] | semmle.label | **mk_string_t_plus_one [string] |
91-
| test.cpp:90:5:90:7 | *str [post update] [string] | semmle.label | *str [post update] [string] |
92-
| test.cpp:90:5:90:34 | ... = ... | semmle.label | ... = ... |
93-
| test.cpp:90:19:90:24 | call to malloc | semmle.label | call to malloc |
94-
| test.cpp:91:5:91:7 | *str [string] | semmle.label | *str [string] |
95-
| test.cpp:92:12:92:14 | *str [string] | semmle.label | *str [string] |
96-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | semmle.label | *call to mk_string_t_plus_one [string] |
97-
| test.cpp:96:21:96:40 | *call to mk_string_t_plus_one [string] | semmle.label | *call to mk_string_t_plus_one [string] |
98-
| test.cpp:99:13:99:15 | *str [string] | semmle.label | *str [string] |
99-
| test.cpp:99:18:99:23 | string | semmle.label | string |
100-
| test.cpp:129:17:129:19 | *str [string] | semmle.label | *str [string] |
101-
| test.cpp:129:22:129:27 | string | semmle.label | string |
102-
| test.cpp:137:17:137:19 | *str [string] | semmle.label | *str [string] |
103-
| test.cpp:137:22:137:27 | string | semmle.label | string |
10469
| test.cpp:147:5:147:7 | *str [post update] [string] | semmle.label | *str [post update] [string] |
10570
| test.cpp:147:5:147:34 | ... = ... | semmle.label | ... = ... |
10671
| test.cpp:147:19:147:24 | call to malloc | semmle.label | call to malloc |
@@ -123,14 +88,6 @@ nodes
12388
| test.cpp:203:22:203:27 | string | semmle.label | string |
12489
| test.cpp:207:17:207:19 | *str [string] | semmle.label | *str [string] |
12590
| test.cpp:207:22:207:27 | string | semmle.label | string |
126-
| test.cpp:214:24:214:24 | p | semmle.label | p |
127-
| test.cpp:216:10:216:10 | p | semmle.label | p |
128-
| test.cpp:220:27:220:54 | call to malloc | semmle.label | call to malloc |
129-
| test.cpp:220:27:220:54 | call to malloc | semmle.label | call to malloc |
130-
| test.cpp:222:15:222:20 | buffer | semmle.label | buffer |
131-
| test.cpp:228:27:228:54 | call to malloc | semmle.label | call to malloc |
132-
| test.cpp:228:27:228:54 | call to malloc | semmle.label | call to malloc |
133-
| test.cpp:232:10:232:15 | buffer | semmle.label | buffer |
13491
| test.cpp:235:27:235:31 | *p_str [Return] [string] | semmle.label | *p_str [Return] [string] |
13592
| test.cpp:235:27:235:31 | *p_str [string] | semmle.label | *p_str [string] |
13693
| test.cpp:235:40:235:45 | buffer | semmle.label | buffer |
@@ -145,9 +102,6 @@ nodes
145102
| test.cpp:249:14:249:33 | call to my_alloc | semmle.label | call to my_alloc |
146103
| test.cpp:249:14:249:33 | call to my_alloc | semmle.label | call to my_alloc |
147104
| test.cpp:250:12:250:12 | p | semmle.label | p |
148-
| test.cpp:256:5:256:25 | ... = ... | semmle.label | ... = ... |
149-
| test.cpp:256:9:256:25 | call to malloc | semmle.label | call to malloc |
150-
| test.cpp:257:12:257:12 | p | semmle.label | p |
151105
| test.cpp:262:15:262:30 | call to malloc | semmle.label | call to malloc |
152106
| test.cpp:262:15:262:30 | call to malloc | semmle.label | call to malloc |
153107
| test.cpp:264:9:264:30 | ... = ... | semmle.label | ... = ... |
@@ -163,9 +117,6 @@ subpaths
163117
| test.cpp:42:5:42:11 | call to strncpy | test.cpp:18:19:18:24 | call to malloc | test.cpp:42:18:42:23 | string | This write may overflow $@ by 1 element. | test.cpp:42:18:42:23 | string | string |
164118
| test.cpp:72:9:72:15 | call to strncpy | test.cpp:18:19:18:24 | call to malloc | test.cpp:72:22:72:27 | string | This write may overflow $@ by 1 element. | test.cpp:72:22:72:27 | string | string |
165119
| test.cpp:80:9:80:15 | call to strncpy | test.cpp:18:19:18:24 | call to malloc | test.cpp:80:22:80:27 | string | This write may overflow $@ by 2 elements. | test.cpp:80:22:80:27 | string | string |
166-
| test.cpp:99:5:99:11 | call to strncpy | test.cpp:90:19:90:24 | call to malloc | test.cpp:99:18:99:23 | string | This write may overflow $@ by 1 element. | test.cpp:99:18:99:23 | string | string |
167-
| test.cpp:129:9:129:15 | call to strncpy | test.cpp:90:19:90:24 | call to malloc | test.cpp:129:22:129:27 | string | This write may overflow $@ by 1 element. | test.cpp:129:22:129:27 | string | string |
168-
| test.cpp:137:9:137:15 | call to strncpy | test.cpp:90:19:90:24 | call to malloc | test.cpp:137:22:137:27 | string | This write may overflow $@ by 2 elements. | test.cpp:137:22:137:27 | string | string |
169120
| test.cpp:152:5:152:11 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:152:18:152:23 | string | This write may overflow $@ by 1 element. | test.cpp:152:18:152:23 | string | string |
170121
| test.cpp:154:5:154:11 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:154:18:154:23 | string | This write may overflow $@ by 1 element. | test.cpp:154:18:154:23 | string | string |
171122
| test.cpp:156:5:156:11 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:156:18:156:23 | string | This write may overflow $@ by 2 elements. | test.cpp:156:18:156:23 | string | string |

0 commit comments

Comments
 (0)