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

Commit 82f6b90c authored by Eric Biggers's avatar Eric Biggers Committed by Android (Google) Code Review
Browse files

Merge changes from topic "vold-log-message-fix" into sc-dev

* changes:
  Avoid misleading log messages about failed to find key [v2]
  Don't try to lock/unlock already locked/unlocked users
  Get list of unlocked users from vold
parents 04561e74 f7e2e2cf
Loading
Loading
Loading
Loading
+56 −10
Original line number Diff line number Diff line
@@ -363,6 +363,12 @@ class StorageManagerService extends IStorageManager.Stub
            users = ArrayUtils.appendInt(users, userId);
            invalidateIsUserUnlockedCache();
        }
        public void appendAll(int[] userIds) {
            for (int userId : userIds) {
                users = ArrayUtils.appendInt(users, userId);
            }
            invalidateIsUserUnlockedCache();
        }
        public void remove(int userId) {
            users = ArrayUtils.removeInt(users, userId);
            invalidateIsUserUnlockedCache();
@@ -1099,6 +1105,10 @@ class StorageManagerService extends IStorageManager.Stub
            }

            try {
                // Reset vold to tear down existing disks/volumes and start from
                // a clean state.  Exception: already-unlocked user storage will
                // remain unlocked and is not affected by the reset.
                //
                // TODO(b/135341433): Remove cautious logging when FUSE is stable
                Slog.i(TAG, "Resetting vold...");
                mVold.reset();
@@ -1113,7 +1123,7 @@ class StorageManagerService extends IStorageManager.Stub
                    mStoraged.onUserStarted(userId);
                }
                if (mIsAutomotive) {
                    restoreAllUnlockedUsers(userManager, users, systemUnlockedUsers);
                    restoreSystemUnlockedUsers(userManager, users, systemUnlockedUsers);
                }
                mVold.onSecureKeyguardStateChanged(mSecureKeyguardShowing);
                mStorageManagerInternal.onReset(mVold);
@@ -1123,7 +1133,7 @@ class StorageManagerService extends IStorageManager.Stub
        }
    }

    private void restoreAllUnlockedUsers(UserManager userManager, List<UserInfo> allUsers,
    private void restoreSystemUnlockedUsers(UserManager userManager, List<UserInfo> allUsers,
            int[] systemUnlockedUsers) throws Exception {
        Arrays.sort(systemUnlockedUsers);
        UserManager.invalidateIsUserUnlockedCache();
@@ -1146,6 +1156,31 @@ class StorageManagerService extends IStorageManager.Stub
        }
    }

    // If vold knows that some users have their storage unlocked already (which
    // can happen after a "userspace reboot"), then add those users to
    // mLocalUnlockedUsers.  Do this right away and don't wait until
    // PHASE_BOOT_COMPLETED, since the system may unlock users before then.
    private void restoreLocalUnlockedUsers() {
        final int[] userIds;
        try {
            userIds = mVold.getUnlockedUsers();
        } catch (Exception e) {
            Slog.e(TAG, "Failed to get unlocked users from vold", e);
            return;
        }
        if (!ArrayUtils.isEmpty(userIds)) {
            Slog.d(TAG, "CE storage for users " + Arrays.toString(userIds)
                    + " is already unlocked");
            synchronized (mLock) {
                // Append rather than replace, just in case we're actually
                // reconnecting to vold after it crashed and was restarted, in
                // which case things will be the other way around --- we'll know
                // about the unlocked users but vold won't.
                mLocalUnlockedUsers.appendAll(userIds);
            }
        }
    }

    private void onUnlockUser(int userId) {
        Slog.d(TAG, "onUnlockUser " + userId);

@@ -1947,6 +1982,7 @@ class StorageManagerService extends IStorageManager.Stub
                connectVold();
            }, DateUtils.SECOND_IN_MILLIS);
        } else {
            restoreLocalUnlockedUsers();
            onDaemonConnected();
        }
    }
@@ -3233,19 +3269,24 @@ class StorageManagerService extends IStorageManager.Stub
                + " hasSecret: " + (secret != null));
        enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);

        if (isUserKeyUnlocked(userId)) {
            Slog.d(TAG, "User " + userId + "'s CE storage is already unlocked");
            return;
        }

        if (isFsEncrypted) {
            // When a user has secure lock screen, require secret to actually unlock.
            // This check is mostly in place for emulation mode.
            if (StorageManager.isFileEncryptedEmulatedOnly() &&
                mLockPatternUtils.isSecure(userId) && ArrayUtils.isEmpty(secret)) {
                throw new IllegalStateException("Secret required to unlock secure user " + userId);
            // When a user has a secure lock screen, a secret is required to
            // unlock the key, so don't bother trying to unlock it without one.
            // This prevents misleading error messages from being logged.  This
            // is also needed for emulated FBE to behave like native FBE.
            if (mLockPatternUtils.isSecure(userId) && ArrayUtils.isEmpty(secret)) {
                Slog.d(TAG, "Not unlocking user " + userId
                        + "'s CE storage yet because a secret is needed");
                return;
            }
            try {
                mVold.unlockUserKey(userId, serialNumber, encodeBytes(token),
                        encodeBytes(secret));
            } catch (ServiceSpecificException sse) {
                Slog.d(TAG, "Expected if the user has not unlocked the device.", sse);
                return;
            } catch (Exception e) {
                Slog.wtf(TAG, e);
                return;
@@ -3267,6 +3308,11 @@ class StorageManagerService extends IStorageManager.Stub

        enforcePermission(android.Manifest.permission.STORAGE_INTERNAL);

        if (!isUserKeyUnlocked(userId)) {
            Slog.d(TAG, "User " + userId + "'s CE storage is already locked");
            return;
        }

        try {
            mVold.lockUserKey(userId);
        } catch (Exception e) {