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

Commit 883d88ec authored by Achim Thesmann's avatar Achim Thesmann Committed by Android (Google) Code Review
Browse files

Merge changes from topic "ag28295029" into main

* changes:
  Use the new enum value to check if we grant BAL permission
  Update tests to check start mode
  Remove extra check for isPendingIntentBackgroundActivityLaunchAllowedByPermission
parents c06ea6e3 ff59d618
Loading
Loading
Loading
Loading
+0 −15
Original line number Diff line number Diff line
@@ -149,21 +149,6 @@ public class PendingIntentController {
                        ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED);
            }

            if (opts != null && opts.isPendingIntentBackgroundActivityLaunchAllowedByPermission()) {
                Slog.wtf(TAG,
                        "Resetting option pendingIntentBackgroundActivityLaunchAllowedByPermission"
                                + " which is set by the pending intent creator ("
                                + packageName
                                + ") because this option is meant for the pending intent sender");
                if (CompatChanges.isChangeEnabled(PendingIntent.PENDING_INTENT_OPTIONS_CHECK,
                        callingUid)) {
                    throw new IllegalArgumentException(
                            "pendingIntentBackgroundActivityLaunchAllowedByPermission "
                                    + "can not be set by creator of a PendingIntent");
                }
                opts.setPendingIntentBackgroundActivityLaunchAllowedByPermission(false);
            }

            final boolean noCreate = (flags & PendingIntent.FLAG_NO_CREATE) != 0;
            final boolean cancelCurrent = (flags & PendingIntent.FLAG_CANCEL_CURRENT) != 0;
            final boolean updateCurrent = (flags & PendingIntent.FLAG_UPDATE_CURRENT) != 0;
+23 −5
Original line number Diff line number Diff line
@@ -20,9 +20,11 @@ import static android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND;
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static android.app.ActivityOptions.BackgroundActivityStartMode;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_COMPAT;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
@@ -226,6 +228,21 @@ public class BackgroundActivityStartController {
        };
    }

    static String balStartModeToString(@BackgroundActivityStartMode int startMode) {
        return switch (startMode) {
            case MODE_BACKGROUND_ACTIVITY_START_ALLOWED -> "MODE_BACKGROUND_ACTIVITY_START_ALLOWED";
            case MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED ->
                    "MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED";
            case MODE_BACKGROUND_ACTIVITY_START_COMPAT -> "MODE_BACKGROUND_ACTIVITY_START_COMPAT";
            case MODE_BACKGROUND_ACTIVITY_START_DENIED -> "MODE_BACKGROUND_ACTIVITY_START_DENIED";
            case MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS ->
                    "MODE_BACKGROUND_ACTIVITY_START_ALWAYS";
            case MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE ->
                    "MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE";
            default -> "MODE_BACKGROUND_ACTIVITY_START_ALLOWED(" + startMode + ")";
        };
    }

    @GuardedBy("mService.mGlobalLock")
    private final HashMap<Integer, FinishedActivityEntry> mTaskIdToFinishedActivity =
            new HashMap<>();
