Skip to content

Commit b54e5b1

Browse files
committed
Revamp the library module
1 parent f13b881 commit b54e5b1

2 files changed

Lines changed: 49 additions & 60 deletions

File tree

java/ql/src/experimental/Security/CWE/CWE-312/CleartextStorageSharedPrefs.ql

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import semmle.code.java.security.SensitiveActions
1818
/** Holds if the method call is a setter method of `SharedPreferences`. */
1919
private predicate sharedPreferencesInput(DataFlow::Node sharedPrefs, Expr input) {
2020
exists(MethodAccess m |
21-
m.getMethod() instanceof SharedPreferences::SetPreferenceMethod and
21+
m.getMethod() instanceof PutSharedPreferenceMethod and
2222
input = m.getArgument(1) and
2323
not exists(EncryptedValueFlowConfig conf | conf.hasFlow(_, DataFlow::exprNode(input))) and
2424
sharedPrefs.asExpr() = m.getQualifier()
@@ -28,7 +28,7 @@ private predicate sharedPreferencesInput(DataFlow::Node sharedPrefs, Expr input)
2828
/** Holds if the method call is the store method of `SharedPreferences`. */
2929
private predicate sharedPreferencesStore(DataFlow::Node sharedPrefs, Expr store) {
3030
exists(MethodAccess m |
31-
m.getMethod() instanceof SharedPreferences::StorePreferenceMethod and
31+
m.getMethod() instanceof StoreSharedPreferenceMethod and
3232
store = m and
3333
sharedPrefs.asExpr() = m.getQualifier()
3434
)
@@ -41,7 +41,7 @@ class SharedPreferencesFlowConfig extends DataFlow::Configuration {
4141
}
4242

4343
override predicate isSource(DataFlow::Node src) {
44-
src.asExpr() instanceof SharedPreferencesEditor
44+
src.asExpr() instanceof SharedPreferencesEditorMethodAccess
4545
}
4646

4747
override predicate isSink(DataFlow::Node sink) {
@@ -56,7 +56,7 @@ class SharedPreferencesFlowConfig extends DataFlow::Configuration {
5656
*/
5757
class EncryptedSensitiveMethodAccess extends MethodAccess {
5858
EncryptedSensitiveMethodAccess() {
59-
getMethod().getName().toLowerCase().matches(["%encrypt%", "%hash%"])
59+
this.getMethod().getName().toLowerCase().matches(["%encrypt%", "%hash%"])
6060
}
6161
}
6262

@@ -70,7 +70,7 @@ class EncryptedValueFlowConfig extends DataFlow5::Configuration {
7070

7171
override predicate isSink(DataFlow5::Node sink) {
7272
exists(MethodAccess ma |
73-
ma.getMethod() instanceof SharedPreferences::SetPreferenceMethod and
73+
ma.getMethod() instanceof PutSharedPreferenceMethod and
7474
sink.asExpr() = ma.getArgument(1)
7575
)
7676
}
@@ -83,18 +83,18 @@ private class EncryptedSharedPrefFlowConfig extends DataFlow4::Configuration {
8383
}
8484

8585
override predicate isSource(DataFlow4::Node src) {
86-
src.asExpr().(MethodAccess).getMethod() instanceof SharedPreferences::CreateEncryptedMethod
86+
src.asExpr().(MethodAccess).getMethod() instanceof CreateEncryptedSharedPreferencesMethod
8787
}
8888

8989
override predicate isSink(DataFlow4::Node sink) {
90-
sink.asExpr().getType() instanceof SharedPreferences::TypeBase
90+
sink.asExpr().getType() instanceof SharedPreferences
9191
}
9292
}
9393

9494
/** The call to get a `SharedPreferences.Editor` object, which can set shared preferences or be stored to device. */
95-
class SharedPreferencesEditor extends MethodAccess {
96-
SharedPreferencesEditor() {
97-
this.getMethod() instanceof SharedPreferences::GetEditorMethod and
95+
class SharedPreferencesEditorMethodAccess extends MethodAccess {
96+
SharedPreferencesEditorMethodAccess() {
97+
this.getMethod() instanceof GetSharedPreferencesEditorMethod and
9898
not exists(
9999
EncryptedSharedPrefFlowConfig config // not exists `SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(...)`
100100
|
@@ -132,7 +132,7 @@ private class SensitiveSharedPrefsFlowConfig extends TaintTracking::Configuratio
132132

133133
override predicate isSink(DataFlow::Node sink) {
134134
exists(MethodAccess m |
135-
m.getMethod() instanceof SharedPreferences::SetPreferenceMethod and
135+
m.getMethod() instanceof PutSharedPreferenceMethod and
136136
sink.asExpr() = m.getArgument(1)
137137
)
138138
}
@@ -154,7 +154,7 @@ class SensitiveSharedPrefsSource extends Expr {
154154
}
155155
}
156156

157-
from SensitiveSharedPrefsSource data, SharedPreferencesEditor s, Expr input, Expr store
157+
from SensitiveSharedPrefsSource data, SharedPreferencesEditorMethodAccess s, Expr input, Expr store
158158
where
159159
input = s.getAnInput() and
160160
store = s.getAStore() and
Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,51 @@
1-
/** Provides classes related to `android.content.SharedPreferences`. */
2-
31
import java
42

5-
/** Definitions related to `android.content.SharedPreferences`. */
6-
module SharedPreferences {
7-
/** The interface `android.content.SharedPreferences` */
8-
class TypeBase extends Interface {
9-
TypeBase() { hasQualifiedName("android.content", "SharedPreferences") }
10-
}
11-
12-
/** The class `androidx.security.crypto.EncryptedSharedPreferences`, which implements `SharedPreferences` with encryption support. */
13-
class TypeEncrypted extends Class {
14-
TypeEncrypted() { hasQualifiedName("androidx.security.crypto", "EncryptedSharedPreferences") }
15-
}
3+
/** The interface `android.content.SharedPreferences`. */
4+
class SharedPreferences extends Interface {
5+
SharedPreferences() { this.hasQualifiedName("android.content", "SharedPreferences") }
6+
}
167

17-
/** The create method of `androidx.security.crypto.EncryptedSharedPreferences` */
18-
class CreateEncryptedMethod extends Method {
19-
CreateEncryptedMethod() {
20-
getDeclaringType() instanceof TypeEncrypted and
21-
hasName("create")
22-
}
8+
/** The class `androidx.security.crypto.EncryptedSharedPreferences`, which implements `SharedPreferences` with encryption support. */
9+
class EncryptedSharedPreferences extends Class {
10+
EncryptedSharedPreferences() {
11+
this.hasQualifiedName("androidx.security.crypto", "EncryptedSharedPreferences")
2312
}
13+
}
2414

25-
/** A getter method of `android.content.SharedPreferences`. */
26-
class GetPreferenceMethod extends Method {
27-
GetPreferenceMethod() {
28-
getDeclaringType() instanceof TypeBase and
29-
getName().matches("get%")
30-
}
15+
/** The `create` method of `androidx.security.crypto.EncryptedSharedPreferences`. */
16+
class CreateEncryptedSharedPreferencesMethod extends Method {
17+
CreateEncryptedSharedPreferencesMethod() {
18+
this.getDeclaringType() instanceof EncryptedSharedPreferences and
19+
this.hasName("create")
3120
}
21+
}
3222

33-
/** Returns `android.content.SharedPreferences.Editor` from the `edit` call of `android.content.SharedPreferences`. */
34-
class GetEditorMethod extends Method {
35-
GetEditorMethod() {
36-
getDeclaringType() instanceof TypeBase and
37-
hasName("edit") and
38-
getReturnType() instanceof TypeEditor
39-
}
23+
/** Returns `android.content.SharedPreferences.Editor` from the `edit` call of `android.content.SharedPreferences`. */
24+
class GetSharedPreferencesEditorMethod extends Method {
25+
GetSharedPreferencesEditorMethod() {
26+
this.getDeclaringType() instanceof SharedPreferences and
27+
this.hasName("edit") and
28+
this.getReturnType() instanceof SharedPreferencesEditor
4029
}
30+
}
4131

42-
/** Definitions related to `android.content.SharedPreferences.Editor`. */
43-
class TypeEditor extends Interface {
44-
TypeEditor() { hasQualifiedName("android.content", "SharedPreferences$Editor") }
45-
}
32+
/** The interface `android.content.SharedPreferences.Editor`. */
33+
class SharedPreferencesEditor extends Interface {
34+
SharedPreferencesEditor() { this.hasQualifiedName("android.content", "SharedPreferences$Editor") }
35+
}
4636

47-
/** A setter method for `android.content.SharedPreferences`. */
48-
class SetPreferenceMethod extends Method {
49-
SetPreferenceMethod() {
50-
getDeclaringType() instanceof TypeEditor and
51-
getName().matches("put%")
52-
}
37+
/** A method that updates a key-value pair in a `android.content.SharedPreferences` through a `SharedPreferences.Editor`. The value is not written until a `StorePreferenceMethod` is called. */
38+
class PutSharedPreferenceMethod extends Method {
39+
PutSharedPreferenceMethod() {
40+
this.getDeclaringType() instanceof SharedPreferencesEditor and
41+
this.getName().matches("put%")
5342
}
43+
}
5444

55-
/** A setter method for `android.content.SharedPreferences`. */
56-
class StorePreferenceMethod extends Method {
57-
StorePreferenceMethod() {
58-
getDeclaringType() instanceof TypeEditor and
59-
hasName(["commit", "apply"])
60-
}
45+
/** A method on `SharedPreferences.Editor` that writes the pending changes to the underlying `android.content.SharedPreferences`. */
46+
class StoreSharedPreferenceMethod extends Method {
47+
StoreSharedPreferenceMethod() {
48+
this.getDeclaringType() instanceof SharedPreferencesEditor and
49+
this.hasName(["commit", "apply"])
6150
}
6251
}

0 commit comments

Comments
 (0)