|
| 1 | +/** Provides definitions to reason about Android Sensitive Communication queries */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.TaintTracking |
| 5 | +import semmle.code.java.frameworks.android.Intent |
| 6 | +import semmle.code.java.security.SensitiveActions |
| 7 | + |
| 8 | +/** |
| 9 | + * Gets regular expression for matching names of Android variables that indicate the value being held contains sensitive information. |
| 10 | + */ |
| 11 | +private string getAndroidSensitiveInfoRegex() { result = "(?i).*(email|phone|ticket).*" } |
| 12 | + |
| 13 | +/** Finds variables that hold sensitive information judging by their names. */ |
| 14 | +private class SensitiveInfoExpr extends Expr { |
| 15 | + SensitiveInfoExpr() { |
| 16 | + exists(Variable v | this = v.getAnAccess() | |
| 17 | + v.getName().regexpMatch([getCommonSensitiveInfoRegex(), getAndroidSensitiveInfoRegex()]) |
| 18 | + ) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +private predicate maybeNullArg(Expr ex) { |
| 23 | + exists(DataFlow::Node src, DataFlow::Node sink, MethodAccess ma | |
| 24 | + ex = ma.getAnArgument() and |
| 25 | + sink.asExpr() = ex and |
| 26 | + src.asExpr() instanceof NullLiteral |
| 27 | + | |
| 28 | + DataFlow::localFlow(src, sink) |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +private predicate maybeEmptyArrayArg(Expr ex) { |
| 33 | + exists(DataFlow::Node src, DataFlow::Node sink, MethodAccess ma | |
| 34 | + ex = ma.getAnArgument() and |
| 35 | + sink.asExpr() = ex and |
| 36 | + src.asExpr().(ArrayCreationExpr).getFirstDimensionSize() = 0 |
| 37 | + | |
| 38 | + DataFlow::localFlow(src, sink) |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Holds if a `sendBroadcast` call doesn't specify receiver permission. |
| 44 | + */ |
| 45 | +private predicate isSensitiveBroadcastSink(DataFlow::Node sendBroadcastCallArg) { |
| 46 | + exists(MethodAccess ma, string name | ma.getMethod().hasName(name) | |
| 47 | + ma.getMethod().getDeclaringType().getASourceSupertype*() instanceof TypeContext and |
| 48 | + sendBroadcastCallArg.asExpr() = ma.getAnArgument() and |
| 49 | + ( |
| 50 | + name = "sendBroadcast" and |
| 51 | + ( |
| 52 | + // sendBroadcast(Intent intent) |
| 53 | + ma.getNumArgument() = 1 |
| 54 | + or |
| 55 | + // sendBroadcast(Intent intent, String receiverPermission) |
| 56 | + maybeNullArg(ma.getArgument(1)) |
| 57 | + ) |
| 58 | + or |
| 59 | + name = "sendBroadcastAsUser" and |
| 60 | + ( |
| 61 | + // sendBroadcastAsUser(Intent intent, UserHandle user) |
| 62 | + ma.getNumArgument() = 2 |
| 63 | + or |
| 64 | + // sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission) |
| 65 | + maybeNullArg(ma.getArgument(2)) |
| 66 | + ) |
| 67 | + or |
| 68 | + // sendBroadcastWithMultiplePermissions(Intent intent, String[] receiverPermissions) |
| 69 | + name = "sendBroadcastWithMultiplePermissions" and |
| 70 | + maybeEmptyArrayArg(ma.getArgument(1)) |
| 71 | + or |
| 72 | + // Method calls of `sendOrderedBroadcast` whose second argument is always `receiverPermission` |
| 73 | + name = "sendOrderedBroadcast" and |
| 74 | + ( |
| 75 | + // sendOrderedBroadcast(Intent intent, String receiverPermission) |
| 76 | + // sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 77 | + maybeNullArg(ma.getArgument(1)) and |
| 78 | + ma.getNumArgument() = [2, 7] |
| 79 | + or |
| 80 | + // sendOrderedBroadcast(Intent intent, String receiverPermission, String receiverAppOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 81 | + maybeNullArg(ma.getArgument(1)) and |
| 82 | + maybeNullArg(ma.getArgument(2)) and |
| 83 | + ma.getNumArgument() = 8 |
| 84 | + ) |
| 85 | + or |
| 86 | + // sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 87 | + name = "sendOrderedBroadcastAsUser" and |
| 88 | + maybeNullArg(ma.getArgument(2)) |
| 89 | + or |
| 90 | + // sendStickyBroadcast(Intent intent) |
| 91 | + // sendStickyBroadcast(Intent intent, Bundle options) |
| 92 | + // sendStickyBroadcastAsUser(Intent intent, UserHandle user) |
| 93 | + // sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 94 | + // sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 95 | + name = |
| 96 | + [ |
| 97 | + "sendStickyBroadcast", "sendStickyBroadcastAsUser", "sendStickyOrderedBroadcast", |
| 98 | + "sendStickyOrderedBroadcastAsUser" |
| 99 | + ] |
| 100 | + ) |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Holds if `arg` is an argument in a use of a `startActivity` or `startService` method that sends an Intent to another application. |
| 106 | + */ |
| 107 | +private predicate isStartActivityOrServiceSink(DataFlow::Node arg) { |
| 108 | + exists(MethodAccess ma, string name | ma.getMethod().hasName(name) | |
| 109 | + arg.asExpr() = ma.getArgument(0) and |
| 110 | + ma.getMethod().getDeclaringType().getASourceSupertype*() instanceof TypeContext and |
| 111 | + // startActivity(Intent intent) |
| 112 | + // startActivity(Intent intent, Bundle options) |
| 113 | + // startActivities(Intent[] intents) |
| 114 | + // startActivities(Intent[] intents, Bundle options) |
| 115 | + // startService(Intent service) |
| 116 | + // startForegroundService(Intent service) |
| 117 | + // bindService (Intent service, int flags, Executor executor, ServiceConnection conn) |
| 118 | + // bindService (Intent service, Executor executor, ServiceConnection conn) |
| 119 | + name = |
| 120 | + ["startActivity", "startActivities", "startService", "startForegroundService", "bindService"] |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +private predicate isCleanIntent(Expr intent) { |
| 125 | + intent.getType() instanceof TypeIntent and |
| 126 | + ( |
| 127 | + exists(MethodAccess setRecieverMa | |
| 128 | + setRecieverMa.getQualifier() = intent and |
| 129 | + setRecieverMa.getMethod().hasName(["setPackage", "setClass", "setClassName", "setComponent"]) |
| 130 | + ) |
| 131 | + or |
| 132 | + // Handle the cases where the PackageContext and Class are set at construction time |
| 133 | + // Intent(Context packageContext, Class<?> cls) |
| 134 | + // Intent(String action, Uri uri, Context packageContext, Class<?> cls) |
| 135 | + exists(ConstructorCall cc | cc = intent | |
| 136 | + cc.getConstructedType() instanceof TypeIntent and |
| 137 | + cc.getNumArgument() > 1 and |
| 138 | + ( |
| 139 | + cc.getArgument(0).getType() instanceof TypeContext and |
| 140 | + not maybeNullArg(cc.getArgument(1)) |
| 141 | + or |
| 142 | + cc.getArgument(2).getType() instanceof TypeContext and |
| 143 | + not maybeNullArg(cc.getArgument(3)) |
| 144 | + ) |
| 145 | + ) |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Taint configuration tracking flow from variables containing sensitive information to broadcast Intents. |
| 151 | + */ |
| 152 | +class SensitiveCommunicationConfig extends TaintTracking::Configuration { |
| 153 | + SensitiveCommunicationConfig() { this = "Sensitive Communication Configuration" } |
| 154 | + |
| 155 | + override predicate isSource(DataFlow::Node source) { |
| 156 | + source.asExpr() instanceof SensitiveInfoExpr |
| 157 | + } |
| 158 | + |
| 159 | + override predicate isSink(DataFlow::Node sink) { |
| 160 | + isSensitiveBroadcastSink(sink) |
| 161 | + or |
| 162 | + isStartActivityOrServiceSink(sink) |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Holds if broadcast doesn't specify receiving package name of the 3rd party app |
| 167 | + */ |
| 168 | + override predicate isSanitizer(DataFlow::Node node) { |
| 169 | + exists(DataFlow::Node intent | isCleanIntent(intent.asExpr()) | |
| 170 | + DataFlow::localFlow(intent, node) |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + override predicate allowImplicitRead(DataFlow::Node node, DataFlow::Content c) { |
| 175 | + super.allowImplicitRead(node, c) |
| 176 | + or |
| 177 | + this.isSink(node) |
| 178 | + } |
| 179 | +} |
0 commit comments