From 19a569ab3d060610a9d252e23f12dc8e30195521 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Wed, 22 May 2019 08:54:58 -0700 Subject: [PATCH] Ensure packages in shared user are valid Package information is tracked by several different structures and updated in different synchronized blocks. As such, the set of packages that are part of a shared user are updated after the set of installed packages is updated. And, in some cases, a removed package will still be listed as a member of a shared user id. Before passing a package to the PermissionManager to check for permission grants, ensure the package actually exists and is known to the system. Bug: 111075456 Test: Manual. Builds and runs. Change-Id: Ibb4f44f50141d43bf2367747d6d5813648d42fd4 --- .../server/pm/PackageManagerService.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index e07a132844571..ac6cb72d73650 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -5646,9 +5646,11 @@ public class PackageManagerService extends IPackageManager.Stub private int checkUidPermissionImpl(String permName, int uid) { synchronized (mPackages) { final String[] packageNames = getPackagesForUid(uid); - final PackageParser.Package pkg = (packageNames != null && packageNames.length > 0) - ? mPackages.get(packageNames[0]) - : null; + PackageParser.Package pkg = null; + final int N = packageNames == null ? 0 : packageNames.length; + for (int i = 0; pkg == null && i < N; i++) { + pkg = mPackages.get(packageNames[i]); + } // Additional logs for b/111075456; ignore system UIDs if (pkg == null && UserHandle.getAppId(uid) >= Process.FIRST_APPLICATION_UID) { if (packageNames == null || packageNames.length < 2) { @@ -6385,6 +6387,16 @@ public class PackageManagerService extends IPackageManager.Stub } } + /** + * IMPORTANT: Not all packages returned by this method may be known + * to the system. There are two conditions in which this may occur: + *
    + *
  1. The package is on adoptable storage and the device has been removed
  2. + *
  3. The package is being removed and the internal structures are partially updated
  4. + *
+ * The second is an artifact of the current data structures and should be fixed. See + * b/111075456 for one such instance. + */ @Override public String[] getPackagesForUid(int uid) { return getPackagesForUid_debug(uid, false);