Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.rollback; |
| 18 | |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 19 | import android.content.pm.PackageManager; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 20 | import android.content.rollback.PackageRollbackInfo; |
| 21 | import android.content.rollback.PackageRollbackInfo.RestoreInfo; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 22 | import android.os.storage.StorageManager; |
| 23 | import android.util.IntArray; |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 24 | import android.util.Slog; |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 25 | import android.util.SparseLongArray; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 26 | |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 27 | import com.android.internal.annotations.GuardedBy; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | import com.android.server.pm.Installer; |
| 30 | import com.android.server.pm.Installer.InstallerException; |
| 31 | |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 32 | import java.util.List; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Encapsulates the logic for initiating userdata snapshots and rollbacks via installd. |
| 36 | */ |
| 37 | @VisibleForTesting |
| 38 | // TODO(narayan): Reason about the failure scenarios that involve one or more IPCs to installd |
| 39 | // failing. We need to decide what course of action to take if calls to snapshotAppData or |
| 40 | // restoreAppDataSnapshot fail. |
| 41 | public class AppDataRollbackHelper { |
| 42 | private static final String TAG = "RollbackManager"; |
| 43 | |
| 44 | private final Installer mInstaller; |
| 45 | |
| 46 | public AppDataRollbackHelper(Installer installer) { |
| 47 | mInstaller = installer; |
| 48 | } |
| 49 | |
| 50 | /** |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 51 | * Creates an app data snapshot for a specified {@code packageRollbackInfo} and the specified |
| 52 | * {@code userIds}. Updates said {@code packageRollbackInfo} with the inodes of the CE user data |
| 53 | * snapshot folders. |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 54 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 55 | @GuardedBy("rollback.mLock") |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 56 | // TODO(b/136241838): Move into Rollback and synchronize there. |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 57 | public void snapshotAppData( |
| 58 | int snapshotId, PackageRollbackInfo packageRollbackInfo, int[] userIds) { |
| 59 | for (int user : userIds) { |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 60 | final int storageFlags; |
| 61 | if (isUserCredentialLocked(user)) { |
| 62 | // We've encountered a user that hasn't unlocked on a FBE device, so we can't copy |
| 63 | // across app user data until the user unlocks their device. |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 64 | Slog.v(TAG, "User: " + user + " isn't unlocked, skipping CE userdata backup."); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 65 | storageFlags = Installer.FLAG_STORAGE_DE; |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 66 | packageRollbackInfo.addPendingBackup(user); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 67 | } else { |
| 68 | storageFlags = Installer.FLAG_STORAGE_CE | Installer.FLAG_STORAGE_DE; |
| 69 | } |
| 70 | |
| 71 | try { |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 72 | long ceSnapshotInode = mInstaller.snapshotAppData( |
| 73 | packageRollbackInfo.getPackageName(), user, snapshotId, storageFlags); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 74 | if ((storageFlags & Installer.FLAG_STORAGE_CE) != 0) { |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 75 | packageRollbackInfo.putCeSnapshotInode(user, ceSnapshotInode); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 76 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 77 | } catch (InstallerException ie) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 78 | Slog.e(TAG, "Unable to create app data snapshot for: " |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 79 | + packageRollbackInfo.getPackageName() + ", userId: " + user, ie); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 85 | * Restores an app data snapshot for a specified {@code packageRollbackInfo}, for a specified |
| 86 | * {@code userId}. |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 87 | * |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 88 | * @return {@code true} iff. a change to the {@code packageRollbackInfo} has been made. Changes |
| 89 | * to {@code packageRollbackInfo} are restricted to the removal or addition of {@code |
| 90 | * userId} to the list of pending backups or restores. |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 91 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 92 | @GuardedBy("rollback.mLock") |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 93 | // TODO(b/136241838): Move into Rollback and synchronize there. |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 94 | public boolean restoreAppData(int rollbackId, PackageRollbackInfo packageRollbackInfo, |
| 95 | int userId, int appId, String seInfo) { |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 96 | int storageFlags = Installer.FLAG_STORAGE_DE; |
| 97 | |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 98 | final IntArray pendingBackups = packageRollbackInfo.getPendingBackups(); |
| 99 | final List<RestoreInfo> pendingRestores = packageRollbackInfo.getPendingRestores(); |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 100 | boolean changedRollback = false; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 101 | |
| 102 | // If we still have a userdata backup pending for this user, it implies that the user |
| 103 | // hasn't unlocked their device between the point of backup and the point of restore, |
| 104 | // so the data cannot have changed. We simply skip restoring CE data in this case. |
| 105 | if (pendingBackups != null && pendingBackups.indexOf(userId) != -1) { |
| 106 | pendingBackups.remove(pendingBackups.indexOf(userId)); |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 107 | changedRollback = true; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 108 | } else { |
| 109 | // There's no pending CE backup for this user, which means that we successfully |
| 110 | // managed to backup data for the user, which means we seek to restore it |
| 111 | if (isUserCredentialLocked(userId)) { |
| 112 | // We've encountered a user that hasn't unlocked on a FBE device, so we can't |
| 113 | // copy across app user data until the user unlocks their device. |
| 114 | pendingRestores.add(new RestoreInfo(userId, appId, seInfo)); |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 115 | changedRollback = true; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 116 | } else { |
| 117 | // This user has unlocked, we can proceed to restore both CE and DE data. |
| 118 | storageFlags = storageFlags | Installer.FLAG_STORAGE_CE; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | try { |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 123 | switch (packageRollbackInfo.getRollbackDataPolicy()) { |
| 124 | case PackageManager.RollbackDataPolicy.WIPE: |
| 125 | mInstaller.clearAppData(null, packageRollbackInfo.getPackageName(), |
| 126 | userId, storageFlags, 0); |
| 127 | break; |
| 128 | case PackageManager.RollbackDataPolicy.RESTORE: |
| 129 | mInstaller.restoreAppDataSnapshot(packageRollbackInfo.getPackageName(), appId, |
| 130 | seInfo, userId, rollbackId, storageFlags); |
| 131 | break; |
| 132 | default: |
| 133 | break; |
| 134 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 135 | } catch (InstallerException ie) { |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 136 | Slog.e(TAG, "Unable to restore/wipe app data: " |
| 137 | + packageRollbackInfo.getPackageName() + " policy=" |
| 138 | + packageRollbackInfo.getRollbackDataPolicy(), ie); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 141 | return changedRollback; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 145 | * Deletes an app data snapshot with a given {@code rollbackId} for a specified package |
| 146 | * {@code packageName} for a given {@code user}. |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 147 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 148 | @GuardedBy("rollback.mLock") |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 149 | // TODO(b/136241838): Move into Rollback and synchronize there. |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 150 | public void destroyAppDataSnapshot(int rollbackId, PackageRollbackInfo packageRollbackInfo, |
| 151 | int user) { |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 152 | int storageFlags = Installer.FLAG_STORAGE_DE; |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 153 | final SparseLongArray ceSnapshotInodes = packageRollbackInfo.getCeSnapshotInodes(); |
| 154 | long ceSnapshotInode = ceSnapshotInodes.get(user); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 155 | if (ceSnapshotInode > 0) { |
| 156 | storageFlags |= Installer.FLAG_STORAGE_CE; |
| 157 | } |
| 158 | try { |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 159 | mInstaller.destroyAppDataSnapshot(packageRollbackInfo.getPackageName(), user, |
| 160 | ceSnapshotInode, rollbackId, storageFlags); |
| 161 | if ((storageFlags & Installer.FLAG_STORAGE_CE) != 0) { |
| 162 | ceSnapshotInodes.delete(user); |
| 163 | } |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 164 | } catch (InstallerException ie) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 165 | Slog.e(TAG, "Unable to delete app data snapshot for " |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 166 | + packageRollbackInfo.getPackageName(), ie); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 171 | * Commits the pending backups and restores for a given {@code userId} and {@code rollback}. If |
| 172 | * the rollback has a pending backup, it is updated with a mapping from {@code userId} to inode |
| 173 | * of the CE user data snapshot. |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 174 | * |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 175 | * @return true if any backups or restores were found for the userId |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 176 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 177 | @GuardedBy("rollback.mLock") |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 178 | boolean commitPendingBackupAndRestoreForUser(int userId, Rollback rollback) { |
| 179 | boolean foundBackupOrRestore = false; |
| 180 | for (PackageRollbackInfo info : rollback.info.getPackages()) { |
| 181 | boolean hasPendingBackup = false; |
| 182 | boolean hasPendingRestore = false; |
| 183 | final IntArray pendingBackupUsers = info.getPendingBackups(); |
| 184 | if (pendingBackupUsers != null) { |
| 185 | if (pendingBackupUsers.indexOf(userId) != -1) { |
| 186 | hasPendingBackup = true; |
| 187 | foundBackupOrRestore = true; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 190 | |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 191 | RestoreInfo ri = info.getRestoreInfo(userId); |
| 192 | if (ri != null) { |
| 193 | hasPendingRestore = true; |
| 194 | foundBackupOrRestore = true; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 195 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 196 | |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 197 | if (hasPendingBackup && hasPendingRestore) { |
| 198 | // Remove unnecessary backup, i.e. when user did not unlock their phone between the |
| 199 | // request to backup data and the request to restore it. |
| 200 | info.removePendingBackup(userId); |
| 201 | info.removePendingRestoreInfo(userId); |
| 202 | continue; |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 203 | } |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 204 | |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 205 | if (hasPendingBackup) { |
| 206 | int idx = pendingBackupUsers.indexOf(userId); |
| 207 | try { |
| 208 | long ceSnapshotInode = mInstaller.snapshotAppData(info.getPackageName(), |
| 209 | userId, rollback.info.getRollbackId(), |
| 210 | Installer.FLAG_STORAGE_CE); |
| 211 | info.putCeSnapshotInode(userId, ceSnapshotInode); |
| 212 | pendingBackupUsers.remove(idx); |
| 213 | } catch (InstallerException ie) { |
| 214 | Slog.e(TAG, |
| 215 | "Unable to create app data snapshot for: " |
Nikita Ioffe | 5dcd1797 | 2019-02-04 11:08:13 +0000 | [diff] [blame] | 216 | + info.getPackageName() + ", userId: " + userId, ie); |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | if (hasPendingRestore) { |
| 221 | try { |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 222 | switch (info.getRollbackDataPolicy()) { |
| 223 | case PackageManager.RollbackDataPolicy.WIPE: |
| 224 | mInstaller.clearAppData(null, info.getPackageName(), userId, |
| 225 | Installer.FLAG_STORAGE_CE, 0); |
| 226 | break; |
| 227 | case PackageManager.RollbackDataPolicy.RESTORE: |
| 228 | mInstaller.restoreAppDataSnapshot(info.getPackageName(), ri.appId, |
| 229 | ri.seInfo, userId, rollback.info.getRollbackId(), |
| 230 | Installer.FLAG_STORAGE_CE); |
| 231 | break; |
| 232 | default: |
| 233 | break; |
| 234 | } |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 235 | info.removeRestoreInfo(ri); |
| 236 | } catch (InstallerException ie) { |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 237 | Slog.e(TAG, "Unable to restore/wipe app data for: " |
| 238 | + info.getPackageName() + " policy=" |
| 239 | + info.getRollbackDataPolicy(), ie); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 243 | return foundBackupOrRestore; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @return {@code true} iff. {@code userId} is locked on an FBE device. |
| 248 | */ |
| 249 | @VisibleForTesting |
| 250 | public boolean isUserCredentialLocked(int userId) { |
| 251 | return StorageManager.isFileEncryptedNativeOrEmulated() |
| 252 | && !StorageManager.isUserKeyUnlocked(userId); |
| 253 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 254 | } |