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

Commit ecd34d4f authored by Saumya Pathak's avatar Saumya Pathak
Browse files

Correct permission check for M_E_S and order of getApplicationInfo in the API

Bug: 207389517
Bug: 210945734
Test: atest StorageManagerTest#testLaunchManageSpaceActivityIntent

Change-Id: I23d884fc5a7c6383b19e2e267b8870d3102d5b7b
parent ce65b04d
Loading
Loading
Loading
Loading
+28 −10
Original line number Diff line number Diff line
@@ -3698,16 +3698,29 @@ class StorageManagerService extends IStorageManager.Stub
    @Nullable
    public PendingIntent getManageSpaceActivityIntent(
            @NonNull String packageName, int requestCode) {
        // Only Apps with MANAGE_EXTERNAL_STORAGE permission should be able to call this API.
        enforcePermission(android.Manifest.permission.MANAGE_EXTERNAL_STORAGE);

        // We want to call the manageSpaceActivity as a SystemService and clear identity
        // of the calling App
        // Only Apps with MANAGE_EXTERNAL_STORAGE permission which have package visibility for
        // packageName should be able to call this API.
        int originalUid = Binder.getCallingUidOrThrow();
        final long token = Binder.clearCallingIdentity();
        try {
            // Get package name for calling app and verify it has MANAGE_EXTERNAL_STORAGE permission
            final String[] packagesFromUid = mIPackageManager.getPackagesForUid(originalUid);
            if (packagesFromUid == null) {
                throw new SecurityException("Unknown uid " + originalUid);
            }
            // Checking first entry in packagesFromUid is enough as using "sharedUserId"
            // mechanism is rare and discouraged. Also, Apps that share same UID share the same
            // permissions.
            if (!mStorageManagerInternal.hasExternalStorageAccess(originalUid,
                    packagesFromUid[0])) {
                throw new SecurityException("Only File Manager Apps permitted");
            }
        } catch (RemoteException re) {
            throw new SecurityException("Unknown uid " + originalUid, re);
        }

        ApplicationInfo appInfo;
        try {
            ApplicationInfo appInfo = mIPackageManager.getApplicationInfo(packageName, 0,
            appInfo = mIPackageManager.getApplicationInfo(packageName, 0,
                    UserHandle.getUserId(originalUid));
            if (appInfo == null) {
                throw new IllegalArgumentException(
@@ -3717,8 +3730,15 @@ class StorageManagerService extends IStorageManager.Stub
                Log.i(TAG, packageName + " doesn't have a manageSpaceActivity");
                return null;
            }
            Context targetAppContext = mContext.createPackageContext(packageName, 0);
        } catch (RemoteException e) {
            throw new SecurityException("Only File Manager Apps permitted");
        }

        // We want to call the manageSpaceActivity as a SystemService and clear identity
        // of the calling App
        final long token = Binder.clearCallingIdentity();
        try {
            Context targetAppContext = mContext.createPackageContext(packageName, 0);
            Intent intent = new Intent(Intent.ACTION_DEFAULT);
            intent.setClassName(packageName,
                    appInfo.manageSpaceActivityName);
@@ -3728,8 +3748,6 @@ class StorageManagerService extends IStorageManager.Stub
                    intent,
                    FLAG_ONE_SHOT | FLAG_CANCEL_CURRENT | FLAG_IMMUTABLE);
            return activity;
        } catch (RemoteException e) {
            throw e.rethrowAsRuntimeException();
        } catch (PackageManager.NameNotFoundException e) {
            throw new IllegalArgumentException(
                    "packageName not found");