|
| 1 | +/** Provides definitions for working with uses of Android external storage */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +private import semmle.code.java.dataflow.ExternalFlow |
| 6 | + |
| 7 | +private class ExternalStorageDirSourceModel extends SourceModelCsv { |
| 8 | + override predicate row(string row) { |
| 9 | + row = |
| 10 | + [ |
| 11 | + //"package;type;overrides;name;signature;ext;spec;kind" |
| 12 | + "android.content;Context;true;getExternalFilesDir;(String);;ReturnValue;android-external-storage-dir", |
| 13 | + "android.content;Context;true;getExternalFilesDirs;(String);;ReturnValue.ArrayElement;android-external-storage-dir", |
| 14 | + "android.content;Context;true;getExternalCachesDir;(String);;ReturnValue;android-external-storage-dir", |
| 15 | + "android.content;Context;true;getExternalCachesDirs;(String);;ReturnValue.ArrayElement;android-external-storage-dir", |
| 16 | + "android.os;Environment;false;getExternalStorageDirectory;(String);;ReturnValue.ArrayElement;android-external-storage-dir", |
| 17 | + "android.os;Environment;false;getExternalStoragePublicDirectory;(String);;ReturnValue.ArrayElement;android-external-storage-dir", |
| 18 | + ] |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +private predicate externalStorageFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 23 | + DataFlow::localFlowStep(node1, node2) |
| 24 | + or |
| 25 | + exists(ConstructorCall c | c.getConstructedType() instanceof TypeFile | |
| 26 | + node1.asExpr() = c.getArgument(1) and |
| 27 | + node2.asExpr() = c |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +private predicate externalStorageFlow(DataFlow::Node node1, DataFlow::Node node2) { |
| 32 | + externalStorageFlowStep*(node1, node2) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Holds if `n` is a node that reads the contents of an external file in Android. |
| 37 | + * This may be controlable by third-party applications, so is treated as a remote flow source. |
| 38 | + */ |
| 39 | +predicate androidExternalStorageSource(DataFlow::Node n) { |
| 40 | + exists(ConstructorCall fInp, DataFlow::Node externalDir | |
| 41 | + fInp.getConstructedType().hasQualifiedName("java.io", "FileInputStream") and |
| 42 | + n.asExpr() = fInp and |
| 43 | + sourceNode(externalDir, "android-external-storage-dir") and |
| 44 | + externalStorageFlow(externalDir, DataFlow::exprNode(fInp.getArgument(0))) |
| 45 | + ) |
| 46 | +} |
0 commit comments