|
| 1 | +/** Definitions for the improper intent verification query. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.xml.AndroidManifest |
| 6 | +import semmle.code.java.frameworks.android.Intent |
| 7 | + |
| 8 | +/** An `onReceive` method of a `BroadcastReceiver` */ |
| 9 | +private class OnReceiveMethod extends Method { |
| 10 | + OnReceiveMethod() { this.getASourceOverriddenMethod*() instanceof AndroidReceiveIntentMethod } |
| 11 | + |
| 12 | + /** Gets the parameter of this method that holds the received `Intent`. */ |
| 13 | + Parameter getIntentParameter() { result = this.getParameter(1) } |
| 14 | +} |
| 15 | + |
| 16 | +/** A configuration to detect whether the `action` of an `Intent` is checked. */ |
| 17 | +private class VerifiedIntentConfig extends DataFlow::Configuration { |
| 18 | + VerifiedIntentConfig() { this = "VerifiedIntentConfig" } |
| 19 | + |
| 20 | + override predicate isSource(DataFlow::Node src) { |
| 21 | + src.asParameter() = any(OnReceiveMethod orm).getIntentParameter() |
| 22 | + } |
| 23 | + |
| 24 | + override predicate isSink(DataFlow::Node sink) { |
| 25 | + exists(MethodAccess ma | |
| 26 | + ma.getMethod().hasQualifiedName("android.content", "Intent", "getAction") and |
| 27 | + sink.asExpr() = ma.getQualifier() |
| 28 | + ) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** An `onReceive` method that doesn't verify the action of the intent it receives. */ |
| 33 | +private class UnverifiedOnReceiveMethod extends OnReceiveMethod { |
| 34 | + UnverifiedOnReceiveMethod() { |
| 35 | + not any(VerifiedIntentConfig c).hasFlow(DataFlow::parameterNode(this.getIntentParameter()), _) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** Gets the name of an intent action that can only be sent by the system. */ |
| 40 | +string getASystemActionName() { |
| 41 | + result = |
| 42 | + [ |
| 43 | + "AIRPLANE_MODE", "AIRPLANE_MODE_CHANGED", "APPLICATION_LOCALE_CHANGED", |
| 44 | + "APPLICATION_RESTRICTIONS_CHANGED", "BATTERY_CHANGED", "BATTERY_LOW", "BATTERY_OKAY", |
| 45 | + "BOOT_COMPLETED", "CONFIGURATION_CHANGED", "DEVICE_STORAGE_LOW", "DEVICE_STORAGE_OK", |
| 46 | + "DREAMING_STARTED", "DREAMING_STOPPED", "EXTERNAL_APPLICATIONS_AVAILABLE", |
| 47 | + "EXTERNAL_APPLICATIONS_UNAVAILABLE", "LOCALE_CHANGED", "LOCKED_BOOT_COMPLETED", |
| 48 | + "MY_PACKAGE_REPLACED", "MY_PACKAGE_SUSPENDED", "MY_PACKAGE_UNSUSPENDED", "NEW_OUTGOING_CALL", |
| 49 | + "PACKAGES_SUSPENDED", "PACKAGES_UNSUSPENDED", "PACKAGE_ADDED", "PACKAGE_CHANGED", |
| 50 | + "PACKAGE_DATA_CLEARED", "PACKAGE_FIRST_LAUNCH", "PACKAGE_FULLY_REMOVED", "PACKAGE_INSTALL", |
| 51 | + "PACKAGE_NEEDS_VERIFICATION", "PACKAGE_REMOVED", "PACKAGE_REPLACED", "PACKAGE_RESTARTED", |
| 52 | + "PACKAGE_VERIFIED", "POWER_CONNECTED", "POWER_DISCONNECTED", "REBOOT", "SCREEN_OFF", |
| 53 | + "SCREEN_ON", "SHUTDOWN", "TIMEZONE_CHANGED", "TIME_TICK", "UID_REMOVED", "USER_PRESENT" |
| 54 | + ] |
| 55 | +} |
| 56 | + |
| 57 | +/** An expression or XML attribute that contains the name of a system intent action. */ |
| 58 | +class SystemActionName extends AndroidActionXmlElement { |
| 59 | + string name; |
| 60 | + |
| 61 | + SystemActionName() { |
| 62 | + name = getASystemActionName() and |
| 63 | + this.getActionName() = "android.intent.action." + name |
| 64 | + } |
| 65 | + |
| 66 | + /** Gets the name of the system intent that this expression or attribute represents. */ |
| 67 | + string getSystemActionName() { result = name } |
| 68 | +} |
| 69 | + |
| 70 | +/** Holds if the XML element `rec` declares a receiver `orm` to receive the system action named `sa` that doesn't verify intents it receives. */ |
| 71 | +predicate unverifiedSystemReceiver( |
| 72 | + AndroidReceiverXmlElement rec, UnverifiedOnReceiveMethod orm, SystemActionName sa |
| 73 | +) { |
| 74 | + exists(Class ormty | |
| 75 | + ormty = orm.getDeclaringType() and |
| 76 | + rec.getComponentName() = ["." + ormty.getName(), ormty.getQualifiedName()] and |
| 77 | + rec.getAnIntentFilterElement().getAnActionElement() = sa |
| 78 | + ) |
| 79 | +} |
0 commit comments