From 189b1157bd751e9fa01e4084c91f703c8f4af642 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Tue, 4 Apr 2023 13:25:34 +0100 Subject: [PATCH] Truncate profile name in setProfileName api Bug: 259942964 Test: NA (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:537fbbb45c919ba9a33f9eb775274c7996d5a466) Merged-In: Ia83167c9683c9aec7b38cdc6e464c32795af3db9 Change-Id: Ia83167c9683c9aec7b38cdc6e464c32795af3db9 --- core/java/android/app/admin/DevicePolicyManager.java | 3 ++- .../server/devicepolicy/DevicePolicyManagerService.java | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 348a8f9f3c805..3ef89b8ca0ada 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -9188,7 +9188,8 @@ public class DevicePolicyManager { * @see #isProfileOwnerApp * @see #isDeviceOwnerApp * @param admin Which {@link DeviceAdminReceiver} this request is associate with. - * @param profileName The name of the profile. + * @param profileName The name of the profile. If the name is longer than 200 characters + * it will be truncated. * @throws SecurityException if {@code admin} is not a device or profile owner. */ public void setProfileName(@NonNull ComponentName admin, String profileName) { diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 7bca80ae56806..27d5eb4541e5c 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -439,6 +439,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { private static final int REQUEST_PROFILE_OFF_DEADLINE = 5572; + private static final int MAX_PROFILE_NAME_LENGTH = 200; + private static final long MS_PER_DAY = TimeUnit.DAYS.toMillis(1); private static final long EXPIRATION_GRACE_PERIOD_MS = 5 * MS_PER_DAY; // 5 days, in ms @@ -9206,8 +9208,10 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { Preconditions.checkCallAuthorization( isDefaultDeviceOwner(caller) || isProfileOwner(caller)); + final String truncatedProfileName = + profileName.substring(0, Math.min(profileName.length(), MAX_PROFILE_NAME_LENGTH)); mInjector.binderWithCleanCallingIdentity(() -> { - mUserManager.setUserName(caller.getUserId(), profileName); + mUserManager.setUserName(caller.getUserId(), truncatedProfileName); DevicePolicyEventLogger .createEvent(DevicePolicyEnums.SET_PROFILE_NAME) .setAdmin(caller.getComponentName())