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

Commit b88956f3 authored by Julia Reynolds's avatar Julia Reynolds Committed by Android Build Coastguard Worker
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
Merged-In: I2be1537803a83cf44cc605f2850a3bb705b9bf32
Change-Id: I2be1537803a83cf44cc605f2850a3bb705b9bf32
parent 09055276
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -4329,6 +4329,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)) {
@@ -4905,8 +4908,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);
@@ -6281,7 +6284,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 "
@@ -6571,7 +6574,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;
            }
@@ -9017,7 +9020,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)
@@ -11856,7 +11859,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);
        }
    }
+12 −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;

@@ -50,11 +52,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));
@@ -63,6 +60,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;
@@ -93,7 +94,12 @@ 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);
+194 −143

File changed.

Preview size limit exceeded, changes collapsed.

+50 −29
Original line number Diff line number Diff line
@@ -1227,19 +1227,21 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, false, USER_SYSTEM);
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                + "<package name=\"com.example.o\" show_badge=\"true\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n";
        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=\"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\" "
@@ -1254,11 +1256,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
@@ -1312,18 +1319,20 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n";
        String expected_o =
                // 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"
@@ -1341,15 +1350,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
@@ -1389,7 +1403,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);
@@ -1399,18 +1413,20 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        ByteArrayOutputStream baos = writeXmlAndPurge(
                PKG_N_MR1, UID_N_MR1, true, USER_SYSTEM);
        String expected = "<ranking version=\"4\" "
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\n"
                + "last_bubbles_version_upgrade=\"" + Build.VERSION.SDK_INT + "\">\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"
@@ -1428,12 +1444,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
@@ -4016,7 +4037,7 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        String dump = dumpToString(mHelper);

        assertThat(dump).contains(
                "AppSettings: com.example.p (10003)\n"
                "AppSettings: com.example.p (" + UID_P + ")\n"
                + "    Delegate: the.delegate.package (456) enabled=true");
    }