-
Notifications
You must be signed in to change notification settings - Fork 9
/
TaintAnalysis.kt
310 lines (243 loc) · 11.1 KB
/
TaintAnalysis.kt
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
package org.usvm.api.targets
import io.ksmt.utils.asExpr
import io.ksmt.utils.uncheckedCast
import org.jacodb.api.cfg.JcAssignInst
import org.jacodb.api.cfg.JcCallExpr
import org.jacodb.api.cfg.JcCallInst
import org.jacodb.api.cfg.JcInst
import org.jacodb.api.cfg.JcInstanceCallExpr
import org.jacodb.api.ext.cfg.callExpr
import org.usvm.UBoolExpr
import org.usvm.UConcreteHeapRef
import org.usvm.UHeapRef
import org.usvm.api.allocateConcreteRef
import org.usvm.collection.set.ref.URefSetEntryLValue
import org.usvm.machine.JcContext
import org.usvm.machine.JcInterpreterObserver
import org.usvm.machine.JcMethodCallBaseInst
import org.usvm.machine.JcMethodEntrypointInst
import org.usvm.machine.interpreter.JcSimpleValueResolver
import org.usvm.machine.interpreter.JcStepScope
import org.usvm.machine.state.JcMethodResult
import org.usvm.machine.state.JcState
import org.usvm.statistics.UMachineObserver
import org.usvm.targets.UTargetController
class TaintAnalysis(
private val configuration: TaintConfiguration,
override val targets: MutableCollection<TaintTarget> = mutableListOf(),
) : UTargetController, JcInterpreterObserver, UMachineObserver<JcState> {
private val taintTargets: MutableMap<JcInst, MutableSet<TaintTarget>> = mutableMapOf()
init {
targets.forEach {
exposeTargets(it, taintTargets)
}
}
// TODO save mapping between initial targets and the states that reach them
// Replace with the corresponding observer-collector?
val collectedStates: MutableList<JcState> = mutableListOf()
private fun exposeTargets(target: TaintTarget, result: MutableMap<JcInst, MutableSet<TaintTarget>>) {
result.getOrPut(target.location!!) { hashSetOf() }.add(target)
target.children.forEach {
exposeTargets(it as TaintTarget, result)
}
}
private val marksAddresses: MutableMap<JcTaintMark, UConcreteHeapRef> = mutableMapOf()
private fun getMarkAddress(mark: JcTaintMark, stepScope: JcStepScope): UConcreteHeapRef =
marksAddresses.getOrPut(mark) {
stepScope.calcOnState { memory.allocateConcreteRef() }
}
private fun writeMark(ref: UHeapRef, mark: JcTaintMark, guard: UBoolExpr, stepScope: JcStepScope) {
stepScope.doWithState {
memory.write(createLValue(ref, mark, stepScope), ctx.trueExpr, guard)
}
}
private fun removeMark(ref: UHeapRef, mark: JcTaintMark, guard: UBoolExpr, stepScope: JcStepScope) {
stepScope.doWithState {
memory.write(createLValue(ref, mark, stepScope), ctx.falseExpr, guard)
}
}
private fun readMark(ref: UHeapRef, mark: JcTaintMark, stepScope: JcStepScope): UBoolExpr =
stepScope.calcOnState {
memory.read(createLValue(ref, mark, stepScope))
}
private fun <Mark : JcTaintMark> createLValue(
ref: UHeapRef,
mark: Mark,
stepScope: JcStepScope,
): URefSetEntryLValue<Mark> = URefSetEntryLValue(ref, getMarkAddress(mark, stepScope), mark)
fun addTarget(target: JcTarget<TaintAnalysis>): TaintAnalysis {
require(target is TaintTarget)
targets += target
exposeTargets(target, taintTargets)
return this
}
private fun findTaintTargets(stmt: JcInst, state: JcState): List<TaintTarget> =
taintTargets[stmt]?.let { targets ->
state.targets.filter { it.uncheckedCast() in targets }
}.orEmpty().toList().uncheckedCast()
override fun onAssignStatement(exprResolver: JcSimpleValueResolver, stmt: JcAssignInst, stepScope: JcStepScope) {
// Sinks are already processed at this moment since we resolved it on a call statement
stmt.callExpr?.let { processTaintConfiguration(it, stepScope, exprResolver) }
// TODO add fields processing
}
private fun processTaintConfiguration(
callExpr: JcCallExpr,
stepScope: JcStepScope,
simpleValueResolver: JcSimpleValueResolver,
) {
val ctx = stepScope.ctx
val methodResult = stepScope.calcOnState { methodResult }
val method = callExpr.method.method
require(methodResult is JcMethodResult.Success) {
"Other result statuses must be processed in `onMethodCallWithUnresolvedArguments`"
}
val callPositionResolver = createCallPositionResolver(ctx, callExpr, simpleValueResolver, methodResult)
val conditionResolver = ConditionResolver(ctx, callPositionResolver, ::readMark)
val actionResolver = TaintActionResolver(
ctx,
callPositionResolver,
::readMark,
::writeMark,
::removeMark,
marksAddresses.keys
)
val sourceConfigurations = configuration.methodSources[method]
val currentStatement = stepScope.calcOnState { currentStatement }
val sourceTargets = findTaintTargets(currentStatement, stepScope.state)
.filterIsInstance<TaintMethodSourceTarget>()
.associateBy { it.configurationRule }
sourceConfigurations?.forEach {
val target = sourceTargets[it]
val resolvedCondition =
conditionResolver.visit(it.condition, simpleValueResolver, stepScope) ?: ctx.trueExpr
val targetCondition = target?.condition ?: ConstantTrue
val resolvedTargetCondition =
conditionResolver.visit(targetCondition, simpleValueResolver, stepScope) ?: ctx.trueExpr
val combinedCondition = ctx.mkAnd(resolvedTargetCondition, resolvedCondition)
it.action.accept(actionResolver, stepScope, combinedCondition)
target?.propagate(stepScope.state)
}
val cleanerConfigurations = configuration.cleaners[method]
cleanerConfigurations?.forEach {
val resolvedCondition = conditionResolver.visit(it.condition, simpleValueResolver, stepScope)
it.action.accept(actionResolver, stepScope, resolvedCondition)
}
val passThroughConfigurations = configuration.passThrough[method]
passThroughConfigurations?.forEach {
val resolvedCondition = conditionResolver.visit(it.condition, simpleValueResolver, stepScope)
it.action.accept(actionResolver, stepScope, resolvedCondition)
}
}
private val JcStepScope.ctx get() = calcOnState { ctx }
private fun createCallPositionResolver(
ctx: JcContext,
callExpr: JcCallExpr,
simpleValueResolver: JcSimpleValueResolver,
methodResult: JcMethodResult.Success?,
) = CallPositionResolver(
resolveCallInstance(callExpr)?.accept(simpleValueResolver)?.asExpr(ctx.addressSort),
callExpr.args.map { it.accept(simpleValueResolver) },
methodResult?.value
)
override fun onEntryPoint(
simpleValueResolver: JcSimpleValueResolver,
stmt: JcMethodEntrypointInst,
stepScope: JcStepScope,
) {
// TODO entry point configuration
}
override fun onMethodCallWithUnresolvedArguments(
simpleValueResolver: JcSimpleValueResolver,
stmt: JcCallExpr,
stepScope: JcStepScope,
) {
val method = stmt.method.method
val methodResult = stepScope.calcOnState { methodResult }
require(methodResult is JcMethodResult.NoCall) { "This signal must be sent before the method call" }
val ctx = stepScope.ctx
val positionResolver = createCallPositionResolver(ctx, stmt, simpleValueResolver, methodResult = null)
val conditionResolver = ConditionResolver(ctx, positionResolver, ::readMark)
if (inTargetedMode) {
val currentStatement = stepScope.calcOnState { currentStatement }
val targets = findTaintTargets(currentStatement, stepScope.state)
targets
.filterIsInstance<TaintMethodSinkTarget>()
.forEach {
processSink(it.configRule, it.condition, conditionResolver, simpleValueResolver, stepScope, it)
}
} else {
val methodSinks = configuration.methodSinks[method] ?: return
methodSinks.forEach {
processSink(it, ConstantTrue, conditionResolver, simpleValueResolver, stepScope)
}
}
}
private val JcStepScope.state get() = calcOnState { this }
private val inTargetedMode: Boolean
get() = targets.isNotEmpty()
private fun processSink(
methodSink: TaintMethodSink,
sinkCondition: Condition,
conditionResolver: ConditionResolver,
simpleValueResolver: JcSimpleValueResolver,
stepScope: JcStepScope,
target: TaintMethodSinkTarget? = null,
) {
val resolvedConfigCondition =
methodSink.condition.visit(conditionResolver, simpleValueResolver, stepScope) ?: return
val resolvedSinkCondition = sinkCondition.visit(conditionResolver, simpleValueResolver, stepScope) ?: return
val resolvedCondition = stepScope.ctx.mkAnd(resolvedConfigCondition, resolvedSinkCondition)
val (originalStateCopy, taintedStepScope) = stepScope.calcOnState {
val originalStateCopy = clone()
originalStateCopy to JcStepScope(originalStateCopy)
}
taintedStepScope.assert(resolvedCondition)?.let {
// TODO remove corresponding target
collectedStates += originalStateCopy
target?.propagate(taintedStepScope.state)
}
}
override fun onMethodCallWithResolvedArguments(
simpleValueResolver: JcSimpleValueResolver,
stmt: JcMethodCallBaseInst,
stepScope: JcStepScope,
) {
// It is a redundant signal
}
override fun onCallStatement(simpleValueResolver: JcSimpleValueResolver, stmt: JcCallInst, stepScope: JcStepScope) {
processTaintConfiguration(stmt.callExpr, stepScope, simpleValueResolver)
}
override fun onState(parent: JcState, forks: Sequence<JcState>) {
propagateIntermediateTarget(parent)
forks.forEach { propagateIntermediateTarget(it) }
}
private fun propagateIntermediateTarget(state: JcState) {
val parent = state.pathLocation.parent ?: error("This is impossible by construction")
val targets = findTaintTargets(parent.statement, state)
targets.forEach {
when (it) {
is TaintIntermediateTarget -> it.propagate(state)
is TaintMethodSourceTarget, is TaintMethodSinkTarget -> return@forEach
}
}
}
private fun resolveCallInstance(
callExpr: JcCallExpr,
) = if (callExpr is JcInstanceCallExpr) callExpr.instance else null
sealed class TaintTarget(location: JcInst) : JcTarget<TaintAnalysis>(location)
class TaintMethodSourceTarget(
location: JcInst,
val condition: Condition,
val configurationRule: TaintMethodSource,
) : TaintTarget(location)
// TODO add field sources and sinks targets
class TaintIntermediateTarget(location: JcInst) : TaintTarget(location)
class TaintMethodSinkTarget(
location: JcInst,
val condition: Condition,
val configRule: TaintMethodSink,
) : TaintTarget(location)
}
sealed interface JcTaintMark
object SqlInjection : JcTaintMark
object SensitiveData : JcTaintMark