Skip to content

Commit 95ff5ba

Browse files
authored
Merge pull request github#16297 from michaelnebel/java/improveapitelemetry
Java: Identify more APIs as supported in the telemetry queries.
2 parents 75615f2 + c07bf65 commit 95ff5ba

37 files changed

+329
-92
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/** Provides classes representing various flow sinks for data flow / taint tracking. */
2+
3+
private import semmle.code.java.dataflow.FlowSinks as FlowSinks
4+
5+
final class SinkNode = FlowSinks::ApiSinkNode;
6+
7+
/**
8+
* Module that adds all API like sinks to `SinkNode`, excluding sinks for cryptography based
9+
* queries, and queries where sinks are not succifiently defined (eg. using broad method name matching).
10+
*/
11+
private module AllApiSinks {
12+
private import semmle.code.java.security.AndroidSensitiveCommunicationQuery
13+
private import semmle.code.java.security.ArbitraryApkInstallation
14+
private import semmle.code.java.security.CleartextStorageAndroidDatabaseQuery
15+
private import semmle.code.java.security.CleartextStorageAndroidFilesystemQuery
16+
private import semmle.code.java.security.CleartextStorageCookieQuery
17+
private import semmle.code.java.security.CleartextStorageSharedPrefsQuery
18+
private import semmle.code.java.security.ExternallyControlledFormatStringQuery
19+
private import semmle.code.java.security.InsecureBasicAuth
20+
private import semmle.code.java.security.IntentUriPermissionManipulation
21+
private import semmle.code.java.security.InsecureLdapAuth
22+
private import semmle.code.java.security.InsecureTrustManager
23+
private import semmle.code.java.security.JndiInjection
24+
private import semmle.code.java.security.JWT
25+
private import semmle.code.java.security.OgnlInjection
26+
private import semmle.code.java.security.SensitiveResultReceiverQuery
27+
private import semmle.code.java.security.SensitiveUiQuery
28+
private import semmle.code.java.security.SpelInjection
29+
private import semmle.code.java.security.SpelInjectionQuery
30+
private import semmle.code.java.security.QueryInjection
31+
private import semmle.code.java.security.TempDirLocalInformationDisclosureQuery
32+
private import semmle.code.java.security.UnsafeAndroidAccess
33+
private import semmle.code.java.security.UnsafeContentUriResolution
34+
private import semmle.code.java.security.UnsafeDeserializationQuery
35+
private import semmle.code.java.security.UrlRedirect
36+
private import semmle.code.java.security.WebviewDebuggingEnabledQuery
37+
private import semmle.code.java.security.XPath
38+
private import semmle.code.java.security.XSS
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** Provides classes representing various flow sources for data flow / taint tracking. */
2+
3+
private import semmle.code.java.dataflow.FlowSources as FlowSources
4+
5+
final class SourceNode = FlowSources::ApiSourceNode;
6+
7+
/**
8+
* Module that adds all API like sources to `SourceNode`, excluding some sources for cryptography based
9+
* queries, and queries where sources are not succifiently defined (eg. using broad method name matching).
10+
*/
11+
private module AllApiSources {
12+
private import semmle.code.java.security.ArbitraryApkInstallation
13+
private import semmle.code.java.security.CleartextStorageAndroidDatabaseQuery
14+
private import semmle.code.java.security.CleartextStorageAndroidFilesystemQuery
15+
private import semmle.code.java.security.CleartextStorageCookieQuery
16+
private import semmle.code.java.security.CleartextStorageSharedPrefsQuery
17+
private import semmle.code.java.security.ImplicitPendingIntentsQuery
18+
private import semmle.code.java.security.ImproperIntentVerificationQuery
19+
private import semmle.code.java.security.InsecureTrustManager
20+
private import semmle.code.java.security.JWT
21+
private import semmle.code.java.security.StackTraceExposureQuery
22+
private import semmle.code.java.security.ZipSlipQuery
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** Provides classes representing various flow sinks for data flow / taint tracking. */
2+
3+
private import java
4+
private import semmle.code.java.dataflow.ExternalFlow
5+
private import semmle.code.java.dataflow.DataFlow
6+
7+
/**
8+
* A data flow sink node for an API, which should be considered
9+
* supported for a modeling perspective.
10+
*/
11+
abstract class ApiSinkNode extends DataFlow::Node { }
12+
13+
/**
14+
* Add all sink models as data sinks.
15+
*/
16+
private class ApiSinkNodeExternal extends ApiSinkNode {
17+
ApiSinkNodeExternal() { sinkNode(this, _) }
18+
}

java/ql/lib/semmle/code/java/dataflow/FlowSources.qll

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,17 @@ private class AndroidExternalStorageSource extends RemoteFlowSource {
194194
}
195195

196196
/** Class for `tainted` user input. */
197-
abstract class UserInput extends DataFlow::Node { }
197+
abstract class UserInput extends SourceNode { }
198198

199199
/**
200200
* Input that may be controlled by a remote user.
201201
*/
202-
private class RemoteUserInput extends UserInput instanceof RemoteFlowSource { }
202+
private class RemoteUserInput extends UserInput instanceof RemoteFlowSource {
203+
override string getThreatModel() { result = RemoteFlowSource.super.getThreatModel() }
204+
}
203205

204206
/** A node with input that may be controlled by a local user. */
205-
abstract class LocalUserInput extends UserInput, SourceNode {
207+
abstract class LocalUserInput extends UserInput {
206208
override string getThreatModel() { result = "local" }
207209
}
208210

@@ -385,3 +387,18 @@ class AndroidJavascriptInterfaceMethodParameter extends RemoteFlowSource {
385387
result = "Parameter of method with JavascriptInterface annotation"
386388
}
387389
}
390+
391+
/**
392+
* A data flow source node for an API, which should be considered
393+
* supported for a modeling perspective.
394+
*/
395+
abstract class ApiSourceNode extends DataFlow::Node { }
396+
397+
private class AddSourceNodes extends ApiSourceNode instanceof SourceNode { }
398+
399+
/**
400+
* Add all source models as data sources.
401+
*/
402+
private class ApiSourceNodeExternal extends ApiSourceNode {
403+
ApiSourceNodeExternal() { sourceNode(this, _) }
404+
}

java/ql/lib/semmle/code/java/security/AndroidSensitiveCommunicationQuery.qll

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java
44
import semmle.code.java.dataflow.TaintTracking
55
import semmle.code.java.frameworks.android.Intent
66
import semmle.code.java.security.SensitiveActions
7+
private import semmle.code.java.dataflow.FlowSinks
78

89
/**
910
* Gets regular expression for matching names of Android variables that indicate the value being held contains sensitive information.
@@ -151,17 +152,24 @@ deprecated class SensitiveCommunicationConfig extends TaintTracking::Configurati
151152
}
152153
}
153154

155+
/**
156+
* A sensitive communication sink node.
157+
*/
158+
private class SensitiveCommunicationSink extends ApiSinkNode {
159+
SensitiveCommunicationSink() {
160+
isSensitiveBroadcastSink(this)
161+
or
162+
isStartActivityOrServiceSink(this)
163+
}
164+
}
165+
154166
/**
155167
* Taint configuration tracking flow from variables containing sensitive information to broadcast Intents.
156168
*/
157169
module SensitiveCommunicationConfig implements DataFlow::ConfigSig {
158170
predicate isSource(DataFlow::Node source) { source.asExpr() instanceof SensitiveInfoExpr }
159171

160-
predicate isSink(DataFlow::Node sink) {
161-
isSensitiveBroadcastSink(sink)
162-
or
163-
isStartActivityOrServiceSink(sink)
164-
}
172+
predicate isSink(DataFlow::Node sink) { sink instanceof SensitiveCommunicationSink }
165173

166174
/**
167175
* Holds if broadcast doesn't specify receiving package name of the 3rd party app

java/ql/lib/semmle/code/java/security/ArbitraryApkInstallation.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java
44
import semmle.code.java.frameworks.android.Intent
55
import semmle.code.java.dataflow.DataFlow
66
private import semmle.code.java.dataflow.ExternalFlow
7+
private import semmle.code.java.dataflow.FlowSinks
78
private import semmle.code.java.dataflow.FlowSources
89

910
/** A string literal that represents the MIME type for Android APKs. */
@@ -48,7 +49,7 @@ class SetDataMethod extends Method {
4849
}
4950

5051
/** A dataflow sink for the URI of an intent. */
51-
class SetDataSink extends DataFlow::ExprNode {
52+
class SetDataSink extends ApiSinkNode, DataFlow::ExprNode {
5253
SetDataSink() {
5354
exists(MethodCall ma |
5455
this.getExpr() = ma.getQualifier() and
@@ -69,7 +70,7 @@ class UriConstructorMethod extends Method {
6970
* A dataflow source representing the URIs which an APK not controlled by the
7071
* application may come from. Including external storage and web URLs.
7172
*/
72-
class ExternalApkSource extends DataFlow::Node {
73+
class ExternalApkSource extends ApiSourceNode {
7374
ExternalApkSource() {
7475
sourceNode(this, "android-external-storage-dir") or
7576
this.asExpr().(MethodCall).getMethod() instanceof UriConstructorMethod or

java/ql/lib/semmle/code/java/security/CleartextStorageAndroidDatabaseQuery.qll

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import semmle.code.java.frameworks.android.ContentProviders
66
import semmle.code.java.frameworks.android.Intent
77
import semmle.code.java.frameworks.android.SQLite
88
import semmle.code.java.security.CleartextStorageQuery
9+
private import semmle.code.java.dataflow.FlowSinks
10+
private import semmle.code.java.dataflow.FlowSources
911

1012
private class LocalDatabaseCleartextStorageSink extends CleartextStorageSink {
1113
LocalDatabaseCleartextStorageSink() { localDatabaseInput(_, this.asExpr()) }
@@ -96,15 +98,24 @@ private predicate localDatabaseStore(DataFlow::Node database, MethodCall store)
9698
)
9799
}
98100

101+
/**
102+
* A local database open method call source node.
103+
*/
104+
private class LocalDatabaseOpenMethodCallSource extends ApiSourceNode {
105+
LocalDatabaseOpenMethodCallSource() { this.asExpr() instanceof LocalDatabaseOpenMethodCall }
106+
}
107+
108+
/**
109+
* A local database sink node.
110+
*/
111+
private class LocalDatabaseSink extends ApiSinkNode {
112+
LocalDatabaseSink() { localDatabaseInput(this, _) or localDatabaseStore(this, _) }
113+
}
114+
99115
private module LocalDatabaseFlowConfig implements DataFlow::ConfigSig {
100-
predicate isSource(DataFlow::Node source) {
101-
source.asExpr() instanceof LocalDatabaseOpenMethodCall
102-
}
116+
predicate isSource(DataFlow::Node source) { source instanceof LocalDatabaseOpenMethodCallSource }
103117

104-
predicate isSink(DataFlow::Node sink) {
105-
localDatabaseInput(sink, _) or
106-
localDatabaseStore(sink, _)
107-
}
118+
predicate isSink(DataFlow::Node sink) { sink instanceof LocalDatabaseSink }
108119

109120
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
110121
// Adds a step for tracking databases through field flow, that is, a database is opened and

java/ql/lib/semmle/code/java/security/CleartextStorageAndroidFilesystemQuery.qll

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import java
77
import semmle.code.java.dataflow.DataFlow
8-
private import semmle.code.java.dataflow.ExternalFlow
98
import semmle.code.java.security.CleartextStorageQuery
109
import semmle.code.xml.AndroidManifest
10+
private import semmle.code.java.dataflow.ExternalFlow
11+
private import semmle.code.java.dataflow.FlowSinks
12+
private import semmle.code.java.dataflow.FlowSources
1113

1214
private class AndroidFilesystemCleartextStorageSink extends CleartextStorageSink {
1315
AndroidFilesystemCleartextStorageSink() {
@@ -79,13 +81,27 @@ private class CloseFileMethod extends Method {
7981
}
8082
}
8183

82-
private module FilesystemFlowConfig implements DataFlow::ConfigSig {
83-
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof LocalFileOpenCall }
84+
/**
85+
* A local file open call source node.
86+
*/
87+
private class LocalFileOpenCallSource extends ApiSourceNode {
88+
LocalFileOpenCallSource() { this.asExpr() instanceof LocalFileOpenCall }
89+
}
8490

85-
predicate isSink(DataFlow::Node sink) {
86-
filesystemInput(sink, _) or
87-
closesFile(sink, _)
91+
/**
92+
* A local file sink node.
93+
*/
94+
private class LocalFileSink extends ApiSinkNode {
95+
LocalFileSink() {
96+
filesystemInput(this, _) or
97+
closesFile(this, _)
8898
}
99+
}
100+
101+
private module FilesystemFlowConfig implements DataFlow::ConfigSig {
102+
predicate isSource(DataFlow::Node src) { src instanceof LocalFileOpenCallSource }
103+
104+
predicate isSink(DataFlow::Node sink) { sink instanceof LocalFileSink }
89105

90106
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
91107
// Add nested Writer constructors as extra data flow steps

java/ql/lib/semmle/code/java/security/CleartextStorageCookieQuery.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import java
44
import semmle.code.java.dataflow.DataFlow
55
deprecated import semmle.code.java.dataflow.DataFlow3
66
import semmle.code.java.security.CleartextStorageQuery
7+
private import semmle.code.java.dataflow.FlowSinks
8+
private import semmle.code.java.dataflow.FlowSources
79

810
private class CookieCleartextStorageSink extends CleartextStorageSink {
911
CookieCleartextStorageSink() { this.asExpr() = cookieInput(_) }
@@ -37,10 +39,24 @@ private predicate cookieStore(DataFlow::Node cookie, Expr store) {
3739
)
3840
}
3941

42+
/**
43+
* A cookie source node.
44+
*/
45+
private class CookieSource extends ApiSourceNode {
46+
CookieSource() { this.asExpr() instanceof Cookie }
47+
}
48+
49+
/**
50+
* A cookie store sink node.
51+
*/
52+
private class CookieStoreSink extends ApiSinkNode {
53+
CookieStoreSink() { cookieStore(this, _) }
54+
}
55+
4056
private module CookieToStoreFlowConfig implements DataFlow::ConfigSig {
41-
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof Cookie }
57+
predicate isSource(DataFlow::Node src) { src instanceof CookieSource }
4258

43-
predicate isSink(DataFlow::Node sink) { cookieStore(sink, _) }
59+
predicate isSink(DataFlow::Node sink) { sink instanceof CookieStoreSink }
4460
}
4561

4662
private module CookieToStoreFlow = DataFlow::Global<CookieToStoreFlowConfig>;

java/ql/lib/semmle/code/java/security/CleartextStorageSharedPrefsQuery.qll

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import java
44
import semmle.code.java.dataflow.DataFlow
55
import semmle.code.java.frameworks.android.SharedPreferences
66
import semmle.code.java.security.CleartextStorageQuery
7+
private import semmle.code.java.dataflow.FlowSinks
8+
private import semmle.code.java.dataflow.FlowSources
79

810
private class SharedPrefsCleartextStorageSink extends CleartextStorageSink {
911
SharedPrefsCleartextStorageSink() {
@@ -67,16 +69,30 @@ private predicate sharedPreferencesStore(DataFlow::Node editor, MethodCall m) {
6769
editor.asExpr() = m.getQualifier().getUnderlyingExpr()
6870
}
6971

70-
/** Flow from `SharedPreferences.Editor` to either a setter or a store method. */
71-
private module SharedPreferencesFlowConfig implements DataFlow::ConfigSig {
72-
predicate isSource(DataFlow::Node src) {
73-
src.asExpr() instanceof SharedPreferencesEditorMethodCall
72+
/**
73+
* A shared preferences editor method call source node.
74+
*/
75+
private class SharedPreferencesEditorMethodCallSource extends ApiSourceNode {
76+
SharedPreferencesEditorMethodCallSource() {
77+
this.asExpr() instanceof SharedPreferencesEditorMethodCall
7478
}
79+
}
7580

76-
predicate isSink(DataFlow::Node sink) {
77-
sharedPreferencesInput(sink, _) or
78-
sharedPreferencesStore(sink, _)
81+
/**
82+
* A shared preferences sink node.
83+
*/
84+
private class SharedPreferencesSink extends ApiSinkNode {
85+
SharedPreferencesSink() {
86+
sharedPreferencesInput(this, _) or
87+
sharedPreferencesStore(this, _)
7988
}
8089
}
8190

91+
/** Flow from `SharedPreferences.Editor` to either a setter or a store method. */
92+
private module SharedPreferencesFlowConfig implements DataFlow::ConfigSig {
93+
predicate isSource(DataFlow::Node src) { src instanceof SharedPreferencesEditorMethodCallSource }
94+
95+
predicate isSink(DataFlow::Node sink) { sink instanceof SharedPreferencesSink }
96+
}
97+
8298
private module SharedPreferencesFlow = DataFlow::Global<SharedPreferencesFlowConfig>;

0 commit comments

Comments
 (0)