From f4710f6f95f748789b37f932204012be5738ecb7 Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Mon, 26 Apr 2021 15:34:59 -0700 Subject: [PATCH] Fixed DPMS.listForegroundAffiliatedUsers(). It was not properly clearing the caller's identity, so it would fail "in real life" when the DPC doesn't have INTERACT_ACROSS_USERS (but it was passing on CTS because the test app had it). Test: atest com.android.cts.devicepolicy.DeviceOwnerTest#testListForegroundAffiliatedUsers_onlyForegroundUserCalledByDeviceOwner # on automotive Fixes: 186455856 Change-Id: I2924f5cdf90e767c4f2ae534788baa5c448126c5 --- .../DevicePolicyManagerService.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 64cdfa65f9c56..585cbdc304d60 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -8883,17 +8883,17 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } @Override - public ComponentName getProfileOwnerAsUser(int userHandle) { + public ComponentName getProfileOwnerAsUser(int userId) { if (!mHasFeature) { return null; } - Preconditions.checkArgumentNonnegative(userHandle, "Invalid userId"); + Preconditions.checkArgumentNonnegative(userId, "Invalid userId"); - final CallerIdentity caller = getCallerIdentity(); - Preconditions.checkCallAuthorization(hasCrossUsersPermission(caller, userHandle)); + CallerIdentity caller = getCallerIdentity(); + Preconditions.checkCallAuthorization(hasCrossUsersPermission(caller, userId)); synchronized (getLockObject()) { - return mOwners.getProfileOwnerComponent(userHandle); + return mOwners.getProfileOwnerComponent(userId); } } @@ -9345,19 +9345,20 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { public List listForegroundAffiliatedUsers() { checkIsDeviceOwner(getCallerIdentity()); - int userId = mInjector.binderWithCleanCallingIdentity(() -> getCurrentForegroundUserId()); + return mInjector.binderWithCleanCallingIdentity(() -> { + int userId = getCurrentForegroundUserId(); + boolean isAffiliated; + synchronized (getLockObject()) { + isAffiliated = isUserAffiliatedWithDeviceLocked(userId); + } - boolean isAffiliated; - synchronized (getLockObject()) { - isAffiliated = isUserAffiliatedWithDeviceLocked(userId); - } + if (!isAffiliated) return Collections.emptyList(); - if (!isAffiliated) return Collections.emptyList(); + List users = new ArrayList<>(1); + users.add(UserHandle.of(userId)); - List users = new ArrayList<>(1); - users.add(UserHandle.of(userId)); - - return users; + return users; + }); } protected int getProfileParentId(int userHandle) { @@ -14111,7 +14112,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } } - private boolean isUserAffiliatedWithDeviceLocked(int userId) { + private boolean isUserAffiliatedWithDeviceLocked(@UserIdInt int userId) { if (!mOwners.hasDeviceOwner()) { return false; }