|
1 | 1 | import java |
2 | 2 | import semmle.code.java.frameworks.Properties |
3 | 3 | import semmle.code.java.frameworks.JAXB |
| 4 | +import semmle.code.java.frameworks.android.SharedPreferences |
4 | 5 | import semmle.code.java.dataflow.TaintTracking |
5 | 6 | import semmle.code.java.dataflow.DataFlow3 |
6 | 7 | import semmle.code.java.dataflow.DataFlow4 |
@@ -28,6 +29,10 @@ private class SensitiveSourceFlowConfig extends TaintTracking::Configuration { |
28 | 29 | m.getMethod() instanceof PropertiesSetPropertyMethod and sink.asExpr() = m.getArgument(1) |
29 | 30 | ) |
30 | 31 | or |
| 32 | + exists(MethodAccess m | |
| 33 | + m.getMethod() instanceof SharedPreferencesSetMethod and sink.asExpr() = m.getArgument(1) |
| 34 | + ) |
| 35 | + or |
31 | 36 | sink.asExpr() = getInstanceInput(_, _) |
32 | 37 | } |
33 | 38 |
|
@@ -243,3 +248,72 @@ class Marshallable extends ClassStore { |
243 | 248 | ) |
244 | 249 | } |
245 | 250 | } |
| 251 | + |
| 252 | +/* Holds if the method call is a setter method of `SharedPreferences`. */ |
| 253 | +private predicate sharedPreferencesInput(DataFlow::Node sharedPrefs, Expr input) { |
| 254 | + exists(MethodAccess m | |
| 255 | + m.getMethod() instanceof SharedPreferencesSetMethod and |
| 256 | + input = m.getArgument(1) and |
| 257 | + sharedPrefs.asExpr() = m.getQualifier() |
| 258 | + ) |
| 259 | +} |
| 260 | + |
| 261 | +/* Holds if the method call is the save method of `SharedPreferences`. */ |
| 262 | +private predicate sharedPreferencesStore(DataFlow::Node sharedPrefs, Expr store) { |
| 263 | + exists(MethodAccess m | |
| 264 | + m.getMethod() instanceof SharedPreferencesStoreMethod and |
| 265 | + store = m and |
| 266 | + sharedPrefs.asExpr() = m.getQualifier() |
| 267 | + ) |
| 268 | +} |
| 269 | + |
| 270 | +/* Flow from `SharedPreferences` to the method call changing its value. */ |
| 271 | +class SharedPreferencesFlowConfig extends TaintTracking::Configuration { |
| 272 | + SharedPreferencesFlowConfig() { this = "SensitiveStorage::SharedPreferencesFlowConfig" } |
| 273 | + |
| 274 | + override predicate isSource(DataFlow::Node src) { |
| 275 | + src.asExpr() instanceof SharedPreferencesEditor |
| 276 | + } |
| 277 | + |
| 278 | + override predicate isSink(DataFlow::Node sink) { |
| 279 | + sharedPreferencesInput(sink, _) or |
| 280 | + sharedPreferencesStore(sink, _) |
| 281 | + } |
| 282 | + |
| 283 | + override predicate isSanitizer(DataFlow::Node n) { |
| 284 | + exists(MethodAccess ma | |
| 285 | + ma.getMethod().getName().toLowerCase().matches("%encrypt%") and |
| 286 | + n.asExpr() = ma.getAnArgument() |
| 287 | + ) |
| 288 | + } |
| 289 | +} |
| 290 | + |
| 291 | +/** The call to get a `SharedPreferences.Editor` object, which can set shared preferences or be stored to device. */ |
| 292 | +class SharedPreferencesEditor extends MethodAccess { |
| 293 | + SharedPreferencesEditor() { |
| 294 | + this.getMethod() instanceof SharedPreferencesGetEditorMethod and |
| 295 | + not exists( |
| 296 | + MethodAccess cma // not exists `SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(...)` |
| 297 | + | |
| 298 | + cma.getQualifier().getType() instanceof TypeEncryptedSharedPreferences and |
| 299 | + cma.getMethod().hasName("create") and |
| 300 | + cma.getParent().(VariableAssign).getDestVar().getAnAccess() = this.getQualifier() |
| 301 | + ) |
| 302 | + } |
| 303 | + |
| 304 | + /** Gets an input, for example `input` in `editor.putString("password", password);`. */ |
| 305 | + Expr getAnInput() { |
| 306 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node n | |
| 307 | + sharedPreferencesInput(n, result) and |
| 308 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 309 | + ) |
| 310 | + } |
| 311 | + |
| 312 | + /** Gets a store, for example `editor.commit();`. */ |
| 313 | + Expr getAStore() { |
| 314 | + exists(SharedPreferencesFlowConfig conf, DataFlow::Node n | |
| 315 | + sharedPreferencesStore(n, result) and |
| 316 | + conf.hasFlow(DataFlow::exprNode(this), n) |
| 317 | + ) |
| 318 | + } |
| 319 | +} |
0 commit comments