Skip to content

Commit 67bc051

Browse files
haggertkdarknius09
authored andcommitted
Merge tag 'android-security-11.0.0_r60' of https://android.googlesource.com/platform/packages/apps/Settings into staging/lineage-18.1_merge_android-security-11.0.0_r60
Android Security 11.0.0 Release 60 (9033583) * tag 'android-security-11.0.0_r60' of https://android.googlesource.com/platform/packages/apps/Settings: Validate config activities with their rule owners Change-Id: I33fd83fe859fa481d1017e3b34ac8e6b16098f34
1 parent 97f25f7 commit 67bc051

File tree

3 files changed

+207
-18
lines changed

3 files changed

+207
-18
lines changed

src/com/android/settings/notification/zen/AbstractZenModeAutomaticRulePreferenceController.java

+37-17
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import android.content.pm.ComponentInfo;
2727
import android.content.pm.PackageManager;
2828
import android.content.pm.ServiceInfo;
29+
import android.os.Binder;
2930
import android.provider.Settings;
3031
import android.service.notification.ConditionProviderService;
32+
import android.util.Log;
33+
import android.util.Slog;
3134

3235
import androidx.fragment.app.Fragment;
3336
import androidx.preference.Preference;
@@ -36,6 +39,7 @@
3639
import com.android.settingslib.core.lifecycle.Lifecycle;
3740

3841
import java.util.Map;
42+
import java.util.Objects;
3943

