diff --git a/services/core/java/com/android/server/pm/Computer.java b/services/core/java/com/android/server/pm/Computer.java index 1bce0cd2e4bae..4ab58b7947e26 100644 --- a/services/core/java/com/android/server/pm/Computer.java +++ b/services/core/java/com/android/server/pm/Computer.java @@ -215,6 +215,9 @@ public interface Computer extends PackageDataSnapshot { boolean isInstantApp(String packageName, int userId); boolean isInstantAppInternal(String packageName, @UserIdInt int userId, int callingUid); boolean isSameProfileGroup(@UserIdInt int callerUserId, @UserIdInt int userId); + boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid, + @Nullable ComponentName component, @PackageManager.ComponentType int componentType, + int userId, boolean filterUninstall); boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid, @Nullable ComponentName component, @PackageManager.ComponentType int componentType, int userId); @@ -222,6 +225,8 @@ public interface Computer extends PackageDataSnapshot { int userId); boolean shouldFilterApplication(@NonNull SharedUserSetting sus, int callingUid, int userId); + boolean shouldFilterApplicationIncludingUninstalled(@Nullable PackageStateInternal ps, + int callingUid, int userId); int checkUidPermission(String permName, int uid); int getPackageUidInternal(String packageName, long flags, int userId, int callingUid); long updateFlagsForApplication(long flags, int userId); diff --git a/services/core/java/com/android/server/pm/ComputerEngine.java b/services/core/java/com/android/server/pm/ComputerEngine.java index ac4fea1a83d55..2ed2a35745f45 100644 --- a/services/core/java/com/android/server/pm/ComputerEngine.java +++ b/services/core/java/com/android/server/pm/ComputerEngine.java @@ -2677,7 +2677,7 @@ public class ComputerEngine implements Computer { */ public final boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid, @Nullable ComponentName component, - @PackageManager.ComponentType int componentType, int userId) { + @PackageManager.ComponentType int componentType, int userId, boolean filterUninstall) { if (Process.isSdkSandboxUid(callingUid)) { int clientAppUid = Process.getAppUidForSdkSandboxUid(callingUid); // SDK sandbox should be able to see it's client app @@ -2691,9 +2691,11 @@ public class ComputerEngine implements Computer { } final String instantAppPkgName = getInstantAppPackageName(callingUid); final boolean callerIsInstantApp = instantAppPkgName != null; - if (ps == null) { - // pretend the application exists, but, needs to be filtered - return callerIsInstantApp; + if (ps == null + || (filterUninstall && !ps.getUserStateOrDefault(userId).isInstalled())) { + // If caller is instant app and ps is null, pretend the application exists, + // but, needs to be filtered + return (callerIsInstantApp || filterUninstall); } // if the target and caller are the same application, don't filter if (isCallerSameApp(ps.getPackageName(), callingUid)) { @@ -2738,15 +2740,26 @@ public class ComputerEngine implements Computer { } /** - * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int) + * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int, boolean) */ - public final boolean shouldFilterApplication( - @Nullable PackageStateInternal ps, int callingUid, int userId) { - return shouldFilterApplication(ps, callingUid, null, TYPE_UNKNOWN, userId); + public final boolean shouldFilterApplication(@Nullable PackageStateInternal ps, + int callingUid, @Nullable ComponentName component, + @PackageManager.ComponentType int componentType, int userId) { + return shouldFilterApplication( + ps, callingUid, component, componentType, userId, false /* filterUninstall */); } /** - * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int) + * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int, boolean) + */ + public final boolean shouldFilterApplication( + @Nullable PackageStateInternal ps, int callingUid, int userId) { + return shouldFilterApplication( + ps, callingUid, null, TYPE_UNKNOWN, userId, false /* filterUninstall */); + } + + /** + * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int, boolean) */ public final boolean shouldFilterApplication(@NonNull SharedUserSetting sus, int callingUid, int userId) { @@ -2754,12 +2767,21 @@ public class ComputerEngine implements Computer { final ArraySet packageStates = (ArraySet) sus.getPackageStates(); for (int index = packageStates.size() - 1; index >= 0 && filterApp; index--) { - filterApp &= shouldFilterApplication(packageStates.valueAt(index), - callingUid, /* component */ null, TYPE_UNKNOWN, userId); + filterApp &= shouldFilterApplication(packageStates.valueAt(index), callingUid, + null /* component */, TYPE_UNKNOWN, userId, false /* filterUninstall */); } return filterApp; } + /** + * @see #shouldFilterApplication(PackageStateInternal, int, ComponentName, int, int, boolean) + */ + public final boolean shouldFilterApplicationIncludingUninstalled( + @Nullable PackageStateInternal ps, int callingUid, int userId) { + return shouldFilterApplication( + ps, callingUid, null, TYPE_UNKNOWN, userId, true /* filterUninstall */); + } + /** * Verification statuses are ordered from the worse to the best, except for * INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER, which is the worse. @@ -5264,9 +5286,10 @@ public class ComputerEngine implements Computer { return false; } final AndroidPackage pkg = mPackages.get(packageName); + final int callingUserId = UserHandle.getUserId(callingUid); if (pkg == null - || shouldFilterApplication(getPackageStateInternal(pkg.getPackageName()), - callingUid, UserHandle.getUserId(callingUid))) { + || shouldFilterApplicationIncludingUninstalled( + getPackageStateInternal(pkg.getPackageName()), callingUid, callingUserId)) { Slog.w(TAG, "KeySet requested for unknown package: " + packageName); throw new IllegalArgumentException("Unknown package: " + packageName); } @@ -5288,9 +5311,10 @@ public class ComputerEngine implements Computer { return false; } final AndroidPackage pkg = mPackages.get(packageName); + final int callingUserId = UserHandle.getUserId(callingUid); if (pkg == null - || shouldFilterApplication(getPackageStateInternal(pkg.getPackageName()), - callingUid, UserHandle.getUserId(callingUid))) { + || shouldFilterApplicationIncludingUninstalled( + getPackageStateInternal(pkg.getPackageName()), callingUid, callingUserId)) { Slog.w(TAG, "KeySet requested for unknown package: " + packageName); throw new IllegalArgumentException("Unknown package: " + packageName); } diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index 5f0ad13d09030..c4290287bcfb3 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -6636,9 +6636,8 @@ public class PackageManagerService implements PackageSender, TestUtilityService @UserIdInt int userId) { PackageStateInternal packageState = computer.getPackageStateInternal(packageName, callingUid); - if (packageState == null - || computer.shouldFilterApplication(packageState, callingUid, userId) - || !packageState.getUserStateOrDefault(userId).isInstalled()) { + if (computer.shouldFilterApplicationIncludingUninstalled( + packageState, callingUid, userId)) { return null; } else { return packageState; diff --git a/services/tests/PackageManagerServiceTests/appenumeration/Android.bp b/services/tests/PackageManagerServiceTests/appenumeration/Android.bp index 479ef8e5f1880..cdb6324988bea 100644 --- a/services/tests/PackageManagerServiceTests/appenumeration/Android.bp +++ b/services/tests/PackageManagerServiceTests/appenumeration/Android.bp @@ -30,6 +30,7 @@ android_test { "compatibility-device-util-axt", "androidx.test.runner", "truth-prebuilt", + "Harrier", ], platform_apis: true, test_suites: ["device-tests"], diff --git a/services/tests/PackageManagerServiceTests/appenumeration/AndroidManifest.xml b/services/tests/PackageManagerServiceTests/appenumeration/AndroidManifest.xml index 2039aaa20945c..0395aa8a1db62 100644 --- a/services/tests/PackageManagerServiceTests/appenumeration/AndroidManifest.xml +++ b/services/tests/PackageManagerServiceTests/appenumeration/AndroidManifest.xml @@ -16,7 +16,15 @@ --> + xmlns:tools="http://schemas.android.com/tools" + package="com.android.server.pm.test.appenumeration"> + + + + + + +