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

Commit 8150d2a2 authored by Adrian Roos's avatar Adrian Roos
Browse files

Require explicit userId in LockPatternUtils

Bug: 18931518
Change-Id: Ib03f37df9135f0324a998c62d165d8eea46328c8
parent c8240411
Loading
Loading
Loading
Loading
+58 −157
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.Manifest;
import android.app.ActivityManagerNative;
import android.app.admin.DevicePolicyManager;
import android.app.trust.TrustManager;
import android.bluetooth.BluetoothClass;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@@ -188,67 +189,55 @@ public class LockPatternUtils {
        return mLockSettingsService;
    }

    public int getRequestedMinimumPasswordLength() {
        return getDevicePolicyManager().getPasswordMinimumLength(null, getCurrentOrCallingUserId());
    public int getRequestedMinimumPasswordLength(int userId) {
        return getDevicePolicyManager().getPasswordMinimumLength(null, userId);
    }

    /**
     * Gets the device policy password mode. If the mode is non-specific, returns
     * MODE_PATTERN which allows the user to choose anything.
     */
    public int getRequestedPasswordQuality() {
        return getDevicePolicyManager().getPasswordQuality(null, getCurrentOrCallingUserId());
    }

    public int getRequestedPasswordHistoryLength() {
        return getRequestedPasswordHistoryLength(getCurrentOrCallingUserId());
    public int getRequestedPasswordQuality(int userId) {
        return getDevicePolicyManager().getPasswordQuality(null, userId);
    }

    private int getRequestedPasswordHistoryLength(int userId) {
        return getDevicePolicyManager().getPasswordHistoryLength(null, userId);
    }

    public int getRequestedPasswordMinimumLetters() {
        return getDevicePolicyManager().getPasswordMinimumLetters(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumLetters(int userId) {
        return getDevicePolicyManager().getPasswordMinimumLetters(null, userId);
    }

    public int getRequestedPasswordMinimumUpperCase() {
        return getDevicePolicyManager().getPasswordMinimumUpperCase(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumUpperCase(int userId) {
        return getDevicePolicyManager().getPasswordMinimumUpperCase(null, userId);
    }

    public int getRequestedPasswordMinimumLowerCase() {
        return getDevicePolicyManager().getPasswordMinimumLowerCase(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumLowerCase(int userId) {
        return getDevicePolicyManager().getPasswordMinimumLowerCase(null, userId);
    }

    public int getRequestedPasswordMinimumNumeric() {
        return getDevicePolicyManager().getPasswordMinimumNumeric(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumNumeric(int userId) {
        return getDevicePolicyManager().getPasswordMinimumNumeric(null, userId);
    }

    public int getRequestedPasswordMinimumSymbols() {
        return getDevicePolicyManager().getPasswordMinimumSymbols(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumSymbols(int userId) {
        return getDevicePolicyManager().getPasswordMinimumSymbols(null, userId);
    }

    public int getRequestedPasswordMinimumNonLetter() {
        return getDevicePolicyManager().getPasswordMinimumNonLetter(null,
                getCurrentOrCallingUserId());
    public int getRequestedPasswordMinimumNonLetter(int userId) {
        return getDevicePolicyManager().getPasswordMinimumNonLetter(null, userId);
    }

    public void reportFailedPasswordAttempt() {
        int userId = getCurrentOrCallingUserId();
    public void reportFailedPasswordAttempt(int userId) {
        getDevicePolicyManager().reportFailedPasswordAttempt(userId);
        getTrustManager().reportUnlockAttempt(false /* authenticated */, userId);
        getTrustManager().reportRequireCredentialEntry(userId);
    }

    public void reportSuccessfulPasswordAttempt() {
        getDevicePolicyManager().reportSuccessfulPasswordAttempt(getCurrentOrCallingUserId());
        getTrustManager().reportUnlockAttempt(true /* authenticated */,
                getCurrentOrCallingUserId());
    public void reportSuccessfulPasswordAttempt(int userId) {
        getDevicePolicyManager().reportSuccessfulPasswordAttempt(userId);
        getTrustManager().reportUnlockAttempt(true /* authenticated */, userId);
    }

    public void setCurrentUser(int userId) {
@@ -267,15 +256,15 @@ public class LockPatternUtils {
        }
    }

    private int getCurrentOrCallingUserId() {
        if (mMultiUserMode) {
            // TODO: This is a little inefficient. See if all users of this are able to
            // handle USER_CURRENT and pass that instead.
            return getCurrentUser();
        } else {
            return UserHandle.getCallingUserId();
        }
    }
//    private int getCurrentOrCallingUserId() {
//        if (mMultiUserMode) {
//            // TODO: This is a little inefficient. See if all users of this are able to
//            // handle USER_CURRENT and pass that instead.
//            return getCurrentUser();
//        } else {
//            return UserHandle.getCallingUserId();
//        }
//    }

    /**
     * Check to see if a pattern matches the saved pattern.
@@ -286,8 +275,7 @@ public class LockPatternUtils {
     * @param challenge The challenge to verify against the pattern
     * @return the attestation that the challenge was verified, or null.
     */
    public byte[] verifyPattern(List<LockPatternView.Cell> pattern, long challenge) {
        final int userId = getCurrentOrCallingUserId();
    public byte[] verifyPattern(List<LockPatternView.Cell> pattern, long challenge, int userId) {
        try {
            return getLockSettings().verifyPattern(patternToString(pattern), challenge, userId);
        } catch (RemoteException re) {
@@ -301,8 +289,7 @@ public class LockPatternUtils {
     * @param pattern The pattern to check.
     * @return Whether the pattern matches the stored one.
     */
    public boolean checkPattern(List<LockPatternView.Cell> pattern) {
        final int userId = getCurrentOrCallingUserId();
    public boolean checkPattern(List<LockPatternView.Cell> pattern, int userId) {
        try {
            return getLockSettings().checkPattern(patternToString(pattern), userId);
        } catch (RemoteException re) {
@@ -319,8 +306,7 @@ public class LockPatternUtils {
     * @param challenge The challenge to verify against the password
     * @return the attestation that the challenge was verified, or null.
     */
    public byte[] verifyPassword(String password, long challenge) {
        final int userId = getCurrentOrCallingUserId();
    public byte[] verifyPassword(String password, long challenge, int userId) {
        try {
            return getLockSettings().verifyPassword(password, challenge, userId);
        } catch (RemoteException re) {
@@ -334,8 +320,7 @@ public class LockPatternUtils {
     * @param password The password to check.
     * @return Whether the password matches the stored one.
     */
    public boolean checkPassword(String password) {
        final int userId = getCurrentOrCallingUserId();
    public boolean checkPassword(String password, int userId) {
        try {
            return getLockSettings().checkPassword(password, userId);
        } catch (RemoteException re) {
@@ -348,8 +333,7 @@ public class LockPatternUtils {
     * Note that this also clears vold's copy of the password.
     * @return Whether the vold password matches or not.
     */
    public boolean checkVoldPassword() {
        final int userId = getCurrentOrCallingUserId();
    public boolean checkVoldPassword(int userId) {
        try {
            return getLockSettings().checkVoldPassword(userId);
        } catch (RemoteException re) {
@@ -364,8 +348,7 @@ public class LockPatternUtils {
     * @param password The password to check.
     * @return Whether the password matches any in the history.
     */
    public boolean checkPasswordHistory(String password) {
        int userId = getCurrentOrCallingUserId();
    public boolean checkPasswordHistory(String password, int userId) {
        String passwordHashString = new String(
                passwordToHash(password, userId), StandardCharsets.UTF_8);
        String passwordHistory = getString(PASSWORD_HISTORY_KEY, userId);
@@ -374,7 +357,7 @@ public class LockPatternUtils {
        }
        // Password History may be too long...
        int passwordHashLength = passwordHashString.length();
        int passwordHistoryLength = getRequestedPasswordHistoryLength();
        int passwordHistoryLength = getRequestedPasswordHistoryLength(userId);
        if(passwordHistoryLength == 0) {
            return false;
        }
@@ -416,16 +399,8 @@ public class LockPatternUtils {
     *
     * @return True if the user has ever chosen a pattern.
     */
    public boolean isPatternEverChosen() {
        return getBoolean(PATTERN_EVER_CHOSEN_KEY, false, getCurrentOrCallingUserId());
    }

    /**
     * Used by device policy manager to validate the current password
     * information it has.
     */
    public int getActivePasswordQuality() {
        return getActivePasswordQuality(getCurrentOrCallingUserId());
    public boolean isPatternEverChosen(int userId) {
        return getBoolean(PATTERN_EVER_CHOSEN_KEY, false, userId);
    }

    /**
@@ -448,10 +423,6 @@ public class LockPatternUtils {
        return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
    }

    public void clearLock() {
        clearLock(getCurrentOrCallingUserId());
    }

    /**
     * Clear any lock pattern or password.
     */
@@ -478,16 +449,6 @@ public class LockPatternUtils {
        onAfterChangingPassword(userHandle);
    }

    /**
     * Disable showing lock screen at all when the DevicePolicyManager allows it.
     * This is only meaningful if pattern, pin or password are not set.
     *
     * @param disable Disables lock screen when true
     */
    public void setLockScreenDisabled(boolean disable) {
        setLockScreenDisabled(disable, getCurrentOrCallingUserId());
    }

    /**
     * Disable showing lock screen at all for a given user.
     * This is only meaningful if pattern, pin or password are not set.
@@ -505,21 +466,16 @@ public class LockPatternUtils {
     *
     * @return true if lock screen is disabled
     */
    public boolean isLockScreenDisabled() {
        return !isSecure() &&
                getBoolean(DISABLE_LOCKSCREEN_KEY, false, getCurrentOrCallingUserId());
    public boolean isLockScreenDisabled(int userId) {
        return !isSecure(userId) &&
                getBoolean(DISABLE_LOCKSCREEN_KEY, false, userId);
    }

    /**
     * Save a lock pattern.
     * @param pattern The new pattern to save.
     * @param savedPattern The previously saved pattern, or null if none
     * @param userId the user whose pattern is to be saved.
     */
    public void saveLockPattern(List<LockPatternView.Cell> pattern,
            String savedPattern) {
        this.saveLockPattern(pattern, savedPattern, getCurrentOrCallingUserId());
    }

    public void saveLockPattern(List<LockPatternView.Cell> pattern, int userId) {
        this.saveLockPattern(pattern, null, userId);
    }
@@ -588,8 +544,7 @@ public class LockPatternUtils {
        updateCryptoUserInfo(userId);
    }

    public void setOwnerInfoEnabled(boolean enabled) {
        int userId = getCurrentOrCallingUserId();
    public void setOwnerInfoEnabled(boolean enabled, int userId) {
        setBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, enabled, userId);
        updateCryptoUserInfo(userId);
    }
@@ -598,11 +553,7 @@ public class LockPatternUtils {
        return getString(LOCK_SCREEN_OWNER_INFO, userId);
    }

    public boolean isOwnerInfoEnabled() {
        return isOwnerInfoEnabled(getCurrentOrCallingUserId());
    }

    private boolean isOwnerInfoEnabled(int userId) {
    public boolean isOwnerInfoEnabled(int userId) {
        return getBoolean(LOCK_SCREEN_OWNER_INFO_ENABLED, false, userId);
    }

@@ -724,21 +675,10 @@ public class LockPatternUtils {
    /**
     * Save a lock password.  Does not ensure that the password is as good
     * as the requested mode, but will adjust the mode to be as good as the
     * pattern.
     * password.
     * @param password The password to save
     * @param savedPassword The previously saved lock password, or null if none
     * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
     */
    public void saveLockPassword(String password, String savedPassword, int quality) {
        saveLockPassword(password, savedPassword, quality, getCurrentOrCallingUserId());
    }

    /**
     * Save a lock password.  Does not ensure that the password is as good
     * as the requested mode, but will adjust the mode to be as good as the
     * pattern.
     * @param password The password to save
     * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
     * @param userHandle The userId of the user to change the password for
     */
    public void saveLockPassword(String password, String savedPassword, int quality,
@@ -864,16 +804,6 @@ public class LockPatternUtils {
        updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null);
    }

    /**
     * Retrieves the quality mode we're in.
     * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
     *
     * @return stored password quality
     */
    public int getKeyguardStoredPasswordQuality() {
        return getKeyguardStoredPasswordQuality(getCurrentOrCallingUserId());
    }

    /**
     * Retrieves the quality mode for {@param userHandle}.
     * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
@@ -996,13 +926,6 @@ public class LockPatternUtils {
        }
    }

    /**
     * @return Whether the lock screen is secured.
     */
    public boolean isSecure() {
        return isSecure(getCurrentOrCallingUserId());
    }

    /**
     * @param userId the user for which to report the value
     * @return Whether the lock screen is secured.
@@ -1012,13 +935,6 @@ public class LockPatternUtils {
        return isLockPatternEnabled(mode, userId) || isLockPasswordEnabled(mode, userId);
    }

    /**
     * @return Whether the lock password is enabled
     */
    public boolean isLockPasswordEnabled() {
        return isLockPasswordEnabled(getCurrentOrCallingUserId());
    }

    public boolean isLockPasswordEnabled(int userId) {
        return isLockPasswordEnabled(getKeyguardStoredPasswordQuality(userId), userId);
    }
@@ -1035,10 +951,6 @@ public class LockPatternUtils {
    /**
     * @return Whether the lock pattern is enabled
     */
    public boolean isLockPatternEnabled() {
        return isLockPatternEnabled(getCurrentOrCallingUserId());
    }

    public boolean isLockPatternEnabled(int userId) {
        return isLockPatternEnabled(getKeyguardStoredPasswordQuality(userId), userId);
    }
@@ -1051,16 +963,14 @@ public class LockPatternUtils {
    /**
     * @return Whether the visible pattern is enabled.
     */
    public boolean isVisiblePatternEnabled() {
        return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false, getCurrentOrCallingUserId());
    public boolean isVisiblePatternEnabled(int userId) {
        return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false, userId);
    }

    /**
     * Set whether the visible pattern is enabled.
     */
    public void setVisiblePatternEnabled(boolean enabled) {
        int userId = getCurrentOrCallingUserId();

    public void setVisiblePatternEnabled(boolean enabled, int userId) {
        setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled, userId);

        // Update for crypto if owner
@@ -1095,9 +1005,9 @@ public class LockPatternUtils {
     * pattern until the deadline has passed.
     * @return the chosen deadline.
     */
    public long setLockoutAttemptDeadline() {
    public long setLockoutAttemptDeadline(int userId) {
        final long deadline = SystemClock.elapsedRealtime() + FAILED_ATTEMPT_TIMEOUT_MS;
        setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, getCurrentOrCallingUserId());
        setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline, userId);
        return deadline;
    }

@@ -1106,8 +1016,8 @@ public class LockPatternUtils {
     *   attempt to enter his/her lock pattern, or 0 if the user is welcome to
     *   enter a pattern.
     */
    public long getLockoutAttemptDeadline() {
        final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L, getCurrentOrCallingUserId());
    public long getLockoutAttemptDeadline(int userId) {
        final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L, userId);
        final long now = SystemClock.elapsedRealtime();
        if (deadline < now || deadline > (now + FAILED_ATTEMPT_TIMEOUT_MS)) {
            return 0L;
@@ -1166,21 +1076,12 @@ public class LockPatternUtils {
        }
    }

    public void setPowerButtonInstantlyLocks(boolean enabled) {
        setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled, getCurrentOrCallingUserId());
    }

    public boolean getPowerButtonInstantlyLocks() {
        return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true,
                getCurrentOrCallingUserId());
    }

    public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents) {
        setEnabledTrustAgents(activeTrustAgents, getCurrentOrCallingUserId());
    public void setPowerButtonInstantlyLocks(boolean enabled, int userId) {
        setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled, userId);
    }

    public List<ComponentName> getEnabledTrustAgents() {
        return getEnabledTrustAgents(getCurrentOrCallingUserId());
    public boolean getPowerButtonInstantlyLocks(int userId) {
        return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true, userId);
    }

    public void setEnabledTrustAgents(Collection<ComponentName> activeTrustAgents, int userId) {
@@ -1192,7 +1093,7 @@ public class LockPatternUtils {
            sb.append(cn.flattenToShortString());
        }
        setString(ENABLED_TRUST_AGENTS, sb.toString(), userId);
        getTrustManager().reportEnabledTrustAgentsChanged(getCurrentOrCallingUserId());
        getTrustManager().reportEnabledTrustAgentsChanged(userId);
    }

    public List<ComponentName> getEnabledTrustAgents(int userId) {
+1 −1
Original line number Diff line number Diff line
@@ -138,7 +138,7 @@ public class EmergencyButton extends Button {
                    visible = mEnableEmergencyCallWhileSimLocked;
                } else {
                    // Only show if there is a secure screen (pin/pattern/SIM pin/SIM puk);
                    visible = mLockPatternUtils.isSecure();
                    visible = mLockPatternUtils.isSecure(mLockPatternUtils.getCurrentUser());
                }
            }
        }
+5 −3
Original line number Diff line number Diff line
@@ -63,7 +63,8 @@ public abstract class KeyguardAbsKeyInputView extends LinearLayout
        // start fresh
        resetPasswordText(false /* animate */);
        // if the user is currently locked out, enforce it.
        long deadline = mLockPatternUtils.getLockoutAttemptDeadline();
        long deadline = mLockPatternUtils.getLockoutAttemptDeadline(
                mLockPatternUtils.getCurrentUser());
        if (shouldLockout(deadline)) {
            handleAttemptLockout(deadline);
        } else {
@@ -106,7 +107,7 @@ public abstract class KeyguardAbsKeyInputView extends LinearLayout

    protected void verifyPasswordAndUnlock() {
        String entry = getPasswordText();
        if (mLockPatternUtils.checkPassword(entry)) {
        if (mLockPatternUtils.checkPassword(entry, mLockPatternUtils.getCurrentUser())) {
            mCallback.reportUnlockAttempt(true);
            mCallback.dismiss(true);
        } else {
@@ -116,7 +117,8 @@ public abstract class KeyguardAbsKeyInputView extends LinearLayout
                mCallback.reportUnlockAttempt(false);
                int attempts = KeyguardUpdateMonitor.getInstance(mContext).getFailedUnlockAttempts();
                if (0 == (attempts % LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT)) {
                    long deadline = mLockPatternUtils.setLockoutAttemptDeadline();
                    long deadline = mLockPatternUtils.setLockoutAttemptDeadline(
                            mLockPatternUtils.getCurrentUser());
                    handleAttemptLockout(deadline);
                }
            }
+7 −4
Original line number Diff line number Diff line
@@ -130,7 +130,8 @@ public class KeyguardPatternView extends LinearLayout implements KeyguardSecurit
        mLockPatternView.setOnPatternListener(new UnlockPatternListener());

        // stealth mode will be the same for the life of this screen
        mLockPatternView.setInStealthMode(!mLockPatternUtils.isVisiblePatternEnabled());
        mLockPatternView.setInStealthMode(!mLockPatternUtils.isVisiblePatternEnabled(
                mLockPatternUtils.getCurrentUser()));

        // vibrate mode will be the same for the life of this screen
        mLockPatternView.setTactileFeedbackEnabled(mLockPatternUtils.isTactileFeedbackEnabled());
@@ -176,7 +177,8 @@ public class KeyguardPatternView extends LinearLayout implements KeyguardSecurit
        mLockPatternView.clearPattern();

        // if the user is currently locked out, enforce it.
        long deadline = mLockPatternUtils.getLockoutAttemptDeadline();
        long deadline = mLockPatternUtils.getLockoutAttemptDeadline(
                mLockPatternUtils.getCurrentUser());
        if (deadline != 0) {
            handleAttemptLockout(deadline);
        } else {
@@ -213,7 +215,7 @@ public class KeyguardPatternView extends LinearLayout implements KeyguardSecurit
        }

        public void onPatternDetected(List<LockPatternView.Cell> pattern) {
            if (mLockPatternUtils.checkPattern(pattern)) {
            if (mLockPatternUtils.checkPattern(pattern, mLockPatternUtils.getCurrentUser())) {
                mCallback.reportUnlockAttempt(true);
                mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Correct);
                mCallback.dismiss(true);
@@ -230,7 +232,8 @@ public class KeyguardPatternView extends LinearLayout implements KeyguardSecurit
                int attempts = mKeyguardUpdateMonitor.getFailedUnlockAttempts();
                if (registeredAttempt &&
                        0 == (attempts % LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT)) {
                    long deadline = mLockPatternUtils.setLockoutAttemptDeadline();
                    long deadline = mLockPatternUtils.setLockoutAttemptDeadline(
                            mLockPatternUtils.getCurrentUser());
                    handleAttemptLockout(deadline);
                } else {
                    mSecurityMessageDisplay.setMessage(R.string.kg_wrong_pattern, true);
+3 −2
Original line number Diff line number Diff line
@@ -296,7 +296,7 @@ public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSe
                (failedAttempts % LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT) == 0;
        }
        monitor.reportFailedUnlockAttempt();
        mLockPatternUtils.reportFailedPasswordAttempt();
        mLockPatternUtils.reportFailedPasswordAttempt(mLockPatternUtils.getCurrentUser());
        if (showTimeout) {
            showTimeoutDialog();
        }
@@ -430,7 +430,8 @@ public class KeyguardSecurityContainer extends FrameLayout implements KeyguardSe
            KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
            if (success) {
                monitor.clearFailedUnlockAttempts();
                mLockPatternUtils.reportSuccessfulPasswordAttempt();
                mLockPatternUtils.reportSuccessfulPasswordAttempt(
                        mLockPatternUtils.getCurrentUser());
            } else {
                KeyguardSecurityContainer.this.reportFailedUnlockAttempt();
            }
Loading