|
| 1 | +/** Provides classes and predicates to reason about cleartext storage in Android databases. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.frameworks.android.ContentProviders |
| 6 | +import semmle.code.java.frameworks.android.Intent |
| 7 | +import semmle.code.java.frameworks.android.SQLite |
| 8 | +import semmle.code.java.security.CleartextStorageQuery |
| 9 | + |
| 10 | +private class LocalDatabaseCleartextStorageSink extends CleartextStorageSink { |
| 11 | + LocalDatabaseCleartextStorageSink() { localDatabaseInput(_, this.asExpr()) } |
| 12 | +} |
| 13 | + |
| 14 | +private class LocalDatabaseCleartextStorageStep extends CleartextStorageAdditionalTaintStep { |
| 15 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 16 | + // EditText.getText() return type is parsed as `Object`, so we need to |
| 17 | + // add a taint step for `Object.toString` to model `editText.getText().toString()` |
| 18 | + exists(MethodAccess ma, Method m | |
| 19 | + ma.getMethod() = m and |
| 20 | + m.getDeclaringType() instanceof TypeObject and |
| 21 | + m.hasName("toString") |
| 22 | + | |
| 23 | + n1.asExpr() = ma.getQualifier() and n2.asExpr() = ma |
| 24 | + ) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** The creation of an object that can be used to store data in a local database. */ |
| 29 | +class LocalDatabaseOpenMethodAccess extends Storable, Call { |
| 30 | + LocalDatabaseOpenMethodAccess() { |
| 31 | + exists(Method m | this.(MethodAccess).getMethod() = m | |
| 32 | + m.getDeclaringType().getASupertype*() instanceof TypeSQLiteOpenHelper and |
| 33 | + m.hasName("getWritableDatabase") |
| 34 | + or |
| 35 | + m.getDeclaringType() instanceof TypeSQLiteDatabase and |
| 36 | + m.hasName(["create", "open%Database", "compileStatement"]) |
| 37 | + or |
| 38 | + m.getDeclaringType().getASupertype*() instanceof TypeContext and |
| 39 | + m.hasName("openOrCreateDatabase") |
| 40 | + ) |
| 41 | + or |
| 42 | + this.(ClassInstanceExpr).getConstructedType() instanceof ContentValues |
| 43 | + } |
| 44 | + |
| 45 | + override Expr getAnInput() { |
| 46 | + exists(LocalDatabaseFlowConfig config, DataFlow::Node database | |
| 47 | + localDatabaseInput(database, result) and |
| 48 | + config.hasFlow(DataFlow::exprNode(this), database) |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + override Expr getAStore() { |
| 53 | + exists(LocalDatabaseFlowConfig config, DataFlow::Node database | |
| 54 | + localDatabaseStore(database, result) and |
| 55 | + config.hasFlow(DataFlow::exprNode(this), database) |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** A method that is both a database input and a database store. */ |
| 61 | +private class LocalDatabaseInputStoreMethod extends Method { |
| 62 | + LocalDatabaseInputStoreMethod() { |
| 63 | + this.getDeclaringType() instanceof TypeSQLiteDatabase and |
| 64 | + this.getName().matches("exec%SQL") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +private predicate localDatabaseInput(DataFlow::Node database, Argument input) { |
| 69 | + exists(Method m | input.getCall().(MethodAccess).getMethod() = m | |
| 70 | + m instanceof LocalDatabaseInputStoreMethod and |
| 71 | + database.asExpr() = input.getCall().getQualifier() |
| 72 | + or |
| 73 | + m.getDeclaringType() instanceof TypeSQLiteDatabase and |
| 74 | + m.hasName("compileStatement") and |
| 75 | + database.asExpr() = input.getCall() |
| 76 | + or |
| 77 | + m.getDeclaringType() instanceof ContentValues and |
| 78 | + m.hasName("put") and |
| 79 | + input.getPosition() = 1 and |
| 80 | + database.asExpr() = input.getCall().getQualifier() |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +private predicate localDatabaseStore(DataFlow::Node database, MethodAccess store) { |
| 85 | + exists(Method m | store.getMethod() = m | |
| 86 | + m instanceof LocalDatabaseInputStoreMethod and |
| 87 | + database.asExpr() = store.getQualifier() |
| 88 | + or |
| 89 | + m.getDeclaringType() instanceof TypeSQLiteDatabase and |
| 90 | + m.getName().matches(["insert%", "replace%", "update%"]) and |
| 91 | + database.asExpr() = store.getAnArgument() and |
| 92 | + database.getType() instanceof ContentValues |
| 93 | + or |
| 94 | + m.getDeclaringType() instanceof TypeSQLiteStatement and |
| 95 | + m.hasName(["executeInsert", "executeUpdateDelete"]) and |
| 96 | + database.asExpr() = store.getQualifier() |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +private class LocalDatabaseFlowConfig extends DataFlow::Configuration { |
| 101 | + LocalDatabaseFlowConfig() { this = "LocalDatabaseFlowConfig" } |
| 102 | + |
| 103 | + override predicate isSource(DataFlow::Node source) { |
| 104 | + source.asExpr() instanceof LocalDatabaseOpenMethodAccess |
| 105 | + } |
| 106 | + |
| 107 | + override predicate isSink(DataFlow::Node sink) { |
| 108 | + localDatabaseInput(sink, _) or |
| 109 | + localDatabaseStore(sink, _) |
| 110 | + } |
| 111 | + |
| 112 | + override predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 113 | + exists(Field f | |
| 114 | + f.getType() instanceof TypeSQLiteDatabase and |
| 115 | + f.getAnAssignedValue() = n1.asExpr() and |
| 116 | + f = n2.asExpr().(FieldRead).getField() |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
0 commit comments