|
| 1 | +/** Provides classes and predicates to reason about cleartext storage in Android's SharedPreferences. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.dataflow.DataFlow4 |
| 6 | +import semmle.code.java.frameworks.android.SharedPreferences |
| 7 | +import semmle.code.java.security.CleartextStorageQuery |
| 8 | + |
| 9 | +private class SharedPrefsCleartextStorageSink extends CleartextStorageSink { |
| 10 | + SharedPrefsCleartextStorageSink() { |
| 11 | + exists(MethodAccess m | |
| 12 | + m.getMethod() instanceof PutSharedPreferenceMethod and |
| 13 | + this.asExpr() = m.getArgument(1) |
| 14 | + ) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * The call to get a `SharedPreferences.Editor` object, which can set shared preferences and be |
| 20 | + * stored to the device. |
| 21 | + */ |
| 22 | +class SharedPreferencesEditorMethodAccess extends Storable, MethodAccess { |
| 23 | + SharedPreferencesEditorMethodAccess() { |
| 24 | + this.getMethod() instanceof GetSharedPreferencesEditorMethod and |
| 25 | + not DataFlow::localExprFlow(any(MethodAccess ma | |
| 26 | + ma.getMethod() instanceof CreateEncryptedSharedPreferencesMethod |
| 27 | + ), this.getQualifier()) |
| 28 | + } |
| 29 | + |
| 30 | + /** Gets an input, for example `password` in `editor.putString("password", password);`. */ |
| 31 | + override Expr getAnInput() { |
| 32 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node editor | |
| 33 | + sharedPreferencesInput(editor, result) and |
| 34 | + conf.hasFlow(DataFlow::exprNode(this), editor) |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + /** Gets a store, for example `editor.commit();`. */ |
| 39 | + override Expr getAStore() { |
| 40 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node editor | |
| 41 | + sharedPreferencesStore(editor, result) and |
| 42 | + conf.hasFlow(DataFlow::exprNode(this), editor) |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Holds if `input` is not encrypted and is the second argument of a setter method |
| 49 | + * called on `editor`, which is an instance of `SharedPreferences$Editor` |
| 50 | + * . |
| 51 | + */ |
| 52 | +private predicate sharedPreferencesInput(DataFlow::Node editor, Expr input) { |
| 53 | + exists(MethodAccess m | |
| 54 | + m.getMethod() instanceof PutSharedPreferenceMethod and |
| 55 | + input = m.getArgument(1) and |
| 56 | + not exists(EncryptedValueFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(input))) and |
| 57 | + editor.asExpr() = m.getQualifier() |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Holds if `m` is a store method called on `editor`, |
| 63 | + * which is an instance of `SharedPreferences$Editor`. |
| 64 | + */ |
| 65 | +private predicate sharedPreferencesStore(DataFlow::Node editor, MethodAccess m) { |
| 66 | + m.getMethod() instanceof StoreSharedPreferenceMethod and |
| 67 | + editor.asExpr() = m.getQualifier() |
| 68 | +} |
| 69 | + |
| 70 | +/** Flow from `SharedPreferences.Editor` to either a setter or a store method. */ |
| 71 | +private class SharedPreferencesFlowConfig extends DataFlow::Configuration { |
| 72 | + SharedPreferencesFlowConfig() { this = "SharedPreferencesFlowConfig" } |
| 73 | + |
| 74 | + override predicate isSource(DataFlow::Node src) { |
| 75 | + src.asExpr() instanceof SharedPreferencesEditorMethodAccess |
| 76 | + } |
| 77 | + |
| 78 | + override predicate isSink(DataFlow::Node sink) { |
| 79 | + sharedPreferencesInput(sink, _) or |
| 80 | + sharedPreferencesStore(sink, _) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Method call for encrypting sensitive information. As there are various implementations of |
| 86 | + * encryption (reversible and non-reversible) from both JDK and third parties, this class simply |
| 87 | + * checks method name to take a best guess to reduce false positives. |
| 88 | + */ |
| 89 | +private class EncryptedSensitiveMethodAccess extends MethodAccess { |
| 90 | + EncryptedSensitiveMethodAccess() { |
| 91 | + this.getMethod().getName().toLowerCase().matches(["%encrypt%", "%hash%"]) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** Flow configuration for encryption methods flowing to inputs of `SharedPreferences`. */ |
| 96 | +private class EncryptedValueFlowConfig extends DataFlow4::Configuration { |
| 97 | + EncryptedValueFlowConfig() { this = "SensitiveStorage::EncryptedValueFlowConfig" } |
| 98 | + |
| 99 | + override predicate isSource(DataFlow::Node src) { |
| 100 | + src.asExpr() instanceof EncryptedSensitiveMethodAccess |
| 101 | + } |
| 102 | + |
| 103 | + override predicate isSink(DataFlow::Node sink) { sink instanceof SharedPrefsCleartextStorageSink } |
| 104 | +} |
0 commit comments