|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.FlowSources |
| 3 | + |
| 4 | +/** |
| 5 | + * A taint-tracking configuration detecting unsafe use of a |
| 6 | + * `DexClassLoader` by an Android app. |
| 7 | + */ |
| 8 | +class InsecureDexConfiguration extends TaintTracking::Configuration { |
| 9 | + InsecureDexConfiguration() { this = "Insecure Dex File Load" } |
| 10 | + |
| 11 | + override predicate isSource(DataFlow::Node source) { source instanceof InsecureDexSource } |
| 12 | + |
| 13 | + override predicate isSink(DataFlow::Node sink) { sink instanceof InsecureDexSink } |
| 14 | + |
| 15 | + override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 16 | + flowStep(pred, succ) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +/** A data flow source for insecure Dex class loading vulnerabilities. */ |
| 21 | +abstract class InsecureDexSource extends DataFlow::Node { } |
| 22 | + |
| 23 | +/** A data flow sink for insecure Dex class loading vulnerabilities. */ |
| 24 | +abstract class InsecureDexSink extends DataFlow::Node { } |
| 25 | + |
| 26 | +private predicate flowStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 27 | + // propagate from a `java.io.File` via the `File.getAbsolutePath` call. |
| 28 | + exists(MethodAccess m | |
| 29 | + m.getMethod().getDeclaringType() instanceof TypeFile and |
| 30 | + m.getMethod().hasName("getAbsolutePath") and |
| 31 | + m.getQualifier() = pred.asExpr() and |
| 32 | + m = succ.asExpr() |
| 33 | + ) |
| 34 | + or |
| 35 | + // propagate from a `java.io.File` via the `File.toString` call. |
| 36 | + exists(MethodAccess m | |
| 37 | + m.getMethod().getDeclaringType() instanceof TypeFile and |
| 38 | + m.getMethod().hasName("toString") and |
| 39 | + m.getQualifier() = pred.asExpr() and |
| 40 | + m = succ.asExpr() |
| 41 | + ) |
| 42 | + or |
| 43 | + // propagate to newly created `File` if the parent directory of the new `File` is tainted |
| 44 | + exists(ConstructorCall cc | |
| 45 | + cc.getConstructedType() instanceof TypeFile and |
| 46 | + cc.getArgument(0) = pred.asExpr() and |
| 47 | + cc = succ.asExpr() |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * An argument to a `DexClassLoader` call taken as a sink for |
| 53 | + * insecure Dex class loading vulnerabilities. |
| 54 | + */ |
| 55 | +private class DexClassLoader extends InsecureDexSink { |
| 56 | + DexClassLoader() { |
| 57 | + exists(ConstructorCall cc | |
| 58 | + cc.getConstructedType().hasQualifiedName("dalvik.system", "DexClassLoader") |
| 59 | + | |
| 60 | + this.asExpr() = cc.getArgument(0) |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * A `File` instance which reads from an SD card |
| 67 | + * taken as a source for insecure Dex class loading vulnerabilities. |
| 68 | + */ |
| 69 | +private class ExternalFile extends InsecureDexSource { |
| 70 | + ExternalFile() { |
| 71 | + exists(ConstructorCall cc, Argument a | |
| 72 | + cc.getConstructedType() instanceof TypeFile and |
| 73 | + a = cc.getArgument(0) and |
| 74 | + a.(CompileTimeConstantExpr).getStringValue().matches("%sdcard%") |
| 75 | + | |
| 76 | + this.asExpr() = a |
| 77 | + ) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * A directory or file which may be stored in an world writable directory |
| 83 | + * taken as a source for insecure Dex class loading vulnerabilities. |
| 84 | + */ |
| 85 | +private class ExternalStorageDirSource extends InsecureDexSource { |
| 86 | + ExternalStorageDirSource() { |
| 87 | + exists(Method m | |
| 88 | + m.getDeclaringType().hasQualifiedName("android.os", "Environment") and |
| 89 | + m.hasName("getExternalStorageDirectory") |
| 90 | + or |
| 91 | + m.getDeclaringType().hasQualifiedName("android.content", "Context") and |
| 92 | + m.hasName([ |
| 93 | + "getExternalFilesDir", "getExternalFilesDirs", "getExternalMediaDirs", |
| 94 | + "getExternalCacheDir", "getExternalCacheDirs" |
| 95 | + ]) |
| 96 | + | |
| 97 | + this.asExpr() = m.getAReference() |
| 98 | + ) |
| 99 | + } |
| 100 | +} |
0 commit comments