|
| 1 | +/** Provides classes and predicates to reason about deep links in Android. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.frameworks.android.Intent |
| 5 | +private import semmle.code.java.frameworks.android.AsyncTask |
| 6 | +private import semmle.code.java.dataflow.DataFlow |
| 7 | +private import semmle.code.java.dataflow.FlowSteps |
| 8 | +private import semmle.code.java.dataflow.ExternalFlow |
| 9 | + |
| 10 | +/** |
| 11 | + * The method `Intent.getSerializableExtra` |
| 12 | + */ |
| 13 | +class AndroidGetSerializableExtraMethod extends Method { |
| 14 | + AndroidGetSerializableExtraMethod() { |
| 15 | + this.hasName("getSerializableExtra") and this.getDeclaringType() instanceof TypeIntent |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * The method `Context.startService`. |
| 21 | + */ |
| 22 | +class ContextStartServiceMethod extends Method { |
| 23 | + ContextStartServiceMethod() { |
| 24 | + this.hasName("startService") and |
| 25 | + this.getDeclaringType() instanceof TypeContext |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// /** |
| 30 | +// * A value-preserving step from the Intent argument of a `startService` call to |
| 31 | +// * a `getSerializableExtra` call in the Service the Intent pointed to in its constructor. |
| 32 | +// */ |
| 33 | +// class StartServiceIntentStep extends AdditionalValueStep { |
| 34 | +// override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 35 | +// exists( |
| 36 | +// MethodAccess startService, MethodAccess getSerializableExtra, ClassInstanceExpr newIntent |
| 37 | +// | |
| 38 | +// startService.getMethod().overrides*(any(ContextStartServiceMethod m)) and |
| 39 | +// getSerializableExtra.getMethod().overrides*(any(AndroidGetSerializableExtraMethod m)) and |
| 40 | +// newIntent.getConstructedType() instanceof TypeIntent and |
| 41 | +// DataFlow::localExprFlow(newIntent, startService.getArgument(0)) and |
| 42 | +// //newIntent.getArgument(1).getType().(ParameterizedType).getATypeArgument() = |
| 43 | +// // getSerializableExtra.getReceiverType() and |
| 44 | +// newIntent.getArgument(1).toString() = "FetcherService.class" and // BAD |
| 45 | +// getSerializableExtra.getFile().getBaseName() = "RouterActivity.java" and // BAD |
| 46 | +// n1.asExpr() = startService.getArgument(0) and |
| 47 | +// n2.asExpr() = getSerializableExtra |
| 48 | +// ) |
| 49 | +// } |
| 50 | +// } |
| 51 | +/** |
| 52 | + * A value-preserving step from the Intent argument of a `startService` call to |
| 53 | + * an `Intent` TypeAccess in the Service the Intent pointed to in its constructor. |
| 54 | + */ |
| 55 | +class StartServiceIntentStep extends AdditionalValueStep { |
| 56 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 57 | + exists(MethodAccess startService, VarAccess intentVar, ClassInstanceExpr newIntent | |
| 58 | + startService.getMethod().overrides*(any(ContextStartServiceMethod m)) and |
| 59 | + //getSerializableExtra.getMethod().overrides*(any(AndroidGetSerializableExtraMethod m)) and |
| 60 | + intentVar.getType() instanceof TypeIntent and |
| 61 | + newIntent.getConstructedType() instanceof TypeIntent and |
| 62 | + DataFlow::localExprFlow(newIntent, startService.getArgument(0)) and |
| 63 | + // newIntent.getArgument(1).getType().(ParameterizedType).getATypeArgument() = |
| 64 | + // intentVar.getBasicBlock().getBasicBlock() and |
| 65 | + // newIntent.getArgument(1).getType().(ParameterizedType).getATypeArgument() = |
| 66 | + // intent.getType().(ParameterizedType).getATypeArgument() and |
| 67 | + newIntent.getArgument(1).toString() = "FetcherService.class" and // BAD |
| 68 | + intentVar.getFile().getBaseName() = "RouterActivity.java" and // BAD |
| 69 | + n1.asExpr() = startService.getArgument(0) and |
| 70 | + n2.asExpr() = intentVar |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// ************************************************************************************************* |
| 76 | +/* |
| 77 | + * The following flow steps aim to model the life-cycle of `AsyncTask`s described here: |
| 78 | + * https://developer.android.com/reference/android/os/AsyncTask#the-4-steps |
| 79 | + */ |
| 80 | + |
| 81 | +/** |
| 82 | + * A taint step from the vararg arguments of `AsyncTask::execute` and `AsyncTask::executeOnExecutor` |
| 83 | + * to the parameter of `AsyncTask::doInBackground`. |
| 84 | + */ |
| 85 | +private class AsyncTaskExecuteAdditionalValueStep extends AdditionalTaintStep { |
| 86 | + override predicate step(DataFlow::Node node1, DataFlow::Node node2) { |
| 87 | + exists(ExecuteAsyncTaskMethodAccess ma, AsyncTaskRunInBackgroundMethod m | |
| 88 | + DataFlow::getInstanceArgument(ma).getType() = m.getDeclaringType() |
| 89 | + | |
| 90 | + node1.asExpr() = ma.getParamsArgument() and |
| 91 | + node2.asParameter() = m.getParameter(0) |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * A value-preserving step from the return value of `AsyncTask::doInBackground` |
| 98 | + * to the parameter of `AsyncTask::onPostExecute`. |
| 99 | + */ |
| 100 | +private class AsyncTaskOnPostExecuteAdditionalValueStep extends AdditionalValueStep { |
| 101 | + override predicate step(DataFlow::Node node1, DataFlow::Node node2) { |
| 102 | + exists( |
| 103 | + AsyncTaskRunInBackgroundMethod runInBackground, AsyncTaskOnPostExecuteMethod onPostExecute |
| 104 | + | |
| 105 | + onPostExecute.getDeclaringType() = runInBackground.getDeclaringType() |
| 106 | + | |
| 107 | + node1.asExpr() = any(ReturnStmt r | r.getEnclosingCallable() = runInBackground).getResult() and |
| 108 | + node2.asParameter() = onPostExecute.getParameter(0) |
| 109 | + ) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * A value-preserving step from field initializers in `AsyncTask`'s constructor or initializer method |
| 115 | + * to the instance parameter of `AsyncTask::runInBackground` and `AsyncTask::onPostExecute`. |
| 116 | + */ |
| 117 | +private class AsyncTaskFieldInitQualifierToInstanceParameterStep extends AdditionalValueStep { |
| 118 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 119 | + exists(AsyncTaskInit init, Callable receiver | |
| 120 | + n1.(DataFlow::PostUpdateNode).getPreUpdateNode() = |
| 121 | + DataFlow::getFieldQualifier(any(FieldWrite f | f.getEnclosingCallable() = init)) and |
| 122 | + n2.(DataFlow::InstanceParameterNode).getCallable() = receiver and |
| 123 | + receiver.getDeclaringType() = init.getDeclaringType() and |
| 124 | + ( |
| 125 | + receiver instanceof AsyncTaskRunInBackgroundMethod or |
| 126 | + receiver instanceof AsyncTaskOnPostExecuteMethod |
| 127 | + ) |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * The Android class `android.os.AsyncTask`. |
| 134 | + */ |
| 135 | +private class AsyncTask extends RefType { |
| 136 | + AsyncTask() { this.hasQualifiedName("android.os", "AsyncTask") } |
| 137 | +} |
| 138 | + |
| 139 | +/** The constructor or initializer method of the `android.os.AsyncTask` class. */ |
| 140 | +private class AsyncTaskInit extends Callable { |
| 141 | + AsyncTaskInit() { |
| 142 | + this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof AsyncTask and |
| 143 | + (this instanceof Constructor or this instanceof InitializerMethod) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** A call to the `execute` or `executeOnExecutor` methods of the `android.os.AsyncTask` class. */ |
| 148 | +private class ExecuteAsyncTaskMethodAccess extends MethodAccess { |
| 149 | + ExecuteAsyncTaskMethodAccess() { |
| 150 | + this.getMethod().hasName(["execute", "executeOnExecutor"]) and |
| 151 | + this.getMethod().getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof |
| 152 | + AsyncTask |
| 153 | + } |
| 154 | + |
| 155 | + /** Returns the `params` argument of this call. */ |
| 156 | + Argument getParamsArgument() { result = this.getAnArgument() and result.isVararg() } |
| 157 | +} |
| 158 | + |
| 159 | +/** The `doInBackground` method of the `android.os.AsyncTask` class. */ |
| 160 | +private class AsyncTaskRunInBackgroundMethod extends Method { |
| 161 | + AsyncTaskRunInBackgroundMethod() { |
| 162 | + this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof AsyncTask and |
| 163 | + this.hasName("doInBackground") |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** The `onPostExecute` method of the `android.os.AsyncTask` class. */ |
| 168 | +private class AsyncTaskOnPostExecuteMethod extends Method { |
| 169 | + AsyncTaskOnPostExecuteMethod() { |
| 170 | + this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof AsyncTask and |
| 171 | + this.hasName("onPostExecute") |
| 172 | + } |
| 173 | +} |
0 commit comments