Skip to content

Commit

Permalink
Add FactAwareConditionEvaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen committed Oct 25, 2023
1 parent 94d9c7e commit e590ac7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ interface DefaultConditionVisitor : ConditionVisitor<Boolean> {
}

class ConditionEvaluator(
private val positionResolver: PositionResolver<JcValue>,
private val fact: Tainted? = null,
internal val positionResolver: PositionResolver<JcValue>,
) : DefaultConditionVisitor {
override fun visit(condition: IsConstant): Boolean {
val value = positionResolver.resolve(condition.position)
Expand Down Expand Up @@ -157,14 +156,7 @@ class ConditionEvaluator(
}

override fun visit(condition: CallParameterContainsMark): Boolean {
if (fact != null && fact.mark == condition.mark) {
val value = positionResolver.resolve(condition.position)
val variable = value.toPath()
if (variable.startsWith(fact.variable)) {
return true
}
}
return false
error("This visitor does not support condition $condition. Use FactAwareConditionEvaluator instead")
}

override fun visit(condition: TypeMatches): Boolean {
Expand All @@ -183,3 +175,26 @@ class ConditionEvaluator(
}
}
}

class FactAwareConditionEvaluator private constructor(
private val fact: Tainted,
private val conditionEvaluator: ConditionEvaluator,
) : ConditionVisitor<Boolean> by conditionEvaluator {
private val positionResolver: PositionResolver<JcValue> = conditionEvaluator.positionResolver

constructor(
fact: Tainted,
positionResolver: PositionResolver<JcValue>,
) : this(fact, ConditionEvaluator(positionResolver))

override fun visit(condition: CallParameterContainsMark): Boolean {
if (fact.mark == condition.mark) {
val value = positionResolver.resolve(condition.position)
val variable = value.toPath()
if (variable.startsWith(fact.variable)) {
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.withContext
import org.jacodb.analysis.config.CallPositionResolverToAccessPath
import org.jacodb.analysis.config.CallPositionResolverToJcValue
import org.jacodb.analysis.config.ConditionEvaluator
import org.jacodb.analysis.config.FactAwareConditionEvaluator
import org.jacodb.analysis.config.evaluate
import org.jacodb.analysis.library.analyzers.TaintAnalysisNode
import org.jacodb.api.JcMethod
Expand Down Expand Up @@ -197,9 +198,9 @@ private class BaseIfdsUnitRunner<UnitType>(
}
if (currentFact is TaintAnalysisNode) {
@Suppress("NAME_SHADOWING")
val conditionEvaluator = ConditionEvaluator(
CallPositionResolverToJcValue(callExpr),
Tainted(currentFact)
val conditionEvaluator = FactAwareConditionEvaluator(
Tainted(currentFact),
CallPositionResolverToJcValue(callExpr)
)
for (item in config.filterIsInstance<TaintPassThrough>()) {
if (item.condition.accept(conditionEvaluator)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import org.jacodb.analysis.config.CallPositionResolverToAccessPath
import org.jacodb.analysis.config.CallPositionResolverToJcValue
import org.jacodb.analysis.config.ConditionEvaluator
import org.jacodb.analysis.config.FactAwareConditionEvaluator
import org.jacodb.analysis.config.TaintActionEvaluator
import org.jacodb.analysis.config.TaintConfig
import org.jacodb.analysis.library.analyzers.TaintNode
Expand Down Expand Up @@ -172,7 +172,7 @@ class TaintForwardFlowFunctions(
// adhoc to satisfy types
if (fact !is Tainted) return@FlowFunctionInstance2 emptyList()

val conditionEvaluator = ConditionEvaluator(CallPositionResolverToJcValue(callExpr), fact)
val conditionEvaluator = FactAwareConditionEvaluator(fact, CallPositionResolverToJcValue(callExpr))
val actionEvaluator = TaintActionEvaluator(fact, CallPositionResolverToAccessPath(callExpr))

val resultingFacts = mutableSetOf<Tainted>()
Expand Down

0 comments on commit e590ac7

Please sign in to comment.