|
| 1 | +/** Provides classes and predicates to reason about Android Fragment injection vulnerabilities. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.dataflow.TaintTracking |
| 5 | +private import semmle.code.java.dataflow.ExternalFlow |
| 6 | +private import semmle.code.java.frameworks.android.Android |
| 7 | +private import semmle.code.java.frameworks.android.Fragment |
| 8 | +private import semmle.code.java.Reflection |
| 9 | + |
| 10 | +/** The method `isValidFragment` of the class `android.preference.PreferenceActivity`. */ |
| 11 | +class IsValidFragmentMethod extends Method { |
| 12 | + IsValidFragmentMethod() { |
| 13 | + this.getDeclaringType() |
| 14 | + .getASupertype*() |
| 15 | + .hasQualifiedName("android.preference", "PreferenceActivity") and |
| 16 | + this.hasName("isValidFragment") |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Holds if this method makes the Activity it is declared in vulnerable to Fragment injection, |
| 21 | + * that is, all code paths in this method return `true` and the Activity is exported. |
| 22 | + */ |
| 23 | + predicate isUnsafe() { |
| 24 | + this.getDeclaringType().(AndroidActivity).isExported() and |
| 25 | + forex(ReturnStmt retStmt | retStmt.getEnclosingCallable() = this | |
| 26 | + retStmt.getResult().(BooleanLiteral).getBooleanValue() = true |
| 27 | + ) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A sink for Fragment injection vulnerabilities, |
| 33 | + * that is, method calls that dynamically add fragments to activities. |
| 34 | + */ |
| 35 | +abstract class FragmentInjectionSink extends DataFlow::Node { } |
| 36 | + |
| 37 | +/** An additional taint step for flows related to Fragment injection vulnerabilites. */ |
| 38 | +class FragmentInjectionAdditionalTaintStep extends Unit { |
| 39 | + /** |
| 40 | + * Holds if the step from `node1` to `node2` should be considered a taint |
| 41 | + * step in flows related to Fragment injection vulnerabilites. |
| 42 | + */ |
| 43 | + abstract predicate step(DataFlow::Node n1, DataFlow::Node n2); |
| 44 | +} |
| 45 | + |
| 46 | +private class FragmentInjectionSinkModels extends SinkModelCsv { |
| 47 | + override predicate row(string row) { |
| 48 | + row = |
| 49 | + ["android.app", "android.support.v4.app", "androidx.fragment.app"] + |
| 50 | + ";FragmentTransaction;true;" + |
| 51 | + [ |
| 52 | + "add;(Class,Bundle,String);;Argument[0]", "add;(Fragment,String);;Argument[0]", |
| 53 | + "add;(int,Class,Bundle);;Argument[1]", "add;(int,Fragment);;Argument[1]", |
| 54 | + "add;(int,Class,Bundle,String);;Argument[1]", "add;(int,Fragment,String);;Argument[1]", |
| 55 | + "attach;(Fragment);;Argument[0]", "replace;(int,Class,Bundle);;Argument[1]", |
| 56 | + "replace;(int,Fragment);;Argument[1]", "replace;(int,Class,Bundle,String);;Argument[1]", |
| 57 | + "replace;(int,Fragment,String);;Argument[1]", |
| 58 | + ] + ";fragment-injection" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +private class DefaultFragmentInjectionSink extends FragmentInjectionSink { |
| 63 | + DefaultFragmentInjectionSink() { sinkNode(this, "fragment-injection") } |
| 64 | +} |
| 65 | + |
| 66 | +private class DefaultFragmentInjectionAdditionalTaintStep extends FragmentInjectionAdditionalTaintStep { |
| 67 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 68 | + exists(ReflectiveClassIdentifierMethodAccess ma | |
| 69 | + ma.getArgument(0) = n1.asExpr() and ma = n2.asExpr() |
| 70 | + ) |
| 71 | + or |
| 72 | + exists(NewInstance ni | |
| 73 | + ni.getQualifier() = n1.asExpr() and |
| 74 | + ni = n2.asExpr() |
| 75 | + ) |
| 76 | + or |
| 77 | + exists(MethodAccess ma | |
| 78 | + ma.getMethod() instanceof FragmentInstantiateMethod and |
| 79 | + ma.getArgument(1) = n1.asExpr() and |
| 80 | + ma = n2.asExpr() |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
0 commit comments