Skip to content

Commit 6c7af8f

Browse files
committed
Avoid auto-granting special permissions when existing state is present
From PermissionService's point of view, installing a new app and restoring an archived app are the same. The only difference is in the PackageInstalledParams.mPermissionStates map: * For a new app install, the INTERNET permission is present in the map and its value corresponds to what the user picked for the network toggle in the installation dialog. * For an archived app restore, the map is always empty. Any permission managed by SpecialRuntimePermUtils, but was not included in the installation request, would be set to the default auto-grant value (eg. global default for OTHER_SENSORS). This is fine for new app installs, but when restoring an app, this would overwrite the existing permission state from when the app was originally archived. This commit fixes the issue by skipping the auto-grant logic when the permission is already present in state. There is no change in behavior for new app installs since that starts off with no state. Fixes: GrapheneOS/os-issue-tracker#5256 Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
1 parent 8edf282 commit 6c7af8f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

services/permission/java/com/android/server/permission/access/permission/PermissionService.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -2443,8 +2443,22 @@ class PermissionService(private val service: AccessCheckingService) :
24432443
)
24442444
val permissionStates = ArrayMap(params.permissionStates)
24452445
if (params.isNewlyInstalledInUserId(userId)) {
2446+
// Despite the method name, this is reached both during initial app installation and
2447+
// when archived apps are restored. For the latter scenario, avoid auto-granting
2448+
// permissions if the permissions already exist in the persisted permissions state
2449+
// since it would overwrite the user's previously set preferences.
24462450
SpecialRuntimePermUtils.getAll().forEach { perm ->
2447-
if (!permissionStates.contains(perm)) {
2451+
val oldFlags = service.getState {
2452+
getPermissionFlagsWithPolicy(
2453+
packageState.appId,
2454+
userId,
2455+
perm,
2456+
// Same as setRequestedPermissionStates() below.
2457+
VirtualDeviceManager.PERSISTENT_DEVICE_ID_DEFAULT,
2458+
)
2459+
}
2460+
2461+
if (!permissionStates.contains(perm) && !oldFlags.hasBits(PermissionFlags.USER_SET)) {
24482462
if (SpecialRuntimePermUtils.shouldAutoGrant(context, androidPackage.packageName, userId, perm)) {
24492463
permissionStates.set(perm, PackageInstaller.SessionParams.PERMISSION_STATE_GRANTED)
24502464
}

0 commit comments

Comments
 (0)