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

Commit 38186761 authored by Pavel Grafov's avatar Pavel Grafov
Browse files

Remove redundant caller check.

Result of this call is not used. And there is no reason to call it for
side effects either: this check can only be performed when factoryRestet
is not null, and this only happens when invoking from
wipeDataWithReason, which already has identical call, so any permission
checks are already performed.
Without that check the code no longer needs valid calling identity, so
the method can again work as a single binderWithCleanCallingIdentity
block.

Also changed system user precondition check from authorization to state
assertion for attempts to remove the system user to make it match the
javadoc.

Fixes: 286212112
Change-Id: I3c604d27e0ab0d2f795feda44716c067c5816865
Test: btest a.d.c.WipeDataTest
Flag: EXEMPT bugfix
parent 191e1c8a
Loading
Loading
Loading
Loading
+23 −38
Original line number Diff line number Diff line
@@ -8031,8 +8031,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                "DevicePolicyManager.wipeDataWithReason() from %s, organization-owned? %s",
                adminName, calledByProfileOwnerOnOrgOwnedDevice);
        wipeDataNoLock(adminComp, flags, internalReason, wipeReasonForUser, userId,
                calledOnParentInstance, factoryReset);
        wipeDataNoLock(adminComp, flags, internalReason, wipeReasonForUser, userId, factoryReset);
    }
    private String getGenericWipeReason(
@@ -8188,17 +8187,16 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
     *                     factory reset
     */
    private void wipeDataNoLock(@Nullable ComponentName admin, int flags, String internalReason,
            String wipeReasonForUser, int userId, boolean calledOnParentInstance,
            @Nullable Boolean factoryReset) {
            String wipeReasonForUser, int userId, @Nullable Boolean factoryReset) {
        wtfIfInLock();
        final String adminPackage;
        if (admin != null) {
            adminPackage = admin.getPackageName();
        } else {
            int callerId = mInjector.binderGetCallingUid();
            String[] adminPackages = mInjector.getPackageManager().getPackagesForUid(callerId);
            int callerUid = mInjector.binderGetCallingUid();
            String[] adminPackages = mInjector.getPackageManager().getPackagesForUid(callerUid);
            Preconditions.checkState(adminPackages.length > 0,
                    "Caller %s does not have any associated packages", callerId);
                    "Caller %s does not have any associated packages", callerUid);
            adminPackage = adminPackages[0];
        }
        mInjector.binderWithCleanCallingIdentity(() -> {
@@ -8220,7 +8218,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                throw new SecurityException("Cannot wipe data. " + restriction
                        + " restriction is set for user " + userId);
            }
        });
            boolean isSystemUser = userId == UserHandle.USER_SYSTEM;
            boolean isMainUser = userId == getMainUserId();
@@ -8234,18 +8231,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
            } else {
                // Explicit behaviour
                if (factoryReset) {
                EnforcingAdmin enforcingAdmin = enforcePermissionsAndGetEnforcingAdmin(
                        /*admin=*/ null,
                        /*permission=*/ new String[]{MANAGE_DEVICE_POLICY_WIPE_DATA,
                                MASTER_CLEAR},
                        USES_POLICY_WIPE_DATA,
                        adminPackage,
                        factoryReset ? UserHandle.USER_ALL :
                                getAffectedUser(calledOnParentInstance));
                    wipeDevice = true;
                } else {
                mInjector.binderWithCleanCallingIdentity(() -> {
                    Preconditions.checkCallAuthorization(!isSystemUser,
                    Preconditions.checkState(!isSystemUser,
                            "User %s is a system user and cannot be removed", userId);
                    boolean isLastNonHeadlessUser = getUserInfo(userId).isFull()
                            && mUserManager.getAliveUsers().stream()
@@ -8253,13 +8241,11 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                            .noneMatch(UserInfo::isFull);
                    Preconditions.checkState(!isLastNonHeadlessUser,
                            "Removing user %s would leave the device without any active users. "
                                    + "Consider factory resetting the device instead.",
                            userId);
                });
                                    + "Consider factory resetting the device instead.", userId);
                    wipeDevice = false;
                }
            }
        mInjector.binderWithCleanCallingIdentity(() -> {
            if (wipeDevice) {
                forceWipeDeviceNoLock(
                        (flags & WIPE_EXTERNAL_STORAGE) != 0,
@@ -8600,7 +8586,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
                        /* reason= */ "reportFailedPasswordAttempt()",
                        getFailedPasswordAttemptWipeMessage(),
                        userId,
                        /* calledOnParentInstance= */ parent,
                        // factoryReset=null to enable U- behaviour
                        /* factoryReset= */ null);
            } catch (SecurityException e) {