forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadMacOrder.qll
More file actions
348 lines (306 loc) · 12 KB
/
BadMacOrder.qll
File metadata and controls
348 lines (306 loc) · 12 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
import java
import experimental.quantum.Language
import codeql.util.Option
/**
* Holds when the src node is the output artifact of a decrypt operation
* that flows to the input artifact of a mac operation.
*/
predicate isDecryptToMacFlow(ArtifactFlow::PathNode src, ArtifactFlow::PathNode sink) {
// Simply flow from decrypt output to a mac input
ArtifactFlow::flowPath(src, sink) and
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and
cipherOp.getAnOutputArtifact().asElement() = src.getNode().asExpr()
) and
exists(Crypto::MacOperationNode macOp |
macOp.getAnInputArtifact().asElement() = sink.getNode().asExpr()
)
}
/**
* Experimental interface for graph generation, supply the
* node to determine if a issue exists, and if so
* the graph can add a property on the node.
*/
predicate isDecryptToMacNode(Crypto::ArtifactNode node) {
exists(ArtifactFlow::PathNode src |
isDecryptToMacFlow(src, _) and
node.asElement() = src.getNode().asExpr()
)
}
predicate isDecryptThenMacFlow(DecryptThenMacFlow::PathNode src, DecryptThenMacFlow::PathNode sink) {
DecryptThenMacFlow::flowPath(src, sink)
}
/**
* Holds when the src node is used as plaintext input to both
* an encryption operation and a mac operation, via the
* argument represented by InterimArg.
*/
predicate isPlaintextInEncryptionAndMac(
PlaintextUseAsMacAndCipherInputFlow::PathNode src,
PlaintextUseAsMacAndCipherInputFlow::PathNode sink, EncryptOrMacCallArg arg
) {
PlaintextUseAsMacAndCipherInputFlow::flowPath(src, sink) and
arg = sink.getState().asSome() and
// the above pathing adds flow steps that may not have consideration for the calling context
// TODO: this is something we want to look into to improving, but for now
// we can filter bad flows with one additional flow check, that the source goes to both
// src and sink through a generic flow
// note that the flow path above ensures src gets to the interim arg, so we just need to
// verify the source to sink.
// TODO: having to copy the generic data flow config into a use-use variant
// should we fix this at the language level to allow use use more intuitively?
// Seems to be a common issue.
GenericDataSourceFlowUseUseFlow::flow(src.getNode(), sink.getNode())
}
/**
* A copy of GenericDataSourceFlow but with use-use flows enabled by removing the barrier out
*/
module GenericDataSourceFlowUseUseConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source = any(Crypto::GenericSourceInstance i).getOutputNode()
}
predicate isSink(DataFlow::Node sink) {
sink = any(Crypto::FlowAwareElement other).getInputNode()
}
predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
or
exists(MethodCall m |
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
node1.asExpr() = m.getQualifier() and
node2.asExpr() = m
)
}
}
module GenericDataSourceFlowUseUseFlow = TaintTracking::Global<GenericDataSourceFlowUseUseConfig>;
module WrapperArgFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
// Start from a parameter and not a call to avoid flow going out of
// the call. We want to flow down a call, so start from a parameter
// and barrier flows through returns
exists(Method m | m.getParameter(_) = source.asParameter())
}
predicate isSink(DataFlow::Node sink) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getAnInputArtifact().asElement() = sink.asExpr()
)
or
exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = sink.asExpr())
}
predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}
predicate isBarrierOut(DataFlow::Node node) {
// stop all flow out of a call return
// TODO: this might be too strict and remove taint flow, need to reassess
exists(Call c | c = node.asExpr()) or
node = any(Crypto::FlowAwareElement element).getInputNode()
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
or
exists(MethodCall m |
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
node1.asExpr() = m.getQualifier() and
node2.asExpr() = m
)
}
}
module WrapperArgFlow = TaintTracking::Global<WrapperArgFlowConfig>;
predicate encryptWrapperArg(DataFlow::Node n, DataFlow::Node sink) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and
cipherOp.getAnInputArtifact().asElement() = sink.asExpr()
) and
(
exists(Parameter p, DataFlow::Node src |
p = src.asParameter() and
WrapperArgFlow::flow(src, sink) and
n.asExpr() = p.getAnArgument()
)
or
n = sink // the call the target operation is considered a wrapper arg to itself
)
}
predicate decryptWrapperArg(DataFlow::Node n, DataFlow::Node sink) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and
cipherOp.getAnInputArtifact().asElement() = sink.asExpr()
) and
(
exists(Parameter p, DataFlow::Node src |
p = src.asParameter() and
WrapperArgFlow::flow(src, sink) and
n.asExpr() = p.getAnArgument()
)
or
n = sink // the call the target operation is considered a wrapper arg to itself
)
}
predicate macWrapperArg(DataFlow::Node n, DataFlow::Node sink) {
exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = sink.asExpr()) and
(
exists(Parameter p, DataFlow::Node src |
p = src.asParameter() and
WrapperArgFlow::flow(src, sink) and
n.asExpr() = p.getAnArgument()
)
or
n = sink // the call the target operation is considered a wrapper arg to itself
)
}
module ArgToEncryptOrMacConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { exists(Call c | c.getAnArgument() = source.asExpr()) }
predicate isSink(DataFlow::Node sink) { encryptOrMacSink(sink) }
// Don't go in to a known out node, this will prevent the plaintext
// from tracing out of cipher operations for example, we just want to trace
// the plaintext to uses.
// NOTE: we are not using a barrier out on input nodes, because
// that would remove 'use-use' flows, which we need
predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
or
exists(MethodCall m |
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
node1.asExpr() = m.getQualifier() and
node2.asExpr() = m
)
}
}
module ArgToEncryptOrMacFlow = TaintTracking::Global<ArgToEncryptOrMacConfig>;
/**
* Target sinks for this query are either encryption operations or mac operation message inputs
*/
predicate encryptOrMacSink(DataFlow::Node n) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and
cipherOp.getAnInputArtifact().asElement() = n.asExpr()
)
or
exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = n.asExpr())
}
/**
* Target sinks for decryption operations
*/
predicate decryptSink(DataFlow::Node n) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and
cipherOp.getAnInputArtifact().asElement() = n.asExpr()
)
}
// /**
// * An argument of a target sink or a parent call whose parameter flows to a target sink
// */
// class EncryptOrMacPartialFlowArg extends DataFlow::Node {
// DataFlow::Node targetSink;
// EncryptOrMacPartialFlowArg() {
// encryptWrapperArg(this, targetSink)
// or
// macWrapperArg(this, targetSink)
// }
// DataFlow::Node getTargetSink() { result = targetSink }
// }
class EncryptOrMacCallArg extends DataFlow::Node {
boolean isEncryption;
EncryptOrMacCallArg() {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TEncryptMode() and
cipherOp.getAnInputArtifact().asElement() = this.asExpr()
) and
isEncryption = true
or
exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = this.asExpr()) and
isEncryption = false
}
predicate isEncryption() { isEncryption = true }
predicate isMac() { isEncryption = false }
}
module PlaintextUseAsMacAndCipherInputConfig implements DataFlow::StateConfigSig {
class FlowState = Option<EncryptOrMacCallArg>::Option;
// TODO: can we approximate a message source better?
predicate isSource(DataFlow::Node source, FlowState state) {
// TODO: can we find the 'closest' parameter to the sinks?
// i.e., use a generic source if we have it, but also isolate the
// lowest level in the flow to the closest parameter node in the call graph?
exists(Crypto::GenericSourceNode other |
other.asElement() = CryptoInput::dfn_to_element(source)
) and
state.isNone()
}
predicate isSink(DataFlow::Node sink, FlowState state) {
sink.(EncryptOrMacCallArg).isMac() and state.asSome().isEncryption()
or
sink.(EncryptOrMacCallArg).isEncryption() and state.asSome().isMac()
}
predicate isBarrierOut(DataFlow::Node node, FlowState state) {
// Stop at the first sink for now
isSink(node, state)
}
// Don't go in to a known out node, this will prevent the plaintext
// from tracing out of cipher operations for example, we just want to trace
// the plaintext to uses.
// NOTE: we are not using a barrier out on input nodes, because
// that would remove 'use-use' flows, which we need
predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
or
exists(MethodCall m |
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
node1.asExpr() = m.getQualifier() and
node2.asExpr() = m
)
}
predicate isAdditionalFlowStep(
DataFlow::Node node1, FlowState state1, DataFlow::Node node2, FlowState state2
) {
// TODO: should we consider isSome cases?
state1.isNone() and
(
encryptWrapperArg(node2, node1)
or
macWrapperArg(node2, node1)
) and
state2.asSome() = node1
}
}
module PlaintextUseAsMacAndCipherInputFlow =
TaintTracking::GlobalWithState<PlaintextUseAsMacAndCipherInputConfig>;
module DecryptThenMacConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
exists(Crypto::CipherOperationNode cipherOp |
cipherOp.getKeyOperationSubtype() = Crypto::TDecryptMode() and
cipherOp.getAnInputArtifact().asElement() = source.asExpr()
)
}
predicate isSink(DataFlow::Node sink) {
exists(Crypto::MacOperationNode macOp | macOp.getAnInputArtifact().asElement() = sink.asExpr())
}
// Don't go in to a known out node, prevents
// from tracing out of an operation
// NOTE: we are not using a barrier out on input nodes, because
// that would remove 'use-use' flows, which we need
predicate isBarrierIn(DataFlow::Node node) {
node = any(Crypto::FlowAwareElement element).getOutputNode()
}
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
node1.(AdditionalFlowInputStep).getOutput() = node2
or
exists(MethodCall m |
m.getMethod().hasQualifiedName("java.lang", "String", "getBytes") and
node1.asExpr() = m.getQualifier() and
node2.asExpr() = m
)
or
decryptWrapperArg(node2, node1)
}
}
module DecryptThenMacFlow = TaintTracking::Global<DecryptThenMacConfig>;