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

Commit cdcb6a16 authored by Achim Thesmann's avatar Achim Thesmann
Browse files

Only auto-opt in real caller

Sending for result should only opt-in the real caller (sender) of the
PendingIntent and not the creator.

Test: atest BackgroundActivityLaunchTest
Bug: 325504942
Change-Id: Idebbf687d609998b166b702435f78ea9fb629a30
parent 92e20b62
Loading
Loading
Loading
Loading
+35 −21
Original line number Diff line number Diff line
@@ -94,6 +94,11 @@ public class BackgroundActivityStartController {
    private static final long ASM_GRACEPERIOD_TIMEOUT_MS = TIMEOUT_MS;
    private static final int ASM_GRACEPERIOD_MAX_REPEATS = 5;
    private static final int NO_PROCESS_UID = -1;

    static final String AUTO_OPT_IN_NOT_PENDING_INTENT = "notPendingIntent";
    static final String AUTO_OPT_IN_CALL_FOR_RESULT = "callForResult";
    static final String AUTO_OPT_IN_SAME_UID = "sameUid";

    /** If enabled the creator will not allow BAL on its behalf by default. */
    @ChangeId
    @EnabledAfter(targetSdkVersion = UPSIDE_DOWN_CAKE)
@@ -249,6 +254,7 @@ public class BackgroundActivityStartController {
        private final boolean mIsCallForResult;
        private final ActivityOptions mCheckedOptions;
        private final String mAutoOptInReason;
        private final boolean mAutoOptInCaller;
        private BalVerdict mResultForCaller;
        private BalVerdict mResultForRealCaller;

@@ -280,26 +286,27 @@ public class BackgroundActivityStartController {
            if (!balImproveRealCallerVisibilityCheck()) {
                // without this fix the auto-opt ins below would violate CTS tests
                mAutoOptInReason = null;
            } else if (mIsCallForResult) {
                mAutoOptInReason = "callForResult";
                mAutoOptInCaller = false;
            } else if (originatingPendingIntent == null) {
                mAutoOptInReason = "notPendingIntent";
                mAutoOptInReason = AUTO_OPT_IN_NOT_PENDING_INTENT;
                mAutoOptInCaller = true;
            } else if (mIsCallForResult) {
                mAutoOptInReason = AUTO_OPT_IN_CALL_FOR_RESULT;
                mAutoOptInCaller = false;
            } else if (callingUid == realCallingUid && !balRequireOptInSameUid()) {
                mAutoOptInReason = "sameUid";
                mAutoOptInReason = AUTO_OPT_IN_SAME_UID;
                mAutoOptInCaller = false;
            } else {
                mAutoOptInReason = null;
                mAutoOptInCaller = false;
            }

            if (mAutoOptInReason != null) {
            if (mAutoOptInCaller) {
                // grant BAL privileges unless explicitly opted out
                mBalAllowedByPiCreatorWithHardening = mBalAllowedByPiCreator =
                        callerBackgroundActivityStartMode == MODE_BACKGROUND_ACTIVITY_START_DENIED
                                ? BackgroundStartPrivileges.NONE
                                : BackgroundStartPrivileges.ALLOW_BAL;
                mBalAllowedByPiSender = realCallerBackgroundActivityStartMode
                        == MODE_BACKGROUND_ACTIVITY_START_DENIED
                        ? BackgroundStartPrivileges.NONE
                        : BackgroundStartPrivileges.ALLOW_BAL;
            } else {
                // for PendingIntents we restrict BAL based on target_sdk
                mBalAllowedByPiCreatorWithHardening = getBackgroundStartPrivilegesAllowedByCreator(
@@ -312,10 +319,21 @@ public class BackgroundActivityStartController {
                mBalAllowedByPiCreator = balRequireOptInByPendingIntentCreator()
                        ? mBalAllowedByPiCreatorWithHardening
                        : mBalAllowedByPiCreatorWithoutHardening;
            }

            if (mAutoOptInReason != null) {
                // grant BAL privileges unless explicitly opted out
                mBalAllowedByPiSender = realCallerBackgroundActivityStartMode
                        == MODE_BACKGROUND_ACTIVITY_START_DENIED
                        ? BackgroundStartPrivileges.NONE
                        : BackgroundStartPrivileges.ALLOW_BAL;
            } else {
                // for PendingIntents we restrict BAL based on target_sdk
                mBalAllowedByPiSender =
                        PendingIntentRecord.getBackgroundStartPrivilegesAllowedByCaller(
                                checkedOptions, realCallingUid, mRealCallingPackage);
            }

            mAppSwitchState = mService.getBalAppSwitchesState();
            mCallingUidProcState = mService.mActiveUids.getUidState(callingUid);
            mIsCallingUidPersistentSystemProcess =
@@ -485,23 +503,19 @@ public class BackgroundActivityStartController {
        }

        public boolean callerExplicitOptInOrAutoOptIn() {
            if (mAutoOptInReason == null) {
            if (mAutoOptInCaller) {
                return !callerExplicitOptOut();
            }
            return mCheckedOptions.getPendingIntentCreatorBackgroundActivityStartMode()
                    == MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
            } else {
                return mCheckedOptions.getPendingIntentCreatorBackgroundActivityStartMode()
                        != MODE_BACKGROUND_ACTIVITY_START_DENIED;
            }
        }

        public boolean realCallerExplicitOptInOrAutoOptIn() {
            if (mAutoOptInReason == null) {
            if (mAutoOptInReason != null) {
                return !realCallerExplicitOptOut();
            }
            return mCheckedOptions.getPendingIntentBackgroundActivityStartMode()
                    == MODE_BACKGROUND_ACTIVITY_START_ALLOWED;
            } else {
                return mCheckedOptions.getPendingIntentBackgroundActivityStartMode()
                        != MODE_BACKGROUND_ACTIVITY_START_DENIED;
            }
        }

        public boolean callerExplicitOptOut() {