-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathUniversalFlow.qll
More file actions
386 lines (327 loc) · 11.5 KB
/
Copy pathUniversalFlow.qll
File metadata and controls
386 lines (327 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Provides predicates for proving data flow properties that hold for all
* paths, that is, reachability is computed using universal quantification over
* the step relation.
*
* Regular data flow search for the existence of a path, that is, reachability
* using existential quantification over the step relation. Hence, this library
* computes the dual reachability predicate that states that a certain property
* always holds for a given node regardless of the path taken.
*
* As a simple comparison, the computed predicate is essentially equivalent to
* the folllowing:
* ```ql
* predicate hasProperty(FlowNode n, Prop t) {
* basecase(n, t)
* or
* forex(FlowNode mid | step(mid, n) | hasProperty(mid, t))
* }
* ```
* More complex property propagation is supported, and strongly connected
* components in the flow graph are handled.
*
* As an initial such property calculation, the library computes the set of
* nodes that are always null. These are then subtracted from the graph such
* that subsequently calculated properties hold under the assumption that the
* value is not null.
*/
overlay[local?]
module;
private import codeql.util.Location
private import codeql.util.Unit
/** Provides the input specification. */
signature module UniversalFlowInput<LocationSig Location> {
/**
* A node for which certain data flow properties may be proved. For example,
* expressions and method declarations.
*/
class FlowNode {
/** Gets a textual representation of this node. */
string toString();
/** Gets the location of this node. */
Location getLocation();
}
/**
* Gets an identifier for node `n`, if any. When no identifier is provided for `n`,
* the library falls back to location-based ranking.
*/
default int getFlowNodeId(FlowNode n) { none() }
/**
* Holds if data can flow from `n1` to `n2` in one step.
*
* For a given `n2`, this predicate must include all possible `n1` that can flow to `n2`.
*/
predicate step(FlowNode n1, FlowNode n2);
/** Holds if `n` represents a `null` value. */
predicate isNullValue(FlowNode n);
/**
* Holds if `n` should be excluded from the set of null values even if
* the null analysis determines that `n` is always null.
*/
default predicate isExcludedFromNullAnalysis(FlowNode n) { none() }
}
/**
* Provides an implementation of universal flow using input `I`.
*/
module Make<LocationSig Location, UniversalFlowInput<Location> I> {
private import I
/**
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is
* functionally determined by `n2`.
*/
private predicate uniqStep(FlowNode n1, FlowNode n2) { n1 = unique(FlowNode n | step(n, n2)) }
/**
* Holds if data can flow from `n1` to `n2` in one step, and `n1` is not
* functionally determined by `n2`.
*/
private predicate joinStep(FlowNode n1, FlowNode n2) { step(n1, n2) and not uniqStep(n1, n2) }
/** Holds if `null` is the only value that flows to `n`. */
private predicate isNull(FlowNode n) {
isNullValue(n)
or
not isExcludedFromNullAnalysis(n) and
(
exists(FlowNode mid | isNull(mid) and uniqStep(mid, n))
or
forex(FlowNode mid | joinStep(mid, n) | isNull(mid))
)
}
private predicate uniqStepNotNull(FlowNode n1, FlowNode n2) {
uniqStep(n1, n2) and not isNull(n1)
}
private import Internal
/** Provides access to internal step relations. */
module Internal {
/**
* Holds if data can flow from `n1` to `n2` in one step, `n1` is not necessarily
* functionally determined by `n2`, and `n1` might take a non-null value.
*/
predicate joinStepNotNull(FlowNode n1, FlowNode n2) { joinStep(n1, n2) and not isNull(n1) }
/**
* Holds if data can flow from `n1` to `n2` in one step, excluding join
* steps from nodes that are always null.
*/
predicate anyStep(FlowNode n1, FlowNode n2) {
joinStepNotNull(n1, n2) or uniqStepNotNull(n1, n2)
}
}
private predicate sccEdge(FlowNode n1, FlowNode n2) { anyStep(n1, n2) and anyStep+(n2, n1) }
private module Scc = QlBuiltins::EquivalenceRelation<FlowNode, sccEdge/2>;
private class FlowScc = Scc::EquivalenceClass;
/** Holds if `n` is part of an SCC of size 2 or more represented by `scc`. */
private predicate sccRepr(FlowNode n, FlowScc scc) { scc = Scc::getEquivalenceClass(n) }
private predicate sccJoinStepNotNull(FlowNode n, FlowScc scc) {
exists(FlowNode mid |
joinStepNotNull(n, mid) and
sccRepr(mid, scc) and
not sccRepr(n, scc)
)
}
private signature class NodeSig;
private signature module Edge {
class Node;
predicate edge(FlowNode n1, Node n2);
}
private signature module RankedEdge<NodeSig Node> {
predicate edgeRank(int r, FlowNode n1, Node n2);
int lastRank(Node n);
}
private module RankEdge<Edge E> implements RankedEdge<E::Node> {
private import E
private predicate needsNodeId(FlowNode n) { edge(n, _) }
private int getFlowNodeIdByLoc(FlowNode n) {
n =
rank[result](FlowNode n0, string filePath, int startline, int startcolumn |
needsNodeId(n0) and
not exists(getFlowNodeId(n0)) and
n0.getLocation().hasLocationInfo(filePath, startline, startcolumn, _, _)
|
n0 order by filePath, startline, startcolumn
)
}
private int getFlowNodeIdExt(FlowNode n) {
n =
rank[result](FlowNode n0, int a, int b |
needsNodeId(n0) and
a = 0 and
b = getFlowNodeId(n0)
or
a = 1 and
b = getFlowNodeIdByLoc(n0)
|
n0 order by a, b
)
}
/**
* Holds if `r` is a ranking of the incoming edges `(n1,n2)` to `n2`. The used
* ordering is not necessarily total, so the ranking may have gaps.
*/
private predicate edgeRank1(int r, FlowNode n1, Node n2) {
n1 =
rank[r](FlowNode n, int id |
edge(n, n2) and
id = getFlowNodeIdExt(n)
|
n order by id
)
}
/**
* Holds if `r2` is a ranking of the ranks from `edgeRank1`. This removes the
* gaps from the ranking.
*/
private predicate edgeRank2(int r2, int r1, Node n) {
r1 = rank[r2](int r | edgeRank1(r, _, n) | r)
}
/** Holds if `r` is a ranking of the incoming edges `(n1,n2)` to `n2`. */
predicate edgeRank(int r, FlowNode n1, Node n2) {
exists(int r1 |
edgeRank1(r1, n1, n2) and
edgeRank2(r, r1, n2)
)
}
int lastRank(Node n) { result = max(int r | edgeRank(r, _, n)) }
}
private signature module PropPropagation {
class Prop;
predicate candProp(FlowNode n, Prop t);
bindingset[t]
predicate supportsProp(FlowNode n, Prop t);
}
/** Implements recursion through `forall` by way of edge ranking. */
private module ForAll<NodeSig Node, RankedEdge<Node> E, PropPropagation T> {
/**
* Holds if `t` is a property that holds on one of the incoming edges to `n` and
* thus is a candidate property for `n`.
*/
pragma[nomagic]
private predicate candJoinProp(Node n, T::Prop t) {
exists(FlowNode mid |
T::candProp(mid, t) and
E::edgeRank(_, mid, n)
)
}
/**
* Holds if `t` is a candidate property for `n` that is also valid for data coming
* through the edges into `n` ranked from `1` to `r`.
*/
private predicate flowJoin(int r, Node n, T::Prop t) {
(
r = 1 and candJoinProp(n, t)
or
flowJoin(r - 1, n, t) and E::edgeRank(r, _, n)
) and
forall(FlowNode mid | E::edgeRank(r, mid, n) | T::supportsProp(mid, t))
}
/**
* Holds if `t` is a candidate property for `n` that is also valid for data
* coming through all the incoming edges, and therefore is a valid property for
* `n`.
*/
predicate flowJoin(Node n, T::Prop t) { flowJoin(E::lastRank(n), n, t) }
}
private module JoinStep implements Edge {
class Node = FlowNode;
predicate edge = joinStepNotNull/2;
}
private module SccJoinStep implements Edge {
class Node = FlowScc;
predicate edge = sccJoinStepNotNull/2;
}
private module RankedJoinStep = RankEdge<JoinStep>;
private module RankedSccJoinStep = RankEdge<SccJoinStep>;
signature module NullaryPropertySig {
predicate hasPropertyBase(FlowNode n);
default predicate barrier(FlowNode n) { none() }
}
/**
* Calculates a (nullary) property using universal flow given a base case
* relation.
*/
module FlowNullary<NullaryPropertySig P> {
private module Propagation implements PropPropagation {
class Prop = Unit;
predicate candProp(FlowNode n, Unit u) { hasProperty(n) and exists(u) }
predicate supportsProp = candProp/2;
}
/**
* Holds if all flow reaching `n` originates from nodes in
* `hasPropertyBase`.
*/
predicate hasProperty(FlowNode n) {
P::hasPropertyBase(n)
or
not P::barrier(n) and
(
exists(FlowNode mid | hasProperty(mid) and uniqStepNotNull(mid, n))
or
// The following is an optimized version of
// `forex(FlowNode mid | joinStepNotNull(mid, n) | hasPropery(mid))`
ForAll<FlowNode, RankedJoinStep, Propagation>::flowJoin(n, _)
or
exists(FlowScc scc |
sccRepr(n, scc) and
// Optimized version of
// `forex(FlowNode mid | sccJoinStepNotNull(mid, scc) | hasPropery(mid))`
ForAll<FlowScc, RankedSccJoinStep, Propagation>::flowJoin(scc, _)
)
)
}
}
signature module PropertySig {
class Prop;
bindingset[t1, t2]
default predicate propImplies(Prop t1, Prop t2) { t1 = t2 }
predicate hasPropertyBase(FlowNode n, Prop t);
default predicate barrier(FlowNode n) { none() }
}
/**
* Calculates a unary property using universal flow given a base case
* relation.
*/
module Flow<PropertySig P> {
private module Propagation implements PropPropagation {
class Prop = P::Prop;
predicate candProp = hasProperty/2;
bindingset[t]
predicate supportsProp(FlowNode n, Prop t) {
exists(Prop t0 | hasProperty(n, t0) and P::propImplies(t0, t))
}
}
/**
* Holds if all flow reaching `n` originates from nodes in
* `hasPropertyBase`. The property `t` is taken from one of those origins
* such that all other origins imply `t`.
*/
predicate hasProperty(FlowNode n, P::Prop t) {
P::hasPropertyBase(n, t)
or
not P::barrier(n) and
(
exists(FlowNode mid | hasProperty(mid, t) and uniqStepNotNull(mid, n))
or
// The following is an optimized version of
// ```
// exists(FlowNode mid | joinStepNotNull(mid, n) | hasPropery(mid, t)) and
// forall(FlowNode mid | joinStepNotNull(mid, n) | hasPropery(mid, _)) and
// forall(FlowNode mid, P::Prop t0 | joinStepNotNull(mid, n) and hasPropery(mid, t0) |
// P::propImplies(t0, t)
// )
// ```
ForAll<FlowNode, RankedJoinStep, Propagation>::flowJoin(n, t)
or
exists(FlowScc scc |
sccRepr(n, scc) and
// Optimized version of
// ```
// exists(FlowNode mid | sccJoinStepNotNull(mid, n) | hasPropery(mid, t)) and
// forall(FlowNode mid | sccJoinStepNotNull(mid, n) | hasPropery(mid, _)) and
// forall(FlowNode mid, P::Prop t0 | sccJoinStepNotNull(mid, n) and hasPropery(mid, t0) |
// P::propImplies(t0, t)
// )
// ```
ForAll<FlowScc, RankedSccJoinStep, Propagation>::flowJoin(scc, t)
)
)
}
}
}