Skip to content

Commit 3ee14f9

Browse files
authored
Merge pull request #50 from egregius313/egregius313/refactor-apk-query-using-dataflow-modules
Convert dataflow configurations in Arbitrary APK Installation query to use new module-configuration
2 parents eeb9a88 + e8f1f36 commit 3ee14f9

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,45 @@
22

33
import java
44
import semmle.code.java.dataflow.DataFlow
5-
import semmle.code.java.dataflow.TaintTracking2
6-
import semmle.code.java.dataflow.TaintTracking3
5+
import semmle.code.java.dataflow.TaintTracking
76
private import semmle.code.java.security.ArbitraryApkInstallation
87

98
/**
109
* A dataflow configuration for flow from an external source of an APK to the
1110
* `setData[AndType][AndNormalize]` method of an intent.
1211
*/
13-
class ApkConfiguration extends DataFlow::Configuration {
14-
ApkConfiguration() { this = "ApkConfiguration" }
12+
private module ApkConf implements DataFlow::ConfigSig {
13+
predicate isSource(DataFlow::Node node) { node instanceof ExternalApkSource }
1514

16-
override predicate isSource(DataFlow::Node node) { node instanceof ExternalApkSource }
17-
18-
override predicate isSink(DataFlow::Node node) {
15+
predicate isSink(DataFlow::Node node) {
1916
exists(MethodAccess ma |
2017
ma.getMethod() instanceof SetDataMethod and
2118
ma.getArgument(0) = node.asExpr() and
2219
(
23-
any(PackageArchiveMimeTypeConfiguration c).hasFlowToExpr(ma.getQualifier())
20+
PackageArchiveMimeTypeConfiguration::hasFlowToExpr(ma.getQualifier())
2421
or
25-
any(InstallPackageActionConfiguration c).hasFlowToExpr(ma.getQualifier())
22+
InstallPackageActionConfiguration::hasFlowToExpr(ma.getQualifier())
2623
)
2724
)
2825
}
2926
}
3027

28+
module ApkConfiguration = DataFlow::Make<ApkConf>;
29+
3130
/**
3231
* A dataflow configuration tracking the flow from the `android.content.Intent.ACTION_INSTALL_PACKAGE`
3332
* constant to either the constructor of an intent or the `setAction` method of an intent.
3433
*
3534
* This is used to track if an intent is used to install an APK.
3635
*/
37-
private class InstallPackageActionConfiguration extends TaintTracking3::Configuration {
38-
InstallPackageActionConfiguration() { this = "InstallPackageActionConfiguration" }
36+
private module InstallPackageActionConfig implements DataFlow::StateConfigSig {
37+
class FlowState = string;
3938

40-
override predicate isSource(DataFlow::Node source) {
41-
source.asExpr() instanceof InstallPackageAction
39+
predicate isSource(DataFlow::Node source, FlowState state) {
40+
source.asExpr() instanceof InstallPackageAction and state instanceof DataFlow::FlowStateEmpty
4241
}
4342

44-
override predicate isAdditionalTaintStep(
43+
predicate isAdditionalFlowStep(
4544
DataFlow::Node node1, DataFlow::FlowState state1, DataFlow::Node node2,
4645
DataFlow::FlowState state2
4746
) {
@@ -63,24 +62,30 @@ private class InstallPackageActionConfiguration extends TaintTracking3::Configur
6362
)
6463
}
6564

66-
override predicate isSink(DataFlow::Node node, DataFlow::FlowState state) {
65+
predicate isSink(DataFlow::Node node, DataFlow::FlowState state) {
6766
state = "hasPackageInstallAction" and node.asExpr().getType() instanceof TypeIntent
6867
}
68+
69+
predicate isBarrier(DataFlow::Node node, FlowState state) { none() }
6970
}
7071

72+
private module InstallPackageActionConfiguration =
73+
TaintTracking::MakeWithState<InstallPackageActionConfig>;
74+
7175
/**
7276
* A dataflow configuration tracking the flow of the Android APK MIME type to
7377
* the `setType` or `setTypeAndNormalize` method of an intent, followed by a call
7478
* to `setData[AndType][AndNormalize]`.
7579
*/
76-
private class PackageArchiveMimeTypeConfiguration extends TaintTracking2::Configuration {
77-
PackageArchiveMimeTypeConfiguration() { this = "PackageArchiveMimeTypeConfiguration" }
80+
private module PackageArchiveMimeTypeConfig implements DataFlow::StateConfigSig {
81+
class FlowState = string;
7882

79-
override predicate isSource(DataFlow::Node node) {
80-
node.asExpr() instanceof PackageArchiveMimeTypeLiteral
83+
predicate isSource(DataFlow::Node node, FlowState state) {
84+
node.asExpr() instanceof PackageArchiveMimeTypeLiteral and
85+
state instanceof DataFlow::FlowStateEmpty
8186
}
8287

83-
override predicate isAdditionalTaintStep(
88+
predicate isAdditionalFlowStep(
8489
DataFlow::Node node1, DataFlow::FlowState state1, DataFlow::Node node2,
8590
DataFlow::FlowState state2
8691
) {
@@ -98,8 +103,13 @@ private class PackageArchiveMimeTypeConfiguration extends TaintTracking2::Config
98103
)
99104
}
100105

101-
override predicate isSink(DataFlow::Node node, DataFlow::FlowState state) {
106+
predicate isSink(DataFlow::Node node, DataFlow::FlowState state) {
102107
state = "typeSet" and
103108
node instanceof SetDataSink
104109
}
110+
111+
predicate isBarrier(DataFlow::Node node, FlowState state) { none() }
105112
}
113+
114+
private module PackageArchiveMimeTypeConfiguration =
115+
TaintTracking::MakeWithState<PackageArchiveMimeTypeConfig>;

java/ql/src/Security/CWE/CWE-094/ArbitraryApkInstallation.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import java
1414
import semmle.code.java.security.ArbitraryApkInstallationQuery
15-
import DataFlow::PathGraph
15+
import ApkConfiguration::PathGraph
1616

17-
from DataFlow::PathNode source, DataFlow::PathNode sink, ApkConfiguration config
18-
where config.hasFlowPath(source, sink)
17+
from ApkConfiguration::PathNode source, ApkConfiguration::PathNode sink
18+
where ApkConfiguration::hasFlowPath(source, sink)
1919
select sink.getNode(), source, sink, "Arbitrary Android APK installation."

java/ql/test/query-tests/security/CWE-094/ApkInstallationTest.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class HasApkInstallationTest extends InlineExpectationsTest {
1010

1111
override predicate hasActualResult(Location location, string element, string tag, string value) {
1212
tag = "hasApkInstallation" and
13-
exists(DataFlow::Node sink, ApkConfiguration conf | conf.hasFlowTo(sink) |
13+
exists(DataFlow::Node sink | ApkConfiguration::hasFlowTo(sink) |
1414
sink.getLocation() = location and
1515
element = sink.toString() and
1616
value = ""

0 commit comments

Comments
 (0)