Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b3a6adc authored by Julia Reynolds's avatar Julia Reynolds Committed by Nishith Khanna
Browse files

Update isSameApp check

Only check uid, without a sandbox sdk exception

This small change involves a major test change since we previously
too permissive in our test setup

Test: NotificationManagerServiceTest
Test: HSUM NotificationManagerServiceTest
Test: SdkSandboxRestrictionsTest
Bug: 396667508
Fixes: 317957802
Flag: EXEMPT bug fix

(cherry picked from commit f5605ae2)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:9931811bc94d5db0d6c17b3e2d18cca2959506a4
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:3d72900800a1bf9667336e40f3f6bdf24f09f295
Merged-In: I2be1537803a83cf44cc605f2850a3bb705b9bf32
Change-Id: I2be1537803a83cf44cc605f2850a3bb705b9bf32
parent 2d1b7f61
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -4037,6 +4037,9 @@ public class NotificationManagerService extends SystemService {
         */
        @Override
        public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
            if (Process.isSdkSandboxUid(uid)) {
                return false;
            }
            enforceSystemOrSystemUIOrSamePackage(pkg,
                    "Caller not system or systemui or same package");
            if (UserHandle.getCallingUserId() != UserHandle.getUserId(uid)) {
@@ -4565,8 +4568,8 @@ public class NotificationManagerService extends SystemService {
        public NotificationChannel getConversationNotificationChannel(String callingPkg, int userId,
                String targetPkg, String channelId, boolean returnParentIfNoConversationChannel,
                String conversationId) {
            if (canNotifyAsPackage(callingPkg, targetPkg, userId)
                    || isCallerSystemOrSystemUiOrShell()) {
            if (isCallerSystemOrSystemUiOrShell()
                    || canNotifyAsPackage(callingPkg, targetPkg, userId)) {
                int targetUid = INVALID_UID;
                try {
                    targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId);
@@ -5951,7 +5954,7 @@ public class NotificationManagerService extends SystemService {
                    String wellbeingPackage = getContext().getResources().getString(
                            com.android.internal.R.string.config_systemWellbeing);
                    boolean isCallerWellbeing = !TextUtils.isEmpty(wellbeingPackage)
                            && mPackageManagerInternal.isSameApp(wellbeingPackage, uid, userId);
                            && isCallerSameApp(wellbeingPackage, uid, userId);
                    if (!isCallerWellbeing) {
                        throw new IllegalArgumentException(
                                "Only the 'Wellbeing' package can use AutomaticZenRules with "
@@ -6238,7 +6241,7 @@ public class NotificationManagerService extends SystemService {
                    ? mEffectsSuppressors.get(0)
                    : null;
            if (isCallerSystemOrSystemUiOrShell() || suppressor == null
                    || mPackageManagerInternal.isSameApp(suppressor.getPackageName(),
                    || isCallerSameApp(suppressor.getPackageName(),
                    Binder.getCallingUid(), UserHandle.getUserId(Binder.getCallingUid()))) {
                return suppressor;
            }
@@ -8613,7 +8616,7 @@ public class NotificationManagerService extends SystemService {
    int resolveNotificationUid(String callingPkg, String targetPkg, int callingUid, int userId)
            throws NameNotFoundException {
        if (userId == USER_ALL) {
            userId = USER_SYSTEM;
            userId = UserHandle.getUserId(callingUid);
        }
        // posted from app A on behalf of app A
        if (isCallerSameApp(targetPkg, callingUid, userId)
@@ -11454,7 +11457,7 @@ public class NotificationManagerService extends SystemService {
        if (uid == Process.ROOT_UID && ROOT_PKG.equals(pkg)) {
            return;
        }
        if (!mPackageManagerInternal.isSameApp(pkg, uid, userId)) {
        if (!UserHandle.isSameApp(uid, mPackageManagerInternal.getPackageUid(pkg, 0L, userId))) {
            throw new SecurityException("Package " + pkg + " is not owned by uid " + uid);
        }
    }
+14 −6
Original line number Diff line number Diff line
@@ -15,7 +15,9 @@ package com.android.server;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@@ -48,11 +50,6 @@ public class UiServiceTestCase {
    protected static final String PKG_P = "com.example.p";
    protected static final String PKG_R = "com.example.r";

    protected static final int UID_N_MR1 = 10001;
    protected static final int UID_O = 10002;
    protected static final int UID_P = 10003;
    protected static final int UID_R = 10004;

    @Rule
    public TestableContext mContext =
            spy(new TestableContext(InstrumentationRegistry.getContext(), null));
@@ -61,6 +58,10 @@ public class UiServiceTestCase {
    protected final @UserIdInt int mUserId = UserHandle.getUserId(mUid);
    protected final UserHandle mUser = UserHandle.of(mUserId);
    protected final String mPkg = mContext.getPackageName();
    protected final int UID_N_MR1 = UserHandle.getUid(mUserId, 1);
    protected final int UID_O = UserHandle.getUid(mUserId, 2);
    protected final int UID_P = UserHandle.getUid(mUserId, 3);
    protected final int UID_R = UserHandle.getUid(mUserId, 4);

    protected TestableContext getContext() {
        return mContext;
@@ -91,7 +92,14 @@ public class UiServiceTestCase {
                            return Build.VERSION_CODES.CUR_DEVELOPMENT;
                    }
                });

        when(mPmi.getPackageUid(eq(PKG_N_MR1), anyLong(), eq(mUserId))).thenReturn(UID_N_MR1);
        when(mPmi.getPackageUid(eq(PKG_O), anyLong(), eq(mUserId))).thenReturn(UID_O);
        when(mPmi.getPackageUid(eq(PKG_P), anyLong(), eq(mUserId))).thenReturn(UID_P);
        when(mPmi.getPackageUid(eq(PKG_R), anyLong(), eq(mUserId))).thenReturn(UID_R);
        when(mPmi.getPackageUid(eq(mContext.getPackageName()), anyLong(), eq(mUserId)))
                .thenReturn(mUid);
        LocalServices.removeServiceForTest(UserManagerInternal.class);
        LocalServices.addService(UserManagerInternal.class, mUmi);
        LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
        LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
        when(mUgmInternal.checkGrantUriPermission(
+191 −139

File changed.

Preview size limit exceeded, changes collapsed.

+45 −26
Original line number Diff line number Diff line
@@ -1218,19 +1218,20 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, false, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
        String expected_o = "<ranking version=\"4\">\n"
                + "<package name=\"com.example.o\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\" uid=\"10002\">\n"
                + "=\"false\" uid=\"" + UID_O + "\">\n"
                + "<channel id=\"id\" name=\"name\" importance=\"2\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" orig_imp=\"2\" />\n"
                + "</package>\n"
                + "<package name=\"com.example.n_mr1\" show_badge=\"true\" "
                + "</package>\n";
        String expected_n =
                "<package name=\"com.example.n_mr1\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\" uid=\"10001\">\n"
                + "=\"false\" uid=\"" + UID_N_MR1 + "\">\n"
                + "<channelGroup id=\"1\" name=\"bye\" blocked=\"false\" locked=\"0\" />\n"
                + "<channelGroup id=\"2\" name=\"hello\" blocked=\"false\" locked=\"0\" />\n"
                + "<channel id=\"id1\" name=\"name1\" importance=\"4\" show_badge=\"true\" "
@@ -1245,11 +1246,16 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                + "<channel id=\"miscellaneous\" name=\"Uncategorized\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
                + "</package>\n"
                + "<package name=\"com.example.p\" show_badge=\"true\" "
                + "</package>\n";
        String expected_p =
                "<package name=\"com.example.p\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"true\" sent_valid_msg=\"true\""
                + " user_demote_msg_app=\"true\" sent_valid_bubble=\"false\" uid=\"10003\"";
        assertThat(baos.toString()).contains(expected);
                + " user_demote_msg_app=\"true\" sent_valid_bubble=\"false\" uid=\""+ UID_P + "\" />";
        String actual = baos.toString();
        assertThat(actual).contains(expected);
        assertThat(actual).contains(expected_o);
        assertThat(actual).contains(expected_n);
        assertThat(actual).contains(expected_p);
    }

    @Test
@@ -1304,16 +1310,17 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
                // Importance 0 because off in permissionhelper
                + "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
                "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\">\n"
                + "<channel id=\"id\" name=\"name\" importance=\"2\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" orig_imp=\"2\" />\n"
                + "</package>\n"
                + "</package>\n";
        String expected_n =
                // Importance default because on in permission helper
                + "<package name=\"com.example.n_mr1\" importance=\"3\" show_badge=\"true\" "
                "<package name=\"com.example.n_mr1\" importance=\"3\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\">\n"
@@ -1331,15 +1338,20 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                + "<channel id=\"miscellaneous\" name=\"Uncategorized\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
                + "</package>\n"
                + "</package>\n";
        String expected_p =
                // Importance default because on in permission helper
                + "<package name=\"com.example.p\" importance=\"3\" show_badge=\"true\" "
                "<package name=\"com.example.p\" importance=\"3\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"true\" sent_valid_msg=\"true\""
                + " user_demote_msg_app=\"true\" sent_valid_bubble=\"false\"";
        assertThat(baos.toString()).contains(expected);
        String actual = baos.toString();
        assertThat(actual).contains(expected);
        assertThat(actual).contains(expected_o);
        assertThat(actual).contains(expected_n);
        assertThat(actual).contains(expected_p);
        // Packages that exist solely in permissionhelper
        assertThat(baos.toString()).contains("<package name=\"first\" importance=\"3\"");
        assertThat(baos.toString()).contains("<package name=\"third\" importance=\"0\"");
        assertThat(actual).contains("<package name=\"first\" importance=\"3\"");
        assertThat(actual).contains("<package name=\"third\" importance=\"0\"");
    }

    @Test
@@ -1379,7 +1391,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false,
                SYSTEM_UID, true);
        mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false,
                UID_N_MR1, false);
                UID_O, false);

        mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true);
        mHelper.setInvalidMessageSent(PKG_P, UID_P);
@@ -1390,16 +1402,18 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
                // Importance 0 because off in permissionhelper
                + "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
        String expected_o =
                "<package name=\"com.example.o\" importance=\"0\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\">\n"
                + "<channel id=\"id\" name=\"name\" importance=\"2\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" orig_imp=\"2\" />\n"
                + "</package>\n"
                // Importance 0 because missing from permission helper
                + "<package name=\"com.example.n_mr1\" importance=\"0\" show_badge=\"true\" "
                + "</package>\n";
        String expected_n =
                // Importance missing because missing from permission helper
                "<package name=\"com.example.n_mr1\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"false\" "
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"false\" sent_valid_bubble"
                + "=\"false\">\n"
@@ -1417,12 +1431,17 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                + "<channel id=\"miscellaneous\" name=\"Uncategorized\" "
                + "sound=\"content://settings/system/notification_sound\" usage=\"5\" "
                + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
                + "</package>\n"
                + "</package>\n";
        String expected_p =
                // Importance default because on in permission helper
                + "<package name=\"com.example.p\" importance=\"3\" show_badge=\"true\" "
                "<package name=\"com.example.p\" importance=\"3\" show_badge=\"true\" "
                + "app_user_locked_fields=\"0\" sent_invalid_msg=\"true\" sent_valid_msg=\"true\""
                + " user_demote_msg_app=\"true\" sent_valid_bubble=\"false\"";
        assertThat(baos.toString()).contains(expected);
        String actual = baos.toString();
        assertThat(actual).contains(expected);
        assertThat(actual).contains(expected_o);
        assertThat(actual).contains(expected_n);
        assertThat(actual).contains(expected_p);
    }

    @Test
@@ -3934,7 +3953,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        String actual = sw.toString();

        // nobody gets any importance
        assertFalse(actual.contains("importance="));
        assertThat(actual).doesNotContain("importance=");
    }

    @Test