@@ -464,10 +481,6 @@ public class BackgroundActivityStartController {
            this.mResultForRealCaller = resultForRealCaller;
        }

        public boolean isPendingIntentBalAllowedByPermission() {
            return PendingIntentRecord.isPendingIntentBalAllowedByPermission(mCheckedOptions);
        }

        public boolean callerExplicitOptInOrAutoOptIn() {
            if (mAutoOptInCaller) {
                return !callerExplicitOptOut();
@@ -528,6 +541,8 @@ public class BackgroundActivityStartController {
            sb.append("; balAllowedByPiCreatorWithHardening: ")
                    .append(mBalAllowedByPiCreatorWithHardening);
            sb.append("; resultIfPiCreatorAllowsBal: ").append(mResultForCaller);
            sb.append("; callerStartMode: ").append(balStartModeToString(
                    mCheckedOptions.getPendingIntentBackgroundActivityStartMode()));
            sb.append("; hasRealCaller: ").append(hasRealCaller());
            sb.append("; isCallForResult: ").append(mIsCallForResult);
            sb.append("; isPendingIntent: ").append(isPendingIntent());
@@ -553,6 +568,8 @@ public class BackgroundActivityStartController {
                }
                sb.append("; balAllowedByPiSender: ").append(mBalAllowedByPiSender);
                sb.append("; resultIfPiSenderAllowsBal: ").append(mResultForRealCaller);
                sb.append("; realCallerStartMode: ").append(balStartModeToString(
                        mCheckedOptions.getPendingIntentCreatorBackgroundActivityStartMode()));
            }
            // features
            sb.append("; balImproveRealCallerVisibilityCheck: ")
@@ -949,7 +966,8 @@ public class BackgroundActivityStartController {
            }
        }

        if (state.isPendingIntentBalAllowedByPermission()
        if (state.mCheckedOptions.getPendingIntentBackgroundActivityStartMode()
                == MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS
                && hasBalPermission(state.mRealCallingUid, state.mRealCallingPid)) {
            return new BalVerdict(BAL_ALLOW_PERMISSION,
                    /*background*/ false,
+8 −10
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.server.wm;

import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS;

import static com.android.server.wm.ActivityTaskManagerService.APP_SWITCH_ALLOW;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_ALLOWLISTED_COMPONENT;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_FOREGROUND;
@@ -23,7 +26,6 @@ import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_SAW_PERMISSION;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_VISIBLE_WINDOW;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.ArgumentMatchers.anyInt;
@@ -58,7 +60,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.quality.Strictness;

import java.lang.reflect.Field;
@@ -134,9 +135,8 @@ public class BackgroundActivityStartControllerExemptionTests {

    ActivityOptions mCheckedOptions = ActivityOptions.makeBasic()
            .setPendingIntentCreatorBackgroundActivityStartMode(
                    ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
            .setPendingIntentBackgroundActivityStartMode(
                    ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
                    MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
            .setPendingIntentBackgroundActivityStartMode(MODE_BACKGROUND_ACTIVITY_START_ALLOWED);

    class TestableBackgroundActivityStartController extends BackgroundActivityStartController {
        private Set<Pair<Integer, Integer>> mBalPermissionUidPidPairs = new HashSet<>();
@@ -175,7 +175,6 @@ public class BackgroundActivityStartControllerExemptionTests {
        when(mService.getAppOpsManager()).thenReturn(mAppOpsManager);
        setViaReflection(mService, "mProcessMap", mProcessMap);

        //Mockito.when(mSupervisor.getBackgroundActivityLaunchController()).thenReturn(mController);
        setViaReflection(mSupervisor, "mRecentTasks", mRecentTasks);

        mController = new TestableBackgroundActivityStartController(mService, mSupervisor);
@@ -397,7 +396,7 @@ public class BackgroundActivityStartControllerExemptionTests {

        // setup state
        WindowProcessControllerMap mProcessMap = new WindowProcessControllerMap();
        WindowProcessController otherProcess = Mockito.mock(WindowProcessController.class);
        WindowProcessController otherProcess = mock(WindowProcessController.class);
        mProcessMap.put(callingPid, mCallerApp);
        mProcessMap.put(REGULAR_PID_1_1, otherProcess);
        setViaReflection(mService, "mProcessMap", mProcessMap);
@@ -516,14 +515,13 @@ public class BackgroundActivityStartControllerExemptionTests {
        BackgroundStartPrivileges forcedBalByPiSender = BackgroundStartPrivileges.NONE;
        Intent intent = TEST_INTENT;
        ActivityOptions checkedOptions = mCheckedOptions;
        checkedOptions.setPendingIntentBackgroundActivityLaunchAllowedByPermission(true);
        checkedOptions.setPendingIntentBackgroundActivityStartMode(
                MODE_BACKGROUND_ACTIVITY_START_ALLOW_ALWAYS);
        BackgroundActivityStartController.BalState balState = mController.new BalState(callingUid,
                callingPid, callingPackage, realCallingUid, realCallingPid, null,
                originatingPendingIntent, forcedBalByPiSender, mResultRecord, intent,
                checkedOptions);

        assertThat(balState.isPendingIntentBalAllowedByPermission()).isTrue();

        // call
        BalVerdict realCallerVerdict = mController.checkBackgroundActivityStartAllowedBySender(
                balState);
+4 −2
Original line number Diff line number Diff line
@@ -547,7 +547,7 @@ public class BackgroundActivityStartControllerTests {
        assertThat(balState.callerExplicitOptInOrOut()).isFalse();
        assertThat(balState.realCallerExplicitOptInOrAutoOptIn()).isTrue();
        assertThat(balState.realCallerExplicitOptInOrOut()).isFalse();
        assertThat(balState.toString()).contains(
        assertThat(balState.toString()).startsWith(
                "[callingPackage: package.app1; "
                        + "callingPackageTargetSdk: -1; "
                        + "callingUid: 10001; "
@@ -563,6 +563,7 @@ public class BackgroundActivityStartControllerTests {
                        + "balAllowedByPiCreator: BSP.ALLOW_BAL; "
                        + "balAllowedByPiCreatorWithHardening: BSP.ALLOW_BAL; "
                        + "resultIfPiCreatorAllowsBal: null; "
                        + "callerStartMode: MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; "
                        + "hasRealCaller: true; "
                        + "isCallForResult: false; "
                        + "isPendingIntent: false; "
@@ -646,7 +647,7 @@ public class BackgroundActivityStartControllerTests {
        assertThat(balState.callerExplicitOptInOrOut()).isFalse();
        assertThat(balState.realCallerExplicitOptInOrAutoOptIn()).isFalse();
        assertThat(balState.realCallerExplicitOptInOrOut()).isFalse();
        assertThat(balState.toString()).contains(
        assertThat(balState.toString()).startsWith(
                "[callingPackage: package.app1; "
                        + "callingPackageTargetSdk: -1; "
                        + "callingUid: 10001; "
@@ -662,6 +663,7 @@ public class BackgroundActivityStartControllerTests {
                        + "balAllowedByPiCreator: BSP.NONE; "
                        + "balAllowedByPiCreatorWithHardening: BSP.NONE; "
                        + "resultIfPiCreatorAllowsBal: null; "
                        + "callerStartMode: MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; "
                        + "hasRealCaller: true; "
                        + "isCallForResult: false; "
                        + "isPendingIntent: true; "