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

Commit 53958e68 authored by Julia Reynolds's avatar Julia Reynolds Committed by mse1969
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 70437e0a
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -3626,6 +3626,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)) {
@@ -3965,8 +3968,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)
                    || isCallerIsSystemOrSysemUiOrShell()) {
            if (isCallerIsSystemOrSystemUiOrShell()
                    || canNotifyAsPackage(callingPkg, targetPkg, userId)) {
                int targetUid = INVALID_UID;
                try {
                    targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId);
@@ -9894,7 +9897,7 @@ public class NotificationManagerService extends SystemService {
                == PERMISSION_GRANTED;
    }
    private boolean isCallerIsSystemOrSysemUiOrShell() {
    private boolean isCallerIsSystemOrSystemUiOrShell() {
        int callingUid = Binder.getCallingUid();
        if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID) {
            return true;
@@ -10005,7 +10008,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.when;

import android.content.pm.PackageManagerInternal;
@@ -47,6 +49,15 @@ public class UiServiceTestCase {
    public final TestableContext mContext =
            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;
    }
@@ -76,7 +87,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(
+157 −104

File changed.File mode changed from 100755 to 100644.

Preview size limit exceeded, changes collapsed.

+15 −12
Original line number Diff line number Diff line
@@ -1064,20 +1064,21 @@ 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\" uid=\"1111\">\n"
                + "sent_valid_msg=\"false\" user_demote_msg_app=\"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\" 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\" 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\" "
@@ -1092,9 +1093,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
@@ -3056,7 +3059,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        String actual = sw.toString();

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

    @Test