Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7b2c863f authored by Nate Myren's avatar Nate Myren
Browse files

Do not kill backup apps during restore for POST_NOTIFICATIONS

If the POST_NOTIFICATIONS permission is denied, and the app has the
BACKUP permission, and the device is restoring a backup, do not kill the
app, to avoid issues with restore.

Fixes: 230758495
Test: manual
Change-Id: Ia97c4ffd9653df5456199eff255771f423fb93e4
parent 4220511d
Loading
Loading
Loading
Loading
+43 −8
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import static android.content.pm.PackageManager.FLAG_PERMISSION_WHITELIST_SYSTEM
import static android.content.pm.PackageManager.FLAG_PERMISSION_WHITELIST_UPGRADE;
import static android.content.pm.PackageManager.MASK_PERMISSION_FLAGS_ALL;
import static android.content.pm.PackageManager.MATCH_DEBUG_TRIAGED_MISSING;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Process.INVALID_UID;
import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
import static android.permission.PermissionManager.KILL_APP_REASON_GIDS_CHANGED;
@@ -97,6 +98,7 @@ import android.os.storage.StorageManager;
import android.permission.IOnPermissionsChangeListener;
import android.permission.PermissionControllerManager;
import android.permission.PermissionManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -333,7 +335,8 @@ public class PermissionManagerServiceImpl implements PermissionManagerServiceInt
            mPackageManagerInt.writeSettings(true);
        }
        @Override
        public void onPermissionRevoked(int uid, int userId, String reason, boolean overrideKill) {
        public void onPermissionRevoked(int uid, int userId, String reason, boolean overrideKill,
                @Nullable String permissionName) {
            mOnPermissionChangeListeners.onPermissionsChanged(uid);

            // Critical; after this call the application should never have the permission
@@ -342,13 +345,41 @@ public class PermissionManagerServiceImpl implements PermissionManagerServiceInt
                return;
            }

            mHandler.post(() -> {
                if (POST_NOTIFICATIONS.equals(permissionName)
                        && isAppBackupAndRestoreRunning(uid)) {
                    return;
                }

                final int appId = UserHandle.getAppId(uid);
                if (reason == null) {
                mHandler.post(() -> killUid(appId, userId, KILL_APP_REASON_PERMISSIONS_REVOKED));
                    killUid(appId, userId, KILL_APP_REASON_PERMISSIONS_REVOKED);
                } else {
                mHandler.post(() -> killUid(appId, userId, reason));
                    killUid(appId, userId, reason);
                }
            });
        }

        private boolean isAppBackupAndRestoreRunning(int uid) {
            if (checkUidPermission(uid, Manifest.permission.BACKUP) != PERMISSION_GRANTED) {
                return false;
            }

            try {
                int userId = UserHandle.getUserId(uid);
                boolean isInSetup = Settings.Secure.getIntForUser(mContext.getContentResolver(),
                        Settings.Secure.USER_SETUP_COMPLETE, userId) == 0;
                boolean isInDeferredSetup = Settings.Secure.getIntForUser(
                        mContext.getContentResolver(),
                        Settings.Secure.USER_SETUP_PERSONALIZATION_STATE, userId)
                        == Settings.Secure.USER_SETUP_PERSONALIZATION_STARTED;
                return isInSetup || isInDeferredSetup;
            } catch (Settings.SettingNotFoundException e) {
                Slog.w(LOG_TAG, "Failed to check if the user is in restore: " + e);
                return false;
            }
        }

        @Override
        public void onInstallPermissionRevoked() {
            mPackageManagerInt.writeSettings(true);
@@ -1600,7 +1631,7 @@ public class PermissionManagerServiceImpl implements PermissionManagerServiceInt
        if (callback != null) {
            if (isRuntimePermission) {
                callback.onPermissionRevoked(UserHandle.getUid(userId, pkg.getUid()), userId,
                        reason, overrideKill);
                        reason, overrideKill, permName);
            } else {
                mDefaultPermissionCallback.onInstallPermissionRevoked();
            }
@@ -5246,7 +5277,11 @@ public class PermissionManagerServiceImpl implements PermissionManagerServiceInt
            onPermissionRevoked(uid, userId, reason, false);
        }
        public void onPermissionRevoked(int uid, @UserIdInt int userId, String reason,
                boolean overrideKill) {}
                boolean overrideKill) {
            onPermissionRevoked(uid, userId, reason, false, null);
        }
        public void onPermissionRevoked(int uid, @UserIdInt int userId, String reason,
                boolean overrideKill, @Nullable String permissionName) {}
        public void onInstallPermissionRevoked() {}
        public void onPermissionUpdated(@UserIdInt int[] updatedUserIds, boolean sync) {}
        public void onPermissionUpdatedNotifyListener(@UserIdInt int[] updatedUserIds, boolean sync,