forked from github/securitylab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.qll
348 lines (309 loc) · 9.91 KB
/
callbacks.qll
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
import cpp
import semmle.code.cpp.dataflow.TaintTracking
import callback_tracking
import bindings
import collections
import pointers.managed_ptr
import pointers.raw_ptr
import common
//-------- dataflow edges
predicate callbackStep(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc instanceof BindCall |
exists(FunctionAccess fa, Function f, int i, int j |
fa.getTarget() = f and fa = fc.getArgument(0).getAChild*() and
(
if (f.isStatic() or not exists(f.getDeclaringType()))then
j = i + 1
else
j = i + 2
)
and
node1.asExpr() = fc.getArgument(j) and
node2.asParameter() = f.getParameter(i)
)
or
exists(LambdaExpression lambda, int i, int j |
j = i + 1 and
lambda = fc.getArgument(0) and
node1.asExpr() = fc.getArgument(j) and
node2.asParameter() = lambda.getLambdaFunction().getParameter(i)
)
)
}
predicate unretainedEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName() = "Unretained" or
fc.getTarget().getName() = "UnretainedWrapper" |
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
predicate retainedRefEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName() = "RetainedRef" or
fc.getTarget().getName() = "RetainedRefWrapper" |
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
predicate passEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getQualifiedName() = "base::Passed" or
fc.getTarget().getName() = "PassedRefWrapper" |
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
predicate ownedEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getQualifiedName() = "base::Owned" or
fc.getTarget().getName() = "OwnedWrapper" |
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
predicate adaptCallbackEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().hasName("AdaptCallbackForRepeating") and
node1.asExpr() = fc.getAnArgument() and node2.asExpr() = fc
)
}
predicate callbackWrapperEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().hasName("OnceCallback") or
fc.getTarget().hasName("RepeatingCallback") |
node1.asExpr() = fc.getAnArgument() and
node2.asExpr() = fc
) or
exists(FunctionCall fc | fc.getTarget().hasName("BarrierClosure") and
node1.asExpr() = fc.getArgument(1) and
node2.asExpr() = fc
)
}
predicate callbackToBindEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall bind | bind.getTarget().getName().matches("Bind%") and
node2.asExpr() = bind and
node1.asExpr() = bind.getAnArgument() and
node1.asExpr().getType().stripType().(Class).getABaseClass*().hasName("CallbackBase")
)
}
//-------- dataflow edges end
/**
* A useful predicate to construct call graphs which takes `Bind` callbacks into account.
*/
predicate reach(Function f, Function g) {
(
exists(FunctionCall gc |
if g instanceof Destructor then
g = gc.getTarget()
else
overrides*(g, gc.getTarget())
|
gc.getEnclosingFunction() = f
) or
exists(CallbackSinks sink | sink.getEnclosingCallable() = f and
sink.getCalledFunction() = g and
(sink instanceof CallbackPostTaskSink or sink instanceof CallbackRunSink)
)
)
}
class UnretainedWrapper extends ClassTemplateInstantiation {
UnretainedWrapper() {
stripType().getName().matches("UnretainedWrapper<%")
}
Type getUnretainedType() {
result = getTemplateArgument(0)
}
}
class RetainedRefWrapper extends ClassTemplateInstantiation {
RetainedRefWrapper() {
stripType().getName().matches("RetainedRefWrapper<%")
}
Type getRetainedType() {
result = getTemplateArgument(0)
}
}
class RunCallback extends FunctionCall {
Expr callback;
RunCallback() {
exists(FunctionCall fc | fc.getTarget() instanceof StdMove and
this.getTarget().getName() = "Run" and
this.getQualifier() = fc and
fc.getAnArgument() = callback
)
}
Expr getCallback() {result = callback}
}
/**
* A general callback, either created from `BindOnce`, `BindRepeating` or
* from a wrapper like `AdaptCallbackForRepeating`.
*/
abstract class GeneralCallback extends FunctionCall {
/** Gets a retained type in the binding set of a callback.*/
Type getARetainedType() {
exists(ClassTemplateInstantiation t |
t.getName().matches("RetainedRefWrapper<%") |
generalStripType(getAnArgument().getType()) = t and
result = t.getTemplateArgument(0)
) or
exists(ManagedPtr t | getAnArgument().getType().stripType() = t and
result = t.getManagedType()
) or
exists(PointerType t, Expr arg | arg = getAnArgument() and
t = arg.getType() and
t.stripType().(Class).getABaseClass*().getName().matches("RefCounted%") and
result = t.stripType()
)
}
/**
* Gets an unretained type in the binding set of a callback.
*/
Type getAnUnretainedType() {
exists(ClassTemplateInstantiation t | generalStripType(getAnArgument().getType()) = t and
t.getName().matches("UnretainedWrapper<%") and
result = t.getTemplateArgument(0)
) or
exists(PointerType t, Expr arg | arg = getAnArgument() and
t = arg.getType() and
not exists(Class c | c.getName().matches("RefCounted%") and c = t.stripType().(Class).getABaseClass*()) and
result = t.stripType()
)
}
/**
* Gets an argument of the callback that is another callback. Use for propagating the dataflow when
* tracking unretained types etc.
*/
Expr getACallbackArg() {
result = getAnArgument() and
generalStripType(result.getType()).(Class).getABaseClass+().hasName("CallbackBase")
}
}
/**
* Callbacks that are created using methods like `BindOnce`, `BindRepeating`.
*/
class BindCall extends GeneralCallback {
BindCall() {
this.getTarget().getName().matches("Bind%") and
not this.getTarget().getName() = "Binding" and
not this.getTarget().getName() = "BindState" and
not this.getTarget().getName() = "BindImpl"
}
//TODO: handle case where first argument is a callback
Function getFunction() {
exists(FunctionAccess fa | fa = getArgument(0) and
result = fa.getTarget()
)
}
}
/**
* A callback that is converted from another callback by wrapping it in various functions.
*/
class CallbackWrapper extends GeneralCallback {
CallbackWrapper() {
getTarget().hasName("WrapCallbackWithDefaultInvokeIfNotRun") or
getTarget().hasName("WrapCallbackWithDropHandler") or
getTarget().hasName("AdaptCallbackForRepeating")
}
Expr getWrappedCallbackArg() {
result = getArgument(0)
}
}
class Callback extends Class {
Callback() {
stripType().(Class).getABaseClass+().getName() = "CallbackBase"
}
}
/**
* A `Field` that stores callbacks, either normal callback or a collection.
*/
class CallbackField extends Field {
CallbackField() {
generalStripType(getType()) instanceof Callback or
(
this instanceof GeneralPointerField and
this.(GeneralPointerField).getPointerType() instanceof Callback
)
or
(
this instanceof GeneralManagedField and
this.(GeneralManagedField).getManagedType() instanceof Callback
)
or
(
this instanceof CollectionField and
(
this.getType().(MapType).getComponentType() instanceof Callback or
this.getType().(ListType).getComponentType() instanceof Callback
)
)
}
}
class CallbackSinks extends DataFlow::Node {
DataFlow::Node source;
CallbackSinks() {
exists(CallbackConfig cfg |
cfg.hasFlow(source, this)
)
}
DataFlow::Node getASource() {
result = source
}
/**
* Gets the actuall function that is called in this callback.
*/
Function getCalledFunction() {
exists(GeneralCallback bc | bc = source.asExpr() |
result = bc.getArgument(0).getAChild*().(FunctionAccess).getTarget() or
result = bc.getArgument(0).getAChild*().(LambdaExpression).getLambdaFunction() or
exists(CallbackSinks sink | sink.asExpr() = bc.getArgument(0) and
result = sink.getCalledFunction()
)
) or
exists(CallbackField f, CallbackSinks sink | f.getAnAccess() = source.asExpr() and
sink.asExpr() = generalAssignValue(f) and
result = sink.getCalledFunction()
)
}
Class getARetainedType() {
exists(GeneralCallback bc | bc = source.asExpr() |
result = bc.getARetainedType() or
//propagates through callback args
exists(CallbackSinks sink | sink.asExpr() = bc.getACallbackArg() and
result = sink.getARetainedType()
)
) or
//Propagates through fields
exists(CallbackField f, CallbackSinks sink | f.getAnAccess() = source.asExpr() and
sink.asExpr() = generalAssignValue(f) and
result = sink.getARetainedType()
)
}
Class getAnUnretainedType() {
exists(GeneralCallback bc | bc = source.asExpr() |
result = bc.getAnUnretainedType() or
//propagates through callback args
exists(CallbackSinks sink | sink.asExpr() = bc.getACallbackArg() and
result = sink.getAnUnretainedType()
)
) or
//Propagates through fields
exists(CallbackField f, CallbackSinks sink | f.getAnAccess() = source.asExpr() and
sink.asExpr() = generalAssignValue(f) and
result = sink.getAnUnretainedType()
)
}
}
class CallbackFieldSink extends CallbackSinks {
CallbackFieldSink() {
isCallbackFieldSink(this)
}
}
class CallbackPostTaskSink extends CallbackSinks {
CallbackPostTaskSink() {
postTaskSink(this)
}
}
class CallbackRunSink extends CallbackSinks {
CallbackRunSink() {
runCallbackSink(this)
}
}
class CallbackInterfacePtrSink extends CallbackSinks {
CallbackInterfacePtrSink() {
interfacePtrCallSink(this)
}
}