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

Commit 51f4f16b 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:57bb0192753ac22c939986b7d19e8d0f09e60c16
Merged-In: I2be1537803a83cf44cc605f2850a3bb705b9bf32
Change-Id: I2be1537803a83cf44cc605f2850a3bb705b9bf32
parent 6dadba2c
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -4020,6 +4020,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)) {
@@ -4379,8 +4382,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 (isCallerIsSystemOrSystemUiOrShell()
                    || canNotifyAsPackage(callingPkg, targetPkg, userId)) {
                int targetUid = INVALID_UID;
                try {
                    targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId);
@@ -11130,7 +11133,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);
        }
    }
+19 −1
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;

@@ -49,6 +51,15 @@ public class UiServiceTestCase {
    public TestableContext mContext =
            spy(new TestableContext(InstrumentationRegistry.getContext(), null));

    protected final int mUid = Binder.getCallingUid();
    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;
    }
@@ -78,7 +89,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(
+160 −101

File changed.File mode changed from 100755 to 100644.

Preview size limit exceeded, changes collapsed.

+16 −14
Original line number Diff line number Diff line
@@ -1104,22 +1104,22 @@ public class PreferencesHelperTest extends UiServiceTestCase {

        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, false, USER_SYSTEM);
        String expected = "<ranking version=\"4\">\n"
                + "<package name=\"com.example.o\" show_badge=\"true\" "
       String expected_o = "<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=\"1111\">\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.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=\"2222\" />\n"
                + "<package name=\"com.example.n_mr1\" show_badge=\"true\" "
                + " user_demote_msg_app=\"true\" uid=\""+ UID_P + "\" />\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=\"0\">\n"
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"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\" "
@@ -1134,9 +1134,11 @@ 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"
                + "</ranking>";
        assertThat(baos.toString()).contains(expected);
                + "</package>";
        String actual = baos.toString();
        assertThat(actual).contains(expected_o);
        assertThat(actual).contains(expected_n);
        assertThat(actual).contains(expected_p);
    }

    @Test
@@ -1267,7 +1269,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);
@@ -3460,7 +3462,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        String actual = sw.toString();

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

    @Test