-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Promote Cleartext storage of sensitive information using SharedPreferences from experimental #6468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
atorralba
merged 7 commits into
github:main
from
atorralba:atorralba/promote-cleartext-sharedprefs
Jan 11, 2022
Merged
Java: Promote Cleartext storage of sensitive information using SharedPreferences from experimental #6468
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55dc783
Move from experimental and refactor
atorralba ec8c234
Fix predicate name
atorralba d17e973
Apply suggestions from code review
atorralba e1e5e78
Apply suggestions from code review
atorralba cc92ce2
Fix QLDoc
atorralba 50caf7d
Move change note to new location and remove import
atorralba 394c4a9
Remove unused code
atorralba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
java/ql/lib/change-notes/2021-08-10-cleartext-storage-sharedprefs-query.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: newQuery | ||
| --- | ||
| * The query "Cleartext storage of sensitive information using `SharedPreferences` on Android" (`java/android/cleartext-storage-shared-prefs`) has been promoted from experimental to the main query pack. Its results will now appear by default. This query was originally [submitted as an experimental query by @luchua-bc](https://github.com/github/codeql/pull/4675). | ||
91 changes: 91 additions & 0 deletions
91
java/ql/lib/semmle/code/java/security/CleartextStorageSharedPrefsQuery.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /** Provides classes and predicates to reason about cleartext storage in Android's SharedPreferences. */ | ||
|
|
||
| import java | ||
| import semmle.code.java.dataflow.DataFlow | ||
| import semmle.code.java.dataflow.DataFlow4 | ||
|
atorralba marked this conversation as resolved.
Outdated
|
||
| import semmle.code.java.frameworks.android.SharedPreferences | ||
| import semmle.code.java.security.CleartextStorageQuery | ||
|
|
||
| private class SharedPrefsCleartextStorageSink extends CleartextStorageSink { | ||
| SharedPrefsCleartextStorageSink() { | ||
| exists(MethodAccess m | | ||
| m.getMethod() instanceof PutSharedPreferenceMethod and | ||
| this.asExpr() = m.getArgument(1) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The call to get a `SharedPreferences.Editor` object, which can set shared preferences and be | ||
| * stored to the device. | ||
| */ | ||
| class SharedPreferencesEditorMethodAccess extends Storable, MethodAccess { | ||
| SharedPreferencesEditorMethodAccess() { | ||
| this.getMethod() instanceof GetSharedPreferencesEditorMethod and | ||
| not DataFlow::localExprFlow(any(MethodAccess ma | | ||
| ma.getMethod() instanceof CreateEncryptedSharedPreferencesMethod | ||
| ), this.getQualifier()) | ||
| } | ||
|
|
||
| /** Gets an input, for example `password` in `editor.putString("password", password);`. */ | ||
| override Expr getAnInput() { | ||
| exists(SharedPreferencesFlowConfig conf, DataFlow::Node editor | | ||
| sharedPreferencesInput(editor, result) and | ||
| conf.hasFlow(DataFlow::exprNode(this), editor) | ||
| ) | ||
| } | ||
|
|
||
| /** Gets a store, for example `editor.commit();`. */ | ||
| override Expr getAStore() { | ||
| exists(SharedPreferencesFlowConfig conf, DataFlow::Node editor | | ||
| sharedPreferencesStore(editor, result) and | ||
| conf.hasFlow(DataFlow::exprNode(this), editor) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `input` is the second argument of a setter method | ||
| * called on `editor`, which is an instance of `SharedPreferences$Editor`. | ||
| */ | ||
| private predicate sharedPreferencesInput(DataFlow::Node editor, Expr input) { | ||
| exists(MethodAccess m | | ||
| m.getMethod() instanceof PutSharedPreferenceMethod and | ||
| input = m.getArgument(1) and | ||
| editor.asExpr() = m.getQualifier() | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `m` is a store method called on `editor`, | ||
| * which is an instance of `SharedPreferences$Editor`. | ||
| */ | ||
| private predicate sharedPreferencesStore(DataFlow::Node editor, MethodAccess m) { | ||
| m.getMethod() instanceof StoreSharedPreferenceMethod and | ||
| editor.asExpr() = m.getQualifier() | ||
| } | ||
|
|
||
| /** Flow from `SharedPreferences.Editor` to either a setter or a store method. */ | ||
| private class SharedPreferencesFlowConfig extends DataFlow::Configuration { | ||
| SharedPreferencesFlowConfig() { this = "SharedPreferencesFlowConfig" } | ||
|
|
||
| override predicate isSource(DataFlow::Node src) { | ||
| src.asExpr() instanceof SharedPreferencesEditorMethodAccess | ||
| } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { | ||
| sharedPreferencesInput(sink, _) or | ||
| sharedPreferencesStore(sink, _) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Method call for encrypting sensitive information. 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. | ||
| */ | ||
| private class EncryptedSensitiveMethodAccess extends MethodAccess { | ||
|
atorralba marked this conversation as resolved.
Outdated
|
||
| EncryptedSensitiveMethodAccess() { | ||
| this.getMethod().getName().toLowerCase().matches(["%encrypt%", "%hash%"]) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
java/ql/src/Security/CWE/CWE-312/CleartextStorageSharedPrefs.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * @name Cleartext storage of sensitive information using `SharedPreferences` on Android | ||
| * @description Cleartext Storage of Sensitive Information using | ||
| * SharedPreferences on Android allows access for users with root | ||
| * privileges or unexpected exposure from chained vulnerabilities. | ||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @precision medium | ||
| * @id java/android/cleartext-storage-shared-prefs | ||
| * @tags security | ||
| * external/cwe/cwe-312 | ||
| */ | ||
|
|
||
| import java | ||
| import semmle.code.java.security.CleartextStorageSharedPrefsQuery | ||
|
|
||
| from SensitiveSource data, SharedPreferencesEditorMethodAccess s, Expr input, Expr store | ||
| where | ||
| input = s.getAnInput() and | ||
| store = s.getAStore() and | ||
| data.flowsTo(input) | ||
| select store, "'SharedPreferences' class $@ containing $@ is stored $@. Data was added $@.", s, | ||
| s.toString(), data, "sensitive data", store, "here", input, "here" |
167 changes: 0 additions & 167 deletions
167
java/ql/src/experimental/Security/CWE/CWE-312/CleartextStorageSharedPrefs.ql
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
java/ql/test/experimental/query-tests/security/CWE-312/CleartextStorageSharedPrefs.expected
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.