|
| 1 | +/** |
| 2 | + * @name Load 3rd party classes or code ('unsafe reflection') without signature check |
| 3 | + * @description Loading classes or code from third-party packages without checking the |
| 4 | + * package signature could make the application |
| 5 | + * susceptible to package namespace squatting attacks, |
| 6 | + * potentially leading to arbitrary code execution. |
| 7 | + * @problem.severity error |
| 8 | + * @precision high |
| 9 | + * @kind path-problem |
| 10 | + * @id java/android/unsafe-reflection |
| 11 | + * @tags security |
| 12 | + * experimental |
| 13 | + * external/cwe/cwe-470 |
| 14 | + */ |
| 15 | + |
| 16 | +import java |
| 17 | +import semmle.code.java.dataflow.TaintTracking |
| 18 | +import semmle.code.java.controlflow.Guards |
| 19 | +import semmle.code.java.dataflow.SSA |
| 20 | +import semmle.code.java.frameworks.android.Intent |
| 21 | + |
| 22 | +class CheckSignaturesGuard extends Guard instanceof EqualityTest { |
| 23 | + MethodCall checkSignatures; |
| 24 | + |
| 25 | + CheckSignaturesGuard() { |
| 26 | + this.getAnOperand() = checkSignatures and |
| 27 | + checkSignatures |
| 28 | + .getMethod() |
| 29 | + .hasQualifiedName("android.content.pm", "PackageManager", "checkSignatures") and |
| 30 | + exists(Expr signatureCheckResult | |
| 31 | + this.getAnOperand() = signatureCheckResult and signatureCheckResult != checkSignatures |
| 32 | + | |
| 33 | + signatureCheckResult.(CompileTimeConstantExpr).getIntValue() = 0 or |
| 34 | + signatureCheckResult |
| 35 | + .(FieldRead) |
| 36 | + .getField() |
| 37 | + .hasQualifiedName("android.content.pm", "PackageManager", "SIGNATURE_MATCH") |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + Expr getCheckedExpr() { result = checkSignatures.getArgument(0) } |
| 42 | +} |
| 43 | + |
| 44 | +predicate signatureChecked(Expr safe) { |
| 45 | + exists(CheckSignaturesGuard g, SsaVariable v | |
| 46 | + v.getAUse() = g.getCheckedExpr() and |
| 47 | + safe = v.getAUse() and |
| 48 | + g.controls(safe.getBasicBlock(), g.(EqualityTest).polarity()) |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +module InsecureLoadingConfig implements DataFlow::ConfigSig { |
| 53 | + predicate isSource(DataFlow::Node src) { |
| 54 | + exists(Method m | m = src.asExpr().(MethodCall).getMethod() | |
| 55 | + m.getDeclaringType().getASourceSupertype*() instanceof TypeContext and |
| 56 | + m.hasName("createPackageContext") and |
| 57 | + not signatureChecked(src.asExpr().(MethodCall).getArgument(0)) |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + predicate isSink(DataFlow::Node sink) { |
| 62 | + exists(MethodCall ma | |
| 63 | + ma.getMethod().hasQualifiedName("java.lang", "ClassLoader", "loadClass") |
| 64 | + | |
| 65 | + sink.asExpr() = ma.getQualifier() |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 70 | + exists(MethodCall ma, Method m | |
| 71 | + ma.getMethod() = m and |
| 72 | + m.getDeclaringType().getASourceSupertype*() instanceof TypeContext and |
| 73 | + m.hasName("getClassLoader") |
| 74 | + | |
| 75 | + node1.asExpr() = ma.getQualifier() and |
| 76 | + node2.asExpr() = ma |
| 77 | + ) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module InsecureLoadFlow = TaintTracking::Global<InsecureLoadingConfig>; |
| 82 | + |
| 83 | +import InsecureLoadFlow::PathGraph |
| 84 | + |
| 85 | +from InsecureLoadFlow::PathNode source, InsecureLoadFlow::PathNode sink |
| 86 | +where InsecureLoadFlow::flowPath(source, sink) |
| 87 | +select sink.getNode(), source, sink, "Class loaded from a $@ without signature check", |
| 88 | + source.getNode(), "third party library" |
0 commit comments