4044
abstract public class AbstractZenModeAutomaticRulePreferenceController extends
4145
AbstractZenModePreferenceController implements PreferenceControllerMixin {
@@ -92,7 +96,7 @@ public static ZenRuleInfo getRuleInfo(PackageManager pm, ComponentInfo ci) {
9296
? ci.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE)
9397
: ci.metaData.getString(NotificationManager.META_DATA_AUTOMATIC_RULE_TYPE);
9498

95-
final ComponentName configurationActivity = getSettingsActivity(null, ci);
99+
final ComponentName configurationActivity = getSettingsActivity(pm, null, ci);
96100
if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
97101
final ZenRuleInfo ri = new ZenRuleInfo();
98102
ri.serviceComponent =
@@ -110,28 +114,44 @@ public static ZenRuleInfo getRuleInfo(PackageManager pm, ComponentInfo ci) {
110114
return null;
111115
}
112116

113-
protected static ComponentName getSettingsActivity(AutomaticZenRule rule, ComponentInfo ci) {
117+
protected static ComponentName getSettingsActivity(PackageManager pm, AutomaticZenRule rule,
118+
ComponentInfo ci) {
119+
String owner = rule != null ? rule.getPackageName() : ci.packageName;
120+
ComponentName settingsActivity = null;
114121
// prefer config activity on the rule itself; fallback to manifest definition
115122
if (rule != null && rule.getConfigurationActivity() != null) {
116-
return rule.getConfigurationActivity();
117-
}
118-
if (ci == null) {
119-
return null;
123+
settingsActivity = rule.getConfigurationActivity();
124+
} else {
125+
if (ci == null) {
126+
settingsActivity = null;
127+
} else if (ci instanceof ActivityInfo) {
128+
// new activity backed rule
129+
settingsActivity = new ComponentName(ci.packageName, ci.name);
130+
} else if (ci.metaData != null) {
131+
// old service backed rule
132+
final String configurationActivity = ci.metaData.getString(
133+
ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY);
134+
if (configurationActivity != null) {
135+
settingsActivity = ComponentName.unflattenFromString(configurationActivity);
136+
}
137+
}
120138
}
121-
// new activity backed rule
122-
if (ci instanceof ActivityInfo) {
123-
return new ComponentName(ci.packageName, ci.name);
139+
if (settingsActivity == null || owner == null) {
140+
return settingsActivity;
124141
}
125-
// old service backed rule
126-
if (ci.metaData != null) {
127-
final String configurationActivity = ci.metaData.getString(
128-
ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY);
129-
if (configurationActivity != null) {
130-
return ComponentName.unflattenFromString(configurationActivity);
142+
try {
143+
int ownerUid = pm.getPackageUid(owner, 0);
144+
int configActivityOwnerUid = pm.getPackageUid(settingsActivity.getPackageName(), 0);
145+
if (ownerUid == configActivityOwnerUid) {
146+
return settingsActivity;
147+
} else {
148+
Log.w(TAG, "Config activity not in owner package for " + rule.getName());
149+
return null;
131150
}
151+
} catch (PackageManager.NameNotFoundException e) {
152+
Log.e(TAG, "Failed to find config activity");
153+
return null;
132154
}
133-
134-
return null;
135155
}
136156

137157
public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener {

src/com/android/settings/notification/zen/ZenRulePreference.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected void setAttributes(AutomaticZenRule rule) {
168168
: isEvent ? ZenModeEventRuleSettings.ACTION : "";
169169
ComponentInfo si = mServiceListing.findService(rule.getOwner());
170170
ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
171-
getSettingsActivity(rule, si);
171+
getSettingsActivity(mPm, rule, si);
172172
mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
173173
settingsActivity, mId);
174174
if (mIntent.resolveActivity(mPm) == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (C) 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.settings.notification.zen;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import static org.mockito.Mockito.when;
22+
23+
import android.app.AutomaticZenRule;
24+
import android.app.NotificationManager;
25+
import android.content.ComponentName;
26+
import android.content.Context;
27+
import android.content.pm.ComponentInfo;
28+
import android.content.pm.PackageManager;
29+
import android.net.Uri;
30+
import android.os.Bundle;
31+
import android.service.notification.ConditionProviderService;
32+
import android.service.notification.ZenPolicy;
33+
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.mockito.Mock;
38+
import org.mockito.MockitoAnnotations;
39+
import org.robolectric.RobolectricTestRunner;
40+
import org.robolectric.RuntimeEnvironment;
41+
42+
@RunWith(RobolectricTestRunner.class)
43+
public class AbstractZenModeAutomaticRulePreferenceControllerTest {
44+
45+
@Mock
46+
private PackageManager mPm;
47+
private Context mContext;
48+
49+
@Before
50+
public void setup() {
51+
MockitoAnnotations.initMocks(this);
52+
mContext = RuntimeEnvironment.application;
53+
}
54+
55+
@Test
56+
public void testGetSettingsActivity_configActivity() throws Exception {
57+
AutomaticZenRule rule = new AutomaticZenRule("name", null,
58+
new ComponentName(mContext.getPackageName(), "test"), Uri.EMPTY,
59+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
60+
rule.setPackageName(mContext.getPackageName());
61+
62+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
63+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
64+
65+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
66+
.getSettingsActivity(mPm, rule, null);
67+
68+
assertThat(actual).isEqualTo(new ComponentName(mContext.getPackageName(), "test"));
69+
}
70+
71+
@Test
72+
public void testGetSettingsActivity_configActivity_wrongPackage() throws Exception {
73+
AutomaticZenRule rule = new AutomaticZenRule("name", null,
74+
new ComponentName("another", "test"), Uri.EMPTY,
75+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
76+
rule.setPackageName(mContext.getPackageName());
77+
78+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
79+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
80+
81+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
82+
.getSettingsActivity(mPm, rule, null);
83+
84+
assertThat(actual).isNull();
85+
}
86+
87+
@Test
88+
public void testGetSettingsActivity_configActivity_unspecifiedOwner() throws Exception {
89+
AutomaticZenRule rule = new AutomaticZenRule("name", null,
90+
new ComponentName("another", "test"), Uri.EMPTY,
91+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
92+
93+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
94+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
95+
96+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
97+
.getSettingsActivity(mPm, rule, null);
98+
99+
assertThat(actual).isEqualTo(new ComponentName("another", "test"));
100+
}
101+
102+
@Test
103+
public void testGetSettingsActivity_cps() throws Exception {
104+
AutomaticZenRule rule = new AutomaticZenRule("name",
105+
new ComponentName(mContext.getPackageName(), "service"), null, Uri.EMPTY,
106+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
107+
rule.setPackageName(mContext.getPackageName());
108+
109+
ComponentInfo ci = new ComponentInfo();
110+
ci.packageName = mContext.getPackageName();
111+
ci.metaData = new Bundle();
112+
ci.metaData.putString(ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY,
113+
ComponentName.flattenToShortString(
114+
new ComponentName(mContext.getPackageName(), "activity")));
115+
116+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
117+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
118+
119+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
120+
.getSettingsActivity(mPm, rule, ci);
121+
122+
assertThat(actual).isEqualTo(new ComponentName(mContext.getPackageName(), "activity"));
123+
}
124+
125+
@Test
126+
public void testGetSettingsActivity_cps_wrongPackage() throws Exception {
127+
AutomaticZenRule rule = new AutomaticZenRule("name",
128+
new ComponentName(mContext.getPackageName(), "service"), null, Uri.EMPTY,
129+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
130+
rule.setPackageName("other");
131+
132+
ComponentInfo ci = new ComponentInfo();
133+
ci.packageName = mContext.getPackageName();
134+
ci.metaData = new Bundle();
135+
ci.metaData.putString(ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY,
136+
ComponentName.flattenToShortString(
137+
new ComponentName(mContext.getPackageName(), "activity")));
138+
139+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
140+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
141+
142+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
143+
.getSettingsActivity(mPm, rule, ci);
144+
145+
assertThat(actual).isNull();
146+
}
147+
148+
@Test
149+
public void testGetSettingsActivity_cps_unspecifiedPackage() throws Exception {
150+
AutomaticZenRule rule = new AutomaticZenRule("name",
151+
new ComponentName(mContext.getPackageName(), "service"), null, Uri.EMPTY,
152+
new ZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
153+
154+
ComponentInfo ci = new ComponentInfo();
155+
ci.packageName = mContext.getPackageName();
156+
ci.metaData = new Bundle();
157+
ci.metaData.putString(ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY,
158+
ComponentName.flattenToShortString(
159+
new ComponentName(mContext.getPackageName(), "activity")));
160+
161+
when(mPm.getPackageUid(null, 0)).thenReturn(-1);
162+
when(mPm.getPackageUid(mContext.getPackageName(), 0)).thenReturn(1);
163+
164+
ComponentName actual = AbstractZenModeAutomaticRulePreferenceController
165+
.getSettingsActivity(mPm, rule, ci);
166+
167+
assertThat(actual).isEqualTo(new ComponentName(mContext.getPackageName(), "activity"));
168+
}
169+
}

0 commit comments

Comments
 (0)