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

Commit ddc50fa7 authored by Julia Reynolds's avatar Julia Reynolds Committed by Evelyn Torres
Browse files

Revert "Check sound Uri permission when creating a notification channel"

This reverts commit 665e1aee2dbad19d436a08d753b4d5cf733abeb5.

Reason for revert: b/337775777
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:2d9506053e521542fc6b8127f44c9d76897904c1)
Merged-In: Ib569f25a616eb13439be177901fdb78fa02f539a
Change-Id: Ib569f25a616eb13439be177901fdb78fa02f539a
parent 3dcc4ea9
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -2240,7 +2240,6 @@ public class NotificationManagerService extends SystemService {
                mZenModeHelper,
                new NotificationChannelLoggerImpl(),
                mAppOps,
                mUgmInternal,
                new SysUiStatsEvent.BuilderFactory());
        mRankingHelper = new RankingHelper(getContext(),
                mRankingHandler,
@@ -5564,7 +5563,13 @@ public class NotificationManagerService extends SystemService {
            final Uri originalSoundUri =
                    (originalChannel != null) ? originalChannel.getSound() : null;
            if (soundUri != null && !Objects.equals(originalSoundUri, soundUri)) {
                PermissionHelper.grantUriPermission(mUgmInternal, soundUri, sourceUid);
                Binder.withCleanCallingIdentity(() -> {
                    mUgmInternal.checkGrantUriPermission(sourceUid, null,
                            ContentProvider.getUriWithoutUserId(soundUri),
                            Intent.FLAG_GRANT_READ_URI_PERMISSION,
                            ContentProvider.getUserIdFromUri(soundUri,
                            UserHandle.getUserId(sourceUid)));
                });
            }
        }

+9 −4
Original line number Diff line number Diff line
@@ -1342,16 +1342,19 @@ public final class NotificationRecord {
     * {@link SecurityException} depending on target SDK of enqueuing app.
     */
    private void visitGrantableUri(Uri uri, boolean userOverriddenUri, boolean isSound) {
        if (mGrantableUris != null && mGrantableUris.contains(uri)) {
            return; // already verified this URI
        }
        if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;

        // We can't grant Uri permissions from system
        final int sourceUid = getSbn().getUid();
        if (sourceUid == android.os.Process.SYSTEM_UID) return;

        final long ident = Binder.clearCallingIdentity();
        try {
            PermissionHelper.grantUriPermission(mUgmInternal, uri, sourceUid);
            // This will throw SecurityException if caller can't grant
            mUgmInternal.checkGrantUriPermission(sourceUid, null,
                    ContentProvider.getUriWithoutUserId(uri),
                    Intent.FLAG_GRANT_READ_URI_PERMISSION,
                    ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));

            if (mGrantableUris == null) {
                mGrantableUris = new ArraySet<>();
@@ -1371,6 +1374,8 @@ public final class NotificationRecord {
                    }
                }
            }
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
    }

+0 −46
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.notification;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.UserHandle;

import com.android.server.uri.UriGrantsManagerInternal;

/**
 * NotificationManagerService helper for querying/setting the app-level notification permission
 */
public final class PermissionHelper {
    private static final String TAG = "PermissionHelper";

    static void grantUriPermission(final UriGrantsManagerInternal ugmInternal, Uri uri,
            int sourceUid) {
        if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;

        Binder.withCleanCallingIdentity(() -> {
            // This will throw a SecurityException if the caller can't grant.
            ugmInternal.checkGrantUriPermission(sourceUid, null,
                    ContentProvider.getUriWithoutUserId(uri),
                    Intent.FLAG_GRANT_READ_URI_PERMISSION,
                    ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
        });
    }
}
+0 −10
Original line number Diff line number Diff line
@@ -68,7 +68,6 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.util.Preconditions;
import com.android.internal.util.XmlUtils;
import com.android.server.uri.UriGrantsManagerInternal;

import org.json.JSONArray;
import org.json.JSONException;
@@ -177,7 +176,6 @@ public class PreferencesHelper implements RankingConfig {
    private final ZenModeHelper mZenModeHelper;
    private final NotificationChannelLogger mNotificationChannelLogger;
    private final AppOpsManager mAppOps;
    private final UriGrantsManagerInternal mUgmInternal;

    private SparseBooleanArray mBadgingEnabled;
    private SparseBooleanArray mBubblesEnabled;
@@ -196,7 +194,6 @@ public class PreferencesHelper implements RankingConfig {
    public PreferencesHelper(Context context, PackageManager pm, RankingHandler rankingHandler,
            ZenModeHelper zenHelper, NotificationChannelLogger notificationChannelLogger,
            AppOpsManager appOpsManager,
            UriGrantsManagerInternal ugmInternal,
            SysUiStatsEvent.BuilderFactory statsEventBuilderFactory) {
        mContext = context;
        mZenModeHelper = zenHelper;
@@ -205,7 +202,6 @@ public class PreferencesHelper implements RankingConfig {
        mNotificationChannelLogger = notificationChannelLogger;
        mAppOps = appOpsManager;
        mStatsEventBuilderFactory = statsEventBuilderFactory;
        mUgmInternal = ugmInternal;

        updateBadgingEnabled();
        updateBubblesEnabled();
@@ -967,12 +963,6 @@ public class PreferencesHelper implements RankingConfig {
                        : NotificationChannel.DEFAULT_ALLOW_BUBBLE);
            }
            clearLockedFieldsLocked(channel);

            // Verify that the app has permission to read the sound Uri
            // Only check for new channels, as regular apps can only set sound
            // before creating. See: {@link NotificationChannel#setSound}
            PermissionHelper.grantUriPermission(mUgmInternal, channel.getSound(), uid);

            channel.setImportanceLockedByOEM(r.oemLockedImportance);
            if (!channel.isImportanceLockedByOEM()) {
                if (r.oemLockedChannels.contains(channel.getId())) {
+1 −36
Original line number Diff line number Diff line
@@ -2694,41 +2694,6 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                .checkGrantUriPermission(eq(Process.myUid()), any(), eq(soundUri),
                    anyInt(), eq(Process.myUserHandle().getIdentifier()));

        mBinderService.updateNotificationChannelFromPrivilegedListener(
                null, mPkg, Process.myUserHandle(), updatedNotificationChannel);

        verify(mPreferencesHelper, times(1)).updateNotificationChannel(
                anyString(), anyInt(), any(), anyBoolean());

        verify(mListeners, never()).notifyNotificationChannelChanged(eq(mPkg),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
                eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
    }

    @Test
    public void
        testUpdateNotificationChannelFromPrivilegedListener_oldSoundNoUriPerm_newSoundHasUriPerm()
            throws Exception {
        mService.setPreferencesHelper(mPreferencesHelper);
        when(mCompanionMgr.getAssociations(mPkg, mUserId))
                .thenReturn(singletonList(mock(AssociationInfo.class)));
        when(mPreferencesHelper.getNotificationChannel(eq(mPkg), anyInt(),
                eq(mTestNotificationChannel.getId()), anyBoolean()))
                .thenReturn(mTestNotificationChannel);

        // Missing Uri permissions for the old channel sound
        final Uri oldSoundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
        doThrow(new SecurityException("no access")).when(mUgmInternal)
                .checkGrantUriPermission(eq(Process.myUid()), any(), eq(oldSoundUri),
                anyInt(), eq(Process.myUserHandle().getIdentifier()));

        // Has Uri permissions for the old channel sound
        final Uri newSoundUri = Uri.parse("content://media/test/sound/uri");
        final NotificationChannel updatedNotificationChannel = new NotificationChannel(
                TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT);
        updatedNotificationChannel.setSound(newSoundUri,
                updatedNotificationChannel.getAudioAttributes());

        mBinderService.updateNotificationChannelFromPrivilegedListener(
                null, PKG, Process.myUserHandle(), updatedNotificationChannel);

Loading