Preserve flags for non-runtime permissions upon package update.

PermissionManagerServiceImpl.restorePermissionState() creates a new
UID permission state for non-shared-UID packages that have been
updated (i.e. replaced), however the existing logic for non-runtime
permission never carried over the flags from the old state. This
wasn't an issue for much older platforms because permission flags
weren't used for non-runtime permissions, however since we are
starting to use them for role protected permissions (ROLE_GRANTED) and
app op permissions (USER_SET), we do need to preserver the permission
flags.

This change merges the logic for granting and revoking a non-runtime
permission in restorePermissionState() into a single if branch, and
appends the logic to copy the flag from the old state in that branch.

Bug: 283006437
Test: PermissionFlagsTest#nonRuntimePermissionFlagsPreservedAfterReinstall
Change-Id: Iea3c66710e7d28c6fc730b1939da64f1172b08db
Merged-In: Iea3c66710e7d28c6fc730b1939da64f1172b08db
This commit is contained in:
Hai Zhang
2023-05-17 01:30:20 -07:00
parent 3059d911ec
commit 0e1ebd84e2

View File

@@ -2882,29 +2882,55 @@ public class PermissionManagerService extends IPermissionManager.Stub {
+ pkg.getPackageName()); + pkg.getPackageName());
} }
if ((bp.isNormal() && shouldGrantNormalPermission) if (bp.isNormal() || bp.isSignature() || bp.isInternal()) {
|| (bp.isSignature() if ((bp.isNormal() && shouldGrantNormalPermission)
&& (!bp.isPrivileged() || CollectionUtils.contains( || (bp.isSignature()
isPrivilegedPermissionAllowlisted, permName)) && (!bp.isPrivileged() || CollectionUtils.contains(
&& (CollectionUtils.contains(shouldGrantSignaturePermission, isPrivilegedPermissionAllowlisted, permName))
permName) && (CollectionUtils.contains(shouldGrantSignaturePermission,
|| (((bp.isPrivileged() && CollectionUtils.contains( permName)
shouldGrantPrivilegedPermissionIfWasGranted, || (((bp.isPrivileged() && CollectionUtils.contains(
permName)) || bp.isDevelopment() || bp.isRole()) shouldGrantPrivilegedPermissionIfWasGranted,
&& origState.isPermissionGranted(permName)))) permName)) || bp.isDevelopment()
|| (bp.isInternal() || bp.isRole())
&& (!bp.isPrivileged() || CollectionUtils.contains( && origState.isPermissionGranted(
isPrivilegedPermissionAllowlisted, permName)) permName))))
&& (CollectionUtils.contains(shouldGrantInternalPermission, || (bp.isInternal()
permName) && (!bp.isPrivileged() || CollectionUtils.contains(
|| (((bp.isPrivileged() && CollectionUtils.contains( isPrivilegedPermissionAllowlisted, permName))
shouldGrantPrivilegedPermissionIfWasGranted, && (CollectionUtils.contains(shouldGrantInternalPermission,
permName)) || bp.isDevelopment() || bp.isRole()) permName)
&& origState.isPermissionGranted(permName))))) { || (((bp.isPrivileged() && CollectionUtils.contains(
// Grant an install permission. shouldGrantPrivilegedPermissionIfWasGranted,
if (uidState.grantPermission(bp)) { permName)) || bp.isDevelopment()
changedInstallPermission = true; || bp.isRole())
&& origState.isPermissionGranted(
permName))))) {
// Grant an install permission.
if (uidState.grantPermission(bp)) {
changedInstallPermission = true;
}
} else {
if (DEBUG_PERMISSIONS) {
boolean wasGranted = uidState.isPermissionGranted(bp.getName());
if (wasGranted || bp.isAppOp()) {
Slog.i(TAG, (wasGranted ? "Un-granting" : "Not granting")
+ " permission " + perm
+ " from package " + friendlyName
+ " (protectionLevel=" + bp.getProtectionLevel()
+ " flags=0x"
+ Integer.toHexString(PackageInfoUtils.appInfoFlags(pkg,
ps))
+ ")");
}
}
if (uidState.revokePermission(bp)) {
changedInstallPermission = true;
}
} }
PermissionState origPermState = origState.getPermissionState(perm);
int flags = origPermState != null ? origPermState.getFlags() : 0;
uidState.updatePermissionFlags(bp, MASK_PERMISSION_FLAGS_ALL, flags);
} else if (bp.isRuntime()) { } else if (bp.isRuntime()) {
boolean hardRestricted = bp.isHardRestricted(); boolean hardRestricted = bp.isHardRestricted();
boolean softRestricted = bp.isSoftRestricted(); boolean softRestricted = bp.isSoftRestricted();
@@ -3018,22 +3044,8 @@ public class PermissionManagerService extends IPermissionManager.Stub {
uidState.updatePermissionFlags(bp, MASK_PERMISSION_FLAGS_ALL, uidState.updatePermissionFlags(bp, MASK_PERMISSION_FLAGS_ALL,
flags); flags);
} else { } else {
if (DEBUG_PERMISSIONS) { Slog.wtf(LOG_TAG, "Unknown permission protection " + bp.getProtection()
boolean wasGranted = uidState.isPermissionGranted(bp.getName()); + " for permission " + bp.getName());
if (wasGranted || bp.isAppOp()) {
Slog.i(TAG, (wasGranted ? "Un-granting" : "Not granting")
+ " permission " + perm
+ " from package " + friendlyName
+ " (protectionLevel=" + bp.getProtectionLevel()
+ " flags=0x"
+ Integer.toHexString(PackageInfoUtils.appInfoFlags(pkg,
ps))
+ ")");
}
}
if (uidState.removePermissionState(bp.getName())) {
changedInstallPermission = true;
}
} }
} }