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

Commit f1c44b93 authored by Palak Chaudhary's avatar Palak Chaudhary Committed by Android (Google) Code Review
Browse files

Merge "Use hook in AdServices to check if process can register to broadcastReceivers"

parents 1e4371b4 56853e1c
Loading
Loading
Loading
Loading
+14 −62
Original line number Diff line number Diff line
@@ -1606,8 +1606,6 @@ public class ActivityManagerService extends IActivityManager.Stub
    // Encapsulates the global setting "hidden_api_blacklist_exemptions"
    final HiddenApiSettings mHiddenApiBlacklist;
    final SdkSandboxSettings mSdkSandboxSettings;
    private final PlatformCompat mPlatformCompat;
    PackageManagerInternal mPackageManagerInt;
@@ -2324,53 +2322,6 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
    }
    /**
     * Handles settings related to the enforcement of SDK sandbox restrictions.
     */
    static class SdkSandboxSettings implements DeviceConfig.OnPropertiesChangedListener {
        private final Context mContext;
        private final Object mLock = new Object();
        @GuardedBy("mLock")
        private boolean mEnforceBroadcastReceiverRestrictions;
        /**
         * Property to enforce broadcast receiver restrictions for SDK sandbox processes. If the
         * value of this property is {@code true}, the restrictions will be enforced.
         */
        public static final String ENFORCE_BROADCAST_RECEIVER_RESTRICTIONS =
                "enforce_broadcast_receiver_restrictions";
        SdkSandboxSettings(Context context) {
            mContext = context;
        }
        void registerObserver() {
            synchronized (mLock) {
                mEnforceBroadcastReceiverRestrictions = DeviceConfig.getBoolean(
                        DeviceConfig.NAMESPACE_SDK_SANDBOX,
                        ENFORCE_BROADCAST_RECEIVER_RESTRICTIONS, false);
                DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_SDK_SANDBOX,
                        mContext.getMainExecutor(), this);
            }
        }
        @Override
        public void onPropertiesChanged(DeviceConfig.Properties properties) {
            synchronized (mLock) {
                mEnforceBroadcastReceiverRestrictions = properties.getBoolean(
                        ENFORCE_BROADCAST_RECEIVER_RESTRICTIONS, false);
            }
        }
        boolean isBroadcastReceiverRestrictionsEnforced() {
            synchronized (mLock) {
                return mEnforceBroadcastReceiverRestrictions;
            }
        }
    }
    AppOpsManager getAppOpsManager() {
        if (mAppOpsManager == null) {
            mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
@@ -2414,7 +2365,6 @@ public class ActivityManagerService extends IActivityManager.Stub
        mProcStartHandlerThread = null;
        mProcStartHandler = null;
        mHiddenApiBlacklist = null;
        mSdkSandboxSettings = null;
        mFactoryTest = FACTORY_TEST_OFF;
        mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
        mInternal = new LocalService();
@@ -2539,7 +2489,6 @@ public class ActivityManagerService extends IActivityManager.Stub
        mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
        mHiddenApiBlacklist = new HiddenApiSettings(mHandler, mContext);
        mSdkSandboxSettings = new SdkSandboxSettings(mContext);
        Watchdog.getInstance().addMonitor(this);
        Watchdog.getInstance().addThread(mHandler);
@@ -8300,7 +8249,6 @@ public class ActivityManagerService extends IActivityManager.Stub
        final boolean alwaysFinishActivities =
                Settings.Global.getInt(resolver, ALWAYS_FINISH_ACTIVITIES, 0) != 0;
        mHiddenApiBlacklist.registerObserver();
        mSdkSandboxSettings.registerObserver();
        mPlatformCompat.registerContentObserver();
        mAppProfiler.retrieveSettings();
@@ -13542,16 +13490,6 @@ public class ActivityManagerService extends IActivityManager.Stub
            String callerFeatureId, String receiverId, IIntentReceiver receiver,
            IntentFilter filter, String permission, int userId, int flags) {
        enforceNotIsolatedCaller("registerReceiver");
        // Allow Sandbox process to register only unexported receivers.
        boolean unexported = (flags & Context.RECEIVER_NOT_EXPORTED) != 0;
        if (mSdkSandboxSettings.isBroadcastReceiverRestrictionsEnforced()
                && Process.isSdkSandboxUid(Binder.getCallingUid())
                && !unexported) {
            throw new SecurityException("SDK sandbox process not allowed to call "
                + "registerReceiver");
        }
        ArrayList<Intent> stickyIntents = null;
        ProcessRecord callerApp = null;
        final boolean visibleToInstantApps
@@ -13615,6 +13553,20 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
            }
            if (Process.isSdkSandboxUid(Binder.getCallingUid())) {
                SdkSandboxManagerLocal sdkSandboxManagerLocal =
                        LocalManagerRegistry.getManager(SdkSandboxManagerLocal.class);
                if (sdkSandboxManagerLocal == null) {
                    throw new IllegalStateException("SdkSandboxManagerLocal not found when checking"
                            + " whether SDK sandbox uid can register to broadcast receivers.");
                }
                if (!sdkSandboxManagerLocal.canRegisterBroadcastReceiver(
                        /*IntentFilter=*/ filter, flags, onlyProtectedBroadcasts)) {
                    throw new SecurityException("SDK sandbox not allowed to register receiver"
                            + " with the given IntentFilter");
                }
            }
            // If the change is enabled, but neither exported or not exported is set, we need to log
            // an error so the consumer can know to explicitly set the value for their flag.
            // If the caller is registering for a sticky broadcast with a null receiver, we won't