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