Repository navigation
Expand file tree
/
Copy pathStaticInitializationVectorExtended.qll
More file actions
232 lines (216 loc) · 8.37 KB
/
Copy pathStaticInitializationVectorExtended.qll
File metadata and controls
232 lines (216 loc) · 8.37 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
/**
* Provides a taint-tracking configuration for finding static initialization
* vectors (IVs) that the standard `swift/static-initialization-vector` query
* does not report:
*
* - IVs passed to the CommonCrypto C API (`CCCrypt`, `CCCryptorCreate`,
* `CCCryptorCreateWithMode` and `CCCryptorReset`), which the standard query
* does not model as sinks.
* - `nil` IVs passed to CommonCrypto, which CommonCrypto replaces with an
* all-zero IV.
* - IVs that come from the default value of a parameter, which is used
* whenever a caller omits the argument.
*/
import swift
import codeql.swift.dataflow.DataFlow
import codeql.swift.dataflow.TaintTracking
import codeql.swift.security.StaticInitializationVectorQuery
private import codeql.swift.generated.ParentChild
/**
* Holds if `call` is a call to the CommonCrypto function `name`.
*/
private predicate isCommonCryptoCall(CallExpr call, string name) {
call.getStaticTarget().(Function).getShortName() = name and
name = ["CCCrypt", "CCCryptorCreate", "CCCryptorCreateWithMode", "CCCryptorReset"]
}
/**
* Holds if `e` refers to a declaration named `name` through expressions that
* preserve its value.
*/
bindingset[name]
private predicate refersTo(Expr e, string name) {
exists(DeclRefExpr ref |
ref.getDecl().(VarDecl).getName() = name and
valuePreservingReference(e, ref)
)
}
/**
* Holds if `e` is `ref`, or is an expression that preserves the value of `ref`.
*/
private predicate valuePreservingReference(Expr e, DeclRefExpr ref) {
e = ref
or
exists(ParenExpr parens |
e = parens and
valuePreservingReference(parens.getImmediateSubExpr(), ref)
)
or
exists(CallExpr conversion |
e = conversion and
conversion.getStaticTarget() instanceof Initializer and
conversion.getType().getName() = ["CCOptions", "UInt32"] and
valuePreservingReference(conversion.getArgument(0).getExpr(), ref)
)
or
exists(OrBitwiseExpr bitwiseOr |
e = bitwiseOr and
valuePreservingReference(bitwiseOr.getAnOperand(), ref)
)
or
exists(DeclRefExpr localRef, VarDecl localVariable, Expr initializer |
e = localRef and
localRef.getDecl() = localVariable and
initializer = localVariable.getParentInitializer() and
DataFlow::localExprFlow(initializer, localRef) and
valuePreservingReference(initializer, ref)
)
or
exists(DeclRefExpr localRef, AssignExpr assignment |
e = localRef and
assignment.getDest() = localRef and
DataFlow::localExprFlow(assignment.getSource(), localRef) and
valuePreservingReference(assignment.getSource(), ref)
)
}
/**
* Holds if `call` is a CommonCrypto call that ignores the IV, because it uses
* ECB mode or the RC4 stream cipher.
*/
private predicate ignoresInitializationVector(CallExpr call) {
isCommonCryptoCall(call, ["CCCrypt", "CCCryptorCreate"]) and
(
refersTo(call.getArgument(1).getExpr(), "kCCAlgorithmRC4") or
refersTo(call.getArgument(2).getExpr(), "kCCOptionECBMode")
)
or
isCommonCryptoCall(call, "CCCryptorCreateWithMode") and
(
refersTo(call.getArgument(1).getExpr(), ["kCCModeECB", "kCCModeRC4"]) or
refersTo(call.getArgument(2).getExpr(), "kCCAlgorithmRC4")
)
}
/**
* An IV argument of a CommonCrypto function. A `nil` IV is replaced by an
* all-zero IV.
*/
class CommonCryptoInitializationVectorSink extends StaticInitializationVectorSink {
CommonCryptoInitializationVectorSink() {
exists(CallExpr call, string name, int ivIndex |
isCommonCryptoCall(call, name) and
(
name = ["CCCrypt", "CCCryptorCreate"] and ivIndex = 5
or
name = "CCCryptorCreateWithMode" and ivIndex = 4
or
name = "CCCryptorReset" and ivIndex = 1
) and
call.getArgument(ivIndex).getExpr() = this.asExpr() and
not ignoresInitializationVector(call)
)
}
}
/**
* A step from the receiver of `withUnsafeBytes` or `withUnsafeMutableBytes`
* to the pointer parameter of the closure passed to it. This covers the
* deprecated generic `Data.withUnsafeBytes` overloads that pass an
* `UnsafePointer<ContentType>`, which is the usual way to pass a `Data` IV
* to CommonCrypto.
*/
private class WithUnsafeBytesStep extends StaticInitializationVectorAdditionalFlowStep {
override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(CallExpr call, ClosureExpr closure |
call.getStaticTarget().(Function).getShortName() =
["withUnsafeBytes", "withUnsafeMutableBytes"] and
call.getQualifier() = nodeFrom.asExpr() and
closure = call.getArgument(0).getExpr() and
nodeTo.asParameter() = closure.getParam(0)
)
}
}
/**
* A step from a (non-`inout`) parameter to a read of that parameter inside a
* nested closure. CommonCrypto calls are usually nested in several closures
* passed to library methods such as `withUnsafeBytes`, which have no body in
* the database, so the dataflow library cannot see these closures being
* called and does not track captured values into them. Parameters cannot be
* reassigned, so the step is safe regardless of when the closure runs.
*/
private class CapturedParameterStep extends StaticInitializationVectorAdditionalFlowStep {
override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(Callable c, ParamDecl p, DeclRefExpr read |
p = c.getAParam() and
not p.isInout() and
nodeFrom.asParameter() = p and
read.getDecl() = p and
read.getEnclosingCallable() instanceof ClosureExpr and
read.getEnclosingCallable() != c and
nodeTo.asExpr() = read
)
}
}
/**
* An expression that is a static IV for this query: a constant
* array, string or number (as in the standard query), a `nil` literal, or a
* default argument value that a caller does not override.
*/
class ExtendedStaticInitializationVectorSource extends Expr {
ExtendedStaticInitializationVectorSource() {
this instanceof StaticInitializationVectorSource
or
this instanceof NilLiteralExpr
or
this instanceof DefaultArgumentExpr and
not this.(DefaultArgumentExpr).hasCallerSideDefault()
}
}
/**
* A taint-tracking configuration from static IVs (including `nil` and
* default argument values) to expressions that use them as an IV.
*/
module StaticInitializationVectorExtendedConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) {
node.asExpr() instanceof ExtendedStaticInitializationVectorSource
}
predicate isSink(DataFlow::Node node) { node instanceof StaticInitializationVectorSink }
predicate isBarrier(DataFlow::Node node) { node instanceof StaticInitializationVectorBarrier }
predicate isBarrierIn(DataFlow::Node node) {
// make sources barriers so that we only report the closest instance
isSource(node)
}
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
any(StaticInitializationVectorAdditionalFlowStep s).step(nodeFrom, nodeTo)
}
}
/** Taint flow from static IVs to expressions that use them as an IV. */
module StaticInitializationVectorExtendedFlow =
TaintTracking::Global<StaticInitializationVectorExtendedConfig>;
/**
* Holds if a static IV `source` flows to `sink` and the result is not
* already reported by the standard `swift/static-initialization-vector`
* query. `message` describes the result.
*/
predicate staticInitializationVectorExtended(
StaticInitializationVectorExtendedFlow::PathNode source,
StaticInitializationVectorExtendedFlow::PathNode sink, string message
) {
StaticInitializationVectorExtendedFlow::flowPath(source, sink) and
exists(Expr src | src = source.getNode().asExpr() |
// `nil` means "all zeros" only for CommonCrypto; other libraries (for
// example RNCryptor) generate a random IV instead.
src instanceof NilLiteralExpr and
sink.getNode() instanceof CommonCryptoInitializationVectorSink and
message = "A nil initialization vector (all zero bytes) is used for encryption."
or
src instanceof DefaultArgumentExpr and
message =
"The default value of parameter '" + src.(DefaultArgumentExpr).getParamDecl().getName() +
"' is used as an initialization vector for encryption."
or
// the standard query already reports static values that reach its own sinks
src instanceof StaticInitializationVectorSource and
sink.getNode() instanceof CommonCryptoInitializationVectorSink and
message =
"The static value '" + src.toString() +
"' is used as an initialization vector for encryption."
)
}