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 | |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.INSTALL_FAILED_ABORTED; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 20 | import static android.content.pm.PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 23 | import static android.content.pm.PackageManager.INSTALL_FAILED_INVALID_APK; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 24 | import static android.system.OsConstants.O_CREAT; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 25 | import static android.system.OsConstants.O_RDONLY; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 26 | import static android.system.OsConstants.O_WRONLY; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 27 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 28 | import static com.android.internal.util.XmlUtils.readBitmapAttribute; |
| 29 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
| 30 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 31 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
| 32 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
| 33 | import static com.android.internal.util.XmlUtils.readUriAttribute; |
| 34 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
| 35 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 36 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
| 37 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
| 38 | import static com.android.internal.util.XmlUtils.writeUriAttribute; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 39 | import static com.android.server.pm.PackageInstallerService.prepareExternalStageCid; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 40 | import static com.android.server.pm.PackageInstallerService.prepareStageDir; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 41 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 42 | import android.Manifest; |
| 43 | import android.annotation.NonNull; |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 44 | import android.annotation.Nullable; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 46 | import android.content.Context; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 47 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 48 | import android.content.IntentSender; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 49 | import android.content.pm.ApplicationInfo; |
| 50 | import android.content.pm.IPackageInstallObserver2; |
| 51 | import android.content.pm.IPackageInstallerSession; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 52 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 53 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageInstaller.SessionInfo; |
| 55 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageManager; |
| 57 | import android.content.pm.PackageParser; |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageParser.ApkLite; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageParser.PackageLite; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 60 | import android.content.pm.PackageParser.PackageParserException; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 61 | import android.content.pm.Signature; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 62 | import android.graphics.Bitmap; |
| 63 | import android.graphics.BitmapFactory; |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 64 | import android.os.Binder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 65 | import android.os.Bundle; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 66 | import android.os.FileBridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 67 | import android.os.FileUtils; |
| 68 | import android.os.Handler; |
| 69 | import android.os.Looper; |
| 70 | import android.os.Message; |
| 71 | import android.os.ParcelFileDescriptor; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 72 | import android.os.ParcelableException; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 73 | import android.os.Process; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 74 | import android.os.RemoteException; |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 75 | import android.os.RevocableFileDescriptor; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 76 | import android.os.UserHandle; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 77 | import android.os.storage.StorageManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 78 | import android.system.ErrnoException; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 79 | import android.system.Os; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 80 | import android.system.OsConstants; |
| 81 | import android.system.StructStat; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 82 | import android.text.TextUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 83 | import android.util.ArraySet; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 84 | import android.util.ExceptionUtils; |
| 85 | import android.util.MathUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 86 | import android.util.Slog; |
| 87 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 88 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 89 | import com.android.internal.content.NativeLibraryHelper; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 90 | import com.android.internal.content.PackageHelper; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 91 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 92 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 93 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 94 | import com.android.server.pm.Installer.InstallerException; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 95 | import com.android.server.pm.PackageInstallerService.PackageInstallObserverAdapter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 96 | |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 97 | import libcore.io.IoUtils; |
| 98 | import libcore.io.Libcore; |
| 99 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 100 | import org.xmlpull.v1.XmlPullParser; |
| 101 | import org.xmlpull.v1.XmlPullParserException; |
| 102 | import org.xmlpull.v1.XmlSerializer; |
| 103 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 104 | import java.io.File; |
| 105 | import java.io.FileDescriptor; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 106 | import java.io.FileFilter; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 107 | import java.io.FileOutputStream; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 108 | import java.io.IOException; |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 109 | import java.security.cert.Certificate; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 110 | import java.util.ArrayList; |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 111 | import java.util.Arrays; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 112 | import java.util.List; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 113 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 114 | |
| 115 | public class PackageInstallerSession extends IPackageInstallerSession.Stub { |
| 116 | private static final String TAG = "PackageInstaller"; |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 117 | private static final boolean LOGD = true; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 118 | private static final String REMOVE_SPLIT_MARKER_EXTENSION = ".removed"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 119 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 120 | private static final int MSG_COMMIT = 0; |
| 121 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 122 | /** XML constants used for persisting a session */ |
| 123 | static final String TAG_SESSION = "session"; |
| 124 | private static final String TAG_GRANTED_RUNTIME_PERMISSION = "granted-runtime-permission"; |
| 125 | private static final String ATTR_SESSION_ID = "sessionId"; |
| 126 | private static final String ATTR_USER_ID = "userId"; |
| 127 | private static final String ATTR_INSTALLER_PACKAGE_NAME = "installerPackageName"; |
| 128 | private static final String ATTR_INSTALLER_UID = "installerUid"; |
| 129 | private static final String ATTR_CREATED_MILLIS = "createdMillis"; |
| 130 | private static final String ATTR_SESSION_STAGE_DIR = "sessionStageDir"; |
| 131 | private static final String ATTR_SESSION_STAGE_CID = "sessionStageCid"; |
| 132 | private static final String ATTR_PREPARED = "prepared"; |
| 133 | private static final String ATTR_SEALED = "sealed"; |
| 134 | private static final String ATTR_MODE = "mode"; |
| 135 | private static final String ATTR_INSTALL_FLAGS = "installFlags"; |
| 136 | private static final String ATTR_INSTALL_LOCATION = "installLocation"; |
| 137 | private static final String ATTR_SIZE_BYTES = "sizeBytes"; |
| 138 | private static final String ATTR_APP_PACKAGE_NAME = "appPackageName"; |
| 139 | @Deprecated |
| 140 | private static final String ATTR_APP_ICON = "appIcon"; |
| 141 | private static final String ATTR_APP_LABEL = "appLabel"; |
| 142 | private static final String ATTR_ORIGINATING_URI = "originatingUri"; |
| 143 | private static final String ATTR_ORIGINATING_UID = "originatingUid"; |
| 144 | private static final String ATTR_REFERRER_URI = "referrerUri"; |
| 145 | private static final String ATTR_ABI_OVERRIDE = "abiOverride"; |
| 146 | private static final String ATTR_VOLUME_UUID = "volumeUuid"; |
| 147 | private static final String ATTR_NAME = "name"; |
| 148 | private static final String ATTR_INSTALL_REASON = "installRason"; |
| 149 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 150 | // TODO: enforce INSTALL_ALLOW_TEST |
| 151 | // TODO: enforce INSTALL_ALLOW_DOWNGRADE |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 153 | private final PackageInstallerService.InternalCallback mCallback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 154 | private final Context mContext; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 155 | private final PackageManagerService mPm; |
| 156 | private final Handler mHandler; |
| 157 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 158 | final int sessionId; |
| 159 | final int userId; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 160 | final SessionParams params; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 161 | final long createdMillis; |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 162 | final int defaultContainerGid; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 163 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 164 | /** Staging location where client data is written. */ |
| 165 | final File stageDir; |
| 166 | final String stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 167 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 168 | private final AtomicInteger mActiveCount = new AtomicInteger(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 169 | |
| 170 | private final Object mLock = new Object(); |
| 171 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 172 | /** Uid of the creator of this session. */ |
| 173 | private final int mOriginalInstallerUid; |
| 174 | |
| 175 | /** Package of the owner of the installer session */ |
| 176 | @GuardedBy("mLock") |
| 177 | private String mInstallerPackageName; |
| 178 | |
| 179 | /** Uid of the owner of the installer session */ |
| 180 | @GuardedBy("mLock") |
| 181 | private int mInstallerUid; |
| 182 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 183 | @GuardedBy("mLock") |
| 184 | private float mClientProgress = 0; |
| 185 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 186 | private float mInternalProgress = 0; |
| 187 | |
| 188 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 189 | private float mProgress = 0; |
| 190 | @GuardedBy("mLock") |
| 191 | private float mReportedProgress = -1; |
| 192 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 193 | /** State of the session. */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 194 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 195 | private boolean mPrepared = false; |
| 196 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 197 | private boolean mSealed = false; |
| 198 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 199 | private boolean mCommitted = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 200 | @GuardedBy("mLock") |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 201 | private boolean mRelinquished = false; |
| 202 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 203 | private boolean mDestroyed = false; |
| 204 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 205 | /** Permissions have been accepted by the user (see {@link #setPermissionsResult}) */ |
| 206 | @GuardedBy("mLock") |
| 207 | private boolean mPermissionsManuallyAccepted = false; |
| 208 | |
| 209 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 210 | private int mFinalStatus; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 211 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 212 | private String mFinalMessage; |
| 213 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 214 | @GuardedBy("mLock") |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 215 | private final ArrayList<RevocableFileDescriptor> mFds = new ArrayList<>(); |
| 216 | @GuardedBy("mLock") |
| 217 | private final ArrayList<FileBridge> mBridges = new ArrayList<>(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 218 | |
| 219 | @GuardedBy("mLock") |
| 220 | private IPackageInstallObserver2 mRemoteObserver; |
| 221 | |
| 222 | /** Fields derived from commit parsing */ |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 223 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 224 | private String mPackageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 225 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 226 | private int mVersionCode; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 227 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 228 | private Signature[] mSignatures; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 229 | @GuardedBy("mLock") |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 230 | private Certificate[][] mCertificates; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Path to the validated base APK for this session, which may point at an |
| 234 | * APK inside the session (when the session defines the base), or it may |
| 235 | * point at the existing base APK (when adding splits to an existing app). |
| 236 | * <p> |
| 237 | * This is used when confirming permissions, since we can't fully stage the |
| 238 | * session inside an ASEC before confirming with user. |
| 239 | */ |
| 240 | @GuardedBy("mLock") |
| 241 | private File mResolvedBaseFile; |
| 242 | |
| 243 | @GuardedBy("mLock") |
| 244 | private File mResolvedStageDir; |
| 245 | |
| 246 | @GuardedBy("mLock") |
| 247 | private final List<File> mResolvedStagedFiles = new ArrayList<>(); |
| 248 | @GuardedBy("mLock") |
| 249 | private final List<File> mResolvedInheritedFiles = new ArrayList<>(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 250 | @GuardedBy("mLock") |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 251 | private final List<String> mResolvedInstructionSets = new ArrayList<>(); |
| 252 | @GuardedBy("mLock") |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 253 | private File mInheritedFilesBase; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 254 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 255 | private static final FileFilter sAddedFilter = new FileFilter() { |
| 256 | @Override |
| 257 | public boolean accept(File file) { |
| 258 | // Installers can't stage directories, so it's fine to ignore |
| 259 | // entries like "lost+found". |
| 260 | if (file.isDirectory()) return false; |
| 261 | if (file.getName().endsWith(REMOVE_SPLIT_MARKER_EXTENSION)) return false; |
| 262 | return true; |
| 263 | } |
| 264 | }; |
| 265 | private static final FileFilter sRemovedFilter = new FileFilter() { |
| 266 | @Override |
| 267 | public boolean accept(File file) { |
| 268 | if (file.isDirectory()) return false; |
| 269 | if (!file.getName().endsWith(REMOVE_SPLIT_MARKER_EXTENSION)) return false; |
| 270 | return true; |
| 271 | } |
| 272 | }; |
| 273 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 274 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public boolean handleMessage(Message msg) { |
| 277 | synchronized (mLock) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 278 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 279 | commitLocked(); |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 280 | } catch (PackageManagerException e) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 281 | final String completeMsg = ExceptionUtils.getCompleteMessage(e); |
| 282 | Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 283 | destroyInternal(); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 284 | dispatchSessionFinished(e.error, completeMsg, null); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 285 | } |
Philip P. Moltmann | 9890f8b | 2017-08-08 10:49:38 -0700 | [diff] [blame] | 286 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 287 | |
| 288 | return true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 289 | } |
| 290 | }; |
| 291 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 292 | /** |
| 293 | * @return {@code true} iff the installing is app an device owner? |
| 294 | */ |
| 295 | private boolean isInstallerDeviceOwnerLocked() { |
| 296 | DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( |
| 297 | Context.DEVICE_POLICY_SERVICE); |
| 298 | |
| 299 | return (dpm != null) && dpm.isDeviceOwnerAppOnCallingUser( |
| 300 | mInstallerPackageName); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Checks if the permissions still need to be confirmed. |
| 305 | * |
| 306 | * <p>This is dependant on the identity of the installer, hence this cannot be cached if the |
| 307 | * installer might still {@link #transfer(String) change}. |
| 308 | * |
| 309 | * @return {@code true} iff we need to ask to confirm the permissions? |
| 310 | */ |
| 311 | private boolean needToAskForPermissionsLocked() { |
| 312 | if (mPermissionsManuallyAccepted) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | final boolean isPermissionGranted = |
| 317 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, |
| 318 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
| 319 | final boolean isInstallerRoot = (mInstallerUid == Process.ROOT_UID); |
| 320 | final boolean forcePermissionPrompt = |
| 321 | (params.installFlags & PackageManager.INSTALL_FORCE_PERMISSION_PROMPT) != 0; |
| 322 | |
| 323 | // Device owners are allowed to silently install packages, so the permission check is |
| 324 | // waived if the installer is the device owner. |
| 325 | return forcePermissionPrompt || !(isPermissionGranted || isInstallerRoot |
| 326 | || isInstallerDeviceOwnerLocked()); |
| 327 | } |
| 328 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 329 | public PackageInstallerSession(PackageInstallerService.InternalCallback callback, |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 330 | Context context, PackageManagerService pm, Looper looper, int sessionId, int userId, |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 331 | String installerPackageName, int installerUid, SessionParams params, long createdMillis, |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 332 | File stageDir, String stageCid, boolean prepared, boolean sealed) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 333 | mCallback = callback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 334 | mContext = context; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 335 | mPm = pm; |
| 336 | mHandler = new Handler(looper, mHandlerCallback); |
| 337 | |
| 338 | this.sessionId = sessionId; |
| 339 | this.userId = userId; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 340 | mOriginalInstallerUid = installerUid; |
| 341 | mInstallerPackageName = installerPackageName; |
| 342 | mInstallerUid = installerUid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 343 | this.params = params; |
| 344 | this.createdMillis = createdMillis; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 345 | this.stageDir = stageDir; |
| 346 | this.stageCid = stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 347 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 348 | if ((stageDir == null) == (stageCid == null)) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 349 | throw new IllegalArgumentException( |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 350 | "Exactly one of stageDir or stageCid stage must be set"); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 353 | mPrepared = prepared; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 354 | mSealed = sealed; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 355 | |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 356 | final long identity = Binder.clearCallingIdentity(); |
| 357 | try { |
| 358 | final int uid = mPm.getPackageUid(PackageManagerService.DEFAULT_CONTAINER_PACKAGE, |
| 359 | PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM); |
| 360 | defaultContainerGid = UserHandle.getSharedAppGid(uid); |
| 361 | } finally { |
| 362 | Binder.restoreCallingIdentity(identity); |
| 363 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 366 | public SessionInfo generateInfo() { |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 367 | return generateInfo(true); |
| 368 | } |
| 369 | |
| 370 | public SessionInfo generateInfo(boolean includeIcon) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 371 | final SessionInfo info = new SessionInfo(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 372 | synchronized (mLock) { |
| 373 | info.sessionId = sessionId; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 374 | info.installerPackageName = mInstallerPackageName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 375 | info.resolvedBaseCodePath = (mResolvedBaseFile != null) ? |
| 376 | mResolvedBaseFile.getAbsolutePath() : null; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 377 | info.progress = mProgress; |
| 378 | info.sealed = mSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 379 | info.active = mActiveCount.get() > 0; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 380 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 381 | info.mode = params.mode; |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 382 | info.installReason = params.installReason; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 383 | info.sizeBytes = params.sizeBytes; |
| 384 | info.appPackageName = params.appPackageName; |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 385 | if (includeIcon) { |
| 386 | info.appIcon = params.appIcon; |
| 387 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 388 | info.appLabel = params.appLabel; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 389 | |
| 390 | info.installLocation = params.installLocation; |
| 391 | info.originatingUri = params.originatingUri; |
| 392 | info.originatingUid = params.originatingUid; |
| 393 | info.referrerUri = params.referrerUri; |
| 394 | info.grantedRuntimePermissions = params.grantedRuntimePermissions; |
| 395 | info.installFlags = params.installFlags; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 396 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 397 | return info; |
| 398 | } |
| 399 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 400 | public boolean isPrepared() { |
| 401 | synchronized (mLock) { |
| 402 | return mPrepared; |
| 403 | } |
| 404 | } |
| 405 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 406 | public boolean isSealed() { |
| 407 | synchronized (mLock) { |
| 408 | return mSealed; |
| 409 | } |
| 410 | } |
| 411 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 412 | private void assertPreparedAndNotSealedLocked(String cookie) { |
| 413 | assertPreparedAndNotCommittedOrDestroyedLocked(cookie); |
| 414 | if (mSealed) { |
| 415 | throw new SecurityException(cookie + " not allowed after sealing"); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | private void assertPreparedAndNotCommittedOrDestroyedLocked(String cookie) { |
| 420 | assertPreparedAndNotDestroyedLocked(cookie); |
| 421 | if (mCommitted) { |
| 422 | throw new SecurityException(cookie + " not allowed after commit"); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private void assertPreparedAndNotDestroyedLocked(String cookie) { |
| 427 | if (!mPrepared) { |
| 428 | throw new IllegalStateException(cookie + " before prepared"); |
| 429 | } |
| 430 | if (mDestroyed) { |
| 431 | throw new SecurityException(cookie + " not allowed after destruction"); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 435 | /** |
| 436 | * Resolve the actual location where staged data should be written. This |
| 437 | * might point at an ASEC mount point, which is why we delay path resolution |
| 438 | * until someone actively works with the session. |
| 439 | */ |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 440 | private File resolveStageDirLocked() throws IOException { |
| 441 | if (mResolvedStageDir == null) { |
| 442 | if (stageDir != null) { |
| 443 | mResolvedStageDir = stageDir; |
| 444 | } else { |
| 445 | final String path = PackageHelper.getSdDir(stageCid); |
| 446 | if (path != null) { |
| 447 | mResolvedStageDir = new File(path); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 448 | } else { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 449 | throw new IOException("Failed to resolve path to container " + stageCid); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 452 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 453 | return mResolvedStageDir; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 456 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 457 | public void setClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 458 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 459 | assertCallerIsOwnerOrRootLocked(); |
| 460 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 461 | // Always publish first staging movement |
| 462 | final boolean forcePublish = (mClientProgress == 0); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 463 | mClientProgress = progress; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 464 | computeProgressLocked(forcePublish); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 465 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 469 | public void addClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 470 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 471 | assertCallerIsOwnerOrRootLocked(); |
| 472 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 473 | setClientProgress(mClientProgress + progress); |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 477 | private void computeProgressLocked(boolean forcePublish) { |
| 478 | mProgress = MathUtils.constrain(mClientProgress * 0.8f, 0f, 0.8f) |
| 479 | + MathUtils.constrain(mInternalProgress * 0.2f, 0f, 0.2f); |
| 480 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 481 | // Only publish when meaningful change |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 482 | if (forcePublish || Math.abs(mProgress - mReportedProgress) >= 0.01) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 483 | mReportedProgress = mProgress; |
| 484 | mCallback.onSessionProgressChanged(this, mProgress); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 489 | public String[] getNames() { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 490 | synchronized (mLock) { |
| 491 | assertCallerIsOwnerOrRootLocked(); |
| 492 | assertPreparedAndNotCommittedOrDestroyedLocked("getNames"); |
| 493 | |
| 494 | try { |
| 495 | return resolveStageDirLocked().list(); |
| 496 | } catch (IOException e) { |
| 497 | throw ExceptionUtils.wrap(e); |
| 498 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 499 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | @Override |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 503 | public void removeSplit(String splitName) { |
| 504 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 505 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 506 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 507 | |
| 508 | synchronized (mLock) { |
| 509 | assertCallerIsOwnerOrRootLocked(); |
| 510 | assertPreparedAndNotCommittedOrDestroyedLocked("removeSplit"); |
| 511 | |
| 512 | try { |
| 513 | createRemoveSplitMarkerLocked(splitName); |
| 514 | } catch (IOException e) { |
| 515 | throw ExceptionUtils.wrap(e); |
| 516 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 520 | private void createRemoveSplitMarkerLocked(String splitName) throws IOException { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 521 | try { |
| 522 | final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION; |
| 523 | if (!FileUtils.isValidExtFilename(markerName)) { |
| 524 | throw new IllegalArgumentException("Invalid marker: " + markerName); |
| 525 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 526 | final File target = new File(resolveStageDirLocked(), markerName); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 527 | target.createNewFile(); |
| 528 | Os.chmod(target.getAbsolutePath(), 0 /*mode*/); |
| 529 | } catch (ErrnoException e) { |
| 530 | throw e.rethrowAsIOException(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | @Override |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 535 | public ParcelFileDescriptor openWrite(String name, long offsetBytes, long lengthBytes) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 536 | try { |
| 537 | return openWriteInternal(name, offsetBytes, lengthBytes); |
| 538 | } catch (IOException e) { |
| 539 | throw ExceptionUtils.wrap(e); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes) |
| 544 | throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 545 | // Quick sanity check of state, and allocate a pipe for ourselves. We |
| 546 | // then do heavy disk allocation outside the lock, but this open pipe |
| 547 | // will block any attempted install transitions. |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 548 | final RevocableFileDescriptor fd; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 549 | final FileBridge bridge; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 550 | final File stageDir; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 551 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 552 | assertCallerIsOwnerOrRootLocked(); |
| 553 | assertPreparedAndNotSealedLocked("openWrite"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 554 | |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 555 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 556 | fd = new RevocableFileDescriptor(); |
| 557 | bridge = null; |
| 558 | mFds.add(fd); |
| 559 | } else { |
| 560 | fd = null; |
| 561 | bridge = new FileBridge(); |
| 562 | mBridges.add(bridge); |
| 563 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 564 | |
| 565 | stageDir = resolveStageDirLocked(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | try { |
| 569 | // Use installer provided name for now; we always rename later |
| 570 | if (!FileUtils.isValidExtFilename(name)) { |
| 571 | throw new IllegalArgumentException("Invalid name: " + name); |
| 572 | } |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 573 | final File target; |
| 574 | final long identity = Binder.clearCallingIdentity(); |
| 575 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 576 | target = new File(stageDir, name); |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 577 | } finally { |
| 578 | Binder.restoreCallingIdentity(identity); |
| 579 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 580 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 581 | // TODO: this should delegate to DCS so the system process avoids |
| 582 | // holding open FDs into containers. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 583 | final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 584 | O_CREAT | O_WRONLY, 0644); |
| 585 | Os.chmod(target.getAbsolutePath(), 0644); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 586 | |
| 587 | // If caller specified a total length, allocate it for them. Free up |
| 588 | // cache space to grow, if needed. |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 589 | if (stageDir != null && lengthBytes > 0) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 590 | mContext.getSystemService(StorageManager.class).allocateBytes(targetFd, lengthBytes, |
| 591 | PackageHelper.translateAllocateFlags(params.installFlags)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (offsetBytes > 0) { |
| 595 | Libcore.os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET); |
| 596 | } |
| 597 | |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 598 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 599 | fd.init(mContext, targetFd); |
| 600 | return fd.getRevocableFileDescriptor(); |
| 601 | } else { |
| 602 | bridge.setTargetFile(targetFd); |
| 603 | bridge.start(); |
| 604 | return new ParcelFileDescriptor(bridge.getClientSocket()); |
| 605 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 606 | |
| 607 | } catch (ErrnoException e) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 608 | throw e.rethrowAsIOException(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | @Override |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 613 | public ParcelFileDescriptor openRead(String name) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 614 | synchronized (mLock) { |
| 615 | assertCallerIsOwnerOrRootLocked(); |
| 616 | assertPreparedAndNotCommittedOrDestroyedLocked("openRead"); |
| 617 | try { |
| 618 | return openReadInternalLocked(name); |
| 619 | } catch (IOException e) { |
| 620 | throw ExceptionUtils.wrap(e); |
| 621 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 625 | private ParcelFileDescriptor openReadInternalLocked(String name) throws IOException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 626 | try { |
| 627 | if (!FileUtils.isValidExtFilename(name)) { |
| 628 | throw new IllegalArgumentException("Invalid name: " + name); |
| 629 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 630 | final File target = new File(resolveStageDirLocked(), name); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 631 | final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_RDONLY, 0); |
| 632 | return new ParcelFileDescriptor(targetFd); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 633 | } catch (ErrnoException e) { |
| 634 | throw e.rethrowAsIOException(); |
| 635 | } |
| 636 | } |
| 637 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 638 | /** |
| 639 | * Check if the caller is the owner of this session. Otherwise throw a |
| 640 | * {@link SecurityException}. |
| 641 | */ |
| 642 | private void assertCallerIsOwnerOrRootLocked() { |
| 643 | final int callingUid = Binder.getCallingUid(); |
| 644 | if (callingUid != Process.ROOT_UID && callingUid != mInstallerUid) { |
| 645 | throw new SecurityException("Session does not belong to uid " + callingUid); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * If anybody is reading or writing data of the session, throw an {@link SecurityException}. |
| 651 | */ |
| 652 | private void assertNoWriteFileTransfersOpenLocked() { |
| 653 | // Verify that all writers are hands-off |
| 654 | for (RevocableFileDescriptor fd : mFds) { |
| 655 | if (!fd.isRevoked()) { |
| 656 | throw new SecurityException("Files still open"); |
| 657 | } |
| 658 | } |
| 659 | for (FileBridge bridge : mBridges) { |
| 660 | if (!bridge.isClosed()) { |
| 661 | throw new SecurityException("Files still open"); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 666 | @Override |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 667 | public void commit(@NonNull IntentSender statusReceiver, boolean forTransfer) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 668 | Preconditions.checkNotNull(statusReceiver); |
| 669 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 670 | // Cache package manager data without the lock held |
| 671 | final PackageInfo installedPkgInfo = mPm.getPackageInfo( |
| 672 | params.appPackageName, PackageManager.GET_SIGNATURES |
| 673 | | PackageManager.MATCH_STATIC_SHARED_LIBRARIES /*flags*/, userId); |
| 674 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 675 | final boolean wasSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 676 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 677 | assertCallerIsOwnerOrRootLocked(); |
| 678 | assertPreparedAndNotDestroyedLocked("commit"); |
| 679 | |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 680 | final PackageInstallObserverAdapter adapter = new PackageInstallObserverAdapter( |
| 681 | mContext, statusReceiver, sessionId, isInstallerDeviceOwnerLocked(), userId); |
| 682 | mRemoteObserver = adapter.getBinder(); |
| 683 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 684 | if (forTransfer) { |
| 685 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, null); |
| 686 | |
| 687 | if (mInstallerUid == mOriginalInstallerUid) { |
| 688 | throw new IllegalArgumentException("Session has not been transferred"); |
| 689 | } |
| 690 | } else { |
| 691 | if (mInstallerUid != mOriginalInstallerUid) { |
| 692 | throw new IllegalArgumentException("Session has been transferred"); |
| 693 | } |
| 694 | } |
| 695 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 696 | wasSealed = mSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 697 | if (!mSealed) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 698 | try { |
| 699 | sealAndValidateLocked(installedPkgInfo); |
| 700 | } catch (PackageManagerException e) { |
| 701 | // Do now throw an exception here to stay compatible with O and older |
| 702 | destroyInternal(); |
| 703 | dispatchSessionFinished(e.error, ExceptionUtils.getCompleteMessage(e), null); |
| 704 | return; |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 705 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 706 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 707 | |
| 708 | // Client staging is fully done at this point |
| 709 | mClientProgress = 1f; |
| 710 | computeProgressLocked(true); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 711 | |
| 712 | // This ongoing commit should keep session active, even though client |
| 713 | // will probably close their end. |
| 714 | mActiveCount.incrementAndGet(); |
| 715 | |
| 716 | mCommitted = true; |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 717 | mHandler.obtainMessage(MSG_COMMIT).sendToTarget(); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 720 | if (!wasSealed) { |
| 721 | // Persist the fact that we've sealed ourselves to prevent |
| 722 | // mutations of any hard links we create. We do this without holding |
| 723 | // the session lock, since otherwise it's a lock inversion. |
| 724 | mCallback.onSessionSealedBlocking(this); |
| 725 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 728 | /** |
| 729 | * Seal the session to prevent further modification and validate the contents of it. |
| 730 | * |
| 731 | * <p>The session will be sealed after calling this method even if it failed. |
| 732 | * |
| 733 | * @param pkgInfo The package info for {@link #params}.packagename |
| 734 | */ |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 735 | private void sealAndValidateLocked(@Nullable PackageInfo pkgInfo) |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 736 | throws PackageManagerException { |
| 737 | assertNoWriteFileTransfersOpenLocked(); |
| 738 | |
| 739 | mSealed = true; |
| 740 | |
| 741 | // Verify that stage looks sane with respect to existing application. |
| 742 | // This currently only ensures packageName, versionCode, and certificate |
| 743 | // consistency. |
| 744 | validateInstallLocked(pkgInfo); |
| 745 | |
| 746 | // Read transfers from the original owner stay open, but as the session's data |
| 747 | // cannot be modified anymore, there is no leak of information. |
| 748 | } |
| 749 | |
| 750 | @Override |
| 751 | public void transfer(String packageName) { |
| 752 | Preconditions.checkNotNull(packageName); |
| 753 | |
| 754 | ApplicationInfo newOwnerAppInfo = mPm.getApplicationInfo(packageName, 0, userId); |
| 755 | if (newOwnerAppInfo == null) { |
| 756 | throw new ParcelableException(new PackageManager.NameNotFoundException(packageName)); |
| 757 | } |
| 758 | |
| 759 | if (PackageManager.PERMISSION_GRANTED != mPm.checkUidPermission( |
| 760 | Manifest.permission.INSTALL_PACKAGES, newOwnerAppInfo.uid)) { |
| 761 | throw new SecurityException("Destination package " + packageName + " does not have " |
| 762 | + "the " + Manifest.permission.INSTALL_PACKAGES + " permission"); |
| 763 | } |
| 764 | |
| 765 | // Cache package manager data without the lock held |
| 766 | final PackageInfo installedPkgInfo = mPm.getPackageInfo( |
| 767 | params.appPackageName, PackageManager.GET_SIGNATURES |
| 768 | | PackageManager.MATCH_STATIC_SHARED_LIBRARIES /*flags*/, userId); |
| 769 | |
| 770 | // Only install flags that can be verified by the app the session is transferred to are |
| 771 | // allowed. The parameters can be read via PackageInstaller.SessionInfo. |
| 772 | if (!params.areHiddenOptionsSet()) { |
| 773 | throw new SecurityException("Can only transfer sessions that use public options"); |
| 774 | } |
| 775 | |
| 776 | synchronized (mLock) { |
| 777 | assertCallerIsOwnerOrRootLocked(); |
| 778 | assertPreparedAndNotSealedLocked("transfer"); |
| 779 | |
| 780 | try { |
| 781 | sealAndValidateLocked(installedPkgInfo); |
| 782 | } catch (PackageManagerException e) { |
| 783 | throw new IllegalArgumentException("Package is not valid", e); |
| 784 | } |
| 785 | |
| 786 | if (!mPackageName.equals(mInstallerPackageName)) { |
| 787 | throw new SecurityException("Can only transfer sessions that update the original " |
| 788 | + "installer"); |
| 789 | } |
| 790 | |
| 791 | mInstallerPackageName = packageName; |
| 792 | mInstallerUid = newOwnerAppInfo.uid; |
| 793 | } |
| 794 | |
| 795 | // Persist the fact that we've sealed ourselves to prevent |
| 796 | // mutations of any hard links we create. We do this without holding |
| 797 | // the session lock, since otherwise it's a lock inversion. |
| 798 | mCallback.onSessionSealedBlocking(this); |
| 799 | } |
| 800 | |
| 801 | private void commitLocked() |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 802 | throws PackageManagerException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 803 | if (mDestroyed) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 804 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session destroyed"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 805 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 806 | if (!mSealed) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 807 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session not sealed"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 810 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 811 | resolveStageDirLocked(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 812 | } catch (IOException e) { |
| 813 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 814 | "Failed to resolve stage location", e); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 817 | Preconditions.checkNotNull(mPackageName); |
Todd Kennedy | 373f0b4 | 2015-12-16 14:45:14 -0800 | [diff] [blame] | 818 | Preconditions.checkNotNull(mSignatures); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 819 | Preconditions.checkNotNull(mResolvedBaseFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 820 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 821 | if (needToAskForPermissionsLocked()) { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 822 | // User needs to accept permissions; give installer an intent they |
| 823 | // can use to involve user. |
| 824 | final Intent intent = new Intent(PackageInstaller.ACTION_CONFIRM_PERMISSIONS); |
Svet Ganov | f1b7f20 | 2015-07-29 08:33:42 -0700 | [diff] [blame] | 825 | intent.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 826 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 827 | try { |
| 828 | mRemoteObserver.onUserActionRequired(intent); |
| 829 | } catch (RemoteException ignored) { |
| 830 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 831 | |
| 832 | // Commit was keeping session marked as active until now; release |
| 833 | // that extra refcount so session appears idle. |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 834 | closeInternal(false); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 835 | return; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 838 | if (stageCid != null) { |
| 839 | // Figure out the final installed size and resize the container once |
| 840 | // and for all. Internally the parser handles straddling between two |
| 841 | // locations when inheriting. |
| 842 | final long finalSize = calculateInstalledSize(); |
| 843 | resizeContainer(stageCid, finalSize); |
| 844 | } |
| 845 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 846 | // Inherit any packages and native libraries from existing install that |
| 847 | // haven't been overridden. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 848 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 849 | try { |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 850 | final List<File> fromFiles = mResolvedInheritedFiles; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 851 | final File toDir = resolveStageDirLocked(); |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 852 | |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 853 | if (LOGD) Slog.d(TAG, "Inherited files: " + mResolvedInheritedFiles); |
| 854 | if (!mResolvedInheritedFiles.isEmpty() && mInheritedFilesBase == null) { |
| 855 | throw new IllegalStateException("mInheritedFilesBase == null"); |
| 856 | } |
| 857 | |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 858 | if (isLinkPossible(fromFiles, toDir)) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 859 | if (!mResolvedInstructionSets.isEmpty()) { |
| 860 | final File oatDir = new File(toDir, "oat"); |
| 861 | createOatDirs(mResolvedInstructionSets, oatDir); |
| 862 | } |
| 863 | linkFiles(fromFiles, toDir, mInheritedFilesBase); |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 864 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 865 | // TODO: this should delegate to DCS so the system process |
| 866 | // avoids holding open FDs into containers. |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 867 | copyFiles(fromFiles, toDir); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 868 | } |
| 869 | } catch (IOException e) { |
| 870 | throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE, |
| 871 | "Failed to inherit existing install", e); |
| 872 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 875 | // TODO: surface more granular state from dexopt |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 876 | mInternalProgress = 0.5f; |
| 877 | computeProgressLocked(true); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 878 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 879 | // Unpack native libraries |
| 880 | extractNativeLibraries(mResolvedStageDir, params.abiOverride); |
| 881 | |
| 882 | // Container is ready to go, let's seal it up! |
| 883 | if (stageCid != null) { |
| 884 | finalizeAndFixContainer(stageCid); |
| 885 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 886 | |
| 887 | // We've reached point of no return; call into PMS to install the stage. |
| 888 | // Regardless of success or failure we always destroy session. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 889 | final IPackageInstallObserver2 localObserver = new IPackageInstallObserver2.Stub() { |
| 890 | @Override |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 891 | public void onUserActionRequired(Intent intent) { |
| 892 | throw new IllegalStateException(); |
| 893 | } |
| 894 | |
| 895 | @Override |
| 896 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 897 | Bundle extras) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 898 | destroyInternal(); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 899 | dispatchSessionFinished(returnCode, msg, extras); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 900 | } |
| 901 | }; |
| 902 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 903 | final UserHandle user; |
| 904 | if ((params.installFlags & PackageManager.INSTALL_ALL_USERS) != 0) { |
| 905 | user = UserHandle.ALL; |
| 906 | } else { |
| 907 | user = new UserHandle(userId); |
| 908 | } |
| 909 | |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 910 | mRelinquished = true; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 911 | mPm.installStage(mPackageName, stageDir, stageCid, localObserver, params, |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 912 | mInstallerPackageName, mInstallerUid, user, mCertificates); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Validate install by confirming that all application packages are have |
| 917 | * consistent package name, version code, and signing certificates. |
| 918 | * <p> |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 919 | * Clears and populates {@link #mResolvedBaseFile}, |
| 920 | * {@link #mResolvedStagedFiles}, and {@link #mResolvedInheritedFiles}. |
| 921 | * <p> |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 922 | * Renames package files in stage to match split names defined inside. |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 923 | * <p> |
| 924 | * Note that upgrade compatibility is still performed by |
| 925 | * {@link PackageManagerService}. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 926 | */ |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 927 | private void validateInstallLocked(@Nullable PackageInfo pkgInfo) |
| 928 | throws PackageManagerException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 929 | mPackageName = null; |
| 930 | mVersionCode = -1; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 931 | mSignatures = null; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 932 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 933 | mResolvedBaseFile = null; |
| 934 | mResolvedStagedFiles.clear(); |
| 935 | mResolvedInheritedFiles.clear(); |
| 936 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 937 | final File[] removedFiles = mResolvedStageDir.listFiles(sRemovedFilter); |
| 938 | final List<String> removeSplitList = new ArrayList<>(); |
| 939 | if (!ArrayUtils.isEmpty(removedFiles)) { |
| 940 | for (File removedFile : removedFiles) { |
| 941 | final String fileName = removedFile.getName(); |
| 942 | final String splitName = fileName.substring( |
| 943 | 0, fileName.length() - REMOVE_SPLIT_MARKER_EXTENSION.length()); |
| 944 | removeSplitList.add(splitName); |
| 945 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 948 | final File[] addedFiles = mResolvedStageDir.listFiles(sAddedFilter); |
| 949 | if (ArrayUtils.isEmpty(addedFiles) && removeSplitList.size() == 0) { |
| 950 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "No packages staged"); |
| 951 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 952 | // Verify that all staged packages are internally consistent |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 953 | final ArraySet<String> stagedSplits = new ArraySet<>(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 954 | for (File addedFile : addedFiles) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 955 | final ApkLite apk; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 956 | try { |
Alex Klyubin | 58bfae4 | 2017-01-03 16:25:33 -0800 | [diff] [blame] | 957 | int flags = PackageParser.PARSE_COLLECT_CERTIFICATES; |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 958 | if ((params.installFlags & PackageManager.INSTALL_INSTANT_APP) != 0) { |
Alex Klyubin | 58bfae4 | 2017-01-03 16:25:33 -0800 | [diff] [blame] | 959 | flags |= PackageParser.PARSE_IS_EPHEMERAL; |
| 960 | } |
| 961 | apk = PackageParser.parseApkLite(addedFile, flags); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 962 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 963 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 966 | if (!stagedSplits.add(apk.splitName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 967 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 968 | "Split " + apk.splitName + " was defined multiple times"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | // Use first package to define unknown values |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 972 | if (mPackageName == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 973 | mPackageName = apk.packageName; |
| 974 | mVersionCode = apk.versionCode; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 975 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 976 | if (mSignatures == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 977 | mSignatures = apk.signatures; |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 978 | mCertificates = apk.certificates; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 981 | assertApkConsistentLocked(String.valueOf(addedFile), apk); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 982 | |
| 983 | // Take this opportunity to enforce uniform naming |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 984 | final String targetName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 985 | if (apk.splitName == null) { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 986 | targetName = "base.apk"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 987 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 988 | targetName = "split_" + apk.splitName + ".apk"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 989 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 990 | if (!FileUtils.isValidExtFilename(targetName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 991 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 992 | "Invalid filename: " + targetName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 993 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 994 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 995 | final File targetFile = new File(mResolvedStageDir, targetName); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 996 | if (!addedFile.equals(targetFile)) { |
| 997 | addedFile.renameTo(targetFile); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | // Base is coming from session |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1001 | if (apk.splitName == null) { |
| 1002 | mResolvedBaseFile = targetFile; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1003 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1004 | |
| 1005 | mResolvedStagedFiles.add(targetFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1008 | if (removeSplitList.size() > 0) { |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 1009 | if (pkgInfo == null) { |
| 1010 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1011 | "Missing existing base package for " + mPackageName); |
| 1012 | } |
| 1013 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1014 | // validate split names marked for removal |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1015 | for (String splitName : removeSplitList) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1016 | if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1017 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1018 | "Split not found: " + splitName); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | // ensure we've got appropriate package name, version code and signatures |
| 1023 | if (mPackageName == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1024 | mPackageName = pkgInfo.packageName; |
| 1025 | mVersionCode = pkgInfo.versionCode; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1026 | } |
| 1027 | if (mSignatures == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1028 | mSignatures = pkgInfo.signatures; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1032 | if (params.mode == SessionParams.MODE_FULL_INSTALL) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1033 | // Full installs must include a base package |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1034 | if (!stagedSplits.contains(null)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1035 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1036 | "Full install must include a base package"); |
| 1037 | } |
| 1038 | |
| 1039 | } else { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1040 | // Partial installs must be consistent with existing install |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1041 | if (pkgInfo == null || pkgInfo.applicationInfo == null) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1042 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1043 | "Missing existing base package for " + mPackageName); |
| 1044 | } |
| 1045 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1046 | final PackageLite existing; |
| 1047 | final ApkLite existingBase; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1048 | ApplicationInfo appInfo = pkgInfo.applicationInfo; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1049 | try { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1050 | existing = PackageParser.parsePackageLite(new File(appInfo.getCodePath()), 0); |
| 1051 | existingBase = PackageParser.parseApkLite(new File(appInfo.getBaseCodePath()), |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1052 | PackageParser.PARSE_COLLECT_CERTIFICATES); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 1053 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 1054 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1057 | assertApkConsistentLocked("Existing base", existingBase); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1058 | |
| 1059 | // Inherit base if not overridden |
| 1060 | if (mResolvedBaseFile == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1061 | mResolvedBaseFile = new File(appInfo.getBaseCodePath()); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1062 | mResolvedInheritedFiles.add(mResolvedBaseFile); |
| 1063 | } |
| 1064 | |
| 1065 | // Inherit splits if not overridden |
| 1066 | if (!ArrayUtils.isEmpty(existing.splitNames)) { |
| 1067 | for (int i = 0; i < existing.splitNames.length; i++) { |
| 1068 | final String splitName = existing.splitNames[i]; |
| 1069 | final File splitFile = new File(existing.splitCodePaths[i]); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1070 | final boolean splitRemoved = removeSplitList.contains(splitName); |
| 1071 | if (!stagedSplits.contains(splitName) && !splitRemoved) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1072 | mResolvedInheritedFiles.add(splitFile); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1076 | |
| 1077 | // Inherit compiled oat directory. |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1078 | final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1079 | mInheritedFilesBase = packageInstallDir; |
| 1080 | final File oatDir = new File(packageInstallDir, "oat"); |
| 1081 | if (oatDir.exists()) { |
| 1082 | final File[] archSubdirs = oatDir.listFiles(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1083 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1084 | // Keep track of all instruction sets we've seen compiled output for. |
| 1085 | // If we're linking (and not copying) inherited files, we can recreate the |
| 1086 | // instruction set hierarchy and link compiled output. |
| 1087 | if (archSubdirs != null && archSubdirs.length > 0) { |
| 1088 | final String[] instructionSets = InstructionSets.getAllDexCodeInstructionSets(); |
| 1089 | for (File archSubDir : archSubdirs) { |
| 1090 | // Skip any directory that isn't an ISA subdir. |
| 1091 | if (!ArrayUtils.contains(instructionSets, archSubDir.getName())) { |
| 1092 | continue; |
| 1093 | } |
| 1094 | |
| 1095 | mResolvedInstructionSets.add(archSubDir.getName()); |
| 1096 | List<File> oatFiles = Arrays.asList(archSubDir.listFiles()); |
Jeff Hao | d1235f5 | 2017-06-13 11:09:10 -0700 | [diff] [blame] | 1097 | |
| 1098 | // Only add compiled files associated with the base. |
| 1099 | // Once b/62269291 is resolved, we can add all compiled files again. |
| 1100 | for (File oatFile : oatFiles) { |
| 1101 | if (oatFile.getName().equals("base.art") |
| 1102 | || oatFile.getName().equals("base.odex") |
| 1103 | || oatFile.getName().equals("base.vdex")) { |
| 1104 | mResolvedInheritedFiles.add(oatFile); |
| 1105 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1106 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1113 | private void assertApkConsistentLocked(String tag, ApkLite apk) |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1114 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1115 | if (!mPackageName.equals(apk.packageName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1116 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag + " package " |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1117 | + apk.packageName + " inconsistent with " + mPackageName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1118 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 1119 | if (params.appPackageName != null && !params.appPackageName.equals(apk.packageName)) { |
| 1120 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
| 1121 | + " specified package " + params.appPackageName |
| 1122 | + " inconsistent with " + apk.packageName); |
| 1123 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1124 | if (mVersionCode != apk.versionCode) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1125 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1126 | + " version code " + apk.versionCode + " inconsistent with " |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1127 | + mVersionCode); |
| 1128 | } |
Todd Kennedy | 373f0b4 | 2015-12-16 14:45:14 -0800 | [diff] [blame] | 1129 | if (!Signature.areExactMatch(mSignatures, apk.signatures)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 1130 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1131 | tag + " signatures are inconsistent"); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /** |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1136 | * Calculate the final install footprint size, combining both staged and |
| 1137 | * existing APKs together and including unpacked native code from both. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1138 | */ |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1139 | private long calculateInstalledSize() throws PackageManagerException { |
| 1140 | Preconditions.checkNotNull(mResolvedBaseFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1141 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1142 | final ApkLite baseApk; |
| 1143 | try { |
| 1144 | baseApk = PackageParser.parseApkLite(mResolvedBaseFile, 0); |
| 1145 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 1146 | throw PackageManagerException.from(e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1147 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1148 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1149 | final List<String> splitPaths = new ArrayList<>(); |
| 1150 | for (File file : mResolvedStagedFiles) { |
| 1151 | if (mResolvedBaseFile.equals(file)) continue; |
| 1152 | splitPaths.add(file.getAbsolutePath()); |
| 1153 | } |
| 1154 | for (File file : mResolvedInheritedFiles) { |
| 1155 | if (mResolvedBaseFile.equals(file)) continue; |
| 1156 | splitPaths.add(file.getAbsolutePath()); |
| 1157 | } |
| 1158 | |
| 1159 | // This is kind of hacky; we're creating a half-parsed package that is |
| 1160 | // straddled between the inherited and staged APKs. |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 1161 | final PackageLite pkg = new PackageLite(null, baseApk, null, null, null, null, |
Narayan Kamath | 96c11c5 | 2017-08-09 13:07:21 +0100 | [diff] [blame] | 1162 | splitPaths.toArray(new String[splitPaths.size()]), null); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1163 | final boolean isForwardLocked = |
| 1164 | (params.installFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 1165 | |
| 1166 | try { |
| 1167 | return PackageHelper.calculateInstalledSize(pkg, isForwardLocked, params.abiOverride); |
| 1168 | } catch (IOException e) { |
| 1169 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1170 | "Failed to calculate install size", e); |
| 1171 | } |
| 1172 | } |
| 1173 | |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 1174 | /** |
| 1175 | * Determine if creating hard links between source and destination is |
| 1176 | * possible. That is, do they all live on the same underlying device. |
| 1177 | */ |
| 1178 | private boolean isLinkPossible(List<File> fromFiles, File toDir) { |
| 1179 | try { |
| 1180 | final StructStat toStat = Os.stat(toDir.getAbsolutePath()); |
| 1181 | for (File fromFile : fromFiles) { |
| 1182 | final StructStat fromStat = Os.stat(fromFile.getAbsolutePath()); |
| 1183 | if (fromStat.st_dev != toStat.st_dev) { |
| 1184 | return false; |
| 1185 | } |
| 1186 | } |
| 1187 | } catch (ErrnoException e) { |
| 1188 | Slog.w(TAG, "Failed to detect if linking possible: " + e); |
| 1189 | return false; |
| 1190 | } |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1194 | /** |
| 1195 | * @return the uid of the owner this session |
| 1196 | */ |
| 1197 | public int getInstallerUid() { |
| 1198 | synchronized (mLock) { |
| 1199 | return mInstallerUid; |
| 1200 | } |
| 1201 | } |
| 1202 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1203 | private static String getRelativePath(File file, File base) throws IOException { |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1204 | final String pathStr = file.getAbsolutePath(); |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1205 | final String baseStr = base.getAbsolutePath(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1206 | // Don't allow relative paths. |
| 1207 | if (pathStr.contains("/.") ) { |
| 1208 | throw new IOException("Invalid path (was relative) : " + pathStr); |
| 1209 | } |
| 1210 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1211 | if (pathStr.startsWith(baseStr)) { |
| 1212 | return pathStr.substring(baseStr.length()); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1215 | throw new IOException("File: " + pathStr + " outside base: " + baseStr); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1218 | private void createOatDirs(List<String> instructionSets, File fromDir) |
| 1219 | throws PackageManagerException { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1220 | for (String instructionSet : instructionSets) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1221 | try { |
| 1222 | mPm.mInstaller.createOatDir(fromDir.getAbsolutePath(), instructionSet); |
| 1223 | } catch (InstallerException e) { |
| 1224 | throw PackageManagerException.from(e); |
| 1225 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | private void linkFiles(List<File> fromFiles, File toDir, File fromDir) |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 1230 | throws IOException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1231 | for (File fromFile : fromFiles) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1232 | final String relativePath = getRelativePath(fromFile, fromDir); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1233 | try { |
| 1234 | mPm.mInstaller.linkFile(relativePath, fromDir.getAbsolutePath(), |
| 1235 | toDir.getAbsolutePath()); |
| 1236 | } catch (InstallerException e) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1237 | throw new IOException("failed linkOrCreateDir(" + relativePath + ", " |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1238 | + fromDir + ", " + toDir + ")", e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 1241 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1242 | Slog.d(TAG, "Linked " + fromFiles.size() + " files into " + toDir); |
| 1243 | } |
| 1244 | |
| 1245 | private static void copyFiles(List<File> fromFiles, File toDir) throws IOException { |
| 1246 | // Remove any partial files from previous attempt |
| 1247 | for (File file : toDir.listFiles()) { |
| 1248 | if (file.getName().endsWith(".tmp")) { |
| 1249 | file.delete(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1250 | } |
| 1251 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1252 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1253 | for (File fromFile : fromFiles) { |
| 1254 | final File tmpFile = File.createTempFile("inherit", ".tmp", toDir); |
| 1255 | if (LOGD) Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile); |
| 1256 | if (!FileUtils.copyFile(fromFile, tmpFile)) { |
| 1257 | throw new IOException("Failed to copy " + fromFile + " to " + tmpFile); |
| 1258 | } |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 1259 | try { |
| 1260 | Os.chmod(tmpFile.getAbsolutePath(), 0644); |
| 1261 | } catch (ErrnoException e) { |
| 1262 | throw new IOException("Failed to chmod " + tmpFile); |
| 1263 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1264 | final File toFile = new File(toDir, fromFile.getName()); |
| 1265 | if (LOGD) Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile); |
| 1266 | if (!tmpFile.renameTo(toFile)) { |
| 1267 | throw new IOException("Failed to rename " + tmpFile + " to " + toFile); |
| 1268 | } |
| 1269 | } |
| 1270 | Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir); |
| 1271 | } |
| 1272 | |
| 1273 | private static void extractNativeLibraries(File packageDir, String abiOverride) |
| 1274 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1275 | // Always start from a clean slate |
| 1276 | final File libDir = new File(packageDir, NativeLibraryHelper.LIB_DIR_NAME); |
| 1277 | NativeLibraryHelper.removeNativeBinariesFromDirLI(libDir, true); |
| 1278 | |
| 1279 | NativeLibraryHelper.Handle handle = null; |
| 1280 | try { |
| 1281 | handle = NativeLibraryHelper.Handle.create(packageDir); |
| 1282 | final int res = NativeLibraryHelper.copyNativeBinariesWithOverride(handle, libDir, |
| 1283 | abiOverride); |
| 1284 | if (res != PackageManager.INSTALL_SUCCEEDED) { |
| 1285 | throw new PackageManagerException(res, |
| 1286 | "Failed to extract native libraries, res=" + res); |
| 1287 | } |
| 1288 | } catch (IOException e) { |
| 1289 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1290 | "Failed to extract native libraries", e); |
| 1291 | } finally { |
| 1292 | IoUtils.closeQuietly(handle); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | private static void resizeContainer(String cid, long targetSize) |
| 1297 | throws PackageManagerException { |
| 1298 | String path = PackageHelper.getSdDir(cid); |
| 1299 | if (path == null) { |
| 1300 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1301 | "Failed to find mounted " + cid); |
| 1302 | } |
| 1303 | |
| 1304 | final long currentSize = new File(path).getTotalSpace(); |
| 1305 | if (currentSize > targetSize) { |
| 1306 | Slog.w(TAG, "Current size " + currentSize + " is larger than target size " |
| 1307 | + targetSize + "; skipping resize"); |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | if (!PackageHelper.unMountSdDir(cid)) { |
| 1312 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1313 | "Failed to unmount " + cid + " before resize"); |
| 1314 | } |
| 1315 | |
| 1316 | if (!PackageHelper.resizeSdDir(targetSize, cid, |
| 1317 | PackageManagerService.getEncryptKey())) { |
| 1318 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1319 | "Failed to resize " + cid + " to " + targetSize + " bytes"); |
| 1320 | } |
| 1321 | |
| 1322 | path = PackageHelper.mountSdDir(cid, PackageManagerService.getEncryptKey(), |
| 1323 | Process.SYSTEM_UID, false); |
| 1324 | if (path == null) { |
| 1325 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1326 | "Failed to mount " + cid + " after resize"); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | private void finalizeAndFixContainer(String cid) throws PackageManagerException { |
| 1331 | if (!PackageHelper.finalizeSdDir(cid)) { |
| 1332 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1333 | "Failed to finalize container " + cid); |
| 1334 | } |
| 1335 | |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1336 | if (!PackageHelper.fixSdPermissions(cid, defaultContainerGid, null)) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1337 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1338 | "Failed to fix permissions on container " + cid); |
| 1339 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1342 | void setPermissionsResult(boolean accepted) { |
| 1343 | if (!mSealed) { |
| 1344 | throw new SecurityException("Must be sealed to accept permissions"); |
| 1345 | } |
| 1346 | |
| 1347 | if (accepted) { |
| 1348 | // Mark and kick off another install pass |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1349 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1350 | mPermissionsManuallyAccepted = true; |
| 1351 | mHandler.obtainMessage(MSG_COMMIT).sendToTarget(); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1352 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1353 | } else { |
| 1354 | destroyInternal(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 1355 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "User rejected permissions", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1359 | public void open() throws IOException { |
| 1360 | if (mActiveCount.getAndIncrement() == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1361 | mCallback.onSessionActiveChanged(this, true); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1362 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1363 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1364 | boolean wasPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1365 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1366 | wasPrepared = mPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1367 | if (!mPrepared) { |
| 1368 | if (stageDir != null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1369 | prepareStageDir(stageDir); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1370 | } else if (stageCid != null) { |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 1371 | final long identity = Binder.clearCallingIdentity(); |
| 1372 | try { |
| 1373 | prepareExternalStageCid(stageCid, params.sizeBytes); |
| 1374 | } finally { |
| 1375 | Binder.restoreCallingIdentity(identity); |
| 1376 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1377 | |
| 1378 | // TODO: deliver more granular progress for ASEC allocation |
| 1379 | mInternalProgress = 0.25f; |
| 1380 | computeProgressLocked(true); |
| 1381 | } else { |
| 1382 | throw new IllegalArgumentException( |
| 1383 | "Exactly one of stageDir or stageCid stage must be set"); |
| 1384 | } |
| 1385 | |
| 1386 | mPrepared = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (!wasPrepared) { |
| 1391 | mCallback.onSessionPrepared(this); |
| 1392 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1395 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1396 | public void close() { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 1397 | closeInternal(true); |
| 1398 | } |
| 1399 | |
| 1400 | private void closeInternal(boolean checkCaller) { |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 1401 | int activeCount; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1402 | synchronized (mLock) { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 1403 | if (checkCaller) { |
| 1404 | assertCallerIsOwnerOrRootLocked(); |
| 1405 | } |
| 1406 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 1407 | activeCount = mActiveCount.decrementAndGet(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 1410 | if (activeCount == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1411 | mCallback.onSessionActiveChanged(this, false); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1412 | } |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | @Override |
| 1416 | public void abandon() { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1417 | synchronized (mLock) { |
| 1418 | assertCallerIsOwnerOrRootLocked(); |
| 1419 | |
| 1420 | if (mRelinquished) { |
| 1421 | Slog.d(TAG, "Ignoring abandon after commit relinquished control"); |
| 1422 | return; |
| 1423 | } |
| 1424 | destroyInternal(); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1425 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1426 | |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 1427 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "Session was abandoned", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | private void dispatchSessionFinished(int returnCode, String msg, Bundle extras) { |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 1431 | final IPackageInstallObserver2 observer; |
| 1432 | final String packageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1433 | synchronized (mLock) { |
| 1434 | mFinalStatus = returnCode; |
| 1435 | mFinalMessage = msg; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1436 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1437 | observer = mRemoteObserver; |
| 1438 | packageName = mPackageName; |
| 1439 | } |
| 1440 | |
| 1441 | if (observer != null) { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1442 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1443 | observer.onPackageInstalled(packageName, returnCode, msg, extras); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1444 | } catch (RemoteException ignored) { |
| 1445 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1446 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1447 | |
| 1448 | final boolean success = (returnCode == PackageManager.INSTALL_SUCCEEDED); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 1449 | |
| 1450 | // Send broadcast to default launcher only if it's a new install |
| 1451 | final boolean isNewInstall = extras == null || !extras.getBoolean(Intent.EXTRA_REPLACING); |
| 1452 | if (success && isNewInstall) { |
Sunny Goyal | a31a74b | 2017-05-11 15:59:19 -0700 | [diff] [blame] | 1453 | mPm.sendSessionCommitBroadcast(generateInfo(), userId); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1456 | mCallback.onSessionFinished(this, success); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1457 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1458 | |
| 1459 | private void destroyInternal() { |
| 1460 | synchronized (mLock) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1461 | mSealed = true; |
| 1462 | mDestroyed = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1463 | |
| 1464 | // Force shut down all bridges |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 1465 | for (RevocableFileDescriptor fd : mFds) { |
| 1466 | fd.revoke(); |
| 1467 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1468 | for (FileBridge bridge : mBridges) { |
| 1469 | bridge.forceClose(); |
| 1470 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1471 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1472 | if (stageDir != null) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1473 | try { |
| 1474 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 1475 | } catch (InstallerException ignored) { |
| 1476 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1477 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1478 | if (stageCid != null) { |
| 1479 | PackageHelper.destroySdDir(stageCid); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1480 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1484 | synchronized (mLock) { |
| 1485 | dumpLocked(pw); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | private void dumpLocked(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1490 | pw.println("Session " + sessionId + ":"); |
| 1491 | pw.increaseIndent(); |
| 1492 | |
| 1493 | pw.printPair("userId", userId); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1494 | pw.printPair("mOriginalInstallerUid", mOriginalInstallerUid); |
| 1495 | pw.printPair("mInstallerPackageName", mInstallerPackageName); |
| 1496 | pw.printPair("mInstallerUid", mInstallerUid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1497 | pw.printPair("createdMillis", createdMillis); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1498 | pw.printPair("stageDir", stageDir); |
| 1499 | pw.printPair("stageCid", stageCid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1500 | pw.println(); |
| 1501 | |
| 1502 | params.dump(pw); |
| 1503 | |
| 1504 | pw.printPair("mClientProgress", mClientProgress); |
| 1505 | pw.printPair("mProgress", mProgress); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1506 | pw.printPair("mSealed", mSealed); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1507 | pw.printPair("mPermissionsManuallyAccepted", mPermissionsManuallyAccepted); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1508 | pw.printPair("mRelinquished", mRelinquished); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1509 | pw.printPair("mDestroyed", mDestroyed); |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 1510 | pw.printPair("mFds", mFds.size()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1511 | pw.printPair("mBridges", mBridges.size()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1512 | pw.printPair("mFinalStatus", mFinalStatus); |
| 1513 | pw.printPair("mFinalMessage", mFinalMessage); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1514 | pw.println(); |
| 1515 | |
| 1516 | pw.decreaseIndent(); |
| 1517 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1518 | |
| 1519 | private static void writeGrantedRuntimePermissionsLocked(XmlSerializer out, |
| 1520 | String[] grantedRuntimePermissions) throws IOException { |
| 1521 | if (grantedRuntimePermissions != null) { |
| 1522 | for (String permission : grantedRuntimePermissions) { |
| 1523 | out.startTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 1524 | writeStringAttribute(out, ATTR_NAME, permission); |
| 1525 | out.endTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | private static File buildAppIconFile(int sessionId, @NonNull File sessionsDir) { |
| 1531 | return new File(sessionsDir, "app_icon." + sessionId + ".png"); |
| 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * Write this session to a {@link XmlSerializer}. |
| 1536 | * |
| 1537 | * @param out Where to write the session to |
| 1538 | * @param sessionsDir The directory containing the sessions |
| 1539 | */ |
| 1540 | void write(@NonNull XmlSerializer out, @NonNull File sessionsDir) throws IOException { |
| 1541 | synchronized (mLock) { |
| 1542 | out.startTag(null, TAG_SESSION); |
| 1543 | |
| 1544 | writeIntAttribute(out, ATTR_SESSION_ID, sessionId); |
| 1545 | writeIntAttribute(out, ATTR_USER_ID, userId); |
| 1546 | writeStringAttribute(out, ATTR_INSTALLER_PACKAGE_NAME, |
| 1547 | mInstallerPackageName); |
| 1548 | writeIntAttribute(out, ATTR_INSTALLER_UID, mInstallerUid); |
| 1549 | writeLongAttribute(out, ATTR_CREATED_MILLIS, createdMillis); |
| 1550 | if (stageDir != null) { |
| 1551 | writeStringAttribute(out, ATTR_SESSION_STAGE_DIR, |
| 1552 | stageDir.getAbsolutePath()); |
| 1553 | } |
| 1554 | if (stageCid != null) { |
| 1555 | writeStringAttribute(out, ATTR_SESSION_STAGE_CID, stageCid); |
| 1556 | } |
| 1557 | writeBooleanAttribute(out, ATTR_PREPARED, isPrepared()); |
| 1558 | writeBooleanAttribute(out, ATTR_SEALED, isSealed()); |
| 1559 | |
| 1560 | writeIntAttribute(out, ATTR_MODE, params.mode); |
| 1561 | writeIntAttribute(out, ATTR_INSTALL_FLAGS, params.installFlags); |
| 1562 | writeIntAttribute(out, ATTR_INSTALL_LOCATION, params.installLocation); |
| 1563 | writeLongAttribute(out, ATTR_SIZE_BYTES, params.sizeBytes); |
| 1564 | writeStringAttribute(out, ATTR_APP_PACKAGE_NAME, params.appPackageName); |
| 1565 | writeStringAttribute(out, ATTR_APP_LABEL, params.appLabel); |
| 1566 | writeUriAttribute(out, ATTR_ORIGINATING_URI, params.originatingUri); |
| 1567 | writeIntAttribute(out, ATTR_ORIGINATING_UID, params.originatingUid); |
| 1568 | writeUriAttribute(out, ATTR_REFERRER_URI, params.referrerUri); |
| 1569 | writeStringAttribute(out, ATTR_ABI_OVERRIDE, params.abiOverride); |
| 1570 | writeStringAttribute(out, ATTR_VOLUME_UUID, params.volumeUuid); |
| 1571 | writeIntAttribute(out, ATTR_INSTALL_REASON, params.installReason); |
| 1572 | |
| 1573 | // Persist app icon if changed since last written |
| 1574 | File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 1575 | if (params.appIcon == null && appIconFile.exists()) { |
| 1576 | appIconFile.delete(); |
| 1577 | } else if (params.appIcon != null |
| 1578 | && appIconFile.lastModified() != params.appIconLastModified) { |
| 1579 | if (LOGD) Slog.w(TAG, "Writing changed icon " + appIconFile); |
| 1580 | FileOutputStream os = null; |
| 1581 | try { |
| 1582 | os = new FileOutputStream(appIconFile); |
| 1583 | params.appIcon.compress(Bitmap.CompressFormat.PNG, 90, os); |
| 1584 | } catch (IOException e) { |
| 1585 | Slog.w(TAG, "Failed to write icon " + appIconFile + ": " + e.getMessage()); |
| 1586 | } finally { |
| 1587 | IoUtils.closeQuietly(os); |
| 1588 | } |
| 1589 | |
| 1590 | params.appIconLastModified = appIconFile.lastModified(); |
| 1591 | } |
| 1592 | |
| 1593 | writeGrantedRuntimePermissionsLocked(out, params.grantedRuntimePermissions); |
| 1594 | } |
| 1595 | |
| 1596 | out.endTag(null, TAG_SESSION); |
| 1597 | } |
| 1598 | |
| 1599 | private static String[] readGrantedRuntimePermissions(XmlPullParser in) |
| 1600 | throws IOException, XmlPullParserException { |
| 1601 | List<String> permissions = null; |
| 1602 | |
| 1603 | final int outerDepth = in.getDepth(); |
| 1604 | int type; |
| 1605 | while ((type = in.next()) != XmlPullParser.END_DOCUMENT |
| 1606 | && (type != XmlPullParser.END_TAG || in.getDepth() > outerDepth)) { |
| 1607 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1608 | continue; |
| 1609 | } |
| 1610 | if (TAG_GRANTED_RUNTIME_PERMISSION.equals(in.getName())) { |
| 1611 | String permission = readStringAttribute(in, ATTR_NAME); |
| 1612 | if (permissions == null) { |
| 1613 | permissions = new ArrayList<>(); |
| 1614 | } |
| 1615 | permissions.add(permission); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | if (permissions == null) { |
| 1620 | return null; |
| 1621 | } |
| 1622 | |
| 1623 | String[] permissionsArray = new String[permissions.size()]; |
| 1624 | permissions.toArray(permissionsArray); |
| 1625 | return permissionsArray; |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * Read new session from a {@link XmlPullParser xml description} and create it. |
| 1630 | * |
| 1631 | * @param in The source of the description |
| 1632 | * @param callback Callback the session uses to notify about changes of it's state |
| 1633 | * @param context Context to be used by the session |
| 1634 | * @param pm PackageManager to use by the session |
| 1635 | * @param installerThread Thread to be used for callbacks of this session |
| 1636 | * @param sessionsDir The directory the sessions are stored in |
| 1637 | * |
| 1638 | * @return The newly created session |
| 1639 | */ |
| 1640 | public static PackageInstallerSession readFromXml(@NonNull XmlPullParser in, |
| 1641 | @NonNull PackageInstallerService.InternalCallback callback, @NonNull Context context, |
| 1642 | @NonNull PackageManagerService pm, Looper installerThread, @NonNull File sessionsDir) |
| 1643 | throws IOException, XmlPullParserException { |
| 1644 | final int sessionId = readIntAttribute(in, ATTR_SESSION_ID); |
| 1645 | final int userId = readIntAttribute(in, ATTR_USER_ID); |
| 1646 | final String installerPackageName = readStringAttribute(in, ATTR_INSTALLER_PACKAGE_NAME); |
| 1647 | final int installerUid = readIntAttribute(in, ATTR_INSTALLER_UID, pm.getPackageUid( |
| 1648 | installerPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, userId)); |
| 1649 | final long createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS); |
| 1650 | final String stageDirRaw = readStringAttribute(in, ATTR_SESSION_STAGE_DIR); |
| 1651 | final File stageDir = (stageDirRaw != null) ? new File(stageDirRaw) : null; |
| 1652 | final String stageCid = readStringAttribute(in, ATTR_SESSION_STAGE_CID); |
| 1653 | final boolean prepared = readBooleanAttribute(in, ATTR_PREPARED, true); |
| 1654 | final boolean sealed = readBooleanAttribute(in, ATTR_SEALED); |
| 1655 | |
| 1656 | final SessionParams params = new SessionParams( |
| 1657 | SessionParams.MODE_INVALID); |
| 1658 | params.mode = readIntAttribute(in, ATTR_MODE); |
| 1659 | params.installFlags = readIntAttribute(in, ATTR_INSTALL_FLAGS); |
| 1660 | params.installLocation = readIntAttribute(in, ATTR_INSTALL_LOCATION); |
| 1661 | params.sizeBytes = readLongAttribute(in, ATTR_SIZE_BYTES); |
| 1662 | params.appPackageName = readStringAttribute(in, ATTR_APP_PACKAGE_NAME); |
| 1663 | params.appIcon = readBitmapAttribute(in, ATTR_APP_ICON); |
| 1664 | params.appLabel = readStringAttribute(in, ATTR_APP_LABEL); |
| 1665 | params.originatingUri = readUriAttribute(in, ATTR_ORIGINATING_URI); |
| 1666 | params.originatingUid = |
| 1667 | readIntAttribute(in, ATTR_ORIGINATING_UID, SessionParams.UID_UNKNOWN); |
| 1668 | params.referrerUri = readUriAttribute(in, ATTR_REFERRER_URI); |
| 1669 | params.abiOverride = readStringAttribute(in, ATTR_ABI_OVERRIDE); |
| 1670 | params.volumeUuid = readStringAttribute(in, ATTR_VOLUME_UUID); |
| 1671 | params.grantedRuntimePermissions = readGrantedRuntimePermissions(in); |
| 1672 | params.installReason = readIntAttribute(in, ATTR_INSTALL_REASON); |
| 1673 | |
| 1674 | final File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 1675 | if (appIconFile.exists()) { |
| 1676 | params.appIcon = BitmapFactory.decodeFile(appIconFile.getAbsolutePath()); |
| 1677 | params.appIconLastModified = appIconFile.lastModified(); |
| 1678 | } |
| 1679 | |
| 1680 | return new PackageInstallerSession(callback, context, pm, |
| 1681 | installerThread, sessionId, userId, installerPackageName, installerUid, |
| 1682 | params, createdMillis, stageDir, stageCid, prepared, sealed); |
| 1683 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1684 | } |