From ef881f533c9f0eff33728129f7c4d70abe56521a Mon Sep 17 00:00:00 2001 From: Lee Shombert Date: Mon, 16 Sep 2024 13:30:59 -0700 Subject: [PATCH] Rename legacy cache keys This CL renames legacy PropertyInvalidatedCache keys to match the new naming convention, which is "cache_key..", where the module is one of "system_server", "telephony", or "bluetooth". The rename is entirely in the framework. There is no backcompat issue. Invalid cache keys are logged, unless the flag is true, in which case a fatal exception is thrown. Tested with a special image that removed the SELinux policies that permitted the legacy property names to work. Booted the image and verified no selinux violations in logcat. This means no caches were using the legacy properties. Test: booted the image and verified that no caches with non-standard names existed in the system. Verification used the output of `dumpsys cacheinfo`. Flag: EXEMPT refactor Bug: 366552454 Change-Id: Icfc43980def04c8aefaa4097a38eabbc7ca3b054 --- .../app/ApplicationPackageManager.java | 5 +- .../android/app/PropertyInvalidatedCache.java | 48 ++++++++++++++++++- .../app/compat/ChangeIdStateCache.java | 4 +- .../display/DisplayManagerGlobal.java | 2 +- core/java/android/os/PowerManager.java | 5 +- .../android/permission/PermissionManager.java | 3 +- .../android/location/LocationManager.java | 2 +- 7 files changed, 60 insertions(+), 9 deletions(-) diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java index dbf9afdd52ff..ed6b85125e66 100644 --- a/core/java/android/app/ApplicationPackageManager.java +++ b/core/java/android/app/ApplicationPackageManager.java @@ -16,6 +16,7 @@ package android.app; +import static android.app.PropertyInvalidatedCache.createSystemCacheKey; import static android.app.admin.DevicePolicyResources.Drawables.Style.SOLID_COLORED; import static android.app.admin.DevicePolicyResources.Drawables.Style.SOLID_NOT_COLORED; import static android.app.admin.DevicePolicyResources.Drawables.WORK_PROFILE_ICON; @@ -817,7 +818,7 @@ public class ApplicationPackageManager extends PackageManager { private final static PropertyInvalidatedCache mHasSystemFeatureCache = new PropertyInvalidatedCache( - 256, "cache_key.has_system_feature") { + 256, createSystemCacheKey("has_system_feature")) { @Override public Boolean recompute(HasSystemFeatureQuery query) { try { @@ -1127,7 +1128,7 @@ public class ApplicationPackageManager extends PackageManager { } private static final String CACHE_KEY_PACKAGES_FOR_UID_PROPERTY = - "cache_key.get_packages_for_uid"; + createSystemCacheKey("get_packages_for_uid"); private static final PropertyInvalidatedCache mGetPackagesForUidCache = new PropertyInvalidatedCache( diff --git a/core/java/android/app/PropertyInvalidatedCache.java b/core/java/android/app/PropertyInvalidatedCache.java index 0c786cb20fdc..0e761fce9346 100644 --- a/core/java/android/app/PropertyInvalidatedCache.java +++ b/core/java/android/app/PropertyInvalidatedCache.java @@ -19,6 +19,7 @@ package android.app; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.TestApi; +import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.Message; @@ -282,6 +283,12 @@ public class PropertyInvalidatedCache { * @hide */ + /** + * The well-known key prefix. + * @hide + */ + private static final String CACHE_KEY_PREFIX = "cache_key"; + /** * The module used for unit tests and cts tests. It is expected that no process in * the system has permissions to write properties with this module. @@ -366,7 +373,44 @@ public class PropertyInvalidatedCache { } } - return "cache_key." + module + "." + new String(suffix); + return CACHE_KEY_PREFIX + "." + module + "." + new String(suffix); + } + + /** + * All legal keys start with one of the following strings. + */ + private static final String[] sValidKeyPrefix = { + CACHE_KEY_PREFIX + "." + MODULE_SYSTEM + ".", + CACHE_KEY_PREFIX + "." + MODULE_BLUETOOTH + ".", + CACHE_KEY_PREFIX + "." + MODULE_TELEPHONY + ".", + CACHE_KEY_PREFIX + "." + MODULE_TEST + ".", + }; + + /** + * Verify that the property name conforms to the standard. Log a warning if this is not true. + * Note that this is done once in the cache constructor; it does not have to be very fast. + */ + private void validateCacheKey(String name) { + if (Build.IS_USER) { + // Do not bother checking keys in user builds. The keys will have been tested in + // eng/userdebug builds already. + return; + } + for (int i = 0; i < sValidKeyPrefix.length; i++) { + if (name.startsWith(sValidKeyPrefix[i])) return; + } + Log.w(TAG, "invalid cache name: " + name); + } + + /** + * Create a cache key for the system module. The parameter is the API name. This reduces + * some of the boilerplate in system caches. It is not needed in other modules because other + * modules must use the {@link IpcDataCache} interfaces. + * @hide + */ + @NonNull + public static String createSystemCacheKey(@NonNull String api) { + return createPropertyName(MODULE_SYSTEM, api); } /** @@ -561,6 +605,7 @@ public class PropertyInvalidatedCache { public PropertyInvalidatedCache(int maxEntries, @NonNull String propertyName, @NonNull String cacheName) { mPropertyName = propertyName; + validateCacheKey(mPropertyName); mCacheName = cacheName; mMaxEntries = maxEntries; mComputer = new DefaultComputer<>(this); @@ -584,6 +629,7 @@ public class PropertyInvalidatedCache { public PropertyInvalidatedCache(int maxEntries, @NonNull String module, @NonNull String api, @NonNull String cacheName, @NonNull QueryHandler computer) { mPropertyName = createPropertyName(module, api); + validateCacheKey(mPropertyName); mCacheName = cacheName; mMaxEntries = maxEntries; mComputer = computer; diff --git a/core/java/android/app/compat/ChangeIdStateCache.java b/core/java/android/app/compat/ChangeIdStateCache.java index 7948cec545c3..db663f8ed4c4 100644 --- a/core/java/android/app/compat/ChangeIdStateCache.java +++ b/core/java/android/app/compat/ChangeIdStateCache.java @@ -16,6 +16,8 @@ package android.app.compat; +import static android.app.PropertyInvalidatedCache.createSystemCacheKey; + import android.annotation.NonNull; import android.app.PropertyInvalidatedCache; import android.content.Context; @@ -31,7 +33,7 @@ import com.android.internal.compat.IPlatformCompat; */ public final class ChangeIdStateCache extends PropertyInvalidatedCache { - private static final String CACHE_KEY = "cache_key.is_compat_change_enabled"; + private static final String CACHE_KEY = createSystemCacheKey("is_compat_change_enabled"); private static final int MAX_ENTRIES = 2048; private static boolean sDisabled = false; private volatile IPlatformCompat mPlatformCompat; diff --git a/core/java/android/hardware/display/DisplayManagerGlobal.java b/core/java/android/hardware/display/DisplayManagerGlobal.java index 9612a53be96e..7185719abdd5 100644 --- a/core/java/android/hardware/display/DisplayManagerGlobal.java +++ b/core/java/android/hardware/display/DisplayManagerGlobal.java @@ -1445,7 +1445,7 @@ public final class DisplayManagerGlobal { * system's display configuration. */ public static final String CACHE_KEY_DISPLAY_INFO_PROPERTY = - "cache_key.display_info"; + PropertyInvalidatedCache.createSystemCacheKey("display_info"); /** * Invalidates the contents of the display info cache for all applications. Can only diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index 026013c34e30..e4c12b6ff834 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -1144,9 +1144,10 @@ public final class PowerManager { } private static final String CACHE_KEY_IS_POWER_SAVE_MODE_PROPERTY = - "cache_key.is_power_save_mode"; + PropertyInvalidatedCache.createSystemCacheKey("is_power_save_mode"); - private static final String CACHE_KEY_IS_INTERACTIVE_PROPERTY = "cache_key.is_interactive"; + private static final String CACHE_KEY_IS_INTERACTIVE_PROPERTY = + PropertyInvalidatedCache.createSystemCacheKey("is_interactive"); private static final int MAX_CACHE_ENTRIES = 1; diff --git a/core/java/android/permission/PermissionManager.java b/core/java/android/permission/PermissionManager.java index 7e51cb020196..e98397d104d6 100644 --- a/core/java/android/permission/PermissionManager.java +++ b/core/java/android/permission/PermissionManager.java @@ -1796,7 +1796,8 @@ public final class PermissionManager { } /** @hide */ - public static final String CACHE_KEY_PACKAGE_INFO = "cache_key.package_info"; + public static final String CACHE_KEY_PACKAGE_INFO = + PropertyInvalidatedCache.createSystemCacheKey("package_info"); /** @hide */ private static final PropertyInvalidatedCache sPermissionCache = diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java index 63424892ec72..306643d8477e 100644 --- a/location/java/android/location/LocationManager.java +++ b/location/java/android/location/LocationManager.java @@ -451,7 +451,7 @@ public class LocationManager { private static final long MAX_SINGLE_LOCATION_TIMEOUT_MS = 30 * 1000; private static final String CACHE_KEY_LOCATION_ENABLED_PROPERTY = - "cache_key.location_enabled"; + PropertyInvalidatedCache.createSystemCacheKey("location_enabled"); static ILocationManager getService() throws RemoteException { try {