Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.pm; |
| 18 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 19 | import static android.content.pm.DataLoaderType.INCREMENTAL; |
| 20 | import static android.content.pm.DataLoaderType.STREAMING; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 21 | import static android.content.pm.PackageInstaller.LOCATION_DATA_APP; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.INSTALL_FAILED_ABORTED; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.INSTALL_FAILED_BAD_SIGNATURE; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 26 | import static android.content.pm.PackageManager.INSTALL_FAILED_INVALID_APK; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 27 | import static android.content.pm.PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 28 | import static android.content.pm.PackageManager.INSTALL_FAILED_MISSING_SPLIT; |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 29 | import static android.content.pm.PackageParser.APEX_FILE_EXTENSION; |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 30 | import static android.content.pm.PackageParser.APK_FILE_EXTENSION; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 31 | import static android.system.OsConstants.O_CREAT; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 32 | import static android.system.OsConstants.O_RDONLY; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 33 | import static android.system.OsConstants.O_WRONLY; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 34 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 35 | import static com.android.internal.util.XmlUtils.readBitmapAttribute; |
| 36 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 37 | import static com.android.internal.util.XmlUtils.readByteArrayAttribute; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 38 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 39 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
| 40 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
| 41 | import static com.android.internal.util.XmlUtils.readUriAttribute; |
| 42 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 43 | import static com.android.internal.util.XmlUtils.writeByteArrayAttribute; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 44 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 45 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
| 46 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
| 47 | import static com.android.internal.util.XmlUtils.writeUriAttribute; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 48 | import static com.android.server.pm.PackageInstallerService.prepareStageDir; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 49 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 50 | import android.Manifest; |
| 51 | import android.annotation.NonNull; |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 52 | import android.annotation.Nullable; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 53 | import android.app.admin.DevicePolicyEventLogger; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 54 | import android.app.admin.DevicePolicyManagerInternal; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 55 | import android.content.ComponentName; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 56 | import android.content.Context; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 57 | import android.content.IIntentReceiver; |
| 58 | import android.content.IIntentSender; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 59 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 60 | import android.content.IntentSender; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 61 | import android.content.pm.ApplicationInfo; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 62 | import android.content.pm.DataLoaderManager; |
| 63 | import android.content.pm.DataLoaderParams; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 64 | import android.content.pm.DataLoaderParamsParcel; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 65 | import android.content.pm.FileSystemControlParcel; |
| 66 | import android.content.pm.IDataLoader; |
| 67 | import android.content.pm.IDataLoaderStatusListener; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 68 | import android.content.pm.IPackageInstallObserver2; |
| 69 | import android.content.pm.IPackageInstallerSession; |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 70 | import android.content.pm.IPackageInstallerSessionFileSystemConnector; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 71 | import android.content.pm.InstallationFile; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 73 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 74 | import android.content.pm.PackageInstaller.SessionInfo; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 75 | import android.content.pm.PackageInstaller.SessionInfo.StagedSessionErrorCode; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 76 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 77 | import android.content.pm.PackageManager; |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 78 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 79 | import android.content.pm.PackageParser; |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 80 | import android.content.pm.PackageParser.ApkLite; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 81 | import android.content.pm.PackageParser.PackageLite; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 82 | import android.content.pm.PackageParser.PackageParserException; |
Sudheer Shanka | a05a994 | 2018-08-29 23:28:23 -0700 | [diff] [blame] | 83 | import android.content.pm.dex.DexMetadataHelper; |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 84 | import android.content.pm.parsing.AndroidPackage; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 85 | import android.content.pm.parsing.ApkLiteParseUtils; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 86 | import android.graphics.Bitmap; |
| 87 | import android.graphics.BitmapFactory; |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 88 | import android.os.Binder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 89 | import android.os.Bundle; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 90 | import android.os.FileBridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 91 | import android.os.FileUtils; |
| 92 | import android.os.Handler; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 93 | import android.os.IBinder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 94 | import android.os.Looper; |
| 95 | import android.os.Message; |
| 96 | import android.os.ParcelFileDescriptor; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 97 | import android.os.ParcelableException; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 98 | import android.os.Process; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 99 | import android.os.RemoteException; |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 100 | import android.os.RevocableFileDescriptor; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 101 | import android.os.SystemProperties; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 102 | import android.os.UserHandle; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 103 | import android.os.incremental.IncrementalFileStorages; |
| 104 | import android.os.incremental.IncrementalManager; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 105 | import android.os.storage.StorageManager; |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 106 | import android.provider.Settings.Secure; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 107 | import android.stats.devicepolicy.DevicePolicyEnums; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 108 | import android.system.ErrnoException; |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 109 | import android.system.Int64Ref; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 110 | import android.system.Os; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 111 | import android.system.OsConstants; |
| 112 | import android.system.StructStat; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 113 | import android.text.TextUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 114 | import android.util.ArraySet; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 115 | import android.util.ExceptionUtils; |
| 116 | import android.util.MathUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 117 | import android.util.Slog; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 118 | import android.util.SparseIntArray; |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 119 | import android.util.apk.ApkSignatureVerifier; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 121 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 122 | import com.android.internal.content.NativeLibraryHelper; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 123 | import com.android.internal.content.PackageHelper; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 124 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 125 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 126 | import com.android.internal.util.IndentingPrintWriter; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 127 | import com.android.server.LocalServices; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 128 | import com.android.server.pm.Installer.InstallerException; |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 129 | import com.android.server.pm.dex.DexManager; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 130 | import com.android.server.security.VerityUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 132 | import libcore.io.IoUtils; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 133 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 134 | import org.xmlpull.v1.XmlPullParser; |
| 135 | import org.xmlpull.v1.XmlPullParserException; |
| 136 | import org.xmlpull.v1.XmlSerializer; |
| 137 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 138 | import java.io.File; |
| 139 | import java.io.FileDescriptor; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 140 | import java.io.FileFilter; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 141 | import java.io.FileOutputStream; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 142 | import java.io.IOException; |
| 143 | import java.util.ArrayList; |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 144 | import java.util.Arrays; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 145 | import java.util.LinkedList; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 146 | import java.util.List; |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 147 | import java.util.Objects; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 148 | import java.util.Set; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 149 | import java.util.concurrent.atomic.AtomicInteger; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 150 | import java.util.stream.Collectors; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 151 | |
| 152 | public class PackageInstallerSession extends IPackageInstallerSession.Stub { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 153 | private static final String TAG = "PackageInstallerSession"; |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 154 | private static final boolean LOGD = true; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 155 | private static final String REMOVE_MARKER_EXTENSION = ".removed"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 156 | |
Patrick Baumann | 14b3de1 | 2018-03-27 09:30:35 -0700 | [diff] [blame] | 157 | private static final int MSG_COMMIT = 1; |
| 158 | private static final int MSG_ON_PACKAGE_INSTALLED = 2; |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 159 | private static final int MSG_SEAL = 3; |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 160 | private static final int MSG_STREAM_AND_VALIDATE = 4; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 161 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 162 | /** XML constants used for persisting a session */ |
| 163 | static final String TAG_SESSION = "session"; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 164 | static final String TAG_CHILD_SESSION = "childSession"; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 165 | static final String TAG_SESSION_FILE = "sessionFile"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 166 | private static final String TAG_GRANTED_RUNTIME_PERMISSION = "granted-runtime-permission"; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 167 | private static final String TAG_WHITELISTED_RESTRICTED_PERMISSION = |
| 168 | "whitelisted-restricted-permission"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 169 | private static final String ATTR_SESSION_ID = "sessionId"; |
| 170 | private static final String ATTR_USER_ID = "userId"; |
| 171 | private static final String ATTR_INSTALLER_PACKAGE_NAME = "installerPackageName"; |
| 172 | private static final String ATTR_INSTALLER_UID = "installerUid"; |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 173 | private static final String ATTR_INITIATING_PACKAGE_NAME = |
| 174 | "installInitiatingPackageName"; |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 175 | private static final String ATTR_ORIGINATING_PACKAGE_NAME = |
| 176 | "installOriginatingPackageName"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 177 | private static final String ATTR_CREATED_MILLIS = "createdMillis"; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 178 | private static final String ATTR_UPDATED_MILLIS = "updatedMillis"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 179 | private static final String ATTR_SESSION_STAGE_DIR = "sessionStageDir"; |
| 180 | private static final String ATTR_SESSION_STAGE_CID = "sessionStageCid"; |
| 181 | private static final String ATTR_PREPARED = "prepared"; |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 182 | private static final String ATTR_COMMITTED = "committed"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 183 | private static final String ATTR_SEALED = "sealed"; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 184 | private static final String ATTR_MULTI_PACKAGE = "multiPackage"; |
| 185 | private static final String ATTR_PARENT_SESSION_ID = "parentSessionId"; |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 186 | private static final String ATTR_STAGED_SESSION = "stagedSession"; |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 187 | private static final String ATTR_IS_READY = "isReady"; |
| 188 | private static final String ATTR_IS_FAILED = "isFailed"; |
| 189 | private static final String ATTR_IS_APPLIED = "isApplied"; |
| 190 | private static final String ATTR_STAGED_SESSION_ERROR_CODE = "errorCode"; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 191 | private static final String ATTR_STAGED_SESSION_ERROR_MESSAGE = "errorMessage"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 192 | private static final String ATTR_MODE = "mode"; |
| 193 | private static final String ATTR_INSTALL_FLAGS = "installFlags"; |
| 194 | private static final String ATTR_INSTALL_LOCATION = "installLocation"; |
| 195 | private static final String ATTR_SIZE_BYTES = "sizeBytes"; |
| 196 | private static final String ATTR_APP_PACKAGE_NAME = "appPackageName"; |
| 197 | @Deprecated |
| 198 | private static final String ATTR_APP_ICON = "appIcon"; |
| 199 | private static final String ATTR_APP_LABEL = "appLabel"; |
| 200 | private static final String ATTR_ORIGINATING_URI = "originatingUri"; |
| 201 | private static final String ATTR_ORIGINATING_UID = "originatingUid"; |
| 202 | private static final String ATTR_REFERRER_URI = "referrerUri"; |
| 203 | private static final String ATTR_ABI_OVERRIDE = "abiOverride"; |
| 204 | private static final String ATTR_VOLUME_UUID = "volumeUuid"; |
| 205 | private static final String ATTR_NAME = "name"; |
| 206 | private static final String ATTR_INSTALL_REASON = "installRason"; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 207 | private static final String ATTR_IS_DATALOADER = "isDataLoader"; |
| 208 | private static final String ATTR_DATALOADER_TYPE = "dataLoaderType"; |
| 209 | private static final String ATTR_DATALOADER_PACKAGE_NAME = "dataLoaderPackageName"; |
| 210 | private static final String ATTR_DATALOADER_CLASS_NAME = "dataLoaderClassName"; |
| 211 | private static final String ATTR_DATALOADER_ARGUMENTS = "dataLoaderArguments"; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 212 | private static final String ATTR_LOCATION = "location"; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 213 | private static final String ATTR_LENGTH_BYTES = "lengthBytes"; |
| 214 | private static final String ATTR_METADATA = "metadata"; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 215 | private static final String ATTR_SIGNATURE = "signature"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 216 | |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 217 | private static final String PROPERTY_NAME_INHERIT_NATIVE = "pi.inherit_native_on_dont_kill"; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 218 | private static final int[] EMPTY_CHILD_SESSION_ARRAY = {}; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 219 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 220 | private static final String SYSTEM_DATA_LOADER_PACKAGE = "android"; |
| 221 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 222 | // TODO: enforce INSTALL_ALLOW_TEST |
| 223 | // TODO: enforce INSTALL_ALLOW_DOWNGRADE |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 224 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 225 | private final PackageInstallerService.InternalCallback mCallback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 226 | private final Context mContext; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 227 | private final PackageManagerService mPm; |
| 228 | private final Handler mHandler; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 229 | private final PackageSessionProvider mSessionProvider; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 230 | private final StagingManager mStagingManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 232 | final int sessionId; |
| 233 | final int userId; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 234 | final SessionParams params; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 235 | final long createdMillis; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 236 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 237 | /** Staging location where client data is written. */ |
| 238 | final File stageDir; |
| 239 | final String stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 240 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 241 | private final AtomicInteger mActiveCount = new AtomicInteger(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 242 | |
| 243 | private final Object mLock = new Object(); |
| 244 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 245 | /** Timestamp of the last time this session changed state */ |
| 246 | @GuardedBy("mLock") |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 247 | private long updatedMillis; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 248 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 249 | /** Uid of the creator of this session. */ |
| 250 | private final int mOriginalInstallerUid; |
| 251 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 252 | /** Uid of the owner of the installer session */ |
| 253 | @GuardedBy("mLock") |
| 254 | private int mInstallerUid; |
| 255 | |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 256 | /** Where this install request came from */ |
| 257 | @GuardedBy("mLock") |
| 258 | private InstallSource mInstallSource; |
| 259 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 260 | @GuardedBy("mLock") |
| 261 | private float mClientProgress = 0; |
| 262 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 263 | private float mInternalProgress = 0; |
| 264 | |
| 265 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 266 | private float mProgress = 0; |
| 267 | @GuardedBy("mLock") |
| 268 | private float mReportedProgress = -1; |
| 269 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 270 | /** State of the session. */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 271 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 272 | private boolean mPrepared = false; |
| 273 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 274 | private boolean mSealed = false; |
| 275 | @GuardedBy("mLock") |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 276 | private boolean mShouldBeSealed = false; |
| 277 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 278 | private boolean mCommitted = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 279 | @GuardedBy("mLock") |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 280 | private boolean mRelinquished = false; |
| 281 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 282 | private boolean mDestroyed = false; |
| 283 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 284 | /** Permissions have been accepted by the user (see {@link #setPermissionsResult}) */ |
| 285 | @GuardedBy("mLock") |
| 286 | private boolean mPermissionsManuallyAccepted = false; |
| 287 | |
| 288 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 289 | private int mFinalStatus; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 290 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 291 | private String mFinalMessage; |
| 292 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 293 | @GuardedBy("mLock") |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 294 | private final ArrayList<RevocableFileDescriptor> mFds = new ArrayList<>(); |
| 295 | @GuardedBy("mLock") |
| 296 | private final ArrayList<FileBridge> mBridges = new ArrayList<>(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 297 | |
| 298 | @GuardedBy("mLock") |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 299 | private IntentSender mRemoteStatusReceiver; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 300 | |
| 301 | /** Fields derived from commit parsing */ |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 302 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 303 | private String mPackageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 304 | @GuardedBy("mLock") |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 305 | private long mVersionCode; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 306 | @GuardedBy("mLock") |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 307 | private PackageParser.SigningDetails mSigningDetails; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 308 | @GuardedBy("mLock") |
| 309 | private SparseIntArray mChildSessionIds = new SparseIntArray(); |
| 310 | @GuardedBy("mLock") |
| 311 | private int mParentSessionId; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 312 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 313 | static class FileInfo { |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 314 | public final int location; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 315 | public final String name; |
| 316 | public final Long lengthBytes; |
| 317 | public final byte[] metadata; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 318 | public final byte[] signature; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 319 | |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 320 | public static FileInfo added(int location, String name, Long lengthBytes, byte[] metadata, |
| 321 | byte[] signature) { |
| 322 | return new FileInfo(location, name, lengthBytes, metadata, signature); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 325 | public static FileInfo removed(int location, String name) { |
| 326 | return new FileInfo(location, name, -1L, null, null); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 329 | FileInfo(int location, String name, Long lengthBytes, byte[] metadata, byte[] signature) { |
| 330 | this.location = location; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 331 | this.name = name; |
| 332 | this.lengthBytes = lengthBytes; |
| 333 | this.metadata = metadata; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 334 | this.signature = signature; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | @GuardedBy("mLock") |
| 339 | private ArrayList<FileInfo> mFiles = new ArrayList<>(); |
| 340 | |
Dario Freni | 71eee5e | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 341 | @GuardedBy("mLock") |
| 342 | private boolean mStagedSessionApplied; |
| 343 | @GuardedBy("mLock") |
| 344 | private boolean mStagedSessionReady; |
| 345 | @GuardedBy("mLock") |
| 346 | private boolean mStagedSessionFailed; |
| 347 | @GuardedBy("mLock") |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 348 | private int mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 349 | @GuardedBy("mLock") |
| 350 | private String mStagedSessionErrorMessage; |
Dario Freni | 71eee5e | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 351 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 352 | /** |
| 353 | * Path to the validated base APK for this session, which may point at an |
| 354 | * APK inside the session (when the session defines the base), or it may |
| 355 | * point at the existing base APK (when adding splits to an existing app). |
| 356 | * <p> |
| 357 | * This is used when confirming permissions, since we can't fully stage the |
| 358 | * session inside an ASEC before confirming with user. |
| 359 | */ |
| 360 | @GuardedBy("mLock") |
| 361 | private File mResolvedBaseFile; |
| 362 | |
| 363 | @GuardedBy("mLock") |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 364 | private final List<File> mResolvedStagedFiles = new ArrayList<>(); |
| 365 | @GuardedBy("mLock") |
| 366 | private final List<File> mResolvedInheritedFiles = new ArrayList<>(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 367 | @GuardedBy("mLock") |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 368 | private final List<String> mResolvedInstructionSets = new ArrayList<>(); |
| 369 | @GuardedBy("mLock") |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 370 | private final List<String> mResolvedNativeLibPaths = new ArrayList<>(); |
| 371 | @GuardedBy("mLock") |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 372 | private File mInheritedFilesBase; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 373 | @GuardedBy("mLock") |
| 374 | private boolean mVerityFound; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 375 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 376 | private boolean mDataLoaderFinished = false; |
| 377 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 378 | // TODO(b/146080380): merge file list with Callback installation. |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 379 | private IncrementalFileStorages mIncrementalFileStorages; |
| 380 | |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 381 | private static final String[] EMPTY_STRING_ARRAY = new String[]{}; |
| 382 | |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 383 | private static final FileFilter sAddedApkFilter = new FileFilter() { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 384 | @Override |
| 385 | public boolean accept(File file) { |
| 386 | // Installers can't stage directories, so it's fine to ignore |
| 387 | // entries like "lost+found". |
| 388 | if (file.isDirectory()) return false; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 389 | if (file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 390 | if (DexMetadataHelper.isDexMetadataFile(file)) return false; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 391 | if (VerityUtils.isFsveritySignatureFile(file)) return false; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | }; |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 395 | private static final FileFilter sAddedFilter = new FileFilter() { |
| 396 | @Override |
| 397 | public boolean accept(File file) { |
| 398 | // Installers can't stage directories, so it's fine to ignore |
| 399 | // entries like "lost+found". |
| 400 | if (file.isDirectory()) return false; |
| 401 | if (file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
| 402 | return true; |
| 403 | } |
| 404 | }; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 405 | private static final FileFilter sRemovedFilter = new FileFilter() { |
| 406 | @Override |
| 407 | public boolean accept(File file) { |
| 408 | if (file.isDirectory()) return false; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 409 | if (!file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | }; |
| 413 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 414 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 415 | @Override |
| 416 | public boolean handleMessage(Message msg) { |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 417 | switch (msg.what) { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 418 | case MSG_SEAL: |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 419 | handleSeal((IntentSender) msg.obj); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 420 | break; |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 421 | case MSG_STREAM_AND_VALIDATE: |
| 422 | handleStreamAndValidate(); |
| 423 | break; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 424 | case MSG_COMMIT: |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 425 | handleCommit(); |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 426 | break; |
| 427 | case MSG_ON_PACKAGE_INSTALLED: |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 428 | final SomeArgs args = (SomeArgs) msg.obj; |
| 429 | final String packageName = (String) args.arg1; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 430 | final String message = (String) args.arg2; |
| 431 | final Bundle extras = (Bundle) args.arg3; |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 432 | final IntentSender statusReceiver = (IntentSender) args.arg4; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 433 | final int returnCode = args.argi1; |
| 434 | args.recycle(); |
| 435 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 436 | PackageInstallerService.sendOnPackageInstalled(mContext, |
| 437 | statusReceiver, sessionId, |
| 438 | isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId, |
| 439 | packageName, returnCode, message, extras); |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 440 | |
| 441 | break; |
Philip P. Moltmann | 9890f8b | 2017-08-08 10:49:38 -0700 | [diff] [blame] | 442 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 443 | |
| 444 | return true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 445 | } |
| 446 | }; |
| 447 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 448 | private boolean isDataLoaderInstallation() { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 449 | return params.dataLoaderParams != null; |
| 450 | } |
| 451 | |
| 452 | private boolean isStreamingInstallation() { |
| 453 | return isDataLoaderInstallation() && params.dataLoaderParams.getType() == STREAMING; |
| 454 | } |
| 455 | |
| 456 | private boolean isIncrementalInstallation() { |
| 457 | return isDataLoaderInstallation() && params.dataLoaderParams.getType() == INCREMENTAL; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 460 | /** |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 461 | * @return {@code true} iff the installing is app an device owner or affiliated profile owner. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 462 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 463 | @GuardedBy("mLock") |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 464 | private boolean isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked() { |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 465 | if (userId != UserHandle.getUserId(mInstallerUid)) { |
| 466 | return false; |
| 467 | } |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 468 | DevicePolicyManagerInternal dpmi = |
| 469 | LocalServices.getService(DevicePolicyManagerInternal.class); |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 470 | return dpmi != null && dpmi.canSilentlyInstallPackage( |
| 471 | mInstallSource.installerPackageName, mInstallerUid); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Checks if the permissions still need to be confirmed. |
| 476 | * |
| 477 | * <p>This is dependant on the identity of the installer, hence this cannot be cached if the |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 478 | * installer might still {@link #transfer(String) change}. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 479 | * |
| 480 | * @return {@code true} iff we need to ask to confirm the permissions? |
| 481 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 482 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 483 | private boolean needToAskForPermissionsLocked() { |
| 484 | if (mPermissionsManuallyAccepted) { |
| 485 | return false; |
| 486 | } |
| 487 | |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 488 | final boolean isInstallPermissionGranted = |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 489 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, |
| 490 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 491 | final boolean isSelfUpdatePermissionGranted = |
| 492 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_SELF_UPDATES, |
| 493 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
Chad Brubaker | adcb522 | 2018-01-11 14:22:15 -0800 | [diff] [blame] | 494 | final boolean isUpdatePermissionGranted = |
| 495 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGE_UPDATES, |
| 496 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
| 497 | final int targetPackageUid = mPm.getPackageUid(mPackageName, 0, userId); |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 498 | final boolean isPermissionGranted = isInstallPermissionGranted |
Chad Brubaker | adcb522 | 2018-01-11 14:22:15 -0800 | [diff] [blame] | 499 | || (isUpdatePermissionGranted && targetPackageUid != -1) |
| 500 | || (isSelfUpdatePermissionGranted && targetPackageUid == mInstallerUid); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 501 | final boolean isInstallerRoot = (mInstallerUid == Process.ROOT_UID); |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 502 | final boolean isInstallerSystem = (mInstallerUid == Process.SYSTEM_UID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 503 | final boolean forcePermissionPrompt = |
| 504 | (params.installFlags & PackageManager.INSTALL_FORCE_PERMISSION_PROMPT) != 0; |
| 505 | |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 506 | // Device owners and affiliated profile owners are allowed to silently install packages, so |
| 507 | // the permission check is waived if the installer is the device owner. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 508 | return forcePermissionPrompt || !(isPermissionGranted || isInstallerRoot |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 509 | || isInstallerSystem || isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked()); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 512 | public PackageInstallerSession(PackageInstallerService.InternalCallback callback, |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 513 | Context context, PackageManagerService pm, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 514 | PackageSessionProvider sessionProvider, Looper looper, StagingManager stagingManager, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 515 | int sessionId, int userId, int installerUid, @NonNull InstallSource installSource, |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 516 | SessionParams params, long createdMillis, |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 517 | File stageDir, String stageCid, FileInfo[] files, boolean prepared, |
| 518 | boolean committed, boolean sealed, |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 519 | @Nullable int[] childSessionIds, int parentSessionId, boolean isReady, |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 520 | boolean isFailed, boolean isApplied, int stagedSessionErrorCode, |
| 521 | String stagedSessionErrorMessage) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 522 | mCallback = callback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 523 | mContext = context; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 524 | mPm = pm; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 525 | mSessionProvider = sessionProvider; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 526 | mHandler = new Handler(looper, mHandlerCallback); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 527 | mStagingManager = stagingManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 528 | |
| 529 | this.sessionId = sessionId; |
| 530 | this.userId = userId; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 531 | mOriginalInstallerUid = installerUid; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 532 | mInstallerUid = installerUid; |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 533 | mInstallSource = Objects.requireNonNull(installSource); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 534 | this.params = params; |
| 535 | this.createdMillis = createdMillis; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 536 | this.updatedMillis = createdMillis; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 537 | this.stageDir = stageDir; |
| 538 | this.stageCid = stageCid; |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 539 | this.mShouldBeSealed = sealed; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 540 | if (childSessionIds != null) { |
| 541 | for (int childSessionId : childSessionIds) { |
| 542 | mChildSessionIds.put(childSessionId, 0); |
| 543 | } |
| 544 | } |
| 545 | this.mParentSessionId = parentSessionId; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 546 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 547 | if (files != null) { |
| 548 | for (FileInfo file : files) { |
| 549 | mFiles.add(file); |
| 550 | } |
| 551 | } |
| 552 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 553 | if (!params.isMultiPackage && (stageDir == null) == (stageCid == null)) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 554 | throw new IllegalArgumentException( |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 555 | "Exactly one of stageDir or stageCid stage must be set"); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 558 | mPrepared = prepared; |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 559 | mCommitted = committed; |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 560 | mStagedSessionReady = isReady; |
| 561 | mStagedSessionFailed = isFailed; |
| 562 | mStagedSessionApplied = isApplied; |
| 563 | mStagedSessionErrorCode = stagedSessionErrorCode; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 564 | mStagedSessionErrorMessage = |
| 565 | stagedSessionErrorMessage != null ? stagedSessionErrorMessage : ""; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 566 | |
| 567 | // TODO(b/136132412): sanity check if session should not be incremental |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 568 | if (!params.isStaged && isIncrementalInstallation()) { |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 569 | IncrementalManager incrementalManager = (IncrementalManager) mContext.getSystemService( |
| 570 | Context.INCREMENTAL_SERVICE); |
| 571 | if (incrementalManager != null) { |
| 572 | mIncrementalFileStorages = |
| 573 | new IncrementalFileStorages(mPackageName, stageDir, incrementalManager, |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 574 | params.dataLoaderParams); |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 577 | |
| 578 | if (isStreamingInstallation() |
| 579 | && this.params.dataLoaderParams.getComponentName().getPackageName() |
| 580 | == SYSTEM_DATA_LOADER_PACKAGE) { |
| 581 | assertShellOrSystemCalling("System data loaders"); |
| 582 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 585 | public SessionInfo generateInfo() { |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 586 | return generateInfo(true); |
| 587 | } |
| 588 | |
| 589 | public SessionInfo generateInfo(boolean includeIcon) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 590 | final SessionInfo info = new SessionInfo(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 591 | synchronized (mLock) { |
| 592 | info.sessionId = sessionId; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 593 | info.userId = userId; |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 594 | info.installerPackageName = mInstallSource.installerPackageName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 595 | info.resolvedBaseCodePath = (mResolvedBaseFile != null) ? |
| 596 | mResolvedBaseFile.getAbsolutePath() : null; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 597 | info.progress = mProgress; |
| 598 | info.sealed = mSealed; |
Nikita Ioffe | 00a08f1 | 2019-03-07 20:55:08 +0000 | [diff] [blame] | 599 | info.isCommitted = mCommitted; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 600 | info.active = mActiveCount.get() > 0; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 601 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 602 | info.mode = params.mode; |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 603 | info.installReason = params.installReason; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 604 | info.sizeBytes = params.sizeBytes; |
| 605 | info.appPackageName = params.appPackageName; |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 606 | if (includeIcon) { |
| 607 | info.appIcon = params.appIcon; |
| 608 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 609 | info.appLabel = params.appLabel; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 610 | |
| 611 | info.installLocation = params.installLocation; |
| 612 | info.originatingUri = params.originatingUri; |
| 613 | info.originatingUid = params.originatingUid; |
| 614 | info.referrerUri = params.referrerUri; |
| 615 | info.grantedRuntimePermissions = params.grantedRuntimePermissions; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 616 | info.whitelistedRestrictedPermissions = params.whitelistedRestrictedPermissions; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 617 | info.installFlags = params.installFlags; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 618 | info.isMultiPackage = params.isMultiPackage; |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 619 | info.isStaged = params.isStaged; |
JW Wang | 0bb6808 | 2019-12-05 18:00:06 +0800 | [diff] [blame] | 620 | info.rollbackDataPolicy = params.rollbackDataPolicy; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 621 | info.parentSessionId = mParentSessionId; |
| 622 | info.childSessionIds = mChildSessionIds.copyKeys(); |
| 623 | if (info.childSessionIds == null) { |
| 624 | info.childSessionIds = EMPTY_CHILD_SESSION_ARRAY; |
| 625 | } |
Dario Freni | 60a96c1 | 2019-02-24 21:01:29 +0000 | [diff] [blame] | 626 | info.isStagedSessionApplied = mStagedSessionApplied; |
| 627 | info.isStagedSessionReady = mStagedSessionReady; |
| 628 | info.isStagedSessionFailed = mStagedSessionFailed; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 629 | info.setStagedSessionErrorCode(mStagedSessionErrorCode, mStagedSessionErrorMessage); |
Pinyao Ting | bc969f4 | 2019-12-09 15:15:01 -0800 | [diff] [blame] | 630 | info.createdMillis = createdMillis; |
Dario Freni | 56c14dd | 2019-04-03 16:20:22 +0100 | [diff] [blame] | 631 | info.updatedMillis = updatedMillis; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 632 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 633 | return info; |
| 634 | } |
| 635 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 636 | public boolean isPrepared() { |
| 637 | synchronized (mLock) { |
| 638 | return mPrepared; |
| 639 | } |
| 640 | } |
| 641 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 642 | public boolean isSealed() { |
| 643 | synchronized (mLock) { |
| 644 | return mSealed; |
| 645 | } |
| 646 | } |
| 647 | |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 648 | /** {@hide} */ |
| 649 | boolean isCommitted() { |
| 650 | synchronized (mLock) { |
| 651 | return mCommitted; |
| 652 | } |
| 653 | } |
| 654 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 655 | /** Returns true if a staged session has reached a final state and can be forgotten about */ |
| 656 | public boolean isStagedAndInTerminalState() { |
| 657 | synchronized (mLock) { |
| 658 | return params.isStaged && (mStagedSessionApplied || mStagedSessionFailed); |
| 659 | } |
| 660 | } |
| 661 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 662 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 663 | private void assertPreparedAndNotSealedLocked(String cookie) { |
| 664 | assertPreparedAndNotCommittedOrDestroyedLocked(cookie); |
| 665 | if (mSealed) { |
| 666 | throw new SecurityException(cookie + " not allowed after sealing"); |
| 667 | } |
| 668 | } |
| 669 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 670 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 671 | private void assertPreparedAndNotCommittedOrDestroyedLocked(String cookie) { |
| 672 | assertPreparedAndNotDestroyedLocked(cookie); |
| 673 | if (mCommitted) { |
| 674 | throw new SecurityException(cookie + " not allowed after commit"); |
| 675 | } |
| 676 | } |
| 677 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 678 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 679 | private void assertPreparedAndNotDestroyedLocked(String cookie) { |
| 680 | if (!mPrepared) { |
| 681 | throw new IllegalStateException(cookie + " before prepared"); |
| 682 | } |
| 683 | if (mDestroyed) { |
| 684 | throw new SecurityException(cookie + " not allowed after destruction"); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 688 | @GuardedBy("mLock") |
| 689 | private void setClientProgressLocked(float progress) { |
| 690 | // Always publish first staging movement |
| 691 | final boolean forcePublish = (mClientProgress == 0); |
| 692 | mClientProgress = progress; |
| 693 | computeProgressLocked(forcePublish); |
| 694 | } |
| 695 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 696 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 697 | public void setClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 698 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 699 | assertCallerIsOwnerOrRootLocked(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 700 | setClientProgressLocked(progress); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 701 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 705 | public void addClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 706 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 707 | assertCallerIsOwnerOrRootLocked(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 708 | setClientProgressLocked(mClientProgress + progress); |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 709 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 712 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 713 | private void computeProgressLocked(boolean forcePublish) { |
| 714 | mProgress = MathUtils.constrain(mClientProgress * 0.8f, 0f, 0.8f) |
| 715 | + MathUtils.constrain(mInternalProgress * 0.2f, 0f, 0.2f); |
| 716 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 717 | // Only publish when meaningful change |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 718 | if (forcePublish || Math.abs(mProgress - mReportedProgress) >= 0.01) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 719 | mReportedProgress = mProgress; |
| 720 | mCallback.onSessionProgressChanged(this, mProgress); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 725 | public String[] getNames() { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 726 | synchronized (mLock) { |
| 727 | assertCallerIsOwnerOrRootLocked(); |
| 728 | assertPreparedAndNotCommittedOrDestroyedLocked("getNames"); |
| 729 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 730 | return getNamesLocked(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 731 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 734 | @GuardedBy("mLock") |
| 735 | private String[] getNamesLocked() { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 736 | if (!isDataLoaderInstallation()) { |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 737 | String[] result = stageDir.list(); |
| 738 | if (result == null) { |
| 739 | result = EMPTY_STRING_ARRAY; |
| 740 | } |
| 741 | return result; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 742 | } |
| 743 | return mFiles.stream().map(fileInfo -> fileInfo.name).toArray(String[]::new); |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | private static File[] filterFiles(File parent, String[] names, FileFilter filter) { |
| 747 | return Arrays.stream(names).map(name -> new File(parent, name)).filter( |
| 748 | file -> filter.accept(file)).toArray(File[]::new); |
| 749 | } |
| 750 | |
| 751 | @GuardedBy("mLock") |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 752 | private File[] getAddedApksLocked() { |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 753 | String[] names = getNamesLocked(); |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 754 | return filterFiles(stageDir, names, sAddedApkFilter); |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | @GuardedBy("mLock") |
| 758 | private File[] getRemovedFilesLocked() { |
| 759 | String[] names = getNamesLocked(); |
| 760 | return filterFiles(stageDir, names, sRemovedFilter); |
| 761 | } |
| 762 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 763 | @Override |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 764 | public void removeSplit(String splitName) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 765 | if (isDataLoaderInstallation()) { |
| 766 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 767 | "Cannot remove splits in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 768 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 769 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 770 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 771 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 772 | |
| 773 | synchronized (mLock) { |
| 774 | assertCallerIsOwnerOrRootLocked(); |
| 775 | assertPreparedAndNotCommittedOrDestroyedLocked("removeSplit"); |
| 776 | |
| 777 | try { |
| 778 | createRemoveSplitMarkerLocked(splitName); |
| 779 | } catch (IOException e) { |
| 780 | throw ExceptionUtils.wrap(e); |
| 781 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 785 | private static String getRemoveMarkerName(String name) { |
| 786 | final String markerName = name + REMOVE_MARKER_EXTENSION; |
| 787 | if (!FileUtils.isValidExtFilename(markerName)) { |
| 788 | throw new IllegalArgumentException("Invalid marker: " + markerName); |
| 789 | } |
| 790 | return markerName; |
| 791 | } |
| 792 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 793 | private void createRemoveSplitMarkerLocked(String splitName) throws IOException { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 794 | try { |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 795 | final File target = new File(stageDir, getRemoveMarkerName(splitName)); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 796 | target.createNewFile(); |
| 797 | Os.chmod(target.getAbsolutePath(), 0 /*mode*/); |
| 798 | } catch (ErrnoException e) { |
| 799 | throw e.rethrowAsIOException(); |
| 800 | } |
| 801 | } |
| 802 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 803 | private void assertShellOrSystemCalling(String operation) { |
| 804 | switch (Binder.getCallingUid()) { |
| 805 | case android.os.Process.SHELL_UID: |
| 806 | case android.os.Process.ROOT_UID: |
| 807 | case android.os.Process.SYSTEM_UID: |
| 808 | break; |
| 809 | default: |
| 810 | throw new SecurityException(operation + " only supported from shell or system"); |
| 811 | } |
| 812 | } |
| 813 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 814 | private void assertCanWrite(boolean reverseMode) { |
| 815 | if (isDataLoaderInstallation()) { |
| 816 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 817 | "Cannot write regular files in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 818 | } |
| 819 | synchronized (mLock) { |
| 820 | assertCallerIsOwnerOrRootLocked(); |
| 821 | assertPreparedAndNotSealedLocked("assertCanWrite"); |
| 822 | } |
| 823 | if (reverseMode) { |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 824 | assertShellOrSystemCalling("Reverse mode"); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 828 | @Override |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 829 | public ParcelFileDescriptor openWrite(String name, long offsetBytes, long lengthBytes) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 830 | assertCanWrite(false); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 831 | try { |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 832 | return doWriteInternal(name, offsetBytes, lengthBytes, null); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 833 | } catch (IOException e) { |
| 834 | throw ExceptionUtils.wrap(e); |
| 835 | } |
| 836 | } |
| 837 | |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 838 | @Override |
| 839 | public void write(String name, long offsetBytes, long lengthBytes, |
| 840 | ParcelFileDescriptor fd) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 841 | assertCanWrite(fd != null); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 842 | try { |
| 843 | doWriteInternal(name, offsetBytes, lengthBytes, fd); |
| 844 | } catch (IOException e) { |
| 845 | throw ExceptionUtils.wrap(e); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | private ParcelFileDescriptor doWriteInternal(String name, long offsetBytes, long lengthBytes, |
| 850 | ParcelFileDescriptor incomingFd) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 851 | // Quick sanity check of state, and allocate a pipe for ourselves. We |
| 852 | // then do heavy disk allocation outside the lock, but this open pipe |
| 853 | // will block any attempted install transitions. |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 854 | final RevocableFileDescriptor fd; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 855 | final FileBridge bridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 856 | synchronized (mLock) { |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 857 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 858 | fd = new RevocableFileDescriptor(); |
| 859 | bridge = null; |
| 860 | mFds.add(fd); |
| 861 | } else { |
| 862 | fd = null; |
| 863 | bridge = new FileBridge(); |
| 864 | mBridges.add(bridge); |
| 865 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | try { |
| 869 | // Use installer provided name for now; we always rename later |
| 870 | if (!FileUtils.isValidExtFilename(name)) { |
| 871 | throw new IllegalArgumentException("Invalid name: " + name); |
| 872 | } |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 873 | final File target; |
| 874 | final long identity = Binder.clearCallingIdentity(); |
| 875 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 876 | target = new File(stageDir, name); |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 877 | } finally { |
| 878 | Binder.restoreCallingIdentity(identity); |
| 879 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 880 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 881 | // TODO: this should delegate to DCS so the system process avoids |
| 882 | // holding open FDs into containers. |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 883 | final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 884 | O_CREAT | O_WRONLY, 0644); |
| 885 | Os.chmod(target.getAbsolutePath(), 0644); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 886 | |
| 887 | // If caller specified a total length, allocate it for them. Free up |
| 888 | // cache space to grow, if needed. |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 889 | if (stageDir != null && lengthBytes > 0) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 890 | mContext.getSystemService(StorageManager.class).allocateBytes(targetFd, lengthBytes, |
| 891 | PackageHelper.translateAllocateFlags(params.installFlags)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (offsetBytes > 0) { |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 895 | Os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 898 | if (incomingFd != null) { |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 899 | // In "reverse" mode, we're streaming data ourselves from the |
| 900 | // incoming FD, which means we never have to hand out our |
| 901 | // sensitive internal FD. We still rely on a "bridge" being |
| 902 | // inserted above to hold the session active. |
| 903 | try { |
| 904 | final Int64Ref last = new Int64Ref(0); |
Jeff Sharkey | 5aae0c9 | 2018-07-09 16:38:20 -0600 | [diff] [blame] | 905 | FileUtils.copy(incomingFd.getFileDescriptor(), targetFd, lengthBytes, null, |
| 906 | Runnable::run, (long progress) -> { |
| 907 | if (params.sizeBytes > 0) { |
| 908 | final long delta = progress - last.value; |
| 909 | last.value = progress; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 910 | synchronized (mLock) { |
| 911 | setClientProgressLocked(mClientProgress |
| 912 | + (float) delta / (float) params.sizeBytes); |
| 913 | } |
Jeff Sharkey | 5aae0c9 | 2018-07-09 16:38:20 -0600 | [diff] [blame] | 914 | } |
| 915 | }); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 916 | } finally { |
| 917 | IoUtils.closeQuietly(targetFd); |
| 918 | IoUtils.closeQuietly(incomingFd); |
| 919 | |
| 920 | // We're done here, so remove the "bridge" that was holding |
| 921 | // the session active. |
| 922 | synchronized (mLock) { |
| 923 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 924 | mFds.remove(fd); |
| 925 | } else { |
Chuanghua Zhao | b584c6e | 2018-10-17 20:00:04 +0800 | [diff] [blame] | 926 | bridge.forceClose(); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 927 | mBridges.remove(bridge); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | return null; |
| 932 | } else if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 933 | fd.init(mContext, targetFd); |
| 934 | return fd.getRevocableFileDescriptor(); |
| 935 | } else { |
| 936 | bridge.setTargetFile(targetFd); |
| 937 | bridge.start(); |
| 938 | return new ParcelFileDescriptor(bridge.getClientSocket()); |
| 939 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 940 | |
| 941 | } catch (ErrnoException e) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 942 | throw e.rethrowAsIOException(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| 946 | @Override |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 947 | public ParcelFileDescriptor openRead(String name) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 948 | if (isDataLoaderInstallation()) { |
| 949 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 950 | "Cannot read regular files in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 951 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 952 | synchronized (mLock) { |
| 953 | assertCallerIsOwnerOrRootLocked(); |
| 954 | assertPreparedAndNotCommittedOrDestroyedLocked("openRead"); |
| 955 | try { |
| 956 | return openReadInternalLocked(name); |
| 957 | } catch (IOException e) { |
| 958 | throw ExceptionUtils.wrap(e); |
| 959 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 963 | private ParcelFileDescriptor openReadInternalLocked(String name) throws IOException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 964 | try { |
| 965 | if (!FileUtils.isValidExtFilename(name)) { |
| 966 | throw new IllegalArgumentException("Invalid name: " + name); |
| 967 | } |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 968 | final File target = new File(stageDir, name); |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 969 | final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), O_RDONLY, 0); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 970 | return new ParcelFileDescriptor(targetFd); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 971 | } catch (ErrnoException e) { |
| 972 | throw e.rethrowAsIOException(); |
| 973 | } |
| 974 | } |
| 975 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 976 | /** |
| 977 | * Check if the caller is the owner of this session. Otherwise throw a |
| 978 | * {@link SecurityException}. |
| 979 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 980 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 981 | private void assertCallerIsOwnerOrRootLocked() { |
| 982 | final int callingUid = Binder.getCallingUid(); |
| 983 | if (callingUid != Process.ROOT_UID && callingUid != mInstallerUid) { |
| 984 | throw new SecurityException("Session does not belong to uid " + callingUid); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * If anybody is reading or writing data of the session, throw an {@link SecurityException}. |
| 990 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 991 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 992 | private void assertNoWriteFileTransfersOpenLocked() { |
| 993 | // Verify that all writers are hands-off |
| 994 | for (RevocableFileDescriptor fd : mFds) { |
| 995 | if (!fd.isRevoked()) { |
| 996 | throw new SecurityException("Files still open"); |
| 997 | } |
| 998 | } |
| 999 | for (FileBridge bridge : mBridges) { |
| 1000 | if (!bridge.isClosed()) { |
| 1001 | throw new SecurityException("Files still open"); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1006 | @Override |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 1007 | public void commit(@NonNull IntentSender statusReceiver, boolean forTransfer) { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 1008 | if (hasParentSessionId()) { |
| 1009 | throw new IllegalStateException( |
| 1010 | "Session " + sessionId + " is a child of multi-package session " |
| 1011 | + mParentSessionId + " and may not be committed directly."); |
| 1012 | } |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1013 | |
| 1014 | assertCanBeCommitted(forTransfer); |
| 1015 | |
| 1016 | if (isMultiPackage()) { |
| 1017 | for (int i = mChildSessionIds.size() - 1; i >= 0; --i) { |
| 1018 | final int childSessionId = mChildSessionIds.keyAt(i); |
| 1019 | mSessionProvider.getSession(childSessionId).assertCanBeCommitted(forTransfer); |
| 1020 | } |
| 1021 | } |
| 1022 | |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 1023 | if (mIncrementalFileStorages != null) { |
| 1024 | mIncrementalFileStorages.finishSetUp(); |
| 1025 | } |
| 1026 | |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1027 | mHandler.obtainMessage(MSG_SEAL, statusReceiver).sendToTarget(); |
| 1028 | } |
| 1029 | |
| 1030 | private void handleSeal(@NonNull IntentSender statusReceiver) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1031 | if (!markAsSealed(statusReceiver)) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1032 | return; |
| 1033 | } |
| 1034 | if (isMultiPackage()) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1035 | final SparseIntArray remainingSessions = mChildSessionIds.clone(); |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1036 | final IntentSender childIntentSender = |
| 1037 | new ChildStatusIntentReceiver(remainingSessions, statusReceiver) |
| 1038 | .getIntentSender(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1039 | boolean sealFailed = false; |
| 1040 | for (int i = mChildSessionIds.size() - 1; i >= 0; --i) { |
| 1041 | final int childSessionId = mChildSessionIds.keyAt(i); |
| 1042 | // seal all children, regardless if any of them fail; we'll throw/return |
| 1043 | // as appropriate once all children have been processed |
| 1044 | if (!mSessionProvider.getSession(childSessionId) |
| 1045 | .markAsSealed(childIntentSender)) { |
| 1046 | sealFailed = true; |
| 1047 | } |
| 1048 | } |
| 1049 | if (sealFailed) { |
| 1050 | return; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | dispatchStreamAndValidate(); |
| 1055 | } |
| 1056 | |
| 1057 | private void dispatchStreamAndValidate() { |
| 1058 | mHandler.obtainMessage(MSG_STREAM_AND_VALIDATE).sendToTarget(); |
| 1059 | } |
| 1060 | |
| 1061 | private void handleStreamAndValidate() { |
| 1062 | // TODO(b/136132412): update with new APIs |
| 1063 | if (mIncrementalFileStorages != null) { |
| 1064 | mIncrementalFileStorages.startLoading(); |
| 1065 | } |
| 1066 | |
| 1067 | boolean commitFailed = !markAsCommitted(); |
| 1068 | |
| 1069 | if (isMultiPackage()) { |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1070 | for (int i = mChildSessionIds.size() - 1; i >= 0; --i) { |
| 1071 | final int childSessionId = mChildSessionIds.keyAt(i); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1072 | // commit all children, regardless if any of them fail; we'll throw/return |
| 1073 | // as appropriate once all children have been processed |
| 1074 | if (!mSessionProvider.getSession(childSessionId) |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1075 | .markAsCommitted()) { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1076 | commitFailed = true; |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | if (commitFailed) { |
| 1082 | return; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1083 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1084 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1085 | mHandler.obtainMessage(MSG_COMMIT).sendToTarget(); |
| 1086 | } |
| 1087 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1088 | private final class FileSystemConnector extends |
| 1089 | IPackageInstallerSessionFileSystemConnector.Stub { |
| 1090 | final Set<String> mAddedFiles; |
| 1091 | |
| 1092 | FileSystemConnector(List<InstallationFile> addedFiles) { |
| 1093 | mAddedFiles = addedFiles.stream().map(file -> file.getName()).collect( |
| 1094 | Collectors.toSet()); |
| 1095 | } |
| 1096 | |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 1097 | @Override |
| 1098 | public void writeData(String name, long offsetBytes, long lengthBytes, |
| 1099 | ParcelFileDescriptor incomingFd) { |
| 1100 | if (incomingFd == null) { |
| 1101 | throw new IllegalArgumentException("incomingFd can't be null"); |
| 1102 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1103 | if (!mAddedFiles.contains(name)) { |
| 1104 | throw new SecurityException("File name is not in the list of added files."); |
| 1105 | } |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 1106 | try { |
| 1107 | doWriteInternal(name, offsetBytes, lengthBytes, incomingFd); |
| 1108 | } catch (IOException e) { |
| 1109 | throw ExceptionUtils.wrap(e); |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1114 | private class ChildStatusIntentReceiver { |
| 1115 | private final SparseIntArray mChildSessionsRemaining; |
| 1116 | private final IntentSender mStatusReceiver; |
| 1117 | private final IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 1118 | @Override |
| 1119 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 1120 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 1121 | statusUpdate(intent); |
| 1122 | } |
| 1123 | }; |
| 1124 | |
| 1125 | private ChildStatusIntentReceiver(SparseIntArray remainingSessions, |
| 1126 | IntentSender statusReceiver) { |
| 1127 | this.mChildSessionsRemaining = remainingSessions; |
| 1128 | this.mStatusReceiver = statusReceiver; |
| 1129 | } |
| 1130 | |
| 1131 | public IntentSender getIntentSender() { |
| 1132 | return new IntentSender((IIntentSender) mLocalSender); |
| 1133 | } |
| 1134 | |
| 1135 | public void statusUpdate(Intent intent) { |
| 1136 | mHandler.post(() -> { |
| 1137 | if (mChildSessionsRemaining.size() == 0) { |
| 1138 | return; |
| 1139 | } |
| 1140 | final int sessionId = intent.getIntExtra( |
| 1141 | PackageInstaller.EXTRA_SESSION_ID, 0); |
| 1142 | final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 1143 | PackageInstaller.STATUS_FAILURE); |
| 1144 | final int sessionIndex = mChildSessionsRemaining.indexOfKey(sessionId); |
| 1145 | if (PackageInstaller.STATUS_SUCCESS == status) { |
| 1146 | mChildSessionsRemaining.removeAt(sessionIndex); |
| 1147 | if (mChildSessionsRemaining.size() == 0) { |
| 1148 | try { |
| 1149 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, |
| 1150 | PackageInstallerSession.this.sessionId); |
| 1151 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1152 | } catch (IntentSender.SendIntentException ignore) { |
| 1153 | } |
| 1154 | } |
| 1155 | } else if (PackageInstaller.STATUS_PENDING_USER_ACTION == status) { |
| 1156 | try { |
| 1157 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1158 | } catch (IntentSender.SendIntentException ignore) { |
| 1159 | } |
| 1160 | } else { |
| 1161 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, |
| 1162 | PackageInstallerSession.this.sessionId); |
| 1163 | mChildSessionsRemaining.clear(); // we're done. Don't send any more. |
| 1164 | try { |
| 1165 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1166 | } catch (IntentSender.SendIntentException ignore) { |
| 1167 | } |
| 1168 | } |
| 1169 | }); |
| 1170 | } |
| 1171 | } |
| 1172 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1173 | /** {@hide} */ |
| 1174 | private class StreamingException extends Exception { |
| 1175 | StreamingException(Throwable cause) { |
| 1176 | super(cause); |
| 1177 | } |
| 1178 | } |
| 1179 | |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1180 | /** |
| 1181 | * Returns whether or not a package can be installed while Secure FRP is enabled. |
| 1182 | * <p> |
| 1183 | * Only callers with the INSTALL_PACKAGES permission are allowed to install. However, |
| 1184 | * prevent the package installer from installing anything because, while it has the |
| 1185 | * permission, it will allows packages to be installed from anywhere. |
| 1186 | */ |
| 1187 | private static boolean isSecureFrpInstallAllowed(Context context, int callingUid) { |
| 1188 | final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
| 1189 | final String[] systemInstaller = pmi.getKnownPackageNames( |
| 1190 | PackageManagerInternal.PACKAGE_INSTALLER, UserHandle.USER_SYSTEM); |
| 1191 | final AndroidPackage callingInstaller = pmi.getPackage(callingUid); |
| 1192 | if (callingInstaller != null |
| 1193 | && ArrayUtils.contains(systemInstaller, callingInstaller.getPackageName())) { |
| 1194 | // don't allow the system package installer to install while under secure FRP |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | // require caller to hold the INSTALL_PACKAGES permission |
| 1199 | return context.checkCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES) |
| 1200 | == PackageManager.PERMISSION_GRANTED; |
| 1201 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1202 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1203 | /** |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1204 | * Sanity checks to make sure it's ok to commit the session. |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1205 | */ |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1206 | private void assertCanBeCommitted(boolean forTransfer) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1207 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1208 | assertCallerIsOwnerOrRootLocked(); |
| 1209 | assertPreparedAndNotDestroyedLocked("commit"); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1210 | assertNoWriteFileTransfersOpenLocked(); |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 1211 | |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1212 | final boolean isSecureFrpEnabled = |
| 1213 | (Secure.getInt(mContext.getContentResolver(), Secure.SECURE_FRP_MODE, 0) == 1); |
| 1214 | if (isSecureFrpEnabled |
| 1215 | && !isSecureFrpInstallAllowed(mContext, Binder.getCallingUid())) { |
| 1216 | throw new SecurityException("Can't install packages while in secure FRP"); |
Todd Kennedy | 7e2b8e6 | 2019-11-25 15:36:12 -0800 | [diff] [blame] | 1217 | } |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1218 | |
Todd Kennedy | 7e2b8e6 | 2019-11-25 15:36:12 -0800 | [diff] [blame] | 1219 | if (forTransfer) { |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1220 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, null); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1221 | if (mInstallerUid == mOriginalInstallerUid) { |
| 1222 | throw new IllegalArgumentException("Session has not been transferred"); |
| 1223 | } |
| 1224 | } else { |
| 1225 | if (mInstallerUid != mOriginalInstallerUid) { |
| 1226 | throw new IllegalArgumentException("Session has been transferred"); |
| 1227 | } |
| 1228 | } |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /** |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1233 | * If this was not already called, the session will be sealed. |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1234 | * |
| 1235 | * This method may be called multiple times to update the status receiver validate caller |
| 1236 | * permissions. |
| 1237 | */ |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1238 | private boolean markAsSealed(@NonNull IntentSender statusReceiver) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1239 | Objects.requireNonNull(statusReceiver); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1240 | |
| 1241 | List<PackageInstallerSession> childSessions = getChildSessions(); |
| 1242 | |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1243 | synchronized (mLock) { |
| 1244 | mRemoteStatusReceiver = statusReceiver; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1245 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1246 | // After updating the observer, we can skip re-sealing. |
| 1247 | if (mSealed) { |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
| 1251 | try { |
| 1252 | sealLocked(childSessions); |
| 1253 | } catch (PackageManagerException e) { |
| 1254 | return false; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | // Persist the fact that we've sealed ourselves to prevent |
| 1259 | // mutations of any hard links we create. We do this without holding |
| 1260 | // the session lock, since otherwise it's a lock inversion. |
| 1261 | mCallback.onSessionSealedBlocking(this); |
| 1262 | |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | private boolean markAsCommitted() { |
| 1267 | synchronized (mLock) { |
| 1268 | Objects.requireNonNull(mRemoteStatusReceiver); |
| 1269 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1270 | if (mCommitted) { |
| 1271 | return true; |
| 1272 | } |
| 1273 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1274 | if (!streamAndValidateLocked()) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1275 | return false; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1276 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1277 | |
| 1278 | // Client staging is fully done at this point |
| 1279 | mClientProgress = 1f; |
| 1280 | computeProgressLocked(true); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1281 | |
| 1282 | // This ongoing commit should keep session active, even though client |
| 1283 | // will probably close their end. |
| 1284 | mActiveCount.incrementAndGet(); |
| 1285 | |
| 1286 | mCommitted = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1289 | return true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1292 | /** Return a list of child sessions or null if the session is not multipackage |
| 1293 | * |
| 1294 | * <p> This method is handy to prevent potential deadlocks (b/123391593) |
| 1295 | */ |
| 1296 | private @Nullable List<PackageInstallerSession> getChildSessions() { |
| 1297 | List<PackageInstallerSession> childSessions = null; |
| 1298 | if (isMultiPackage()) { |
| 1299 | final int[] childSessionIds = getChildSessionIds(); |
| 1300 | childSessions = new ArrayList<>(childSessionIds.length); |
| 1301 | for (int childSessionId : childSessionIds) { |
| 1302 | childSessions.add(mSessionProvider.getSession(childSessionId)); |
| 1303 | } |
| 1304 | } |
| 1305 | return childSessions; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Assert multipackage install has consistent sessions. |
| 1310 | * |
| 1311 | * @throws PackageManagerException if child sessions don't match parent session |
| 1312 | * in respect to staged and enable rollback parameters. |
| 1313 | */ |
| 1314 | @GuardedBy("mLock") |
| 1315 | private void assertMultiPackageConsistencyLocked( |
| 1316 | @NonNull List<PackageInstallerSession> childSessions) throws PackageManagerException { |
| 1317 | for (PackageInstallerSession childSession : childSessions) { |
| 1318 | // It might be that the parent session is loaded before all of it's child sessions are, |
| 1319 | // e.g. when reading sessions from XML. Those sessions will be null here, and their |
| 1320 | // conformance with the multipackage params will be checked when they're loaded. |
| 1321 | if (childSession == null) { |
| 1322 | continue; |
| 1323 | } |
| 1324 | assertConsistencyWithLocked(childSession); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * Assert consistency with the given session. |
| 1330 | * |
| 1331 | * @throws PackageManagerException if other sessions doesn't match this session |
| 1332 | * in respect to staged and enable rollback parameters. |
| 1333 | */ |
| 1334 | @GuardedBy("mLock") |
| 1335 | private void assertConsistencyWithLocked(PackageInstallerSession other) |
| 1336 | throws PackageManagerException { |
| 1337 | // Session groups must be consistent wrt to isStaged parameter. Non-staging session |
| 1338 | // cannot be grouped with staging sessions. |
| 1339 | if (this.params.isStaged != other.params.isStaged) { |
| 1340 | throw new PackageManagerException( |
| 1341 | PackageManager.INSTALL_FAILED_MULTIPACKAGE_INCONSISTENCY, |
| 1342 | "Multipackage Inconsistency: session " + other.sessionId |
| 1343 | + " and session " + sessionId |
| 1344 | + " have inconsistent staged settings"); |
| 1345 | } |
| 1346 | if (this.params.getEnableRollback() != other.params.getEnableRollback()) { |
| 1347 | throw new PackageManagerException( |
| 1348 | PackageManager.INSTALL_FAILED_MULTIPACKAGE_INCONSISTENCY, |
| 1349 | "Multipackage Inconsistency: session " + other.sessionId |
| 1350 | + " and session " + sessionId |
| 1351 | + " have inconsistent rollback settings"); |
| 1352 | } |
| 1353 | } |
| 1354 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1355 | /** |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1356 | * Seal the session to prevent further modification. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1357 | * |
| 1358 | * <p>The session will be sealed after calling this method even if it failed. |
| 1359 | * |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 1360 | * @throws PackageManagerException if the session was sealed but something went wrong. If the |
| 1361 | * session was sealed this is the only possible exception. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1362 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1363 | @GuardedBy("mLock") |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1364 | private void sealLocked(List<PackageInstallerSession> childSessions) |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1365 | throws PackageManagerException { |
| 1366 | try { |
| 1367 | assertNoWriteFileTransfersOpenLocked(); |
| 1368 | assertPreparedAndNotDestroyedLocked("sealing of session"); |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 1369 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1370 | mSealed = true; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1371 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1372 | if (childSessions != null) { |
| 1373 | assertMultiPackageConsistencyLocked(childSessions); |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1374 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1375 | } catch (PackageManagerException e) { |
| 1376 | throw onSessionVerificationFailure(e); |
| 1377 | } catch (Throwable e) { |
| 1378 | // Convert all exceptions into package manager exceptions as only those are handled |
| 1379 | // in the code above. |
| 1380 | throw onSessionVerificationFailure(new PackageManagerException(e)); |
| 1381 | } |
| 1382 | } |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1383 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1384 | /** |
| 1385 | * Prepare DataLoader and stream content for DataLoader sessions. |
| 1386 | * Validate the contents of all session. |
| 1387 | * |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1388 | * @return false if validation failed. |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1389 | */ |
| 1390 | @GuardedBy("mLock") |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1391 | private boolean streamAndValidateLocked() { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1392 | try { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1393 | // Read transfers from the original owner stay open, but as the session's data cannot |
| 1394 | // be modified anymore, there is no leak of information. For staged sessions, further |
| 1395 | // validation is performed by the staging manager. |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1396 | if (!params.isMultiPackage) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1397 | if (!prepareDataLoaderLocked()) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1401 | final PackageInfo pkgInfo = mPm.getPackageInfo( |
| 1402 | params.appPackageName, PackageManager.GET_SIGNATURES |
| 1403 | | PackageManager.MATCH_STATIC_SHARED_LIBRARIES /*flags*/, userId); |
| 1404 | |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1405 | if (isApexInstallation()) { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1406 | validateApexInstallLocked(); |
| 1407 | } else { |
| 1408 | validateApkInstallLocked(pkgInfo); |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1409 | } |
| 1410 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1411 | |
| 1412 | if (params.isStaged) { |
| 1413 | mStagingManager.checkNonOverlappingWithStagedSessions(this); |
| 1414 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1415 | |
| 1416 | return true; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1417 | } catch (PackageManagerException e) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1418 | onSessionVerificationFailure(e); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1419 | } catch (Throwable e) { |
| 1420 | // Convert all exceptions into package manager exceptions as only those are handled |
| 1421 | // in the code above. |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1422 | onSessionVerificationFailure(new PackageManagerException(e)); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1423 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1424 | return false; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1427 | private PackageManagerException onSessionVerificationFailure(PackageManagerException e) { |
| 1428 | // Session is sealed but could not be verified, we need to destroy it. |
| 1429 | destroyInternal(); |
| 1430 | // Dispatch message to remove session from PackageInstallerService. |
| 1431 | dispatchSessionFinished(e.error, ExceptionUtils.getCompleteMessage(e), null); |
| 1432 | |
| 1433 | return e; |
| 1434 | } |
| 1435 | |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1436 | /** |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1437 | * If session should be sealed, then it's sealed to prevent further modification. |
| 1438 | * If the session can't be sealed then it's destroyed. |
Mohammad Samiul Islam | 6367296 | 2020-01-22 12:02:38 +0000 | [diff] [blame] | 1439 | * |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1440 | * Additionally for staged APEX sessions read+validate the package and populate req'd fields. |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1441 | * |
| 1442 | * <p> This is meant to be called after all of the sessions are loaded and added to |
| 1443 | * PackageInstallerService |
| 1444 | */ |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1445 | void onAfterSessionRead() { |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1446 | synchronized (mLock) { |
Dario Freni | f2449f7 | 2019-05-02 15:46:03 +0100 | [diff] [blame] | 1447 | if (!mShouldBeSealed || isStagedAndInTerminalState()) { |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1448 | return; |
| 1449 | } |
| 1450 | } |
| 1451 | List<PackageInstallerSession> childSessions = getChildSessions(); |
| 1452 | synchronized (mLock) { |
| 1453 | try { |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1454 | sealLocked(childSessions); |
| 1455 | |
| 1456 | if (isApexInstallation()) { |
| 1457 | // APEX installations rely on certain fields to be populated after reboot. |
| 1458 | // E.g. mPackageName. |
| 1459 | validateApexInstallLocked(); |
| 1460 | } |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1461 | } catch (PackageManagerException e) { |
| 1462 | Slog.e(TAG, "Package not valid", e); |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 1467 | /** Update the timestamp of when the staged session last changed state */ |
| 1468 | public void markUpdated() { |
| 1469 | synchronized (mLock) { |
| 1470 | this.updatedMillis = System.currentTimeMillis(); |
| 1471 | } |
| 1472 | } |
| 1473 | |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1474 | @Override |
| 1475 | public void transfer(String packageName) { |
| 1476 | Objects.requireNonNull(packageName); |
| 1477 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1478 | ApplicationInfo newOwnerAppInfo = mPm.getApplicationInfo(packageName, 0, userId); |
| 1479 | if (newOwnerAppInfo == null) { |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1480 | throw new ParcelableException(new PackageManager.NameNotFoundException(packageName)); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | if (PackageManager.PERMISSION_GRANTED != mPm.checkUidPermission( |
| 1484 | Manifest.permission.INSTALL_PACKAGES, newOwnerAppInfo.uid)) { |
| 1485 | throw new SecurityException("Destination package " + packageName + " does not have " |
| 1486 | + "the " + Manifest.permission.INSTALL_PACKAGES + " permission"); |
| 1487 | } |
| 1488 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1489 | // Only install flags that can be verified by the app the session is transferred to are |
| 1490 | // allowed. The parameters can be read via PackageInstaller.SessionInfo. |
| 1491 | if (!params.areHiddenOptionsSet()) { |
| 1492 | throw new SecurityException("Can only transfer sessions that use public options"); |
| 1493 | } |
| 1494 | |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1495 | List<PackageInstallerSession> childSessions = getChildSessions(); |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1496 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1497 | synchronized (mLock) { |
| 1498 | assertCallerIsOwnerOrRootLocked(); |
| 1499 | assertPreparedAndNotSealedLocked("transfer"); |
| 1500 | |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1501 | try { |
| 1502 | sealLocked(childSessions); |
| 1503 | } catch (PackageManagerException e) { |
| 1504 | throw new IllegalArgumentException("Package is not valid", e); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1505 | } |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (!mPackageName.equals(mInstallSource.installerPackageName)) { |
| 1508 | throw new SecurityException("Can only transfer sessions that update the original " |
| 1509 | + "installer"); |
| 1510 | } |
| 1511 | |
| 1512 | mInstallerUid = newOwnerAppInfo.uid; |
| 1513 | mInstallSource = InstallSource.create(packageName, null, packageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // Persist the fact that we've sealed ourselves to prevent |
| 1517 | // mutations of any hard links we create. We do this without holding |
| 1518 | // the session lock, since otherwise it's a lock inversion. |
| 1519 | mCallback.onSessionSealedBlocking(this); |
| 1520 | } |
| 1521 | |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1522 | private void handleCommit() { |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 1523 | if (isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked()) { |
| 1524 | DevicePolicyEventLogger |
| 1525 | .createEvent(DevicePolicyEnums.INSTALL_PACKAGE) |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 1526 | .setAdmin(mInstallSource.installerPackageName) |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 1527 | .write(); |
| 1528 | } |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 1529 | if (params.isStaged) { |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1530 | mStagingManager.commitSession(this); |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 1531 | destroyInternal(); |
| 1532 | dispatchSessionFinished(PackageManager.INSTALL_SUCCEEDED, "Session staged", null); |
| 1533 | return; |
| 1534 | } |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1535 | |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1536 | if (isApexInstallation()) { |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1537 | destroyInternal(); |
| 1538 | dispatchSessionFinished(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 1539 | "APEX packages can only be installed using staged sessions.", null); |
| 1540 | return; |
Dario Freni | 3ad7361 | 2019-02-06 15:03:05 +0000 | [diff] [blame] | 1541 | } |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1542 | |
| 1543 | // For a multiPackage session, read the child sessions |
| 1544 | // outside of the lock, because reading the child |
| 1545 | // sessions with the lock held could lead to deadlock |
| 1546 | // (b/123391593). |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1547 | List<PackageInstallerSession> childSessions = getChildSessions(); |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1548 | |
| 1549 | try { |
| 1550 | synchronized (mLock) { |
| 1551 | commitNonStagedLocked(childSessions); |
| 1552 | } |
| 1553 | } catch (PackageManagerException e) { |
| 1554 | final String completeMsg = ExceptionUtils.getCompleteMessage(e); |
| 1555 | Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg); |
| 1556 | destroyInternal(); |
| 1557 | dispatchSessionFinished(e.error, completeMsg, null); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | @GuardedBy("mLock") |
| 1562 | private void commitNonStagedLocked(List<PackageInstallerSession> childSessions) |
| 1563 | throws PackageManagerException { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1564 | final PackageManagerService.ActiveInstallSession committingSession = |
| 1565 | makeSessionActiveLocked(); |
| 1566 | if (committingSession == null) { |
Philip P. Moltmann | d9d343c | 2018-07-03 15:17:11 -0700 | [diff] [blame] | 1567 | return; |
| 1568 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1569 | if (isMultiPackage()) { |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1570 | List<PackageManagerService.ActiveInstallSession> activeChildSessions = |
| 1571 | new ArrayList<>(childSessions.size()); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1572 | boolean success = true; |
| 1573 | PackageManagerException failure = null; |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1574 | for (int i = 0; i < childSessions.size(); ++i) { |
| 1575 | final PackageInstallerSession session = childSessions.get(i); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1576 | try { |
| 1577 | final PackageManagerService.ActiveInstallSession activeSession = |
| 1578 | session.makeSessionActiveLocked(); |
| 1579 | if (activeSession != null) { |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1580 | activeChildSessions.add(activeSession); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1581 | } |
| 1582 | } catch (PackageManagerException e) { |
| 1583 | failure = e; |
| 1584 | success = false; |
| 1585 | } |
| 1586 | } |
| 1587 | if (!success) { |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1588 | PackageInstallerService.sendOnPackageInstalled(mContext, |
| 1589 | mRemoteStatusReceiver, sessionId, |
| 1590 | isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId, null, |
| 1591 | failure.error, failure.getLocalizedMessage(), null); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1592 | return; |
| 1593 | } |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1594 | mPm.installStage(activeChildSessions); |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1595 | } else { |
Dario Freni | 3ad7361 | 2019-02-06 15:03:05 +0000 | [diff] [blame] | 1596 | mPm.installStage(committingSession); |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1600 | /** |
| 1601 | * Stages this session for install and returns a |
| 1602 | * {@link PackageManagerService.ActiveInstallSession} representing this new staged state or null |
| 1603 | * in case permissions need to be requested before install can proceed. |
| 1604 | */ |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1605 | @GuardedBy("mLock") |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1606 | private PackageManagerService.ActiveInstallSession makeSessionActiveLocked() |
| 1607 | throws PackageManagerException { |
| 1608 | if (mRelinquished) { |
| 1609 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1610 | "Session relinquished"); |
| 1611 | } |
| 1612 | if (mDestroyed) { |
| 1613 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session destroyed"); |
| 1614 | } |
| 1615 | if (!mSealed) { |
| 1616 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session not sealed"); |
Jie Song | 7e1c9d7 | 2018-11-07 22:59:18 +0000 | [diff] [blame] | 1617 | } |
Patrick | 44da627 | 2018-09-13 15:06:22 -0700 | [diff] [blame] | 1618 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1619 | final IPackageInstallObserver2 localObserver; |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1620 | if (isApexInstallation()) { |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1621 | localObserver = null; |
| 1622 | } else { |
| 1623 | if (!params.isMultiPackage) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1624 | Objects.requireNonNull(mPackageName); |
| 1625 | Objects.requireNonNull(mSigningDetails); |
| 1626 | Objects.requireNonNull(mResolvedBaseFile); |
Jie Song | 7e1c9d7 | 2018-11-07 22:59:18 +0000 | [diff] [blame] | 1627 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1628 | if (needToAskForPermissionsLocked()) { |
| 1629 | // User needs to confirm installation; |
| 1630 | // give installer an intent they can use to involve |
| 1631 | // user. |
| 1632 | final Intent intent = new Intent(PackageInstaller.ACTION_CONFIRM_INSTALL); |
| 1633 | intent.setPackage(mPm.getPackageInstallerPackageName()); |
| 1634 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1635 | |
| 1636 | PackageInstallerService.sendOnUserActionRequired(mContext, |
| 1637 | mRemoteStatusReceiver, sessionId, intent); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1638 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1639 | // Commit was keeping session marked as active until now; release |
| 1640 | // that extra refcount so session appears idle. |
| 1641 | closeInternal(false); |
| 1642 | return null; |
| 1643 | } |
| 1644 | |
| 1645 | // Inherit any packages and native libraries from existing install that |
| 1646 | // haven't been overridden. |
| 1647 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING) { |
| 1648 | try { |
| 1649 | final List<File> fromFiles = mResolvedInheritedFiles; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1650 | final File toDir = stageDir; |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1651 | |
| 1652 | if (LOGD) Slog.d(TAG, "Inherited files: " + mResolvedInheritedFiles); |
| 1653 | if (!mResolvedInheritedFiles.isEmpty() && mInheritedFilesBase == null) { |
| 1654 | throw new IllegalStateException("mInheritedFilesBase == null"); |
Patrick | 44da627 | 2018-09-13 15:06:22 -0700 | [diff] [blame] | 1655 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1656 | |
| 1657 | if (isLinkPossible(fromFiles, toDir)) { |
| 1658 | if (!mResolvedInstructionSets.isEmpty()) { |
| 1659 | final File oatDir = new File(toDir, "oat"); |
| 1660 | createOatDirs(mResolvedInstructionSets, oatDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1661 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1662 | // pre-create lib dirs for linking if necessary |
| 1663 | if (!mResolvedNativeLibPaths.isEmpty()) { |
| 1664 | for (String libPath : mResolvedNativeLibPaths) { |
| 1665 | // "/lib/arm64" -> ["lib", "arm64"] |
| 1666 | final int splitIndex = libPath.lastIndexOf('/'); |
| 1667 | if (splitIndex < 0 || splitIndex >= libPath.length() - 1) { |
| 1668 | Slog.e(TAG, |
| 1669 | "Skipping native library creation for linking due" |
| 1670 | + " to invalid path: " + libPath); |
| 1671 | continue; |
| 1672 | } |
| 1673 | final String libDirPath = libPath.substring(1, splitIndex); |
| 1674 | final File libDir = new File(toDir, libDirPath); |
| 1675 | if (!libDir.exists()) { |
| 1676 | NativeLibraryHelper.createNativeLibrarySubdir(libDir); |
| 1677 | } |
| 1678 | final String archDirPath = libPath.substring(splitIndex + 1); |
| 1679 | NativeLibraryHelper.createNativeLibrarySubdir( |
| 1680 | new File(libDir, archDirPath)); |
| 1681 | } |
| 1682 | } |
| 1683 | linkFiles(fromFiles, toDir, mInheritedFilesBase); |
| 1684 | } else { |
| 1685 | // TODO: this should delegate to DCS so the system process |
| 1686 | // avoids holding open FDs into containers. |
| 1687 | copyFiles(fromFiles, toDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1688 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1689 | } catch (IOException e) { |
| 1690 | throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE, |
| 1691 | "Failed to inherit existing install", e); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 1692 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1693 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1694 | |
| 1695 | // TODO: surface more granular state from dexopt |
| 1696 | mInternalProgress = 0.5f; |
| 1697 | computeProgressLocked(true); |
| 1698 | |
Songchun Fan | 13068a5 | 2019-12-12 15:56:06 -0800 | [diff] [blame] | 1699 | // Unpack native libraries for non-incremental installation |
Songchun Fan | 2b0b898 | 2020-01-07 13:27:03 -0800 | [diff] [blame] | 1700 | if (!isIncrementalInstallation()) { |
Songchun Fan | 13068a5 | 2019-12-12 15:56:06 -0800 | [diff] [blame] | 1701 | extractNativeLibraries(stageDir, params.abiOverride, mayInheritNativeLibs()); |
| 1702 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1703 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1704 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1705 | // We've reached point of no return; call into PMS to install the stage. |
| 1706 | // Regardless of success or failure we always destroy session. |
| 1707 | localObserver = new IPackageInstallObserver2.Stub() { |
| 1708 | @Override |
| 1709 | public void onUserActionRequired(Intent intent) { |
| 1710 | throw new IllegalStateException(); |
| 1711 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1712 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1713 | @Override |
| 1714 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 1715 | Bundle extras) { |
| 1716 | destroyInternal(); |
| 1717 | dispatchSessionFinished(returnCode, msg, extras); |
| 1718 | } |
| 1719 | }; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1720 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1721 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 1722 | final UserHandle user; |
| 1723 | if ((params.installFlags & PackageManager.INSTALL_ALL_USERS) != 0) { |
| 1724 | user = UserHandle.ALL; |
| 1725 | } else { |
| 1726 | user = new UserHandle(userId); |
| 1727 | } |
| 1728 | |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1729 | mRelinquished = true; |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1730 | return new PackageManagerService.ActiveInstallSession(mPackageName, stageDir, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 1731 | localObserver, params, mInstallerUid, mInstallSource, user, mSigningDetails); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1734 | private static void maybeRenameFile(File from, File to) throws PackageManagerException { |
| 1735 | if (!from.equals(to)) { |
| 1736 | if (!from.renameTo(to)) { |
| 1737 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1738 | "Could not rename file " + from + " to " + to); |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1743 | /** |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 1744 | * Returns true if the session should attempt to inherit any existing native libraries already |
| 1745 | * extracted at the current install location. This is necessary to prevent double loading of |
| 1746 | * native libraries already loaded by the running app. |
| 1747 | */ |
| 1748 | private boolean mayInheritNativeLibs() { |
| 1749 | return SystemProperties.getBoolean(PROPERTY_NAME_INHERIT_NATIVE, true) && |
| 1750 | params.mode == SessionParams.MODE_INHERIT_EXISTING && |
| 1751 | (params.installFlags & PackageManager.DONT_KILL_APP) != 0; |
| 1752 | } |
| 1753 | |
| 1754 | /** |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1755 | * Returns true if the session is installing an APEX package. |
| 1756 | */ |
| 1757 | private boolean isApexInstallation() { |
| 1758 | return (params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 1759 | } |
| 1760 | |
| 1761 | /** |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1762 | * Validate apex install. |
| 1763 | * <p> |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1764 | * Sets {@link #mResolvedBaseFile} for RollbackManager to use. Sets {@link #mPackageName} for |
| 1765 | * StagingManager to use. |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1766 | */ |
| 1767 | @GuardedBy("mLock") |
| 1768 | private void validateApexInstallLocked() |
| 1769 | throws PackageManagerException { |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 1770 | final File[] addedFiles = getAddedApksLocked(); |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1771 | if (ArrayUtils.isEmpty(addedFiles)) { |
| 1772 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "No packages staged"); |
| 1773 | } |
| 1774 | |
| 1775 | if (ArrayUtils.size(addedFiles) > 1) { |
| 1776 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1777 | "Too many files for apex install"); |
| 1778 | } |
| 1779 | |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1780 | File addedFile = addedFiles[0]; // there is only one file |
| 1781 | |
| 1782 | // Ensure file name has proper suffix |
| 1783 | final String sourceName = addedFile.getName(); |
| 1784 | final String targetName = sourceName.endsWith(APEX_FILE_EXTENSION) |
| 1785 | ? sourceName |
| 1786 | : sourceName + APEX_FILE_EXTENSION; |
| 1787 | if (!FileUtils.isValidExtFilename(targetName)) { |
| 1788 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1789 | "Invalid filename: " + targetName); |
| 1790 | } |
| 1791 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1792 | final File targetFile = new File(stageDir, targetName); |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1793 | resolveAndStageFile(addedFile, targetFile); |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1794 | mResolvedBaseFile = targetFile; |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1795 | |
| 1796 | // Populate package name of the apex session |
| 1797 | mPackageName = null; |
| 1798 | final ApkLite apk; |
| 1799 | try { |
| 1800 | apk = PackageParser.parseApkLite( |
| 1801 | mResolvedBaseFile, PackageParser.PARSE_COLLECT_CERTIFICATES); |
| 1802 | } catch (PackageParserException e) { |
| 1803 | throw PackageManagerException.from(e); |
| 1804 | } |
| 1805 | |
| 1806 | if (mPackageName == null) { |
| 1807 | mPackageName = apk.packageName; |
| 1808 | mVersionCode = apk.getLongVersionCode(); |
| 1809 | } |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /** |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1813 | * Validate install by confirming that all application packages are have |
| 1814 | * consistent package name, version code, and signing certificates. |
| 1815 | * <p> |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1816 | * Clears and populates {@link #mResolvedBaseFile}, |
| 1817 | * {@link #mResolvedStagedFiles}, and {@link #mResolvedInheritedFiles}. |
| 1818 | * <p> |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1819 | * Renames package files in stage to match split names defined inside. |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1820 | * <p> |
| 1821 | * Note that upgrade compatibility is still performed by |
| 1822 | * {@link PackageManagerService}. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1823 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1824 | @GuardedBy("mLock") |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1825 | private void validateApkInstallLocked(@Nullable PackageInfo pkgInfo) |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 1826 | throws PackageManagerException { |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 1827 | ApkLite baseApk = null; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1828 | mPackageName = null; |
| 1829 | mVersionCode = -1; |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 1830 | mSigningDetails = PackageParser.SigningDetails.UNKNOWN; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1831 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1832 | mResolvedBaseFile = null; |
| 1833 | mResolvedStagedFiles.clear(); |
| 1834 | mResolvedInheritedFiles.clear(); |
| 1835 | |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 1836 | // Partial installs must be consistent with existing install |
| 1837 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING |
| 1838 | && (pkgInfo == null || pkgInfo.applicationInfo == null)) { |
| 1839 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1840 | "Missing existing base package"); |
| 1841 | } |
| 1842 | // Default to require only if existing base has fs-verity. |
Victor Hsieh | 0663df4 | 2019-01-07 15:28:27 -0800 | [diff] [blame] | 1843 | mVerityFound = PackageManagerServiceUtils.isApkVerityEnabled() |
| 1844 | && params.mode == SessionParams.MODE_INHERIT_EXISTING |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 1845 | && VerityUtils.hasFsverity(pkgInfo.applicationInfo.getBaseCodePath()); |
| 1846 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1847 | final File[] removedFiles = getRemovedFilesLocked(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1848 | final List<String> removeSplitList = new ArrayList<>(); |
| 1849 | if (!ArrayUtils.isEmpty(removedFiles)) { |
| 1850 | for (File removedFile : removedFiles) { |
| 1851 | final String fileName = removedFile.getName(); |
| 1852 | final String splitName = fileName.substring( |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1853 | 0, fileName.length() - REMOVE_MARKER_EXTENSION.length()); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1854 | removeSplitList.add(splitName); |
| 1855 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 1858 | final File[] addedFiles = getAddedApksLocked(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1859 | if (ArrayUtils.isEmpty(addedFiles) && removeSplitList.size() == 0) { |
| 1860 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "No packages staged"); |
| 1861 | } |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1862 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1863 | // Verify that all staged packages are internally consistent |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1864 | final ArraySet<String> stagedSplits = new ArraySet<>(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1865 | for (File addedFile : addedFiles) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1866 | final ApkLite apk; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1867 | try { |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 1868 | apk = ApkLiteParseUtils.parseApkLite( |
Patrick Baumann | 2aede85 | 2018-01-04 12:17:22 -0800 | [diff] [blame] | 1869 | addedFile, PackageParser.PARSE_COLLECT_CERTIFICATES); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1870 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 1871 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1874 | if (!stagedSplits.add(apk.splitName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1875 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1876 | "Split " + apk.splitName + " was defined multiple times"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | // Use first package to define unknown values |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 1880 | if (mPackageName == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1881 | mPackageName = apk.packageName; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1882 | mVersionCode = apk.getLongVersionCode(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1883 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 1884 | if (mSigningDetails == PackageParser.SigningDetails.UNKNOWN) { |
| 1885 | mSigningDetails = apk.signingDetails; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1888 | assertApkConsistentLocked(String.valueOf(addedFile), apk); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1889 | |
| 1890 | // Take this opportunity to enforce uniform naming |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1891 | final String targetName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1892 | if (apk.splitName == null) { |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1893 | targetName = "base" + APK_FILE_EXTENSION; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1894 | } else { |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1895 | targetName = "split_" + apk.splitName + APK_FILE_EXTENSION; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1896 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1897 | if (!FileUtils.isValidExtFilename(targetName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1898 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1899 | "Invalid filename: " + targetName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1900 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1901 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1902 | final File targetFile = new File(stageDir, targetName); |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 1903 | resolveAndStageFile(addedFile, targetFile); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1904 | |
| 1905 | // Base is coming from session |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1906 | if (apk.splitName == null) { |
| 1907 | mResolvedBaseFile = targetFile; |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 1908 | baseApk = apk; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1909 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1910 | |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1911 | final File dexMetadataFile = DexMetadataHelper.findDexMetadataForFile(addedFile); |
| 1912 | if (dexMetadataFile != null) { |
| 1913 | if (!FileUtils.isValidExtFilename(dexMetadataFile.getName())) { |
| 1914 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1915 | "Invalid filename: " + dexMetadataFile); |
| 1916 | } |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1917 | final File targetDexMetadataFile = new File(stageDir, |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1918 | DexMetadataHelper.buildDexMetadataPathForApk(targetName)); |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 1919 | resolveAndStageFile(dexMetadataFile, targetDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1920 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1923 | if (removeSplitList.size() > 0) { |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 1924 | if (pkgInfo == null) { |
| 1925 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1926 | "Missing existing base package for " + mPackageName); |
| 1927 | } |
| 1928 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1929 | // validate split names marked for removal |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1930 | for (String splitName : removeSplitList) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1931 | if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1932 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1933 | "Split not found: " + splitName); |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | // ensure we've got appropriate package name, version code and signatures |
| 1938 | if (mPackageName == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1939 | mPackageName = pkgInfo.packageName; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 1940 | mVersionCode = pkgInfo.getLongVersionCode(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1941 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 1942 | if (mSigningDetails == PackageParser.SigningDetails.UNKNOWN) { |
| 1943 | try { |
Gavin Corkery | ed521ab | 2019-01-31 16:59:41 +0000 | [diff] [blame] | 1944 | mSigningDetails = ApkSignatureVerifier.unsafeGetCertsWithoutVerification( |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 1945 | pkgInfo.applicationInfo.sourceDir, |
| 1946 | PackageParser.SigningDetails.SignatureSchemeVersion.JAR); |
| 1947 | } catch (PackageParserException e) { |
| 1948 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1949 | "Couldn't obtain signatures from base APK"); |
| 1950 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1954 | if (params.mode == SessionParams.MODE_FULL_INSTALL) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1955 | // Full installs must include a base package |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1956 | if (!stagedSplits.contains(null)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1957 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1958 | "Full install must include a base package"); |
| 1959 | } |
| 1960 | |
| 1961 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1962 | final PackageLite existing; |
| 1963 | final ApkLite existingBase; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1964 | ApplicationInfo appInfo = pkgInfo.applicationInfo; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1965 | try { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1966 | existing = PackageParser.parsePackageLite(new File(appInfo.getCodePath()), 0); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 1967 | existingBase = ApkLiteParseUtils.parseApkLite(new File(appInfo.getBaseCodePath()), |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1968 | PackageParser.PARSE_COLLECT_CERTIFICATES); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1969 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 1970 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1973 | assertApkConsistentLocked("Existing base", existingBase); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1974 | |
| 1975 | // Inherit base if not overridden |
| 1976 | if (mResolvedBaseFile == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1977 | mResolvedBaseFile = new File(appInfo.getBaseCodePath()); |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 1978 | resolveInheritedFile(mResolvedBaseFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1979 | // Inherit the dex metadata if present. |
| 1980 | final File baseDexMetadataFile = |
| 1981 | DexMetadataHelper.findDexMetadataForFile(mResolvedBaseFile); |
| 1982 | if (baseDexMetadataFile != null) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 1983 | resolveInheritedFile(baseDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1984 | } |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 1985 | baseApk = existingBase; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | // Inherit splits if not overridden |
| 1989 | if (!ArrayUtils.isEmpty(existing.splitNames)) { |
| 1990 | for (int i = 0; i < existing.splitNames.length; i++) { |
| 1991 | final String splitName = existing.splitNames[i]; |
| 1992 | final File splitFile = new File(existing.splitCodePaths[i]); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1993 | final boolean splitRemoved = removeSplitList.contains(splitName); |
| 1994 | if (!stagedSplits.contains(splitName) && !splitRemoved) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 1995 | resolveInheritedFile(splitFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1996 | // Inherit the dex metadata if present. |
| 1997 | final File splitDexMetadataFile = |
| 1998 | DexMetadataHelper.findDexMetadataForFile(splitFile); |
| 1999 | if (splitDexMetadataFile != null) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2000 | resolveInheritedFile(splitDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2001 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2005 | |
| 2006 | // Inherit compiled oat directory. |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2007 | final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2008 | mInheritedFilesBase = packageInstallDir; |
| 2009 | final File oatDir = new File(packageInstallDir, "oat"); |
| 2010 | if (oatDir.exists()) { |
| 2011 | final File[] archSubdirs = oatDir.listFiles(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2012 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2013 | // Keep track of all instruction sets we've seen compiled output for. |
| 2014 | // If we're linking (and not copying) inherited files, we can recreate the |
| 2015 | // instruction set hierarchy and link compiled output. |
| 2016 | if (archSubdirs != null && archSubdirs.length > 0) { |
| 2017 | final String[] instructionSets = InstructionSets.getAllDexCodeInstructionSets(); |
| 2018 | for (File archSubDir : archSubdirs) { |
| 2019 | // Skip any directory that isn't an ISA subdir. |
| 2020 | if (!ArrayUtils.contains(instructionSets, archSubDir.getName())) { |
| 2021 | continue; |
| 2022 | } |
| 2023 | |
| 2024 | mResolvedInstructionSets.add(archSubDir.getName()); |
| 2025 | List<File> oatFiles = Arrays.asList(archSubDir.listFiles()); |
Calin Juravle | 4a4a4e8 | 2017-10-13 23:46:26 +0000 | [diff] [blame] | 2026 | if (!oatFiles.isEmpty()) { |
| 2027 | mResolvedInheritedFiles.addAll(oatFiles); |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2028 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | } |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2032 | |
| 2033 | // Inherit native libraries for DONT_KILL sessions. |
| 2034 | if (mayInheritNativeLibs() && removeSplitList.isEmpty()) { |
| 2035 | File[] libDirs = new File[]{ |
| 2036 | new File(packageInstallDir, NativeLibraryHelper.LIB_DIR_NAME), |
| 2037 | new File(packageInstallDir, NativeLibraryHelper.LIB64_DIR_NAME)}; |
| 2038 | for (File libDir : libDirs) { |
| 2039 | if (!libDir.exists() || !libDir.isDirectory()) { |
| 2040 | continue; |
| 2041 | } |
| 2042 | final List<File> libDirsToInherit = new LinkedList<>(); |
| 2043 | for (File archSubDir : libDir.listFiles()) { |
| 2044 | if (!archSubDir.isDirectory()) { |
| 2045 | continue; |
| 2046 | } |
| 2047 | String relLibPath; |
| 2048 | try { |
| 2049 | relLibPath = getRelativePath(archSubDir, packageInstallDir); |
| 2050 | } catch (IOException e) { |
| 2051 | Slog.e(TAG, "Skipping linking of native library directory!", e); |
| 2052 | // shouldn't be possible, but let's avoid inheriting these to be safe |
| 2053 | libDirsToInherit.clear(); |
| 2054 | break; |
| 2055 | } |
| 2056 | if (!mResolvedNativeLibPaths.contains(relLibPath)) { |
| 2057 | mResolvedNativeLibPaths.add(relLibPath); |
| 2058 | } |
| 2059 | libDirsToInherit.addAll(Arrays.asList(archSubDir.listFiles())); |
| 2060 | } |
| 2061 | mResolvedInheritedFiles.addAll(libDirsToInherit); |
| 2062 | } |
| 2063 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2064 | } |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2065 | if (baseApk.useEmbeddedDex) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2066 | for (File file : mResolvedStagedFiles) { |
| 2067 | if (file.getName().endsWith(".apk") |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2068 | && !DexManager.auditUncompressedDexInApk(file.getPath())) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2069 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2070 | "Some dex are not uncompressed and aligned correctly for " |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2071 | + mPackageName); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 2075 | if (baseApk.isSplitRequired && stagedSplits.size() <= 1) { |
| 2076 | throw new PackageManagerException(INSTALL_FAILED_MISSING_SPLIT, |
| 2077 | "Missing split for " + mPackageName); |
| 2078 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2081 | private void resolveAndStageFile(File origFile, File targetFile) |
| 2082 | throws PackageManagerException { |
| 2083 | mResolvedStagedFiles.add(targetFile); |
| 2084 | maybeRenameFile(origFile, targetFile); |
| 2085 | |
| 2086 | final File originalSignature = new File( |
| 2087 | VerityUtils.getFsveritySignatureFilePath(origFile.getPath())); |
| 2088 | // Make sure .fsv_sig exists when it should, then resolve and stage it. |
| 2089 | if (originalSignature.exists()) { |
| 2090 | // mVerityFound can only change from false to true here during the staging loop. Since |
| 2091 | // all or none of files should have .fsv_sig, this should only happen in the first time |
| 2092 | // (or never), otherwise bail out. |
| 2093 | if (!mVerityFound) { |
| 2094 | mVerityFound = true; |
| 2095 | if (mResolvedStagedFiles.size() > 1) { |
| 2096 | throw new PackageManagerException(INSTALL_FAILED_BAD_SIGNATURE, |
| 2097 | "Some file is missing fs-verity signature"); |
| 2098 | } |
| 2099 | } |
| 2100 | } else { |
| 2101 | if (!mVerityFound) { |
| 2102 | return; |
| 2103 | } |
| 2104 | throw new PackageManagerException(INSTALL_FAILED_BAD_SIGNATURE, |
| 2105 | "Missing corresponding fs-verity signature to " + origFile); |
| 2106 | } |
| 2107 | |
| 2108 | final File stagedSignature = new File( |
| 2109 | VerityUtils.getFsveritySignatureFilePath(targetFile.getPath())); |
| 2110 | maybeRenameFile(originalSignature, stagedSignature); |
| 2111 | mResolvedStagedFiles.add(stagedSignature); |
| 2112 | } |
| 2113 | |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2114 | private void resolveInheritedFile(File origFile) { |
| 2115 | mResolvedInheritedFiles.add(origFile); |
| 2116 | |
| 2117 | // Inherit the fsverity signature file if present. |
| 2118 | final File fsveritySignatureFile = new File( |
| 2119 | VerityUtils.getFsveritySignatureFilePath(origFile.getPath())); |
| 2120 | if (fsveritySignatureFile.exists()) { |
| 2121 | mResolvedInheritedFiles.add(fsveritySignatureFile); |
| 2122 | } |
| 2123 | } |
| 2124 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2125 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2126 | private void assertApkConsistentLocked(String tag, ApkLite apk) |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2127 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2128 | if (!mPackageName.equals(apk.packageName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2129 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag + " package " |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2130 | + apk.packageName + " inconsistent with " + mPackageName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2131 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2132 | if (params.appPackageName != null && !params.appPackageName.equals(apk.packageName)) { |
| 2133 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
| 2134 | + " specified package " + params.appPackageName |
| 2135 | + " inconsistent with " + apk.packageName); |
| 2136 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 2137 | if (mVersionCode != apk.getLongVersionCode()) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2138 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2139 | + " version code " + apk.versionCode + " inconsistent with " |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2140 | + mVersionCode); |
| 2141 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2142 | if (!mSigningDetails.signaturesMatchExactly(apk.signingDetails)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2143 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2144 | tag + " signatures are inconsistent"); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /** |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 2149 | * Determine if creating hard links between source and destination is |
| 2150 | * possible. That is, do they all live on the same underlying device. |
| 2151 | */ |
| 2152 | private boolean isLinkPossible(List<File> fromFiles, File toDir) { |
| 2153 | try { |
| 2154 | final StructStat toStat = Os.stat(toDir.getAbsolutePath()); |
| 2155 | for (File fromFile : fromFiles) { |
| 2156 | final StructStat fromStat = Os.stat(fromFile.getAbsolutePath()); |
| 2157 | if (fromStat.st_dev != toStat.st_dev) { |
| 2158 | return false; |
| 2159 | } |
| 2160 | } |
| 2161 | } catch (ErrnoException e) { |
| 2162 | Slog.w(TAG, "Failed to detect if linking possible: " + e); |
| 2163 | return false; |
| 2164 | } |
| 2165 | return true; |
| 2166 | } |
| 2167 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2168 | /** |
| 2169 | * @return the uid of the owner this session |
| 2170 | */ |
| 2171 | public int getInstallerUid() { |
| 2172 | synchronized (mLock) { |
| 2173 | return mInstallerUid; |
| 2174 | } |
| 2175 | } |
| 2176 | |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 2177 | /** |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 2178 | * @return the package name of this session |
| 2179 | */ |
| 2180 | String getPackageName() { |
| 2181 | synchronized (mLock) { |
| 2182 | return mPackageName; |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | /** |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 2187 | * @return the timestamp of when this session last changed state |
| 2188 | */ |
| 2189 | public long getUpdatedMillis() { |
| 2190 | synchronized (mLock) { |
| 2191 | return updatedMillis; |
| 2192 | } |
| 2193 | } |
| 2194 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2195 | String getInstallerPackageName() { |
Alan Stokes | 0f0b3e5 | 2020-01-27 12:09:01 +0000 | [diff] [blame] | 2196 | return getInstallSource().installerPackageName; |
| 2197 | } |
| 2198 | |
| 2199 | InstallSource getInstallSource() { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2200 | synchronized (mLock) { |
Alan Stokes | 0f0b3e5 | 2020-01-27 12:09:01 +0000 | [diff] [blame] | 2201 | return mInstallSource; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2205 | private static String getRelativePath(File file, File base) throws IOException { |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2206 | final String pathStr = file.getAbsolutePath(); |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2207 | final String baseStr = base.getAbsolutePath(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2208 | // Don't allow relative paths. |
| 2209 | if (pathStr.contains("/.") ) { |
| 2210 | throw new IOException("Invalid path (was relative) : " + pathStr); |
| 2211 | } |
| 2212 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2213 | if (pathStr.startsWith(baseStr)) { |
| 2214 | return pathStr.substring(baseStr.length()); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2215 | } |
| 2216 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2217 | throw new IOException("File: " + pathStr + " outside base: " + baseStr); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2218 | } |
| 2219 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2220 | private void createOatDirs(List<String> instructionSets, File fromDir) |
| 2221 | throws PackageManagerException { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2222 | for (String instructionSet : instructionSets) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2223 | try { |
| 2224 | mPm.mInstaller.createOatDir(fromDir.getAbsolutePath(), instructionSet); |
| 2225 | } catch (InstallerException e) { |
| 2226 | throw PackageManagerException.from(e); |
| 2227 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | private void linkFiles(List<File> fromFiles, File toDir, File fromDir) |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2232 | throws IOException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2233 | for (File fromFile : fromFiles) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2234 | final String relativePath = getRelativePath(fromFile, fromDir); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2235 | try { |
| 2236 | mPm.mInstaller.linkFile(relativePath, fromDir.getAbsolutePath(), |
| 2237 | toDir.getAbsolutePath()); |
| 2238 | } catch (InstallerException e) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2239 | throw new IOException("failed linkOrCreateDir(" + relativePath + ", " |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2240 | + fromDir + ", " + toDir + ")", e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2241 | } |
| 2242 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2243 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2244 | Slog.d(TAG, "Linked " + fromFiles.size() + " files into " + toDir); |
| 2245 | } |
| 2246 | |
| 2247 | private static void copyFiles(List<File> fromFiles, File toDir) throws IOException { |
| 2248 | // Remove any partial files from previous attempt |
| 2249 | for (File file : toDir.listFiles()) { |
| 2250 | if (file.getName().endsWith(".tmp")) { |
| 2251 | file.delete(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2252 | } |
| 2253 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 2254 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2255 | for (File fromFile : fromFiles) { |
| 2256 | final File tmpFile = File.createTempFile("inherit", ".tmp", toDir); |
| 2257 | if (LOGD) Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile); |
| 2258 | if (!FileUtils.copyFile(fromFile, tmpFile)) { |
| 2259 | throw new IOException("Failed to copy " + fromFile + " to " + tmpFile); |
| 2260 | } |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 2261 | try { |
| 2262 | Os.chmod(tmpFile.getAbsolutePath(), 0644); |
| 2263 | } catch (ErrnoException e) { |
| 2264 | throw new IOException("Failed to chmod " + tmpFile); |
| 2265 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2266 | final File toFile = new File(toDir, fromFile.getName()); |
| 2267 | if (LOGD) Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile); |
| 2268 | if (!tmpFile.renameTo(toFile)) { |
| 2269 | throw new IOException("Failed to rename " + tmpFile + " to " + toFile); |
| 2270 | } |
| 2271 | } |
| 2272 | Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir); |
| 2273 | } |
| 2274 | |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2275 | private static void extractNativeLibraries(File packageDir, String abiOverride, boolean inherit) |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2276 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2277 | final File libDir = new File(packageDir, NativeLibraryHelper.LIB_DIR_NAME); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2278 | if (!inherit) { |
| 2279 | // Start from a clean slate |
| 2280 | NativeLibraryHelper.removeNativeBinariesFromDirLI(libDir, true); |
| 2281 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2282 | |
| 2283 | NativeLibraryHelper.Handle handle = null; |
| 2284 | try { |
| 2285 | handle = NativeLibraryHelper.Handle.create(packageDir); |
| 2286 | final int res = NativeLibraryHelper.copyNativeBinariesWithOverride(handle, libDir, |
| 2287 | abiOverride); |
| 2288 | if (res != PackageManager.INSTALL_SUCCEEDED) { |
| 2289 | throw new PackageManagerException(res, |
| 2290 | "Failed to extract native libraries, res=" + res); |
| 2291 | } |
| 2292 | } catch (IOException e) { |
| 2293 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 2294 | "Failed to extract native libraries", e); |
| 2295 | } finally { |
| 2296 | IoUtils.closeQuietly(handle); |
| 2297 | } |
| 2298 | } |
| 2299 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2300 | void setPermissionsResult(boolean accepted) { |
| 2301 | if (!mSealed) { |
| 2302 | throw new SecurityException("Must be sealed to accept permissions"); |
| 2303 | } |
| 2304 | |
| 2305 | if (accepted) { |
| 2306 | // Mark and kick off another install pass |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 2307 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2308 | mPermissionsManuallyAccepted = true; |
| 2309 | mHandler.obtainMessage(MSG_COMMIT).sendToTarget(); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 2310 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2311 | } else { |
| 2312 | destroyInternal(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 2313 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "User rejected permissions", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2317 | /** |
| 2318 | * Adds a child session ID without any safety / sanity checks. This should only be used to |
| 2319 | * build a session from XML or similar. |
| 2320 | */ |
| 2321 | void addChildSessionIdInternal(int sessionId) { |
| 2322 | mChildSessionIds.put(sessionId, 0); |
| 2323 | } |
| 2324 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2325 | public void open() throws IOException { |
| 2326 | if (mActiveCount.getAndIncrement() == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 2327 | mCallback.onSessionActiveChanged(this, true); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2328 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2329 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2330 | boolean wasPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2331 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2332 | wasPrepared = mPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2333 | if (!mPrepared) { |
| 2334 | if (stageDir != null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 2335 | prepareStageDir(stageDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2336 | } else if (params.isMultiPackage) { |
| 2337 | // it's all ok |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2338 | } else { |
Jeff Sharkey | f8bb244 | 2017-09-21 19:09:30 -0600 | [diff] [blame] | 2339 | throw new IllegalArgumentException("stageDir must be set"); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | mPrepared = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2343 | } |
| 2344 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2345 | |
| 2346 | if (!wasPrepared) { |
| 2347 | mCallback.onSessionPrepared(this); |
| 2348 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2351 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 2352 | public void close() { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 2353 | closeInternal(true); |
| 2354 | } |
| 2355 | |
| 2356 | private void closeInternal(boolean checkCaller) { |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2357 | int activeCount; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2358 | synchronized (mLock) { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 2359 | if (checkCaller) { |
| 2360 | assertCallerIsOwnerOrRootLocked(); |
| 2361 | } |
| 2362 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2363 | activeCount = mActiveCount.decrementAndGet(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2366 | if (activeCount == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 2367 | mCallback.onSessionActiveChanged(this, false); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 2368 | } |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | @Override |
| 2372 | public void abandon() { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2373 | if (hasParentSessionId()) { |
| 2374 | throw new IllegalStateException( |
| 2375 | "Session " + sessionId + " is a child of multi-package session " |
| 2376 | + mParentSessionId + " and may not be abandoned directly."); |
| 2377 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2378 | synchronized (mLock) { |
| 2379 | assertCallerIsOwnerOrRootLocked(); |
| 2380 | |
Dario Freni | 503c8a0 | 2019-04-25 10:57:50 +0100 | [diff] [blame] | 2381 | if (isStagedAndInTerminalState()) { |
| 2382 | // We keep the session in the database if it's in a finalized state. It will be |
| 2383 | // removed by PackageInstallerService when the last update time is old enough. |
| 2384 | // Also, in such cases cleanStageDir() has already been executed so no need to |
| 2385 | // do it now. |
| 2386 | return; |
| 2387 | } |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 2388 | if (mCommitted && params.isStaged) { |
| 2389 | synchronized (mLock) { |
| 2390 | mDestroyed = true; |
| 2391 | } |
| 2392 | mStagingManager.abortCommittedSession(this); |
| 2393 | |
shafik | 988f779 | 2019-02-26 12:15:57 +0000 | [diff] [blame] | 2394 | cleanStageDir(); |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2397 | if (mRelinquished) { |
| 2398 | Slog.d(TAG, "Ignoring abandon after commit relinquished control"); |
| 2399 | return; |
| 2400 | } |
| 2401 | destroyInternal(); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 2402 | } |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 2403 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "Session was abandoned", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2406 | @Override |
| 2407 | public boolean isMultiPackage() { |
| 2408 | return params.isMultiPackage; |
| 2409 | } |
| 2410 | |
| 2411 | @Override |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 2412 | public boolean isStaged() { |
| 2413 | return params.isStaged; |
| 2414 | } |
| 2415 | |
| 2416 | @Override |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2417 | public DataLoaderParamsParcel getDataLoaderParams() { |
| 2418 | return params.dataLoaderParams != null ? params.dataLoaderParams.getData() : null; |
| 2419 | } |
| 2420 | |
| 2421 | @Override |
| 2422 | public void addFile(int location, String name, long lengthBytes, byte[] metadata, |
| 2423 | byte[] signature) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2424 | if (!isDataLoaderInstallation()) { |
| 2425 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2426 | "Cannot add files to non-data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2427 | } |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2428 | if (!isIncrementalInstallation()) { |
| 2429 | if (location != LOCATION_DATA_APP) { |
| 2430 | throw new IllegalArgumentException( |
| 2431 | "Non-incremental installation only supports /data/app placement: " + name); |
| 2432 | } |
| 2433 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2434 | // Use installer provided name for now; we always rename later |
| 2435 | if (!FileUtils.isValidExtFilename(name)) { |
| 2436 | throw new IllegalArgumentException("Invalid name: " + name); |
| 2437 | } |
| 2438 | |
| 2439 | synchronized (mLock) { |
| 2440 | assertCallerIsOwnerOrRootLocked(); |
| 2441 | assertPreparedAndNotSealedLocked("addFile"); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2442 | |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2443 | mFiles.add(FileInfo.added(location, name, lengthBytes, metadata, signature)); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | @Override |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2448 | public void removeFile(int location, String name) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2449 | if (!isDataLoaderInstallation()) { |
| 2450 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2451 | "Cannot add files to non-data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2452 | } |
| 2453 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 2454 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 2455 | } |
| 2456 | |
| 2457 | synchronized (mLock) { |
| 2458 | assertCallerIsOwnerOrRootLocked(); |
| 2459 | assertPreparedAndNotSealedLocked("removeFile"); |
| 2460 | |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2461 | mFiles.add(FileInfo.removed(location, getRemoveMarkerName(name))); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | /** |
| 2466 | * Makes sure files are present in staging location. |
| 2467 | */ |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2468 | @GuardedBy("mLock") |
| 2469 | private boolean prepareDataLoaderLocked() |
| 2470 | throws PackageManagerException { |
Songchun Fan | 9439be2 | 2020-01-07 18:16:27 -0800 | [diff] [blame] | 2471 | if (!isDataLoaderInstallation()) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2472 | return true; |
| 2473 | } |
| 2474 | if (mDataLoaderFinished) { |
| 2475 | return true; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2476 | } |
| 2477 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2478 | final List<InstallationFile> addedFiles = mFiles.stream().filter( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2479 | file -> sAddedFilter.accept(new File(file.name))).map( |
| 2480 | file -> new InstallationFile( |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2481 | file.name, file.lengthBytes, file.metadata)).collect( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2482 | Collectors.toList()); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2483 | final List<String> removedFiles = mFiles.stream().filter( |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2484 | file -> sRemovedFilter.accept(new File(file.name))).map( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2485 | file -> file.name.substring( |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2486 | 0, file.name.length() - REMOVE_MARKER_EXTENSION.length())).collect( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2487 | Collectors.toList()); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2488 | |
Songchun Fan | 9439be2 | 2020-01-07 18:16:27 -0800 | [diff] [blame] | 2489 | if (mIncrementalFileStorages != null) { |
| 2490 | for (InstallationFile file : addedFiles) { |
| 2491 | try { |
| 2492 | mIncrementalFileStorages.addFile(file); |
| 2493 | } catch (IOException ex) { |
| 2494 | // TODO(b/146080380): add incremental-specific error code |
| 2495 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 2496 | "Failed to add and configure Incremental File: " + file.getName(), ex); |
| 2497 | } |
| 2498 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2499 | return true; |
Songchun Fan | 9439be2 | 2020-01-07 18:16:27 -0800 | [diff] [blame] | 2500 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2501 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2502 | final DataLoaderManager dataLoaderManager = mContext.getSystemService( |
| 2503 | DataLoaderManager.class); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2504 | if (dataLoaderManager == null) { |
| 2505 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2506 | "Failed to find data loader manager service"); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2507 | } |
| 2508 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2509 | IDataLoaderStatusListener listener = new IDataLoaderStatusListener.Stub() { |
| 2510 | @Override |
| 2511 | public void onStatusChanged(int dataLoaderId, int status) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2512 | try { |
| 2513 | if (status == IDataLoaderStatusListener.DATA_LOADER_DESTROYED) { |
| 2514 | return; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2515 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2516 | |
| 2517 | IDataLoader dataLoader = dataLoaderManager.getDataLoader(dataLoaderId); |
| 2518 | if (dataLoader == null) { |
| 2519 | mDataLoaderFinished = true; |
| 2520 | onSessionVerificationFailure( |
| 2521 | new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2522 | "Failure to obtain data loader")); |
| 2523 | return; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2524 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2525 | |
| 2526 | switch (status) { |
| 2527 | case IDataLoaderStatusListener.DATA_LOADER_CREATED: { |
| 2528 | dataLoader.start(); |
| 2529 | break; |
| 2530 | } |
| 2531 | case IDataLoaderStatusListener.DATA_LOADER_STARTED: { |
| 2532 | dataLoader.prepareImage(addedFiles, removedFiles); |
| 2533 | break; |
| 2534 | } |
| 2535 | case IDataLoaderStatusListener.DATA_LOADER_IMAGE_READY: { |
| 2536 | mDataLoaderFinished = true; |
| 2537 | if (hasParentSessionId()) { |
| 2538 | mSessionProvider.getSession( |
| 2539 | mParentSessionId).dispatchStreamAndValidate(); |
| 2540 | } else { |
| 2541 | dispatchStreamAndValidate(); |
| 2542 | } |
| 2543 | dataLoader.destroy(); |
| 2544 | break; |
| 2545 | } |
| 2546 | case IDataLoaderStatusListener.DATA_LOADER_IMAGE_NOT_READY: { |
| 2547 | mDataLoaderFinished = true; |
| 2548 | onSessionVerificationFailure( |
| 2549 | new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2550 | "Failed to prepare image.")); |
| 2551 | dataLoader.destroy(); |
| 2552 | break; |
| 2553 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2554 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2555 | } catch (RemoteException e) { |
| 2556 | // In case of streaming failure we don't want to fail or commit the session. |
| 2557 | // Just return from this method and allow caller to commit again. |
| 2558 | PackageInstallerService.sendPendingStreaming(mContext, |
| 2559 | mRemoteStatusReceiver, |
| 2560 | sessionId, new StreamingException(e)); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2561 | } |
| 2562 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2563 | }; |
| 2564 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2565 | final FileSystemConnector connector = new FileSystemConnector(addedFiles); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2566 | final FileSystemControlParcel control = new FileSystemControlParcel(); |
| 2567 | control.callback = connector; |
| 2568 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 2569 | final DataLoaderParams params = this.params.dataLoaderParams; |
| 2570 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2571 | Bundle dataLoaderParams = new Bundle(); |
| 2572 | dataLoaderParams.putParcelable("componentName", params.getComponentName()); |
| 2573 | dataLoaderParams.putParcelable("control", control); |
| 2574 | dataLoaderParams.putParcelable("params", params.getData()); |
| 2575 | |
| 2576 | if (!dataLoaderManager.initializeDataLoader(sessionId, dataLoaderParams, listener)) { |
| 2577 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2578 | "Failed to initialize data loader"); |
| 2579 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2580 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2581 | return false; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | @Override |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2585 | public int[] getChildSessionIds() { |
| 2586 | final int[] childSessionIds = mChildSessionIds.copyKeys(); |
| 2587 | if (childSessionIds != null) { |
| 2588 | return childSessionIds; |
| 2589 | } |
| 2590 | return EMPTY_CHILD_SESSION_ARRAY; |
| 2591 | } |
| 2592 | |
| 2593 | @Override |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2594 | public void addChildSessionId(int childSessionId) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2595 | final PackageInstallerSession childSession = mSessionProvider.getSession(childSessionId); |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2596 | if (childSession == null |
| 2597 | || (childSession.hasParentSessionId() && childSession.mParentSessionId != sessionId) |
| 2598 | || childSession.mCommitted |
| 2599 | || childSession.mDestroyed) { |
| 2600 | throw new IllegalStateException("Unable to add child session " + childSessionId |
| 2601 | + " as it does not exist or is in an invalid state."); |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2602 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2603 | synchronized (mLock) { |
Richard Uhler | 257a487 | 2019-02-21 16:02:01 +0000 | [diff] [blame] | 2604 | assertCallerIsOwnerOrRootLocked(); |
| 2605 | assertPreparedAndNotSealedLocked("addChildSessionId"); |
| 2606 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2607 | final int indexOfSession = mChildSessionIds.indexOfKey(childSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2608 | if (indexOfSession >= 0) { |
| 2609 | return; |
| 2610 | } |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2611 | childSession.setParentSessionId(this.sessionId); |
| 2612 | addChildSessionIdInternal(childSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | @Override |
| 2617 | public void removeChildSessionId(int sessionId) { |
| 2618 | final PackageInstallerSession session = mSessionProvider.getSession(sessionId); |
| 2619 | synchronized (mLock) { |
| 2620 | final int indexOfSession = mChildSessionIds.indexOfKey(sessionId); |
| 2621 | if (session != null) { |
| 2622 | session.setParentSessionId(SessionInfo.INVALID_ID); |
| 2623 | } |
| 2624 | if (indexOfSession < 0) { |
| 2625 | // not added in the first place; no-op |
| 2626 | return; |
| 2627 | } |
| 2628 | mChildSessionIds.removeAt(indexOfSession); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | /** |
| 2633 | * Sets the parent session ID if not already set. |
| 2634 | * If {@link SessionInfo#INVALID_ID} is passed, it will be unset. |
| 2635 | */ |
| 2636 | void setParentSessionId(int parentSessionId) { |
| 2637 | synchronized (mLock) { |
| 2638 | if (parentSessionId != SessionInfo.INVALID_ID |
| 2639 | && mParentSessionId != SessionInfo.INVALID_ID) { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2640 | throw new IllegalStateException("The parent of " + sessionId + " is" + " already" |
| 2641 | + "set to " + mParentSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2642 | } |
| 2643 | this.mParentSessionId = parentSessionId; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | boolean hasParentSessionId() { |
| 2648 | return mParentSessionId != SessionInfo.INVALID_ID; |
| 2649 | } |
| 2650 | |
| 2651 | @Override |
| 2652 | public int getParentSessionId() { |
| 2653 | return mParentSessionId; |
| 2654 | } |
| 2655 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2656 | private void dispatchSessionFinished(int returnCode, String msg, Bundle extras) { |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 2657 | final IntentSender statusReceiver; |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 2658 | final String packageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2659 | synchronized (mLock) { |
| 2660 | mFinalStatus = returnCode; |
| 2661 | mFinalMessage = msg; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2662 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 2663 | statusReceiver = mRemoteStatusReceiver; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2664 | packageName = mPackageName; |
| 2665 | } |
| 2666 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 2667 | if (statusReceiver != null) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2668 | // Execute observer.onPackageInstalled on different thread as we don't want callers |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 2669 | // inside the system server have to worry about catching the callbacks while they are |
| 2670 | // calling into the session |
| 2671 | final SomeArgs args = SomeArgs.obtain(); |
| 2672 | args.arg1 = packageName; |
| 2673 | args.arg2 = msg; |
| 2674 | args.arg3 = extras; |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 2675 | args.arg4 = statusReceiver; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 2676 | args.argi1 = returnCode; |
| 2677 | |
| 2678 | mHandler.obtainMessage(MSG_ON_PACKAGE_INSTALLED, args).sendToTarget(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2679 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2680 | |
| 2681 | final boolean success = (returnCode == PackageManager.INSTALL_SUCCEEDED); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 2682 | |
| 2683 | // Send broadcast to default launcher only if it's a new install |
Gavin Corkery | 8f5109dd | 2019-11-11 12:35:14 +0000 | [diff] [blame] | 2684 | // TODO(b/144270665): Secure the usage of this broadcast. |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 2685 | final boolean isNewInstall = extras == null || !extras.getBoolean(Intent.EXTRA_REPLACING); |
Gavin Corkery | 8f5109dd | 2019-11-11 12:35:14 +0000 | [diff] [blame] | 2686 | if (success && isNewInstall && mPm.mInstallerService.okToSendBroadcasts() |
| 2687 | && (params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
Sunny Goyal | a31a74b | 2017-05-11 15:59:19 -0700 | [diff] [blame] | 2688 | mPm.sendSessionCommitBroadcast(generateInfo(), userId); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2691 | mCallback.onSessionFinished(this, success); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2692 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2693 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2694 | /** {@hide} */ |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 2695 | void setStagedSessionReady() { |
| 2696 | synchronized (mLock) { |
| 2697 | mStagedSessionReady = true; |
| 2698 | mStagedSessionApplied = false; |
| 2699 | mStagedSessionFailed = false; |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 2700 | mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2701 | mStagedSessionErrorMessage = ""; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 2702 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2703 | mCallback.onStagedSessionChanged(this); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 2706 | /** {@hide} */ |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2707 | void setStagedSessionFailed(@StagedSessionErrorCode int errorCode, |
| 2708 | String errorMessage) { |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 2709 | synchronized (mLock) { |
| 2710 | mStagedSessionReady = false; |
| 2711 | mStagedSessionApplied = false; |
| 2712 | mStagedSessionFailed = true; |
| 2713 | mStagedSessionErrorCode = errorCode; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2714 | mStagedSessionErrorMessage = errorMessage; |
| 2715 | Slog.d(TAG, "Marking session " + sessionId + " as failed: " + errorMessage); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 2716 | } |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 2717 | cleanStageDir(); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2718 | mCallback.onStagedSessionChanged(this); |
| 2719 | } |
| 2720 | |
| 2721 | /** {@hide} */ |
| 2722 | void setStagedSessionApplied() { |
| 2723 | synchronized (mLock) { |
| 2724 | mStagedSessionReady = false; |
| 2725 | mStagedSessionApplied = true; |
| 2726 | mStagedSessionFailed = false; |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 2727 | mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2728 | mStagedSessionErrorMessage = ""; |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 2729 | Slog.d(TAG, "Marking session " + sessionId + " as applied"); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2730 | } |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 2731 | cleanStageDir(); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2732 | mCallback.onStagedSessionChanged(this); |
| 2733 | } |
| 2734 | |
| 2735 | /** {@hide} */ |
| 2736 | boolean isStagedSessionReady() { |
| 2737 | return mStagedSessionReady; |
| 2738 | } |
| 2739 | |
| 2740 | /** {@hide} */ |
| 2741 | boolean isStagedSessionApplied() { |
| 2742 | return mStagedSessionApplied; |
| 2743 | } |
| 2744 | |
| 2745 | /** {@hide} */ |
| 2746 | boolean isStagedSessionFailed() { |
| 2747 | return mStagedSessionFailed; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 2750 | /** {@hide} */ |
| 2751 | @StagedSessionErrorCode int getStagedSessionErrorCode() { |
| 2752 | return mStagedSessionErrorCode; |
| 2753 | } |
| 2754 | |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2755 | /** {@hide} */ |
| 2756 | String getStagedSessionErrorMessage() { |
| 2757 | return mStagedSessionErrorMessage; |
| 2758 | } |
| 2759 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2760 | private void destroyInternal() { |
| 2761 | synchronized (mLock) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 2762 | mSealed = true; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 2763 | if (!params.isStaged || isStagedAndInTerminalState()) { |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 2764 | mDestroyed = true; |
| 2765 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2766 | // Force shut down all bridges |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 2767 | for (RevocableFileDescriptor fd : mFds) { |
| 2768 | fd.revoke(); |
| 2769 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2770 | for (FileBridge bridge : mBridges) { |
| 2771 | bridge.forceClose(); |
| 2772 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2773 | } |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 2774 | // For staged sessions, we don't delete the directory where the packages have been copied, |
shafik | 988f779 | 2019-02-26 12:15:57 +0000 | [diff] [blame] | 2775 | // since these packages are supposed to be read on reboot. |
| 2776 | // Those dirs are deleted when the staged session has reached a final state. |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 2777 | if (stageDir != null && !params.isStaged) { |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 2778 | if (mIncrementalFileStorages != null) { |
| 2779 | mIncrementalFileStorages.cleanUp(); |
| 2780 | } |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2781 | try { |
| 2782 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 2783 | } catch (InstallerException ignored) { |
| 2784 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2785 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 2788 | private void cleanStageDir() { |
| 2789 | if (isMultiPackage()) { |
| 2790 | for (int childSessionId : getChildSessionIds()) { |
| 2791 | mSessionProvider.getSession(childSessionId).cleanStageDir(); |
| 2792 | } |
| 2793 | } else { |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 2794 | if (mIncrementalFileStorages != null) { |
| 2795 | mIncrementalFileStorages.cleanUp(); |
| 2796 | } |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 2797 | try { |
| 2798 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 2799 | } catch (InstallerException ignored) { |
| 2800 | } |
| 2801 | } |
| 2802 | } |
| 2803 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2804 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2805 | synchronized (mLock) { |
| 2806 | dumpLocked(pw); |
| 2807 | } |
| 2808 | } |
| 2809 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2810 | @GuardedBy("mLock") |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2811 | private void dumpLocked(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2812 | pw.println("Session " + sessionId + ":"); |
| 2813 | pw.increaseIndent(); |
| 2814 | |
| 2815 | pw.printPair("userId", userId); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2816 | pw.printPair("mOriginalInstallerUid", mOriginalInstallerUid); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 2817 | pw.printPair("installerPackageName", mInstallSource.installerPackageName); |
| 2818 | pw.printPair("installInitiatingPackageName", mInstallSource.initiatingPackageName); |
| 2819 | pw.printPair("installOriginatingPackageName", mInstallSource.originatingPackageName); |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 2820 | pw.printPair("mInstallerUid", mInstallerUid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2821 | pw.printPair("createdMillis", createdMillis); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 2822 | pw.printPair("updatedMillis", updatedMillis); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2823 | pw.printPair("stageDir", stageDir); |
| 2824 | pw.printPair("stageCid", stageCid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2825 | pw.println(); |
| 2826 | |
| 2827 | params.dump(pw); |
| 2828 | |
| 2829 | pw.printPair("mClientProgress", mClientProgress); |
| 2830 | pw.printPair("mProgress", mProgress); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 2831 | pw.printPair("mCommitted", mCommitted); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 2832 | pw.printPair("mSealed", mSealed); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2833 | pw.printPair("mPermissionsManuallyAccepted", mPermissionsManuallyAccepted); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 2834 | pw.printPair("mRelinquished", mRelinquished); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 2835 | pw.printPair("mDestroyed", mDestroyed); |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 2836 | pw.printPair("mFds", mFds.size()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2837 | pw.printPair("mBridges", mBridges.size()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2838 | pw.printPair("mFinalStatus", mFinalStatus); |
| 2839 | pw.printPair("mFinalMessage", mFinalMessage); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2840 | pw.printPair("params.isMultiPackage", params.isMultiPackage); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 2841 | pw.printPair("params.isStaged", params.isStaged); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 2842 | pw.printPair("mParentSessionId", mParentSessionId); |
| 2843 | pw.printPair("mChildSessionIds", mChildSessionIds); |
| 2844 | pw.printPair("mStagedSessionApplied", mStagedSessionApplied); |
| 2845 | pw.printPair("mStagedSessionFailed", mStagedSessionFailed); |
| 2846 | pw.printPair("mStagedSessionReady", mStagedSessionReady); |
| 2847 | pw.printPair("mStagedSessionErrorCode", mStagedSessionErrorCode); |
| 2848 | pw.printPair("mStagedSessionErrorMessage", mStagedSessionErrorMessage); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 2849 | pw.println(); |
| 2850 | |
| 2851 | pw.decreaseIndent(); |
| 2852 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2853 | |
| 2854 | private static void writeGrantedRuntimePermissionsLocked(XmlSerializer out, |
| 2855 | String[] grantedRuntimePermissions) throws IOException { |
| 2856 | if (grantedRuntimePermissions != null) { |
| 2857 | for (String permission : grantedRuntimePermissions) { |
| 2858 | out.startTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 2859 | writeStringAttribute(out, ATTR_NAME, permission); |
| 2860 | out.endTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 2865 | private static void writeWhitelistedRestrictedPermissionsLocked(@NonNull XmlSerializer out, |
| 2866 | @Nullable List<String> whitelistedRestrictedPermissions) throws IOException { |
| 2867 | if (whitelistedRestrictedPermissions != null) { |
| 2868 | final int permissionCount = whitelistedRestrictedPermissions.size(); |
| 2869 | for (int i = 0; i < permissionCount; i++) { |
| 2870 | out.startTag(null, TAG_WHITELISTED_RESTRICTED_PERMISSION); |
| 2871 | writeStringAttribute(out, ATTR_NAME, whitelistedRestrictedPermissions.get(i)); |
| 2872 | out.endTag(null, TAG_WHITELISTED_RESTRICTED_PERMISSION); |
| 2873 | } |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2878 | private static File buildAppIconFile(int sessionId, @NonNull File sessionsDir) { |
| 2879 | return new File(sessionsDir, "app_icon." + sessionId + ".png"); |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * Write this session to a {@link XmlSerializer}. |
| 2884 | * |
| 2885 | * @param out Where to write the session to |
| 2886 | * @param sessionsDir The directory containing the sessions |
| 2887 | */ |
| 2888 | void write(@NonNull XmlSerializer out, @NonNull File sessionsDir) throws IOException { |
| 2889 | synchronized (mLock) { |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 2890 | if (mDestroyed) { |
| 2891 | return; |
| 2892 | } |
| 2893 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2894 | out.startTag(null, TAG_SESSION); |
| 2895 | |
| 2896 | writeIntAttribute(out, ATTR_SESSION_ID, sessionId); |
| 2897 | writeIntAttribute(out, ATTR_USER_ID, userId); |
| 2898 | writeStringAttribute(out, ATTR_INSTALLER_PACKAGE_NAME, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 2899 | mInstallSource.installerPackageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2900 | writeIntAttribute(out, ATTR_INSTALLER_UID, mInstallerUid); |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 2901 | writeStringAttribute(out, ATTR_INITIATING_PACKAGE_NAME, |
| 2902 | mInstallSource.initiatingPackageName); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 2903 | writeStringAttribute(out, ATTR_ORIGINATING_PACKAGE_NAME, |
| 2904 | mInstallSource.originatingPackageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2905 | writeLongAttribute(out, ATTR_CREATED_MILLIS, createdMillis); |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 2906 | writeLongAttribute(out, ATTR_UPDATED_MILLIS, updatedMillis); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2907 | if (stageDir != null) { |
| 2908 | writeStringAttribute(out, ATTR_SESSION_STAGE_DIR, |
| 2909 | stageDir.getAbsolutePath()); |
| 2910 | } |
| 2911 | if (stageCid != null) { |
| 2912 | writeStringAttribute(out, ATTR_SESSION_STAGE_CID, stageCid); |
| 2913 | } |
| 2914 | writeBooleanAttribute(out, ATTR_PREPARED, isPrepared()); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 2915 | writeBooleanAttribute(out, ATTR_COMMITTED, isCommitted()); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2916 | writeBooleanAttribute(out, ATTR_SEALED, isSealed()); |
| 2917 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2918 | writeBooleanAttribute(out, ATTR_MULTI_PACKAGE, params.isMultiPackage); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 2919 | writeBooleanAttribute(out, ATTR_STAGED_SESSION, params.isStaged); |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 2920 | writeBooleanAttribute(out, ATTR_IS_READY, mStagedSessionReady); |
| 2921 | writeBooleanAttribute(out, ATTR_IS_FAILED, mStagedSessionFailed); |
| 2922 | writeBooleanAttribute(out, ATTR_IS_APPLIED, mStagedSessionApplied); |
| 2923 | writeIntAttribute(out, ATTR_STAGED_SESSION_ERROR_CODE, mStagedSessionErrorCode); |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 2924 | writeStringAttribute(out, ATTR_STAGED_SESSION_ERROR_MESSAGE, |
| 2925 | mStagedSessionErrorMessage); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2926 | // TODO(patb,109941548): avoid writing to xml and instead infer / validate this after |
| 2927 | // we've read all sessions. |
| 2928 | writeIntAttribute(out, ATTR_PARENT_SESSION_ID, mParentSessionId); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2929 | writeIntAttribute(out, ATTR_MODE, params.mode); |
| 2930 | writeIntAttribute(out, ATTR_INSTALL_FLAGS, params.installFlags); |
| 2931 | writeIntAttribute(out, ATTR_INSTALL_LOCATION, params.installLocation); |
| 2932 | writeLongAttribute(out, ATTR_SIZE_BYTES, params.sizeBytes); |
| 2933 | writeStringAttribute(out, ATTR_APP_PACKAGE_NAME, params.appPackageName); |
| 2934 | writeStringAttribute(out, ATTR_APP_LABEL, params.appLabel); |
| 2935 | writeUriAttribute(out, ATTR_ORIGINATING_URI, params.originatingUri); |
| 2936 | writeIntAttribute(out, ATTR_ORIGINATING_UID, params.originatingUid); |
| 2937 | writeUriAttribute(out, ATTR_REFERRER_URI, params.referrerUri); |
| 2938 | writeStringAttribute(out, ATTR_ABI_OVERRIDE, params.abiOverride); |
| 2939 | writeStringAttribute(out, ATTR_VOLUME_UUID, params.volumeUuid); |
| 2940 | writeIntAttribute(out, ATTR_INSTALL_REASON, params.installReason); |
| 2941 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2942 | final boolean isDataLoader = params.dataLoaderParams != null; |
| 2943 | writeBooleanAttribute(out, ATTR_IS_DATALOADER, isDataLoader); |
| 2944 | if (isDataLoader) { |
| 2945 | writeIntAttribute(out, ATTR_DATALOADER_TYPE, params.dataLoaderParams.getType()); |
| 2946 | writeStringAttribute(out, ATTR_DATALOADER_PACKAGE_NAME, |
| 2947 | params.dataLoaderParams.getComponentName().getPackageName()); |
| 2948 | writeStringAttribute(out, ATTR_DATALOADER_CLASS_NAME, |
| 2949 | params.dataLoaderParams.getComponentName().getClassName()); |
| 2950 | writeStringAttribute(out, ATTR_DATALOADER_ARGUMENTS, |
| 2951 | params.dataLoaderParams.getArguments()); |
| 2952 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2953 | |
Philip P. Moltmann | 656a2a0 | 2018-03-01 09:14:44 -0800 | [diff] [blame] | 2954 | writeGrantedRuntimePermissionsLocked(out, params.grantedRuntimePermissions); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 2955 | writeWhitelistedRestrictedPermissionsLocked(out, |
| 2956 | params.whitelistedRestrictedPermissions); |
Philip P. Moltmann | 656a2a0 | 2018-03-01 09:14:44 -0800 | [diff] [blame] | 2957 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2958 | // Persist app icon if changed since last written |
| 2959 | File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 2960 | if (params.appIcon == null && appIconFile.exists()) { |
| 2961 | appIconFile.delete(); |
| 2962 | } else if (params.appIcon != null |
| 2963 | && appIconFile.lastModified() != params.appIconLastModified) { |
| 2964 | if (LOGD) Slog.w(TAG, "Writing changed icon " + appIconFile); |
| 2965 | FileOutputStream os = null; |
| 2966 | try { |
| 2967 | os = new FileOutputStream(appIconFile); |
| 2968 | params.appIcon.compress(Bitmap.CompressFormat.PNG, 90, os); |
| 2969 | } catch (IOException e) { |
| 2970 | Slog.w(TAG, "Failed to write icon " + appIconFile + ": " + e.getMessage()); |
| 2971 | } finally { |
| 2972 | IoUtils.closeQuietly(os); |
| 2973 | } |
| 2974 | |
| 2975 | params.appIconLastModified = appIconFile.lastModified(); |
| 2976 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2977 | final int[] childSessionIds = getChildSessionIds(); |
| 2978 | for (int childSessionId : childSessionIds) { |
| 2979 | out.startTag(null, TAG_CHILD_SESSION); |
| 2980 | writeIntAttribute(out, ATTR_SESSION_ID, childSessionId); |
| 2981 | out.endTag(null, TAG_CHILD_SESSION); |
| 2982 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2983 | for (FileInfo fileInfo : mFiles) { |
| 2984 | out.startTag(null, TAG_SESSION_FILE); |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2985 | writeIntAttribute(out, ATTR_LOCATION, fileInfo.location); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2986 | writeStringAttribute(out, ATTR_NAME, fileInfo.name); |
| 2987 | writeLongAttribute(out, ATTR_LENGTH_BYTES, fileInfo.lengthBytes); |
| 2988 | writeByteArrayAttribute(out, ATTR_METADATA, fileInfo.metadata); |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2989 | writeByteArrayAttribute(out, ATTR_SIGNATURE, fileInfo.signature); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2990 | out.endTag(null, TAG_SESSION_FILE); |
| 2991 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | out.endTag(null, TAG_SESSION); |
| 2995 | } |
| 2996 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 2997 | // Sanity check to be performed when the session is restored from an external file. Only one |
| 2998 | // of the session states should be true, or none of them. |
| 2999 | private static boolean isStagedSessionStateValid(boolean isReady, boolean isApplied, |
| 3000 | boolean isFailed) { |
| 3001 | return (!isReady && !isApplied && !isFailed) |
| 3002 | || (isReady && !isApplied && !isFailed) |
| 3003 | || (!isReady && isApplied && !isFailed) |
| 3004 | || (!isReady && !isApplied && isFailed); |
| 3005 | } |
| 3006 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3007 | /** |
| 3008 | * Read new session from a {@link XmlPullParser xml description} and create it. |
| 3009 | * |
| 3010 | * @param in The source of the description |
| 3011 | * @param callback Callback the session uses to notify about changes of it's state |
| 3012 | * @param context Context to be used by the session |
| 3013 | * @param pm PackageManager to use by the session |
| 3014 | * @param installerThread Thread to be used for callbacks of this session |
| 3015 | * @param sessionsDir The directory the sessions are stored in |
| 3016 | * |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3017 | * @param sessionProvider |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3018 | * @return The newly created session |
| 3019 | */ |
| 3020 | public static PackageInstallerSession readFromXml(@NonNull XmlPullParser in, |
| 3021 | @NonNull PackageInstallerService.InternalCallback callback, @NonNull Context context, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3022 | @NonNull PackageManagerService pm, Looper installerThread, |
| 3023 | @NonNull StagingManager stagingManager, @NonNull File sessionsDir, |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3024 | @NonNull PackageSessionProvider sessionProvider) |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3025 | throws IOException, XmlPullParserException { |
| 3026 | final int sessionId = readIntAttribute(in, ATTR_SESSION_ID); |
| 3027 | final int userId = readIntAttribute(in, ATTR_USER_ID); |
| 3028 | final String installerPackageName = readStringAttribute(in, ATTR_INSTALLER_PACKAGE_NAME); |
| 3029 | final int installerUid = readIntAttribute(in, ATTR_INSTALLER_UID, pm.getPackageUid( |
| 3030 | installerPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, userId)); |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 3031 | final String installInitiatingPackageName = |
| 3032 | readStringAttribute(in, ATTR_INITIATING_PACKAGE_NAME); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 3033 | final String installOriginatingPackageName = |
| 3034 | readStringAttribute(in, ATTR_ORIGINATING_PACKAGE_NAME); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3035 | final long createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS); |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 3036 | long updatedMillis = readLongAttribute(in, ATTR_UPDATED_MILLIS); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3037 | final String stageDirRaw = readStringAttribute(in, ATTR_SESSION_STAGE_DIR); |
| 3038 | final File stageDir = (stageDirRaw != null) ? new File(stageDirRaw) : null; |
| 3039 | final String stageCid = readStringAttribute(in, ATTR_SESSION_STAGE_CID); |
| 3040 | final boolean prepared = readBooleanAttribute(in, ATTR_PREPARED, true); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 3041 | final boolean committed = readBooleanAttribute(in, ATTR_COMMITTED); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3042 | final boolean sealed = readBooleanAttribute(in, ATTR_SEALED); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3043 | final int parentSessionId = readIntAttribute(in, ATTR_PARENT_SESSION_ID, |
| 3044 | SessionInfo.INVALID_ID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3045 | |
| 3046 | final SessionParams params = new SessionParams( |
| 3047 | SessionParams.MODE_INVALID); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3048 | params.isMultiPackage = readBooleanAttribute(in, ATTR_MULTI_PACKAGE, false); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 3049 | params.isStaged = readBooleanAttribute(in, ATTR_STAGED_SESSION, false); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3050 | params.mode = readIntAttribute(in, ATTR_MODE); |
| 3051 | params.installFlags = readIntAttribute(in, ATTR_INSTALL_FLAGS); |
| 3052 | params.installLocation = readIntAttribute(in, ATTR_INSTALL_LOCATION); |
| 3053 | params.sizeBytes = readLongAttribute(in, ATTR_SIZE_BYTES); |
| 3054 | params.appPackageName = readStringAttribute(in, ATTR_APP_PACKAGE_NAME); |
| 3055 | params.appIcon = readBitmapAttribute(in, ATTR_APP_ICON); |
| 3056 | params.appLabel = readStringAttribute(in, ATTR_APP_LABEL); |
| 3057 | params.originatingUri = readUriAttribute(in, ATTR_ORIGINATING_URI); |
| 3058 | params.originatingUid = |
| 3059 | readIntAttribute(in, ATTR_ORIGINATING_UID, SessionParams.UID_UNKNOWN); |
| 3060 | params.referrerUri = readUriAttribute(in, ATTR_REFERRER_URI); |
| 3061 | params.abiOverride = readStringAttribute(in, ATTR_ABI_OVERRIDE); |
| 3062 | params.volumeUuid = readStringAttribute(in, ATTR_VOLUME_UUID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3063 | params.installReason = readIntAttribute(in, ATTR_INSTALL_REASON); |
| 3064 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 3065 | if (readBooleanAttribute(in, ATTR_IS_DATALOADER)) { |
| 3066 | params.dataLoaderParams = new DataLoaderParams( |
| 3067 | readIntAttribute(in, ATTR_DATALOADER_TYPE), |
| 3068 | new ComponentName( |
| 3069 | readStringAttribute(in, ATTR_DATALOADER_PACKAGE_NAME), |
| 3070 | readStringAttribute(in, ATTR_DATALOADER_CLASS_NAME)), |
| 3071 | readStringAttribute(in, ATTR_DATALOADER_ARGUMENTS), |
| 3072 | null); |
| 3073 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3074 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3075 | final File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 3076 | if (appIconFile.exists()) { |
| 3077 | params.appIcon = BitmapFactory.decodeFile(appIconFile.getAbsolutePath()); |
| 3078 | params.appIconLastModified = appIconFile.lastModified(); |
| 3079 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3080 | final boolean isReady = readBooleanAttribute(in, ATTR_IS_READY); |
| 3081 | final boolean isFailed = readBooleanAttribute(in, ATTR_IS_FAILED); |
| 3082 | final boolean isApplied = readBooleanAttribute(in, ATTR_IS_APPLIED); |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3083 | final int stagedSessionErrorCode = readIntAttribute(in, ATTR_STAGED_SESSION_ERROR_CODE, |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 3084 | SessionInfo.STAGED_SESSION_NO_ERROR); |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3085 | final String stagedSessionErrorMessage = readStringAttribute(in, |
| 3086 | ATTR_STAGED_SESSION_ERROR_MESSAGE); |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3087 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3088 | if (!isStagedSessionStateValid(isReady, isApplied, isFailed)) { |
| 3089 | throw new IllegalArgumentException("Can't restore staged session with invalid state."); |
| 3090 | } |
| 3091 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3092 | // Parse sub tags of this session, typically used for repeated values / arrays. |
| 3093 | // Sub tags can come in any order, therefore we need to keep track of what we find while |
| 3094 | // parsing and only set the right values at the end. |
| 3095 | |
| 3096 | // Store the current depth. We should stop parsing when we reach an end tag at the same |
| 3097 | // depth. |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3098 | List<String> grantedRuntimePermissions = new ArrayList<>(); |
| 3099 | List<String> whitelistedRestrictedPermissions = new ArrayList<>(); |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3100 | List<Integer> childSessionIds = new ArrayList<>(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3101 | List<FileInfo> files = new ArrayList<>(); |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3102 | int outerDepth = in.getDepth(); |
| 3103 | int type; |
| 3104 | while ((type = in.next()) != XmlPullParser.END_DOCUMENT |
| 3105 | && (type != XmlPullParser.END_TAG || in.getDepth() > outerDepth)) { |
| 3106 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 3107 | continue; |
| 3108 | } |
| 3109 | if (TAG_GRANTED_RUNTIME_PERMISSION.equals(in.getName())) { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3110 | grantedRuntimePermissions.add(readStringAttribute(in, ATTR_NAME)); |
| 3111 | } |
| 3112 | if (TAG_WHITELISTED_RESTRICTED_PERMISSION.equals(in.getName())) { |
| 3113 | whitelistedRestrictedPermissions.add(readStringAttribute(in, ATTR_NAME)); |
| 3114 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3115 | } |
| 3116 | if (TAG_CHILD_SESSION.equals(in.getName())) { |
| 3117 | childSessionIds.add(readIntAttribute(in, ATTR_SESSION_ID, SessionInfo.INVALID_ID)); |
| 3118 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3119 | if (TAG_SESSION_FILE.equals(in.getName())) { |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 3120 | files.add(new FileInfo( |
| 3121 | readIntAttribute(in, ATTR_LOCATION, 0), |
| 3122 | readStringAttribute(in, ATTR_NAME), |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3123 | readLongAttribute(in, ATTR_LENGTH_BYTES, -1), |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 3124 | readByteArrayAttribute(in, ATTR_METADATA), |
| 3125 | readByteArrayAttribute(in, ATTR_SIGNATURE))); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3126 | } |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3129 | if (grantedRuntimePermissions.size() > 0) { |
| 3130 | params.grantedRuntimePermissions = grantedRuntimePermissions |
| 3131 | .stream().toArray(String[]::new); |
| 3132 | } |
| 3133 | |
| 3134 | if (whitelistedRestrictedPermissions.size() > 0) { |
| 3135 | params.whitelistedRestrictedPermissions = whitelistedRestrictedPermissions; |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | int[] childSessionIdsArray; |
| 3139 | if (childSessionIds.size() > 0) { |
| 3140 | childSessionIdsArray = childSessionIds.stream().mapToInt(i -> i).toArray(); |
| 3141 | } else { |
| 3142 | childSessionIdsArray = EMPTY_CHILD_SESSION_ARRAY; |
| 3143 | } |
| 3144 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3145 | FileInfo[] fileInfosArray = null; |
| 3146 | if (!files.isEmpty()) { |
| 3147 | fileInfosArray = files.stream().toArray(FileInfo[]::new); |
| 3148 | } |
| 3149 | |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3150 | InstallSource installSource = InstallSource.create(installInitiatingPackageName, |
Alan Stokes | 72b7e67 | 2020-01-07 15:46:49 +0000 | [diff] [blame] | 3151 | installOriginatingPackageName, installerPackageName); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3152 | return new PackageInstallerSession(callback, context, pm, sessionProvider, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3153 | installerThread, stagingManager, sessionId, userId, installerUid, |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3154 | installSource, params, createdMillis, stageDir, stageCid, fileInfosArray, |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 3155 | prepared, committed, sealed, childSessionIdsArray, parentSessionId, |
| 3156 | isReady, isFailed, isApplied, stagedSessionErrorCode, stagedSessionErrorMessage); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3157 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 3158 | } |