Skip to content

Commit d68bec9

Browse files
committed
Refactor CWE-940/AndroidIntentRedirection
1 parent 1e0c681 commit d68bec9

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import semmle.code.java.dataflow.TaintTracking3
88
import semmle.code.java.security.AndroidIntentRedirection
99

1010
/**
11+
* DEPRECATED: Use `IntentRedirectionFlow` instead.
12+
*
1113
* A taint tracking configuration for tainted Intents being used to start Android components.
1214
*/
13-
class IntentRedirectionConfiguration extends TaintTracking::Configuration {
15+
deprecated class IntentRedirectionConfiguration extends TaintTracking::Configuration {
1416
IntentRedirectionConfiguration() { this = "IntentRedirectionConfiguration" }
1517

1618
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
@@ -26,31 +28,44 @@ class IntentRedirectionConfiguration extends TaintTracking::Configuration {
2628
}
2729
}
2830

31+
private module IntentRedirectionConfig implements DataFlow::ConfigSig {
32+
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
33+
34+
predicate isSink(DataFlow::Node sink) { sink instanceof IntentRedirectionSink }
35+
36+
predicate isBarrier(DataFlow::Node sanitizer) { sanitizer instanceof IntentRedirectionSanitizer }
37+
38+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
39+
any(IntentRedirectionAdditionalTaintStep c).step(node1, node2)
40+
}
41+
}
42+
43+
/** A taint tracking configuration for tainted Intents being used to start Android components. */
44+
module IntentRedirectionFlow = TaintTracking::Make<IntentRedirectionConfig>;
45+
2946
/**
3047
* A sanitizer for sinks that receive the original incoming Intent,
3148
* since its component cannot be arbitrarily set.
3249
*/
3350
private class OriginalIntentSanitizer extends IntentRedirectionSanitizer {
34-
OriginalIntentSanitizer() { any(SameIntentBeingRelaunchedConfiguration c).hasFlowTo(this) }
51+
OriginalIntentSanitizer() { SameIntentBeingRelaunchedFlow::hasFlowTo(this) }
3552
}
3653

3754
/**
3855
* Data flow configuration used to discard incoming Intents
3956
* flowing directly to sinks that start Android components.
4057
*/
41-
private class SameIntentBeingRelaunchedConfiguration extends DataFlow2::Configuration {
42-
SameIntentBeingRelaunchedConfiguration() { this = "SameIntentBeingRelaunchedConfiguration" }
58+
private module SameIntentBeingRelaunchedConfig implements DataFlow::ConfigSig {
59+
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
4360

44-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
45-
46-
override predicate isSink(DataFlow::Node sink) { sink instanceof IntentRedirectionSink }
61+
predicate isSink(DataFlow::Node sink) { sink instanceof IntentRedirectionSink }
4762

48-
override predicate isBarrier(DataFlow::Node barrier) {
63+
predicate isBarrier(DataFlow::Node barrier) {
4964
// Don't discard the Intent if its original component is tainted
5065
barrier instanceof IntentWithTaintedComponent
5166
}
5267

53-
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
68+
predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
5469
// Intents being built with the copy constructor from the original Intent are discarded too
5570
exists(ClassInstanceExpr cie |
5671
cie.getConstructedType() instanceof TypeIntent and
@@ -61,29 +76,31 @@ private class SameIntentBeingRelaunchedConfiguration extends DataFlow2::Configur
6176
}
6277
}
6378

79+
private module SameIntentBeingRelaunchedFlow = DataFlow::Make<SameIntentBeingRelaunchedConfig>;
80+
6481
/** An `Intent` with a tainted component. */
6582
private class IntentWithTaintedComponent extends DataFlow::Node {
6683
IntentWithTaintedComponent() {
67-
exists(IntentSetComponent setExpr, TaintedIntentComponentConf conf |
84+
exists(IntentSetComponent setExpr |
6885
setExpr.getQualifier() = this.asExpr() and
69-
conf.hasFlowTo(DataFlow::exprNode(setExpr.getSink()))
86+
TaintedIntentComponentFlow::hasFlowTo(DataFlow::exprNode(setExpr.getSink()))
7087
)
7188
}
7289
}
7390

7491
/**
7592
* A taint tracking configuration for tainted data flowing to an `Intent`'s component.
7693
*/
77-
private class TaintedIntentComponentConf extends TaintTracking3::Configuration {
78-
TaintedIntentComponentConf() { this = "TaintedIntentComponentConf" }
94+
private module TaintedIntentComponentConfig implements DataFlow::ConfigSig {
95+
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
7996

80-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
81-
82-
override predicate isSink(DataFlow::Node sink) {
97+
predicate isSink(DataFlow::Node sink) {
8398
any(IntentSetComponent setComponent).getSink() = sink.asExpr()
8499
}
85100
}
86101

102+
private module TaintedIntentComponentFlow = TaintTracking::Make<TaintedIntentComponentConfig>;
103+
87104
/** A call to a method that changes the component of an `Intent`. */
88105
private class IntentSetComponent extends MethodAccess {
89106
int sinkArg;

java/ql/src/Security/CWE/CWE-940/AndroidIntentRedirection.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
import java
1717
import semmle.code.java.security.AndroidIntentRedirectionQuery
18-
import DataFlow::PathGraph
18+
import IntentRedirectionFlow::PathGraph
1919

20-
from DataFlow::PathNode source, DataFlow::PathNode sink, IntentRedirectionConfiguration conf
21-
where conf.hasFlowPath(source, sink)
20+
from IntentRedirectionFlow::PathNode source, IntentRedirectionFlow::PathNode sink
21+
where IntentRedirectionFlow::hasFlowPath(source, sink)
2222
select sink.getNode(), source, sink,
2323
"Arbitrary Android activities or services can be started from a $@.", source.getNode(),
2424
"user-provided value"

java/ql/test/query-tests/security/CWE-940/AndroidIntentRedirectionTest.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class HasAndroidIntentRedirectionTest extends InlineExpectationsTest {
99

1010
override predicate hasActualResult(Location location, string element, string tag, string value) {
1111
tag = "hasAndroidIntentRedirection" and
12-
exists(DataFlow::Node sink, IntentRedirectionConfiguration conf | conf.hasFlowTo(sink) |
12+
exists(DataFlow::Node sink | IntentRedirectionFlow::hasFlowTo(sink) |
1313
sink.getLocation() = location and
1414
element = sink.toString() and
1515
value = ""

0 commit comments

Comments
 (0)