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

Commit 332e32b8 authored by John Wu's avatar John Wu
Browse files

Introduce Intent#prepareToEnterSystemServer()

Put all operations that need to be performed to an intent upon
receiving it in system_server into its own dedicated method for easier
maintenance in the future.

- Prevent leaked file descriptors
- Remove all system only flags and extended flags

Bug: 326100473
Test: TH
Flag: EXEMPT refactor
Change-Id: Id2931d1c1d8b8f1caf4a92e4e2357c1f2e9d0925
parent b9b1de53
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -7624,6 +7624,13 @@ public class Intent implements Parcelable, Cloneable {
            | FLAG_GRANT_WRITE_URI_PERMISSION | FLAG_GRANT_PERSISTABLE_URI_PERMISSION
            | FLAG_GRANT_PREFIX_URI_PERMISSION;
    /**
     * Flags that are not normally set by application code, but set for you by the system.
     */
    private static final int SYSTEM_ONLY_FLAGS = FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
            | FLAG_ACTIVITY_BROUGHT_TO_FRONT
            | FLAG_RECEIVER_FROM_SHELL;
    /**
     * Local flag indicating this instance was created by copy constructor.
     */
@@ -7677,6 +7684,11 @@ public class Intent implements Parcelable, Cloneable {
    @TestApi
    public static final int EXTENDED_FLAG_FILTER_MISMATCH = 1 << 0;
    /**
     * Extended flags that are not normally set by application code, but set for you by the system.
     */
    private static final int SYSTEM_ONLY_EXTENDED_FLAGS = EXTENDED_FLAG_FILTER_MISMATCH;
    // ---------------------------------------------------------------------
    // ---------------------------------------------------------------------
    // toUri() and parseUri() options.
@@ -12641,6 +12653,28 @@ public class Intent implements Parcelable, Cloneable {
        }
    }
    /**
     * Prepare this {@link Intent} to enter system_server.
     *
     * @hide
     */
    public void prepareToEnterSystemServer() {
        // Refuse possible leaked file descriptors
        if (hasFileDescriptors()) {
            throw new IllegalArgumentException("File descriptors passed in Intent");
        }
        // These flags are set only by the system, and should be stripped out as soon as the intent
        // is received by system_server from the caller so it can be properly updated later.
        removeFlags(SYSTEM_ONLY_FLAGS);
        removeExtendedFlags(SYSTEM_ONLY_EXTENDED_FLAGS);
        if (mOriginalIntent != null) {
            mOriginalIntent.prepareToEnterSystemServer();
        }
        if (mSelector != null) {
            mSelector.prepareToEnterSystemServer();
        }
    }
    /** @hide */
    public boolean hasWebURI() {
        if (getData() == null) {
+4 −22
Original line number Diff line number Diff line
@@ -5519,9 +5519,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            for (int i=0; i<intents.length; i++) {
                Intent intent = intents[i];
                if (intent != null) {
                    if (intent.hasFileDescriptors()) {
                        throw new IllegalArgumentException("File descriptors passed in Intent");
                    }
                    intent.prepareToEnterSystemServer();
                    if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
                            (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
                        throw new IllegalArgumentException(
@@ -5554,7 +5552,6 @@ public class ActivityManagerService extends IActivityManager.Stub
                        }
                    }
                    intents[i] = new Intent(intent);
                    intents[i].removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
                }
            }
            if (resolvedTypes != null && resolvedTypes.length != intents.length) {
@@ -13922,12 +13919,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        enforceNotIsolatedCaller("startService");
        enforceAllowedToStartOrBindServiceIfSdkSandbox(service);
        if (service != null) {
            // Refuse possible leaked file descriptors
            if (service.hasFileDescriptors()) {
                throw new IllegalArgumentException("File descriptors passed in Intent");
            }
            // Remove existing mismatch flag so it can be properly updated later
            service.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
            service.prepareToEnterSystemServer();
        }
        if (callingPackage == null) {
@@ -14164,12 +14156,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        enforceAllowedToStartOrBindServiceIfSdkSandbox(service);
        if (service != null) {
            // Refuse possible leaked file descriptors
            if (service.hasFileDescriptors()) {
                throw new IllegalArgumentException("File descriptors passed in Intent");
            }
            // Remove existing mismatch flag so it can be properly updated later
            service.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
            service.prepareToEnterSystemServer();
        }
        if (callingPackage == null) {
@@ -16168,12 +16155,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    final Intent verifyBroadcastLocked(Intent intent) {
        if (intent != null) {
            // Refuse possible leaked file descriptors
            if (intent.hasFileDescriptors()) {
                throw new IllegalArgumentException("File descriptors passed in Intent");
            }
            // Remove existing mismatch flag so it can be properly updated later
            intent.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
            intent.prepareToEnterSystemServer();
        }
        int flags = intent.getFlags();
+1 −7
Original line number Diff line number Diff line
@@ -424,19 +424,13 @@ public class ActivityStartController {
                Intent intent = intents[i];
                NeededUriGrants intentGrants = null;

                // Refuse possible leaked file descriptors.
                if (intent.hasFileDescriptors()) {
                    throw new IllegalArgumentException("File descriptors passed in Intent");
                }
                intent.prepareToEnterSystemServer();

                // Get the flag earlier because the intent may be modified in resolveActivity below.
                final boolean componentSpecified = intent.getComponent() != null;
                // Don't modify the client's object!
                intent = new Intent(intent);

                // Remove existing mismatch flag so it can be properly updated later
                intent.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);

                // Collect information about the target of the Intent.
                ActivityInfo aInfo = mSupervisor.resolveActivity(intent, resolvedTypes[i],
                        0 /* startFlags */, null /* profilerInfo */, userId, filterCallingUid,
+1 −7
Original line number Diff line number Diff line
@@ -717,13 +717,7 @@ class ActivityStarter {
            onExecutionStarted();

            if (mRequest.intent != null) {
                // Refuse possible leaked file descriptors
                if (mRequest.intent.hasFileDescriptors()) {
                    throw new IllegalArgumentException("File descriptors passed in Intent");
                }

                // Remove existing mismatch flag so it can be properly updated later
                mRequest.intent.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
                mRequest.intent.prepareToEnterSystemServer();
            }

            final LaunchingState launchingState;
+4 −11
Original line number Diff line number Diff line
@@ -1318,12 +1318,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
            String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle bOptions) {
        enforceNotIsolatedCaller("startActivityIntentSender");
        if (fillInIntent != null) {
            // Refuse possible leaked file descriptors
            if (fillInIntent.hasFileDescriptors()) {
                throw new IllegalArgumentException("File descriptors passed in Intent");
            }
            // Remove existing mismatch flag so it can be properly updated later
            fillInIntent.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
            fillInIntent.prepareToEnterSystemServer();
        }

        if (!(target instanceof PendingIntentRecord)) {
@@ -1349,10 +1344,10 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
    @Override
    public boolean startNextMatchingActivity(IBinder callingActivity, Intent intent,
            Bundle bOptions) {
        // Refuse possible leaked file descriptors
        if (intent != null && intent.hasFileDescriptors()) {
            throw new IllegalArgumentException("File descriptors passed in Intent");
        if (intent != null) {
            intent.prepareToEnterSystemServer();
        }

        SafeActivityOptions options = SafeActivityOptions.fromBundle(bOptions);

        synchronized (mGlobalLock) {
@@ -1367,8 +1362,6 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
                return false;
            }
            intent = new Intent(intent);
            // Remove existing mismatch flag so it can be properly updated later
            intent.removeExtendedFlags(Intent.EXTENDED_FLAG_FILTER_MISMATCH);
            // The caller is not allowed to change the data.
            intent.setDataAndType(r.intent.getData(), r.intent.getType());
            // And we are resetting to find the next component...