DO NOT MERGE Chk QUERY_ALL_PACKAGES when resolving
When resolving intents, check whether QUERY_ALL_PACKAGES permission was
granted, as well as whether the caller is a system process, to determine
whether filtering may occur.
This allows prefered activity records to be deleted by non-system
processes if they have QUERY_ALL_PACKAGES permission.
Before this change, configuring a custom activity resolver doesn't work
properly because it's not allowed to delete the record of the last
chosen app before writing a new record.
Test: Create a custom activity resolver, choose an app just once, choose
another app next time, verify it defaults to the other app after
that.
Bug: 173543124
Change-Id: If4dfea0374352b5e903d15e87e90baf012abf40b
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 40723e0..b54706e 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -20,6 +20,7 @@
import static android.Manifest.permission.INSTALL_PACKAGES;
import static android.Manifest.permission.MANAGE_DEVICE_ADMINS;
import static android.Manifest.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS;
+import static android.Manifest.permission.QUERY_ALL_PACKAGES;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.REQUEST_DELETE_PACKAGES;
import static android.Manifest.permission.SET_HARMFUL_APP_WARNINGS;
@@ -28,6 +29,7 @@
import static android.app.AppOpsManager.MODE_DEFAULT;
import static android.app.AppOpsManager.MODE_IGNORED;
import static android.content.Intent.ACTION_MAIN;
+import static android.content.Intent.CATEGORY_BROWSABLE;
import static android.content.Intent.CATEGORY_DEFAULT;
import static android.content.Intent.CATEGORY_HOME;
import static android.content.Intent.EXTRA_LONG_VERSION_CODE;
@@ -35,8 +37,6 @@
import static android.content.Intent.EXTRA_VERSION_CODE;
import static android.content.pm.PackageManager.CERT_INPUT_RAW_X509;
import static android.content.pm.PackageManager.CERT_INPUT_SHA256;
-import static android.content.Intent.CATEGORY_BROWSABLE;
-import static android.content.Intent.CATEGORY_DEFAULT;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
@@ -6456,14 +6456,10 @@
true /*allowDynamicSplits*/);
Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
- final boolean queryMayBeFiltered =
- UserHandle.getAppId(filterCallingUid) >= Process.FIRST_APPLICATION_UID
- && !resolveForStart;
-
final ResolveInfo bestChoice =
chooseBestActivity(
intent, resolvedType, flags, privateResolveFlags, query, userId,
- queryMayBeFiltered);
+ queryMayBeFiltered(filterCallingUid, resolveForStart));
final boolean nonBrowserOnly =
(privateResolveFlags & PackageManagerInternal.RESOLVE_NON_BROWSER_ONLY) != 0;
if (nonBrowserOnly && bestChoice != null && bestChoice.handleAllWebDataURI) {
@@ -6475,6 +6471,25 @@
}
}
+ /**
+ * Returns whether the query may be filtered to packages which are visible to the caller.
+ * Filtering occurs except in the following cases:
+ * <ul>
+ * <li>system processes
+ * <li>applications granted {@link android.Manifest.permission#QUERY_ALL_PACKAGES}
+ * <li>when querying to start an app
+ * </ul>
+ *
+ * @param filterCallingUid the UID of the calling application
+ * @param queryForStart whether query is to start an app
+ * @return whether filtering may occur
+ */
+ private boolean queryMayBeFiltered(int filterCallingUid, boolean queryForStart) {
+ return UserHandle.getAppId(filterCallingUid) >= Process.FIRST_APPLICATION_UID
+ && checkUidPermission(QUERY_ALL_PACKAGES, filterCallingUid) != PERMISSION_GRANTED
+ && !queryForStart;
+ }
+
@Override
public ResolveInfo findPersistentPreferredActivity(Intent intent, int userId) {
if (!UserHandle.isSameApp(Binder.getCallingUid(), Process.SYSTEM_UID)) {
@@ -6840,7 +6855,7 @@
boolean removeMatches, boolean debug, int userId) {
return findPreferredActivityNotLocked(
intent, resolvedType, flags, query, priority, always, removeMatches, debug, userId,
- UserHandle.getAppId(Binder.getCallingUid()) >= Process.FIRST_APPLICATION_UID);
+ queryMayBeFiltered(Binder.getCallingUid(), /* queryForStart= */ false));
}
// TODO: handle preferred activities missing while user has amnesia