From 72704403aabf41e2807cc67388faafdf4457f924 Mon Sep 17 00:00:00 2001 From: Alex Kershaw Date: Sun, 29 Nov 2020 18:21:57 +0000 Subject: [PATCH] [RESTRICT AUTOMERGE] Make WPMS look for DOs and POs in the correct calling user Currently, it will always look in user 0 since it uses the DPM from mContext, which will always be from user 0 as WPMS is in the system server process. Extend DPMI to provide the necessary external helper API. This is preferable to just using createContextAsUser before getting the DPM instance since it avoids a second binding. Fixes: 144048540 Fixes: 172682826 Bug: 153995973 Bug: 174642338 Test: atest com.android.cts.devicepolicy.MixedManagedProfileOwnerTest#testSetWallpaper_disallowed Change-Id: I52b71000fac31ff6725ddded58206f69b263ae33 (cherry picked from commit 5b36ee3f1d2c60473f4c30161cca6a62cd9ea71f) --- .../admin/DevicePolicyManagerInternal.java | 7 +++++++ .../wallpaper/WallpaperManagerService.java | 10 +++++----- .../DevicePolicyManagerService.java | 20 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/core/java/android/app/admin/DevicePolicyManagerInternal.java b/core/java/android/app/admin/DevicePolicyManagerInternal.java index 62ac84b2b1e60..d24694faff930 100644 --- a/core/java/android/app/admin/DevicePolicyManagerInternal.java +++ b/core/java/android/app/admin/DevicePolicyManagerInternal.java @@ -16,6 +16,7 @@ package android.app.admin; +import android.annotation.Nullable; import android.annotation.UserIdInt; import android.content.ComponentName; import android.content.Intent; @@ -221,6 +222,7 @@ public abstract class DevicePolicyManagerInternal { /** * Returns the profile owner component for the given user, or {@code null} if there is not one. */ + @Nullable public abstract ComponentName getProfileOwnerAsUser(int userHandle); /** @@ -234,4 +236,9 @@ public abstract class DevicePolicyManagerInternal { * {@link #supportsResetOp(int)} is true. */ public abstract void resetOp(int op, String packageName, @UserIdInt int userId); + + /** + * Returns whether the given package is a device owner or a profile owner in the calling user. + */ + public abstract boolean isDeviceOrProfileOwnerInCallingUser(String packageName); } diff --git a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java index 1300e657333c7..aaf27d32452d5 100644 --- a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java +++ b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java @@ -39,7 +39,7 @@ import android.app.WallpaperColors; import android.app.WallpaperInfo; import android.app.WallpaperManager; import android.app.WallpaperManager.SetWallpaperFlags; -import android.app.admin.DevicePolicyManager; +import android.app.admin.DevicePolicyManagerInternal; import android.app.backup.WallpaperBackupHelper; import android.content.BroadcastReceiver; import android.content.ComponentName; @@ -2861,10 +2861,10 @@ public class WallpaperManagerService extends IWallpaperManager.Stub if (!uidMatchPackage) { return false; // callingPackage was faked. } - - // TODO(b/144048540): DPM needs to take into account the userId, not just the package. - final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); - if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) { + DevicePolicyManagerInternal devicePolicyManagerInternal = + LocalServices.getService(DevicePolicyManagerInternal.class); + if (devicePolicyManagerInternal != null && + devicePolicyManagerInternal.isDeviceOrProfileOwnerInCallingUser(callingPackage)) { return true; } final int callingUserId = UserHandle.getCallingUserId(); diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index b8a20ed4fe5b7..86e130577ea8f 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -12839,6 +12839,26 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { ? AppOpsManager.MODE_ALLOWED : AppOpsManager.opToDefaultMode(AppOpsManager.OP_INTERACT_ACROSS_PROFILES); } + + public boolean isDeviceOrProfileOwnerInCallingUser(String packageName) { + return isDeviceOwnerInCallingUser(packageName) + || isProfileOwnerInCallingUser(packageName); + } + + private boolean isDeviceOwnerInCallingUser(String packageName) { + final ComponentName deviceOwnerInCallingUser = + DevicePolicyManagerService.this.getDeviceOwnerComponent( + /* callingUserOnly= */ true); + return deviceOwnerInCallingUser != null + && packageName.equals(deviceOwnerInCallingUser.getPackageName()); + } + + private boolean isProfileOwnerInCallingUser(String packageName) { + final ComponentName profileOwnerInCallingUser = + getProfileOwnerAsUser(UserHandle.getCallingUserId()); + return profileOwnerInCallingUser != null + && packageName.equals(profileOwnerInCallingUser.getPackageName()); + } } private Intent createShowAdminSupportIntent(ComponentName admin, int userId) {