|
| 1 | +/** |
| 2 | + * @name Broadcasting sensitive data to all Android applications |
| 3 | + * @id java/sensitive-broadcast |
| 4 | + * @description An Android application uses implicit intents to broadcast sensitive data to all applications without specifying any receiver permission. |
| 5 | + * @kind path-problem |
| 6 | + * @tags security |
| 7 | + * external/cwe-927 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.dataflow.DataFlow3 |
| 12 | +import semmle.code.java.dataflow.TaintTracking |
| 13 | +import semmle.code.java.frameworks.android.Intent |
| 14 | +import semmle.code.java.security.SensitiveActions |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets regular expression for matching names of Android variables that indicate the value being held contains sensitive information. |
| 19 | + */ |
| 20 | +private string getAndroidSensitiveInfoRegex() { result = "(?i).*(email|phone|ticket).*" } |
| 21 | + |
| 22 | +/** |
| 23 | + * Method call to pass information to the `Intent` object. |
| 24 | + */ |
| 25 | +class PutIntentExtraMethodAccess extends MethodAccess { |
| 26 | + PutIntentExtraMethodAccess() { |
| 27 | + ( |
| 28 | + getMethod().getName().matches("put%Extra") or |
| 29 | + getMethod().hasName("putExtras") |
| 30 | + ) and |
| 31 | + getMethod().getDeclaringType() instanceof TypeIntent |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Method call to pass information to the intent extra bundle object. |
| 37 | + */ |
| 38 | +class PutBundleExtraMethodAccess extends MethodAccess { |
| 39 | + PutBundleExtraMethodAccess() { |
| 40 | + getMethod().getName().regexpMatch("put\\w+") and |
| 41 | + getMethod().getDeclaringType().getASupertype*().hasQualifiedName("android.os", "BaseBundle") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** Finds variables that hold sensitive information judging by their names. */ |
| 46 | +class SensitiveInfoExpr extends Expr { |
| 47 | + SensitiveInfoExpr() { |
| 48 | + exists(Variable v | this = v.getAnAccess() | |
| 49 | + v.getName().regexpMatch([getCommonSensitiveInfoRegex(), getAndroidSensitiveInfoRegex()]) |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * A method access of the `Context.sendBroadcast` family. |
| 56 | + */ |
| 57 | +class SendBroadcastMethodAccess extends MethodAccess { |
| 58 | + SendBroadcastMethodAccess() { |
| 59 | + this.getMethod().getDeclaringType() instanceof TypeContext and |
| 60 | + this.getMethod().getName().matches("send%Broadcast%") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +private class NullArgFlowConfig extends DataFlow2::Configuration { |
| 65 | + NullArgFlowConfig() { this = "Flow configuration with a null argument" } |
| 66 | + |
| 67 | + override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof NullLiteral } |
| 68 | + |
| 69 | + override predicate isSink(DataFlow::Node sink) { |
| 70 | + exists(SendBroadcastMethodAccess ma | sink.asExpr() = ma.getAnArgument()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +private class EmptyArrayArgFlowConfig extends DataFlow3::Configuration { |
| 75 | + EmptyArrayArgFlowConfig() { this = "Flow configuration with an empty array argument" } |
| 76 | + |
| 77 | + override predicate isSource(DataFlow::Node src) { |
| 78 | + src.asExpr().(ArrayCreationExpr).getFirstDimensionSize() = 0 |
| 79 | + } |
| 80 | + |
| 81 | + override predicate isSink(DataFlow::Node sink) { |
| 82 | + exists(SendBroadcastMethodAccess ma | sink.asExpr() = ma.getAnArgument()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Holds if a `sendBroadcast` call doesn't specify receiver permission. |
| 88 | + */ |
| 89 | +predicate isSensitiveBroadcastSink(DataFlow::Node sink) { |
| 90 | + exists(SendBroadcastMethodAccess ma | |
| 91 | + sink.asExpr() = ma.getAnArgument() and |
| 92 | + ( |
| 93 | + ma.getMethod().hasName("sendBroadcast") and |
| 94 | + ( |
| 95 | + ma.getNumArgument() = 1 // sendBroadcast(Intent intent) |
| 96 | + or |
| 97 | + // sendBroadcast(Intent intent, String receiverPermission) |
| 98 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(1)))) |
| 99 | + ) |
| 100 | + or |
| 101 | + ma.getMethod().hasName("sendBroadcastAsUser") and |
| 102 | + ( |
| 103 | + ma.getNumArgument() = 2 or // sendBroadcastAsUser(Intent intent, UserHandle user) |
| 104 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(2)))) // sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission) |
| 105 | + ) |
| 106 | + or |
| 107 | + ma.getMethod().hasName("sendBroadcastWithMultiplePermissions") and |
| 108 | + exists(EmptyArrayArgFlowConfig config | |
| 109 | + config.hasFlow(_, DataFlow::exprNode(ma.getArgument(1))) // sendBroadcastWithMultiplePermissions(Intent intent, String[] receiverPermissions) |
| 110 | + ) |
| 111 | + or |
| 112 | + // Method calls of `sendOrderedBroadcast` whose second argument is always `receiverPermission` |
| 113 | + ma.getMethod().hasName("sendOrderedBroadcast") and |
| 114 | + ( |
| 115 | + // sendOrderedBroadcast(Intent intent, String receiverPermission) or sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 116 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(1)))) and |
| 117 | + ma.getNumArgument() <= 7 |
| 118 | + or |
| 119 | + // sendOrderedBroadcast(Intent intent, String receiverPermission, String receiverAppOp, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) |
| 120 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(1)))) and |
| 121 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(2)))) and |
| 122 | + ma.getNumArgument() = 8 |
| 123 | + ) |
| 124 | + or |
| 125 | + // Method call of `sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)` |
| 126 | + ma.getMethod().hasName("sendOrderedBroadcastAsUser") and |
| 127 | + exists(NullArgFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(ma.getArgument(2)))) |
| 128 | + ) |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Taint configuration tracking flow from variables containing sensitive information to broadcast intents. |
| 134 | + */ |
| 135 | +class SensitiveBroadcastConfig extends TaintTracking::Configuration { |
| 136 | + SensitiveBroadcastConfig() { this = "Sensitive Broadcast Configuration" } |
| 137 | + |
| 138 | + override predicate isSource(DataFlow::Node source) { |
| 139 | + source.asExpr() instanceof SensitiveInfoExpr |
| 140 | + } |
| 141 | + |
| 142 | + override predicate isSink(DataFlow::Node sink) { isSensitiveBroadcastSink(sink) } |
| 143 | + |
| 144 | + /** |
| 145 | + * Holds if there is an additional flow step from `PutIntentExtraMethodAccess` or `PutBundleExtraMethodAccess` that taints the `Intent` or its extras `Bundle`. |
| 146 | + */ |
| 147 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 148 | + exists(PutIntentExtraMethodAccess pia | |
| 149 | + node1.asExpr() = pia.getAnArgument() and node2.asExpr() = pia.getQualifier() |
| 150 | + ) |
| 151 | + or |
| 152 | + exists(PutBundleExtraMethodAccess pba | |
| 153 | + node1.asExpr() = pba.getAnArgument() and node2.asExpr() = pba.getQualifier() |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Holds if broadcast doesn't specify receiving package name of the 3rd party app |
| 159 | + */ |
| 160 | + override predicate isSanitizer(DataFlow::Node node) { |
| 161 | + exists(MethodAccess setReceiverMa | |
| 162 | + setReceiverMa.getMethod().hasName(["setPackage", "setClass", "setClassName", "setComponent"]) and |
| 163 | + setReceiverMa.getQualifier().(VarAccess).getVariable().getAnAccess() = node.asExpr() |
| 164 | + ) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +from SensitiveBroadcastConfig cfg, DataFlow::PathNode source, DataFlow::PathNode sink |
| 169 | +where cfg.hasFlowPath(source, sink) |
| 170 | +select sink.getNode(), source, sink, "Sending $@ to broadcast.", source.getNode(), |
| 171 | + "sensitive information" |
0 commit comments