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

Commit 05f560a7 authored by Sumedh Sen's avatar Sumedh Sen
Browse files

Perform protected pkg and active admin check for all users

If an app being uninstalled is data protected, or is an active admin for
any users present on device, abort the uninstallation.

Bug: 285869527
Test: Manual. Create work profile via TestDPC, uninstall TestDPC app
from personal and work profiles. App shouldn't be deleted

Change-Id: Ife96d91ac18239f4805013ed1a6d2dd92a98817e
parent af0f3422
Loading
Loading
Loading
Loading
+35 −16
Original line number Diff line number Diff line
@@ -134,10 +134,6 @@ final class DeletePackageHelper {
        final int removeUser = (deleteFlags & PackageManager.DELETE_ALL_USERS) != 0
                ? UserHandle.USER_ALL : userId;

        if (mPm.isPackageDeviceAdmin(packageName, removeUser)) {
            Slog.w(TAG, "Not removing package " + packageName + ": has active device admin");
            return PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER;
        }

        final PackageSetting uninstalledPs;
        final PackageSetting disabledSystemPs;
@@ -699,18 +695,6 @@ final class DeletePackageHelper {
        final String packageName = versionedPackage.getPackageName();
        final long versionCode = versionedPackage.getLongVersionCode();

        if (mPm.mProtectedPackages.isPackageDataProtected(userId, packageName)) {
            mPm.mHandler.post(() -> {
                try {
                    Slog.w(TAG, "Attempted to delete protected package: " + packageName);
                    observer.onPackageDeleted(packageName,
                            PackageManager.DELETE_FAILED_INTERNAL_ERROR, null);
                } catch (RemoteException re) {
                }
            });
            return;
        }

        try {
            if (mPm.mInjector.getLocalService(ActivityTaskManagerInternal.class)
                    .isBaseOfLockedTask(packageName)) {
@@ -751,6 +735,41 @@ final class DeletePackageHelper {
                    "deletePackage for user " + userId);
        }

        final long token = Binder.clearCallingIdentity();
        try {
            // If a package is device admin, or is data protected for any user, it should not be
            // uninstalled from that user, or from any users if DELETE_ALL_USERS flag is passed.
            for (int user : users) {
                if (mPm.isPackageDeviceAdmin(packageName, user)) {
                    mPm.mHandler.post(() -> {
                        try {
                            Slog.w(TAG, "Not removing package " + packageName
                                    + ": has active device admin");
                            observer.onPackageDeleted(packageName,
                                    PackageManager.DELETE_FAILED_DEVICE_POLICY_MANAGER, null);
                        } catch (RemoteException e) {
                            // no-op
                        }
                    });
                    return;
                }
                if (mPm.mProtectedPackages.isPackageDataProtected(user, packageName)) {
                    mPm.mHandler.post(() -> {
                        try {
                            Slog.w(TAG, "Attempted to delete protected package: " + packageName);
                            observer.onPackageDeleted(packageName,
                                    PackageManager.DELETE_FAILED_INTERNAL_ERROR, null);
                        } catch (RemoteException re) {
                            // no-op
                        }
                    });
                    return;
                }
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }

        if (mPm.isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)) {
            mPm.mHandler.post(() -> {
                try {