|
| 1 | +/** |
| 2 | + * @name Cleartext storage of sensitive information using `SharedPreferences` on Android |
| 3 | + * @description Cleartext Storage of Sensitive Information using SharedPreferences on Android allows access for users with root privileges or unexpected exposure from chained vulnerabilities. |
| 4 | + * @kind problem |
| 5 | + * @id java/android/cleartext-storage-shared-prefs |
| 6 | + * @tags security |
| 7 | + * external/cwe/cwe-312 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.dataflow.DataFlow4 |
| 12 | +import semmle.code.java.dataflow.DataFlow5 |
| 13 | +import semmle.code.java.dataflow.TaintTracking |
| 14 | +import semmle.code.java.frameworks.android.Intent |
| 15 | +import semmle.code.java.frameworks.android.SharedPreferences |
| 16 | +import semmle.code.java.security.SensitiveActions |
| 17 | + |
| 18 | +/** Holds if the method call is a setter method of `SharedPreferences`. */ |
| 19 | +private predicate sharedPreferencesInput(DataFlow::Node sharedPrefs, Expr input) { |
| 20 | + exists(MethodAccess m | |
| 21 | + m.getMethod() instanceof PutSharedPreferenceMethod and |
| 22 | + input = m.getArgument(1) and |
| 23 | + not exists(EncryptedValueFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(input))) and |
| 24 | + sharedPrefs.asExpr() = m.getQualifier() |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +/** Holds if the method call is the store method of `SharedPreferences`. */ |
| 29 | +private predicate sharedPreferencesStore(DataFlow::Node sharedPrefs, Expr store) { |
| 30 | + exists(MethodAccess m | |
| 31 | + m.getMethod() instanceof StoreSharedPreferenceMethod and |
| 32 | + store = m and |
| 33 | + sharedPrefs.asExpr() = m.getQualifier() |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +/** Flow from `SharedPreferences` to either a setter or a store method. */ |
| 38 | +class SharedPreferencesFlowConfig extends DataFlow::Configuration { |
| 39 | + SharedPreferencesFlowConfig() { |
| 40 | + this = "CleartextStorageSharedPrefs::SharedPreferencesFlowConfig" |
| 41 | + } |
| 42 | + |
| 43 | + override predicate isSource(DataFlow::Node src) { |
| 44 | + src.asExpr() instanceof SharedPreferencesEditorMethodAccess |
| 45 | + } |
| 46 | + |
| 47 | + override predicate isSink(DataFlow::Node sink) { |
| 48 | + sharedPreferencesInput(sink, _) or |
| 49 | + sharedPreferencesStore(sink, _) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Method call of encrypting sensitive information. |
| 55 | + * As there are various implementations of encryption (reversible and non-reversible) from both JDK and third parties, this class simply checks method name to take a best guess to reduce false positives. |
| 56 | + */ |
| 57 | +class EncryptedSensitiveMethodAccess extends MethodAccess { |
| 58 | + EncryptedSensitiveMethodAccess() { |
| 59 | + this.getMethod().getName().toLowerCase().matches(["%encrypt%", "%hash%"]) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** Flow configuration of encrypting sensitive information. */ |
| 64 | +class EncryptedValueFlowConfig extends DataFlow5::Configuration { |
| 65 | + EncryptedValueFlowConfig() { this = "CleartextStorageSharedPrefs::EncryptedValueFlowConfig" } |
| 66 | + |
| 67 | + override predicate isSource(DataFlow5::Node src) { |
| 68 | + exists(EncryptedSensitiveMethodAccess ema | src.asExpr() = ema) |
| 69 | + } |
| 70 | + |
| 71 | + override predicate isSink(DataFlow5::Node sink) { |
| 72 | + exists(MethodAccess ma | |
| 73 | + ma.getMethod() instanceof PutSharedPreferenceMethod and |
| 74 | + sink.asExpr() = ma.getArgument(1) |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** Flow from the create method of `androidx.security.crypto.EncryptedSharedPreferences` to its instance. */ |
| 80 | +private class EncryptedSharedPrefFlowConfig extends DataFlow4::Configuration { |
| 81 | + EncryptedSharedPrefFlowConfig() { |
| 82 | + this = "CleartextStorageSharedPrefs::EncryptedSharedPrefFlowConfig" |
| 83 | + } |
| 84 | + |
| 85 | + override predicate isSource(DataFlow4::Node src) { |
| 86 | + src.asExpr().(MethodAccess).getMethod() instanceof CreateEncryptedSharedPreferencesMethod |
| 87 | + } |
| 88 | + |
| 89 | + override predicate isSink(DataFlow4::Node sink) { |
| 90 | + sink.asExpr().getType() instanceof SharedPreferences |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** The call to get a `SharedPreferences.Editor` object, which can set shared preferences or be stored to device. */ |
| 95 | +class SharedPreferencesEditorMethodAccess extends MethodAccess { |
| 96 | + SharedPreferencesEditorMethodAccess() { |
| 97 | + this.getMethod() instanceof GetSharedPreferencesEditorMethod and |
| 98 | + not exists( |
| 99 | + EncryptedSharedPrefFlowConfig config // not exists `SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(...)` |
| 100 | + | |
| 101 | + config.hasFlow(_, DataFlow::exprNode(this.getQualifier())) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + /** Gets an input, for example `password` in `editor.putString("password", password);`. */ |
| 106 | + Expr getAnInput() { |
| 107 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node n | |
| 108 | + sharedPreferencesInput(n, result) and |
| 109 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + /** Gets a store, for example `editor.commit();`. */ |
| 114 | + Expr getAStore() { |
| 115 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node n | |
| 116 | + sharedPreferencesStore(n, result) and |
| 117 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 118 | + ) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Flow from sensitive expressions to shared preferences. |
| 124 | + * Note it can be merged into `SensitiveSourceFlowConfig` of `Security/CWE/CWE-312/SensitiveStorage.qll` when this query is promoted from the experimental directory. |
| 125 | + */ |
| 126 | +private class SensitiveSharedPrefsFlowConfig extends TaintTracking::Configuration { |
| 127 | + SensitiveSharedPrefsFlowConfig() { |
| 128 | + this = "CleartextStorageSharedPrefs::SensitiveSharedPrefsFlowConfig" |
| 129 | + } |
| 130 | + |
| 131 | + override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SensitiveExpr } |
| 132 | + |
| 133 | + override predicate isSink(DataFlow::Node sink) { |
| 134 | + exists(MethodAccess m | |
| 135 | + m.getMethod() instanceof PutSharedPreferenceMethod and |
| 136 | + sink.asExpr() = m.getArgument(1) |
| 137 | + ) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/** Class for shared preferences that may contain 'sensitive' information */ |
| 142 | +class SensitiveSharedPrefsSource extends Expr { |
| 143 | + SensitiveSharedPrefsSource() { |
| 144 | + // SensitiveExpr is abstract, this lets us inherit from it without |
| 145 | + // being a technical subclass |
| 146 | + this instanceof SensitiveExpr |
| 147 | + } |
| 148 | + |
| 149 | + /** Holds if this source flows to the `sink`. */ |
| 150 | + predicate flowsTo(Expr sink) { |
| 151 | + exists(SensitiveSharedPrefsFlowConfig conf | |
| 152 | + conf.hasFlow(DataFlow::exprNode(this), DataFlow::exprNode(sink)) |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +from SensitiveSharedPrefsSource data, SharedPreferencesEditorMethodAccess s, Expr input, Expr store |
| 158 | +where |
| 159 | + input = s.getAnInput() and |
| 160 | + store = s.getAStore() and |
| 161 | + data.flowsTo(input) |
| 162 | +select store, "'SharedPreferences' class $@ containing $@ is stored here. Data was added $@.", s, |
| 163 | + s.toString(), data, "sensitive data", input, "here" |
0 commit comments