Skip to content

Commit 616b12d

Browse files
authored
Merge pull request #8956 from atorralba/atorralba/intent-redirection-sanitizer-fix
Java: Fix Intent Redirection sanitizer
2 parents ae83190 + 12320aa commit 616b12d

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
Fixed a sanitizer of the query `java/android/intent-redirection`. Now, for an intent to be considered
5+
safe against intent redirection, both its package name and class name must be checked.

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,24 @@ private class DefaultIntentRedirectionSink extends IntentRedirectionSink {
6565
}
6666

6767
/**
68-
* A default sanitizer for nodes dominated by calls to `ComponentName.getPackageName`
69-
* or `ComponentName.getClassName`. These are used to check whether the origin or destination
68+
* A default sanitizer for `Intent` nodes dominated by calls to `ComponentName.getPackageName`
69+
* and `ComponentName.getClassName`. These are used to check whether the origin or destination
7070
* components are trusted.
7171
*/
7272
private class DefaultIntentRedirectionSanitizer extends IntentRedirectionSanitizer {
7373
DefaultIntentRedirectionSanitizer() {
74+
this.getType() instanceof TypeIntent and
7475
exists(MethodAccess ma, Method m, Guard g, boolean branch |
7576
ma.getMethod() = m and
7677
m.getDeclaringType() instanceof TypeComponentName and
77-
m.hasName(["getPackageName", "getClassName"]) and
78+
m.hasName("getPackageName") and
79+
g.isEquality(ma, _, branch) and
80+
g.controls(this.asExpr().getBasicBlock(), branch)
81+
) and
82+
exists(MethodAccess ma, Method m, Guard g, boolean branch |
83+
ma.getMethod() = m and
84+
m.getDeclaringType() instanceof TypeComponentName and
85+
m.hasName("getClassName") and
7886
g.isEquality(ma, _, branch) and
7987
g.controls(this.asExpr().getBasicBlock(), branch)
8088
)

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,23 @@ public void onCreate(Bundle savedInstanceState) {
4040
sendStickyOrderedBroadcastAsUser(intent, null, null, null, 0, null, null); // $ hasAndroidIntentRedirection
4141
// @formatter:on
4242

43+
// Sanitizing only the package or the class still allows redirecting
44+
// to non-exported activities in the same package
45+
// or activities with the same name in other packages, respectively.
4346
if (intent.getComponent().getPackageName().equals("something")) {
44-
startActivity(intent); // Safe - sanitized
47+
startActivity(intent); // $ hasAndroidIntentRedirection
4548
} else {
4649
startActivity(intent); // $ hasAndroidIntentRedirection
4750
}
4851
if (intent.getComponent().getClassName().equals("something")) {
49-
startActivity(intent); // Safe - sanitized
52+
startActivity(intent); // $ hasAndroidIntentRedirection
53+
} else {
54+
startActivity(intent); // $ hasAndroidIntentRedirection
55+
}
56+
57+
if (intent.getComponent().getPackageName().equals("something")
58+
&& intent.getComponent().getClassName().equals("something")) {
59+
startActivity(intent); // Safe
5060
} else {
5161
startActivity(intent); // $ hasAndroidIntentRedirection
5262
}
@@ -94,8 +104,7 @@ public void onCreate(Bundle savedInstanceState) {
94104
}
95105
{
96106
Intent fwdIntent = new Intent();
97-
ComponentName component =
98-
new ComponentName("", intent.getStringExtra("className"));
107+
ComponentName component = new ComponentName("", intent.getStringExtra("className"));
99108
fwdIntent.setComponent(component);
100109
startActivity(fwdIntent); // $ hasAndroidIntentRedirection
101110
}

0 commit comments

Comments
 (0)