{{ message }}
Repository navigation
Expand file tree
/
Copy pathDataFlowStack.qll
More file actions
433 lines (384 loc) · 16 KB
/
Copy pathDataFlowStack.qll
File metadata and controls
433 lines (384 loc) · 16 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
overlay[local?]
module;
private import codeql.dataflow.DataFlow as DF
private import codeql.util.Location
signature module DataFlowStackSig<
LocationSig Location, DF::InputSig<Location> Lang, DF::Configs<Location, Lang>::ConfigSig Config>
{
Lang::Node getNode(DF::DataFlowMake<Location, Lang>::Global<Config>::PathNode n);
predicate isSource(DF::DataFlowMake<Location, Lang>::Global<Config>::PathNode n);
DF::DataFlowMake<Location, Lang>::Global<Config>::PathNode getASuccessor(
DF::DataFlowMake<Location, Lang>::Global<Config>::PathNode n
);
Lang::DataFlowCallable getARuntimeTarget(Lang::DataFlowCall call);
Lang::Node getAnArgumentNode(Lang::DataFlowCall call);
}
module DataFlowStackMake<LocationSig Location, DF::InputSig<Location> Lang> {
module DataFlow = DF::DataFlowMake<Location, Lang>;
module BiStackAnalysis<
DF::Configs<Location, Lang>::ConfigSig ConfigA,
DataFlowStackSig<Location, Lang, ConfigA> DataFlowStackA,
DF::Configs<Location, Lang>::ConfigSig ConfigB,
DataFlowStackSig<Location, Lang, ConfigB> DataFlowStackB>
{
module FlowA = DataFlow::Global<ConfigA>;
module FlowStackA = FlowStack<ConfigA, DataFlowStackA>;
module FlowB = DataFlow::Global<ConfigB>;
module FlowStackB = FlowStack<ConfigB, DataFlowStackB>;
/**
* Holds if either the Stack associated with `sourceNodeA` is a subset of the stack associated with `sourceNodeB`
* or vice-versa.
*/
predicate eitherStackSubset(
FlowA::PathNode sourceNodeA, FlowA::PathNode sinkNodeA, FlowB::PathNode sourceNodeB,
FlowB::PathNode sinkNodeB
) {
FlowStackA::isSource(sourceNodeA) and
FlowStackB::isSource(sourceNodeB) and
FlowStackA::isSink(sinkNodeA) and
FlowStackB::isSink(sinkNodeB) and
exists(FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB |
flowStackA = FlowStackA::createFlowStack(sourceNodeA, sinkNodeA) and
flowStackB = FlowStackB::createFlowStack(sourceNodeB, sinkNodeB) and
(
BiStackAnalysisImpl<ConfigA, DataFlowStackA, ConfigB, DataFlowStackB>::flowStackIsSubsetOf(flowStackA,
flowStackB)
or
BiStackAnalysisImpl<ConfigB, DataFlowStackB, ConfigA, DataFlowStackA>::flowStackIsSubsetOf(flowStackB,
flowStackA)
)
)
}
/**
* Holds if the stack associated with path `sourceNodeA` is a subset (and shares a common stack bottom) with
* the stack associated with path `sourceNodeB`, or vice-versa.
*
* For the given pair of (source, sink) for two (potentially disparate) DataFlows,
* determine whether one Flow's Stack (at time of sink execution) is a subset of the other flow's Stack.
*/
predicate eitherStackTerminatingSubset(
FlowA::PathNode sourceNodeA, FlowA::PathNode sinkNodeA, FlowB::PathNode sourceNodeB,
FlowB::PathNode sinkNodeB
) {
FlowStackA::isSource(sourceNodeA) and
FlowStackB::isSource(sourceNodeB) and
FlowStackA::isSink(sinkNodeA) and
FlowStackB::isSink(sinkNodeB) and
exists(FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB |
flowStackA = FlowStackA::createFlowStack(sourceNodeA, sinkNodeA) and
flowStackB = FlowStackB::createFlowStack(sourceNodeB, sinkNodeB) and
(
BiStackAnalysisImpl<ConfigA, DataFlowStackA, ConfigB, DataFlowStackB>::flowStackIsConvergingTerminatingSubsetOf(flowStackA,
flowStackB)
or
BiStackAnalysisImpl<ConfigB, DataFlowStackB, ConfigA, DataFlowStackA>::flowStackIsConvergingTerminatingSubsetOf(flowStackB,
flowStackA)
)
)
}
/**
* Alias for BiStackAnalysisImpl<FlowA,FlowB>::flowStackIsSubsetOf
*
* Holds if stackA is a subset of stackB,
* The top of stackA is in stackB and the bottom of stackA is then some successor further down stackB.
*/
predicate flowStackIsSubsetOf(FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB) {
BiStackAnalysisImpl<ConfigA, DataFlowStackA, ConfigB, DataFlowStackB>::flowStackIsSubsetOf(flowStackA,
flowStackB)
}
/**
* Alias for BiStackAnalysisImpl<FlowA,FlowB>::flowStackIsConvergingTerminatingSubsetOf
*
* If the top of stackA is in stackB at any location, and the bottoms of the stack are the same call.
*/
predicate flowStackIsConvergingTerminatingSubsetOf(
FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB
) {
BiStackAnalysisImpl<ConfigA, DataFlowStackA, ConfigB, DataFlowStackB>::flowStackIsConvergingTerminatingSubsetOf(flowStackA,
flowStackB)
}
}
private module BiStackAnalysisImpl<
DF::Configs<Location, Lang>::ConfigSig ConfigA,
DataFlowStackSig<Location, Lang, ConfigA> DataFlowStackA,
DF::Configs<Location, Lang>::ConfigSig ConfigB,
DataFlowStackSig<Location, Lang, ConfigB> DataFlowStackB>
{
module FlowStackA = FlowStack<ConfigA, DataFlowStackA>;
module FlowStackB = FlowStack<ConfigB, DataFlowStackB>;
/**
* Holds if stackA is a subset of stackB,
* The top of stackA is in stackB and the bottom of stackA is then some successor further down stackB.
*/
predicate flowStackIsSubsetOf(FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB) {
exists(
FlowStackA::FlowStackFrame highestStackFrameA, FlowStackB::FlowStackFrame highestStackFrameB
|
highestStackFrameA = flowStackA.getTopFrame() and
highestStackFrameB = flowStackB.getTopFrame() and
// Check if some intermediary frame `intStackFrameB`of StackB is in the stack of highestStackFrameA
exists(FlowStackB::FlowStackFrame intStackFrameB |
intStackFrameB = highestStackFrameB.getASucceedingTerminalStateFrame*() and
sharesCallWith(highestStackFrameA, intStackFrameB) and
sharesCallWith(flowStackA.getTerminalFrame(),
intStackFrameB.getASucceedingTerminalStateFrame*())
)
)
}
/**
* If the top of stackA is in stackB at any location, and the bottoms of the stack are the same call.
*/
predicate flowStackIsConvergingTerminatingSubsetOf(
FlowStackA::FlowStack flowStackA, FlowStackB::FlowStack flowStackB
) {
flowStackA.getTerminalFrame().getCall() = flowStackB.getTerminalFrame().getCall() and
exists(FlowStackB::FlowStackFrame intStackFrameB |
intStackFrameB = flowStackB.getTopFrame().getASucceedingTerminalStateFrame*() and
sharesCallWith(flowStackA.getTopFrame(), intStackFrameB)
)
}
/**
* Holds if the given FlowStackFrames share the same call.
* i.e. they are both arguments of the same function call.
*/
predicate sharesCallWith(FlowStackA::FlowStackFrame frameA, FlowStackB::FlowStackFrame frameB) {
frameA.getCall() = frameB.getCall()
}
}
module FlowStack<
DF::Configs<Location, Lang>::ConfigSig Config,
DataFlowStackSig<Location, Lang, Config> DataFlowStack>
{
private module Flow = DF::DataFlowMake<Location, Lang>::Global<Config>;
/**
* Determines whether or not the given PathNode is a source
* TODO: Refactor to Flow::PathNode signature
*/
predicate isSource(Flow::PathNode node) { DataFlowStack::isSource(node) }
/**
* Determines whether or not the given PathNode is a sink
* TODO: Refactor to Flow::PathNode signature
*/
predicate isSink(Flow::PathNode node) { not exists(DataFlowStack::getASuccessor(node)) }
/** A FlowStack encapsulates flows between a source and a sink, and all the pathways inbetween (possibly multiple) */
private newtype FlowStackType =
TFlowStack(Flow::PathNode source, Flow::PathNode sink) {
DataFlowStack::isSource(source) and
not exists(DataFlowStack::getASuccessor(sink)) and
DataFlowStack::getASuccessor*(source) = sink
}
class FlowStack extends FlowStackType, TFlowStack {
string toString() { result = "FlowStack" }
/**
* Get the first frame in the DataFlowStack, irregardless of whether or not it has a parent.
*/
FlowStackFrame getFirstFrame() {
exists(FlowStackFrame flowStackFrame, CallFrame frame |
flowStackFrame = TFlowStackFrame(this, frame) and
not exists(frame.getPredecessor()) and
result = flowStackFrame
)
}
/**
* Get the top frame in the DataFlowStack, ie the frame that is the highest in the stack for the given flow.
*/
FlowStackFrame getTopFrame() {
exists(FlowStackFrame flowStackFrame |
flowStackFrame = TFlowStackFrame(this, _) and
not exists(flowStackFrame.getParentStackFrame()) and
result = flowStackFrame
)
}
/**
* Get the terminal frame in the DataFlowStack, ie the frame that is the end of the flow.
*/
FlowStackFrame getTerminalFrame() {
exists(FlowStackFrame flowStackFrame, CallFrame frame |
flowStackFrame = TFlowStackFrame(this, frame) and
not exists(frame.getSuccessor()) and
result = flowStackFrame
)
}
}
FlowStack createFlowStack(Flow::PathNode source, Flow::PathNode sink) {
result = TFlowStack(source, sink)
}
/** A FlowStackFrame encapsulates a Stack frame that is bound between a given source and sink. */
private newtype FlowStackFrameType =
TFlowStackFrame(FlowStack flowStack, CallFrame frame) {
exists(Flow::PathNode source, Flow::PathNode sink |
flowStack = TFlowStack(source, sink) and
frame.getPathNode() = DataFlowStack::getASuccessor*(source) and
DataFlowStack::getASuccessor*(frame.getPathNode()) = sink
)
}
class FlowStackFrame extends FlowStackFrameType, TFlowStackFrame {
string toString() { result = "FlowStackFrame" }
/**
* Get the next frame in the DataFlow Stack
*/
FlowStackFrame getASuccessor() {
exists(FlowStack flowStack, CallFrame frame, CallFrame nextFrame |
this = TFlowStackFrame(flowStack, frame) and
nextFrame = frame.getSuccessor() and
result = TFlowStackFrame(flowStack, nextFrame)
)
}
/**
* Gets the next FlowStackFrame from the direct descendents that is a frame in the end-state (terminal) stack.
*/
FlowStackFrame getASucceedingTerminalStateFrame() {
result = this.getChildStackFrame() and
// There are no other direct children that are further in the flow
not result.getASuccessor+() = this.getChildStackFrame()
}
/**
* Gets a predecessor FlowStackFrame of this FlowStackFrame.
*/
FlowStackFrame getAPredecessor() { result.getASuccessor() = this }
/**
* Gets a predecessor FlowStackFrame that is a parent in the stack.
*/
FlowStackFrame getParentStackFrame() { result.getChildStackFrame() = this }
/**
* Gets the set of succeeding FlowStackFrame which are a direct descendant of this frame in the Stack.
*/
FlowStackFrame getChildStackFrame() {
exists(FlowStackFrame transitiveSuccessor |
transitiveSuccessor = this.getASuccessor+() and
DataFlowStack::getARuntimeTarget(this.getCall()) =
transitiveSuccessor.getCall().getEnclosingCallable() and
result = transitiveSuccessor
)
}
/**
* Unpacks the PathNode associated with this FlowStackFrame
*/
Flow::PathNode getPathNode() {
exists(CallFrame callFrame |
this = TFlowStackFrame(_, callFrame) and
result = callFrame.getPathNode()
)
}
/**
* Unpacks the DataFlowCall associated with this FlowStackFrame
*/
Lang::DataFlowCall getCall() { result = this.getCallFrame().getCall() }
/**
* Unpacks the CallFrame associated with this FlowStackFrame
*/
CallFrame getCallFrame() { this = TFlowStackFrame(_, result) }
}
/**
* A CallFrame is a PathNode that represents a (DataFlowCall/Accessor).
*/
private newtype TCallFrameType =
TCallFrame(Flow::PathNode node) {
exists(Lang::DataFlowCall c |
DataFlowStack::getAnArgumentNode(c) = DataFlowStack::getNode(node)
)
}
/**
* The CallFrame is a PathNode that represents an argument to a Call.
*/
private class CallFrame extends TCallFrameType, TCallFrame {
string toString() {
exists(Lang::DataFlowCall call |
call = this.getCall() and
result = call.toString()
)
}
/**
* Find the set of CallFrames that are immediate successors of this CallFrame.
*/
CallFrame getSuccessor() { result = TCallFrame(getSuccessorCall(this.getPathNode())) }
/**
* Find the set of CallFrames that are an immediate predecessor of this CallFrame.
*/
CallFrame getPredecessor() {
exists(CallFrame prior |
prior.getSuccessor() = this and
result = prior
)
}
/**
* Unpack the CallFrame and retrieve the associated DataFlowCall.
*/
Lang::DataFlowCall getCall() {
exists(Lang::DataFlowCall call, Flow::PathNode node |
this = TCallFrame(node) and
DataFlowStack::getAnArgumentNode(call) = DataFlowStack::getNode(node) and
result = call
)
}
/**
* Unpack the CallFrame and retrieve the associated PathNode.
*/
Flow::PathNode getPathNode() {
exists(Flow::PathNode n |
this = TCallFrame(n) and
result = n
)
}
}
/**
* From the given PathNode argument, find the set of successors that are an argument in a DataFlowCall,
* and return them as the result.
*/
private Flow::PathNode getSuccessorCall(Flow::PathNode n) {
exists(Flow::PathNode succ |
succ = DataFlowStack::getASuccessor(n) and
if
exists(Lang::DataFlowCall c |
DataFlowStack::getAnArgumentNode(c) = DataFlowStack::getNode(succ)
)
then result = succ
else result = getSuccessorCall(succ)
)
}
/**
* A user-supplied predicate which given a Stack Frame, returns some Node associated with it.
*/
signature Lang::Node extractNodeFromFrame(Flow::PathNode pathNode);
/**
* Provides some higher-order predicates for analyzing Stacks
*/
module StackFrameAnalysis<extractNodeFromFrame/1 customFrameCond> {
/**
* Find the highest stack frame that satisfies the given predicate,
* and return the Node(s) that the user-supplied predicate returns.
*
* There should be no higher stack frame that satisfies the user-supplied predicate FROM the point that the
* argument .
*/
Lang::Node extractingFromHighestStackFrame(FlowStack flowStack) {
exists(FlowStackFrame topStackFrame, FlowStackFrame someStackFrame |
topStackFrame = flowStack.getTopFrame() and
someStackFrame = topStackFrame.getASuccessor*() and
result = customFrameCond(someStackFrame.getPathNode()) and
not exists(FlowStackFrame predecessor |
predecessor = someStackFrame.getAPredecessor+() and
// The predecessor is *not* prior to the user-given 'top' of the stack frame.
not predecessor = topStackFrame.getAPredecessor+() and
exists(customFrameCond(predecessor.getPathNode()))
)
)
}
/**
* Find the lowest stack frame that satisfies the given predicate,
* and return the Node(s) that the user-supplied predicate returns.
*/
Lang::Node extractingFromLowestStackFrame(FlowStack flowStack) {
exists(FlowStackFrame topStackFrame, FlowStackFrame someStackFrame |
topStackFrame = flowStack.getTopFrame() and
someStackFrame = topStackFrame.getChildStackFrame*() and
result = customFrameCond(someStackFrame.getPathNode()) and
not exists(FlowStackFrame successor |
successor = someStackFrame.getChildStackFrame+() and
exists(customFrameCond(successor.getPathNode()))
)
)
}
}
}
}
You can’t perform that action at this time.
