|
| 1 | +/** |
| 2 | + * Provides classes and predicates to reason about Intent URI permission manipulation |
| 3 | + * vulnerabilities on Android. |
| 4 | + */ |
| 5 | + |
| 6 | +import java |
| 7 | +private import semmle.code.java.controlflow.Guards |
| 8 | +private import semmle.code.java.dataflow.DataFlow |
| 9 | +private import semmle.code.java.dataflow.TaintTracking |
| 10 | +private import semmle.code.java.frameworks.android.Android |
| 11 | +private import semmle.code.java.frameworks.android.Intent |
| 12 | + |
| 13 | +/** |
| 14 | + * A sink for Intent URI permission manipulation vulnerabilities in Android, |
| 15 | + * that is, method calls that return an Intent as the result of an Activity. |
| 16 | + */ |
| 17 | +abstract class IntentUriPermissionManipulationSink extends DataFlow::Node { } |
| 18 | + |
| 19 | +/** |
| 20 | + * A sanitizer that makes sure that an Intent is safe to be returned to another Activity. |
| 21 | + * |
| 22 | + * Usually, this is done by setting the Intent's data URI and/or its flags to safe values. |
| 23 | + */ |
| 24 | +abstract class IntentUriPermissionManipulationSanitizer extends DataFlow::Node { } |
| 25 | + |
| 26 | +/** |
| 27 | + * A guard that makes sure that an Intent is safe to be returned to another Activity. |
| 28 | + * |
| 29 | + * Usually, this is done by checking that the Intent's data URI and/or its flags contain |
| 30 | + * expected values. |
| 31 | + */ |
| 32 | +abstract class IntentUriPermissionManipulationGuard extends DataFlow::BarrierGuard { } |
| 33 | + |
| 34 | +/** |
| 35 | + * An additional taint step for flows related to Intent URI permission manipulation |
| 36 | + * vulnerabilities. |
| 37 | + */ |
| 38 | +class IntentUriPermissionManipulationAdditionalTaintStep extends Unit { |
| 39 | + /** |
| 40 | + * Holds if the step from `node1` to `node2` should be considered a taint |
| 41 | + * step for flows related to Intent URI permission manipulation vulnerabilities. |
| 42 | + */ |
| 43 | + abstract predicate step(DataFlow::Node node1, DataFlow::Node node2); |
| 44 | +} |
| 45 | + |
| 46 | +private class DefaultIntentUriPermissionManipulationSink extends IntentUriPermissionManipulationSink { |
| 47 | + DefaultIntentUriPermissionManipulationSink() { |
| 48 | + exists(MethodAccess ma | ma.getMethod() instanceof ActivitySetResultMethod | |
| 49 | + ma.getArgument(1) = this.asExpr() |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Sanitizer that prevents access to arbitrary content providers by modifying the Intent in one of |
| 56 | + * the following ways: |
| 57 | + * * Removing the flags `FLAG_GRANT_READ_URI_PERMISSION` and `FLAG_GRANT_WRITE_URI_PERMISSION`. |
| 58 | + * * Setting the flags to a combination that doesn't include `FLAG_GRANT_READ_URI_PERMISSION` or |
| 59 | + * `FLAG_GRANT_WRITE_URI_PERMISSION`. |
| 60 | + * * Replacing the data URI. |
| 61 | + */ |
| 62 | +private class IntentFlagsOrDataChangedSanitizer extends IntentUriPermissionManipulationSanitizer { |
| 63 | + IntentFlagsOrDataChangedSanitizer() { |
| 64 | + exists(MethodAccess ma, Method m | |
| 65 | + ma.getMethod() = m and |
| 66 | + m.getDeclaringType() instanceof TypeIntent and |
| 67 | + this.asExpr() = ma.getQualifier() |
| 68 | + | |
| 69 | + m.hasName("removeFlags") and |
| 70 | + bitwiseLocalTaintStep*(DataFlow::exprNode(any(GrantReadUriPermissionFlag f).getAnAccess()), |
| 71 | + DataFlow::exprNode(ma.getArgument(0))) and |
| 72 | + bitwiseLocalTaintStep*(DataFlow::exprNode(any(GrantWriteUriPermissionFlag f).getAnAccess()), |
| 73 | + DataFlow::exprNode(ma.getArgument(0))) |
| 74 | + or |
| 75 | + m.hasName("setFlags") and |
| 76 | + not bitwiseLocalTaintStep*(DataFlow::exprNode(any(GrantUriPermissionFlag f).getAnAccess()), |
| 77 | + DataFlow::exprNode(ma.getArgument(0))) |
| 78 | + or |
| 79 | + m.hasName("setData") |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * A guard that checks an Intent's flags or data URI to make sure they are trusted. |
| 86 | + * It matches the following patterns: |
| 87 | + * |
| 88 | + * ```java |
| 89 | + * if (intent.getData().equals("trustedValue")) {} |
| 90 | + * |
| 91 | + * if (intent.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION == 0 && |
| 92 | + * intent.getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION == 0) {} |
| 93 | + * |
| 94 | + * if (intent.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION != 0 || |
| 95 | + * intent.getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION != 0) {} |
| 96 | + * ``` |
| 97 | + */ |
| 98 | +private class IntentFlagsOrDataCheckedGuard extends IntentUriPermissionManipulationGuard { |
| 99 | + Expr condition; |
| 100 | + |
| 101 | + IntentFlagsOrDataCheckedGuard() { intentFlagsOrDataChecked(this, _, _) } |
| 102 | + |
| 103 | + override predicate checks(Expr e, boolean branch) { intentFlagsOrDataChecked(this, e, branch) } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Holds if `g` checks `intent` when the result of an `intent.getFlags` or `intent.getData` call |
| 108 | + * is equality-tested. |
| 109 | + */ |
| 110 | +private predicate intentFlagsOrDataChecked(Guard g, Expr intent, boolean branch) { |
| 111 | + exists(MethodAccess ma, Method m, Expr checkedValue | |
| 112 | + ma.getQualifier() = intent and |
| 113 | + ma.getMethod() = m and |
| 114 | + m.getDeclaringType() instanceof TypeIntent and |
| 115 | + m.hasName(["getFlags", "getData"]) and |
| 116 | + bitwiseLocalTaintStep*(DataFlow::exprNode(ma), DataFlow::exprNode(checkedValue)) |
| 117 | + | |
| 118 | + bitwiseCheck(g, branch) and |
| 119 | + checkedValue = g.(EqualityTest).getAnOperand().(AndBitwiseExpr) |
| 120 | + or |
| 121 | + g.(MethodAccess).getMethod() instanceof EqualsMethod and |
| 122 | + branch = true and |
| 123 | + checkedValue = [g.(MethodAccess).getArgument(0), g.(MethodAccess).getQualifier()] |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Holds if `g` is a bitwise check. `branch` is `true` when the expected value is `0` |
| 129 | + * and `false` otherwise. |
| 130 | + */ |
| 131 | +private predicate bitwiseCheck(Guard g, boolean branch) { |
| 132 | + exists(CompileTimeConstantExpr operand | operand = g.(EqualityTest).getAnOperand() | |
| 133 | + if operand.getIntValue() = 0 |
| 134 | + then g.(EqualityTest).polarity() = branch |
| 135 | + else g.(EqualityTest).polarity().booleanNot() = branch |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Holds if taint can flow from `source` to `sink` in one local step, |
| 141 | + * including bitwise operations. |
| 142 | + */ |
| 143 | +private predicate bitwiseLocalTaintStep(DataFlow::Node source, DataFlow::Node sink) { |
| 144 | + TaintTracking::localTaintStep(source, sink) or |
| 145 | + source.asExpr() = sink.asExpr().(BitwiseExpr).(BinaryExpr).getAnOperand() |
| 146 | +} |
0 commit comments