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