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 | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 19 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 21 | |
Svetoslav | 805b63e | 2015-04-09 17:28:54 -0700 | [diff] [blame] | 22 | import android.Manifest; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 25 | import android.app.AppOpsManager; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 26 | import android.app.Notification; |
| 27 | import android.app.NotificationManager; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 28 | import android.app.PackageDeleteObserver; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 29 | import android.app.admin.DevicePolicyEventLogger; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManagerInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 31 | import android.content.Context; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 33 | import android.content.IntentSender; |
| 34 | import android.content.IntentSender.SendIntentException; |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 36 | import android.content.pm.IPackageInstaller; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 37 | import android.content.pm.IPackageInstallerCallback; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 38 | import android.content.pm.IPackageInstallerSession; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 39 | import android.content.pm.PackageInfo; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageInstaller.SessionInfo; |
| 42 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 44 | import android.content.pm.ParceledListSlice; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 45 | import android.content.pm.VersionedPackage; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 47 | import android.net.Uri; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 48 | import android.os.Binder; |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 49 | import android.os.Build; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 50 | import android.os.Bundle; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 51 | import android.os.Environment; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 52 | import android.os.Handler; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 53 | import android.os.HandlerThread; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 54 | import android.os.Looper; |
| 55 | import android.os.Message; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 56 | import android.os.Process; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 57 | import android.os.RemoteCallbackList; |
| 58 | import android.os.RemoteException; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 59 | import android.os.SELinux; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 60 | import android.os.UserManager; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 61 | import android.os.storage.StorageManager; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 62 | import android.stats.devicepolicy.DevicePolicyEnums; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 63 | import android.system.ErrnoException; |
| 64 | import android.system.Os; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 65 | import android.text.TextUtils; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 66 | import android.text.format.DateUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 67 | import android.util.ArraySet; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 68 | import android.util.AtomicFile; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 69 | import android.util.ExceptionUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 70 | import android.util.Slog; |
| 71 | import android.util.SparseArray; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 72 | import android.util.SparseBooleanArray; |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 73 | import android.util.SparseIntArray; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 74 | import android.util.Xml; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 75 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 76 | import com.android.internal.R; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 77 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 78 | import com.android.internal.content.PackageHelper; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 79 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 80 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 81 | import com.android.internal.util.FastXmlSerializer; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 82 | import com.android.internal.util.ImageUtils; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 83 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 84 | import com.android.server.IoThread; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 85 | import com.android.server.LocalServices; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 86 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 87 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 88 | import libcore.io.IoUtils; |
| 89 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 90 | import org.xmlpull.v1.XmlPullParser; |
| 91 | import org.xmlpull.v1.XmlPullParserException; |
| 92 | import org.xmlpull.v1.XmlSerializer; |
| 93 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 94 | import java.io.CharArrayWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 95 | import java.io.File; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 96 | import java.io.FileInputStream; |
| 97 | import java.io.FileNotFoundException; |
| 98 | import java.io.FileOutputStream; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 99 | import java.io.FilenameFilter; |
| 100 | import java.io.IOException; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 101 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 102 | import java.security.SecureRandom; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 103 | import java.util.ArrayList; |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 104 | import java.util.Collections; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 105 | import java.util.List; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 106 | import java.util.Objects; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 107 | import java.util.Random; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 108 | import java.util.function.IntPredicate; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 109 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 110 | /** The service responsible for installing packages. */ |
| 111 | public class PackageInstallerService extends IPackageInstaller.Stub implements |
| 112 | PackageSessionProvider { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 113 | private static final String TAG = "PackageInstaller"; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 114 | private static final boolean LOGD = false; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 115 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 116 | // TODO: remove outstanding sessions when installer package goes away |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 117 | // TODO: notify listeners in other users when package has been installed there |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 118 | // TODO: purge expired sessions periodically in addition to at reboot |
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 | /** XML constants used in {@link #mSessionsFile} */ |
| 121 | private static final String TAG_SESSIONS = "sessions"; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 122 | |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 123 | /** Automatically destroy sessions older than this */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 124 | private static final long MAX_AGE_MILLIS = 3 * DateUtils.DAY_IN_MILLIS; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 125 | /** Automatically destroy staged sessions that have not changed state in this time */ |
| 126 | private static final long MAX_TIME_SINCE_UPDATE_MILLIS = 7 * DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 127 | /** Upper bound on number of active sessions for a UID */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 128 | private static final long MAX_ACTIVE_SESSIONS = 1024; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 129 | /** Upper bound on number of historical sessions for a UID */ |
| 130 | private static final long MAX_HISTORICAL_SESSIONS = 1048576; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 132 | private final Context mContext; |
| 133 | private final PackageManagerService mPm; |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 134 | private final ApexManager mApexManager; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 135 | private final StagingManager mStagingManager; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 136 | private final PermissionManagerServiceInternal mPermissionManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 137 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 138 | private AppOpsManager mAppOps; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 140 | private final HandlerThread mInstallThread; |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 141 | private final Handler mInstallHandler; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 143 | private final Callbacks mCallbacks; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 144 | |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 145 | private volatile boolean mOkToSendBroadcasts = false; |
Dario Freni | 9694b80 | 2019-01-27 23:26:06 +0000 | [diff] [blame] | 146 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 147 | /** |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 148 | * File storing persisted {@link #mSessions} metadata. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 149 | */ |
| 150 | private final AtomicFile mSessionsFile; |
| 151 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Directory storing persisted {@link #mSessions} metadata which is too |
| 154 | * heavy to store directly in {@link #mSessionsFile}. |
| 155 | */ |
| 156 | private final File mSessionsDir; |
| 157 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 158 | private final InternalCallback mInternalCallback = new InternalCallback(); |
| 159 | |
| 160 | /** |
| 161 | * Used for generating session IDs. Since this is created at boot time, |
| 162 | * normal random might be predictable. |
| 163 | */ |
| 164 | private final Random mRandom = new SecureRandom(); |
| 165 | |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 166 | /** All sessions allocated */ |
| 167 | @GuardedBy("mSessions") |
| 168 | private final SparseBooleanArray mAllocatedSessions = new SparseBooleanArray(); |
| 169 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 170 | @GuardedBy("mSessions") |
| 171 | private final SparseArray<PackageInstallerSession> mSessions = new SparseArray<>(); |
| 172 | |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 173 | /** Historical sessions kept around for debugging purposes */ |
| 174 | @GuardedBy("mSessions") |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 175 | private final List<String> mHistoricalSessions = new ArrayList<>(); |
| 176 | |
| 177 | @GuardedBy("mSessions") |
| 178 | private final SparseIntArray mHistoricalSessionsByInstaller = new SparseIntArray(); |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 179 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 180 | /** Sessions allocated to legacy users */ |
| 181 | @GuardedBy("mSessions") |
| 182 | private final SparseBooleanArray mLegacySessions = new SparseBooleanArray(); |
| 183 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 184 | private static final FilenameFilter sStageFilter = new FilenameFilter() { |
| 185 | @Override |
| 186 | public boolean accept(File dir, String name) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 187 | return isStageName(name); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 191 | public PackageInstallerService(Context context, PackageManagerService pm, ApexManager am) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 192 | mContext = context; |
| 193 | mPm = pm; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 194 | mPermissionManager = LocalServices.getService(PermissionManagerServiceInternal.class); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 195 | |
| 196 | mInstallThread = new HandlerThread(TAG); |
| 197 | mInstallThread.start(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 199 | mInstallHandler = new Handler(mInstallThread.getLooper()); |
| 200 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 201 | mCallbacks = new Callbacks(mInstallThread.getLooper()); |
| 202 | |
| 203 | mSessionsFile = new AtomicFile( |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 204 | new File(Environment.getDataSystemDirectory(), "install_sessions.xml"), |
| 205 | "package-session"); |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 206 | mSessionsDir = new File(Environment.getDataSystemDirectory(), "install_sessions"); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 207 | mSessionsDir.mkdirs(); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 208 | |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 209 | mApexManager = am; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 210 | |
Nikita Ioffe | 1a7965e | 2019-03-12 22:32:41 +0000 | [diff] [blame] | 211 | mStagingManager = new StagingManager(this, am, context); |
Tony Mak | 606f8e7 | 2017-02-15 18:40:03 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 214 | boolean okToSendBroadcasts() { |
| 215 | return mOkToSendBroadcasts; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Tony Mak | 606f8e7 | 2017-02-15 18:40:03 +0000 | [diff] [blame] | 218 | public void systemReady() { |
| 219 | mAppOps = mContext.getSystemService(AppOpsManager.class); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 220 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 221 | synchronized (mSessions) { |
| 222 | readSessionsLocked(); |
| 223 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 224 | reconcileStagesLocked(StorageManager.UUID_PRIVATE_INTERNAL); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 226 | final ArraySet<File> unclaimedIcons = newArraySet( |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 227 | mSessionsDir.listFiles()); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 228 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 229 | // Ignore stages and icons claimed by active sessions |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 230 | for (int i = 0; i < mSessions.size(); i++) { |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 231 | final PackageInstallerSession session = mSessions.valueAt(i); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 232 | unclaimedIcons.remove(buildAppIconFile(session.sessionId)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 233 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 234 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 235 | // Clean up orphaned icons |
| 236 | for (File icon : unclaimedIcons) { |
| 237 | Slog.w(TAG, "Deleting orphan icon " + icon); |
| 238 | icon.delete(); |
| 239 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 240 | |
| 241 | // Invalid sessions might have been marked while parsing. Re-write the database with |
| 242 | // the updated information. |
| 243 | writeSessionsLocked(); |
| 244 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | void restoreAndApplyStagedSessionIfNeeded() { |
| 249 | List<PackageInstallerSession> stagedSessionsToRestore = new ArrayList<>(); |
| 250 | synchronized (mSessions) { |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 251 | for (int i = 0; i < mSessions.size(); i++) { |
| 252 | final PackageInstallerSession session = mSessions.valueAt(i); |
| 253 | if (session.isStaged()) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 254 | stagedSessionsToRestore.add(session); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 257 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 258 | // Don't hold mSessions lock when calling restoreSession, since it might trigger an APK |
| 259 | // atomic install which needs to query sessions, which requires lock on mSessions. |
| 260 | for (PackageInstallerSession session : stagedSessionsToRestore) { |
Gavin Corkery | 67ce22c | 2019-10-22 14:55:54 +0100 | [diff] [blame] | 261 | if (mPm.isDeviceUpgrading() && !session.isStagedAndInTerminalState()) { |
Gavin Corkery | de8174b | 2019-09-27 15:02:28 +0100 | [diff] [blame] | 262 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
| 263 | "Build fingerprint has changed"); |
| 264 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 265 | mStagingManager.restoreSession(session); |
| 266 | } |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 267 | // Broadcasts are not sent while we restore sessions on boot, since no processes would be |
| 268 | // ready to listen to them. From now on, we greedily assume that broadcasts requests are |
| 269 | // safe to send out. The worst that can happen is that a broadcast is attempted before |
| 270 | // ActivityManagerService completes its own systemReady(), in which case it will be rejected |
| 271 | // with an otherwise harmless exception. |
| 272 | // A more appropriate way to do this would be to wait until the correct boot phase is |
| 273 | // reached, but since we are not a SystemService we can't override onBootPhase. |
| 274 | // Waiting on the BOOT_COMPLETED broadcast can take several minutes, so that's not a viable |
| 275 | // way either. |
| 276 | mOkToSendBroadcasts = true; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 279 | @GuardedBy("mSessions") |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 280 | private void reconcileStagesLocked(String volumeUuid) { |
| 281 | final File stagingDir = getTmpSessionDir(volumeUuid); |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 282 | final ArraySet<File> unclaimedStages = newArraySet( |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 283 | stagingDir.listFiles(sStageFilter)); |
| 284 | |
| 285 | // Ignore stages claimed by active sessions |
| 286 | for (int i = 0; i < mSessions.size(); i++) { |
| 287 | final PackageInstallerSession session = mSessions.valueAt(i); |
| 288 | unclaimedStages.remove(session.stageDir); |
| 289 | } |
| 290 | |
| 291 | // Clean up orphaned staging directories |
| 292 | for (File stage : unclaimedStages) { |
| 293 | Slog.w(TAG, "Deleting orphan stage " + stage); |
| 294 | synchronized (mPm.mInstallLock) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 295 | mPm.removeCodePathLI(stage); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public void onPrivateVolumeMounted(String volumeUuid) { |
| 301 | synchronized (mSessions) { |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 302 | reconcileStagesLocked(volumeUuid); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 306 | public static boolean isStageName(String name) { |
| 307 | final boolean isFile = name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 308 | final boolean isContainer = name.startsWith("smdl") && name.endsWith(".tmp"); |
| 309 | final boolean isLegacyContainer = name.startsWith("smdl2tmp"); |
| 310 | return isFile || isContainer || isLegacyContainer; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 313 | @Deprecated |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 314 | public File allocateStageDirLegacy(String volumeUuid, boolean isEphemeral) throws IOException { |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 315 | synchronized (mSessions) { |
| 316 | try { |
| 317 | final int sessionId = allocateSessionIdLocked(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 318 | mLegacySessions.put(sessionId, true); |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 319 | final File sessionStageDir = buildTmpSessionDir(sessionId, volumeUuid); |
| 320 | prepareStageDir(sessionStageDir); |
| 321 | return sessionStageDir; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 322 | } catch (IllegalStateException e) { |
| 323 | throw new IOException(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 328 | @Deprecated |
| 329 | public String allocateExternalStageCidLegacy() { |
| 330 | synchronized (mSessions) { |
| 331 | final int sessionId = allocateSessionIdLocked(); |
| 332 | mLegacySessions.put(sessionId, true); |
| 333 | return "smdl" + sessionId + ".tmp"; |
| 334 | } |
| 335 | } |
| 336 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 337 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 338 | private void readSessionsLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 339 | if (LOGD) Slog.v(TAG, "readSessionsLocked()"); |
| 340 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 341 | mSessions.clear(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 342 | |
| 343 | FileInputStream fis = null; |
| 344 | try { |
| 345 | fis = mSessionsFile.openRead(); |
| 346 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 347 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 348 | |
| 349 | int type; |
| 350 | while ((type = in.next()) != END_DOCUMENT) { |
| 351 | if (type == START_TAG) { |
| 352 | final String tag = in.getName(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 353 | if (PackageInstallerSession.TAG_SESSION.equals(tag)) { |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 354 | final PackageInstallerSession session; |
| 355 | try { |
| 356 | session = PackageInstallerSession.readFromXml(in, mInternalCallback, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 357 | mContext, mPm, mInstallThread.getLooper(), mStagingManager, |
| 358 | mSessionsDir, this); |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 359 | } catch (Exception e) { |
| 360 | Slog.e(TAG, "Could not read session", e); |
| 361 | continue; |
| 362 | } |
| 363 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 364 | final long age = System.currentTimeMillis() - session.createdMillis; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 365 | final long timeSinceUpdate = |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 366 | System.currentTimeMillis() - session.getUpdatedMillis(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 367 | final boolean valid; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 368 | if (session.isStaged()) { |
Dario Freni | 7f5a695 | 2019-05-10 19:40:50 +0100 | [diff] [blame] | 369 | if (timeSinceUpdate >= MAX_TIME_SINCE_UPDATE_MILLIS |
| 370 | && session.isStagedAndInTerminalState()) { |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 371 | valid = false; |
| 372 | } else { |
| 373 | valid = true; |
| 374 | } |
| 375 | } else if (age >= MAX_AGE_MILLIS) { |
| 376 | Slog.w(TAG, "Abandoning old session created at " |
| 377 | + session.createdMillis); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 378 | valid = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 379 | } else { |
| 380 | valid = true; |
| 381 | } |
| 382 | |
| 383 | if (valid) { |
| 384 | mSessions.put(session.sessionId, session); |
| 385 | } else { |
| 386 | // Since this is early during boot we don't send |
| 387 | // any observer events about the session, but we |
| 388 | // keep details around for dumpsys. |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 389 | addHistoricalSessionLocked(session); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 390 | } |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 391 | mAllocatedSessions.put(session.sessionId, true); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | } catch (FileNotFoundException e) { |
| 396 | // Missing sessions are okay, probably first boot |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 397 | } catch (IOException | XmlPullParserException e) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 398 | Slog.wtf(TAG, "Failed reading install sessions", e); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 399 | } finally { |
| 400 | IoUtils.closeQuietly(fis); |
| 401 | } |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 402 | // After all of the sessions were loaded, they are ready to be sealed and validated |
| 403 | for (int i = 0; i < mSessions.size(); ++i) { |
| 404 | PackageInstallerSession session = mSessions.valueAt(i); |
| 405 | session.sealAndValidateIfNecessary(); |
| 406 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 409 | @GuardedBy("mSessions") |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 410 | private void addHistoricalSessionLocked(PackageInstallerSession session) { |
| 411 | CharArrayWriter writer = new CharArrayWriter(); |
| 412 | IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
| 413 | session.dump(pw); |
| 414 | mHistoricalSessions.add(writer.toString()); |
| 415 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 416 | int installerUid = session.getInstallerUid(); |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 417 | // Increment the number of sessions by this installerUid. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 418 | mHistoricalSessionsByInstaller.put(installerUid, |
| 419 | mHistoricalSessionsByInstaller.get(installerUid) + 1); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 422 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 423 | private void writeSessionsLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 424 | if (LOGD) Slog.v(TAG, "writeSessionsLocked()"); |
| 425 | |
| 426 | FileOutputStream fos = null; |
| 427 | try { |
| 428 | fos = mSessionsFile.startWrite(); |
| 429 | |
| 430 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 431 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 432 | out.startDocument(null, true); |
| 433 | out.startTag(null, TAG_SESSIONS); |
| 434 | final int size = mSessions.size(); |
| 435 | for (int i = 0; i < size; i++) { |
| 436 | final PackageInstallerSession session = mSessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 437 | session.write(out, mSessionsDir); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 438 | } |
| 439 | out.endTag(null, TAG_SESSIONS); |
| 440 | out.endDocument(); |
| 441 | |
| 442 | mSessionsFile.finishWrite(fos); |
| 443 | } catch (IOException e) { |
| 444 | if (fos != null) { |
| 445 | mSessionsFile.failWrite(fos); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 450 | private File buildAppIconFile(int sessionId) { |
| 451 | return new File(mSessionsDir, "app_icon." + sessionId + ".png"); |
| 452 | } |
| 453 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 454 | private void writeSessionsAsync() { |
| 455 | IoThread.getHandler().post(new Runnable() { |
| 456 | @Override |
| 457 | public void run() { |
| 458 | synchronized (mSessions) { |
| 459 | writeSessionsLocked(); |
| 460 | } |
| 461 | } |
| 462 | }); |
| 463 | } |
| 464 | |
| 465 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 466 | public int createSession(SessionParams params, String installerPackageName, int userId) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 467 | try { |
| 468 | return createSessionInternal(params, installerPackageName, userId); |
| 469 | } catch (IOException e) { |
| 470 | throw ExceptionUtils.wrap(e); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | private int createSessionInternal(SessionParams params, String installerPackageName, int userId) |
| 475 | throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 476 | final int callingUid = Binder.getCallingUid(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 477 | mPermissionManager.enforceCrossUserPermission( |
| 478 | callingUid, userId, true, true, "createSession"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 479 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 480 | if (mPm.isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 481 | throw new SecurityException("User restriction prevents installing"); |
| 482 | } |
| 483 | |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 484 | String requestedInstallerPackageName = params.installerPackageName != null |
| 485 | ? params.installerPackageName : installerPackageName; |
| 486 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 487 | if ((callingUid == Process.SHELL_UID) || (callingUid == Process.ROOT_UID)) { |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 488 | params.installFlags |= PackageManager.INSTALL_FROM_ADB; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 489 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 490 | } else { |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 491 | if (callingUid != Process.SYSTEM_UID) { |
| 492 | // The supplied installerPackageName must always belong to the calling app. |
| 493 | mAppOps.checkPackage(callingUid, installerPackageName); |
| 494 | } |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 495 | // Only apps with INSTALL_PACKAGES are allowed to set an installer that is not the |
| 496 | // caller. |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 497 | if (!requestedInstallerPackageName.equals(installerPackageName)) { |
| 498 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES) |
| 499 | != PackageManager.PERMISSION_GRANTED) { |
| 500 | mAppOps.checkPackage(callingUid, requestedInstallerPackageName); |
| 501 | } |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 502 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 503 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 504 | params.installFlags &= ~PackageManager.INSTALL_FROM_ADB; |
| 505 | params.installFlags &= ~PackageManager.INSTALL_ALL_USERS; |
Todd Kennedy | 0151916 | 2019-09-20 13:45:15 -0700 | [diff] [blame] | 506 | params.installFlags &= ~PackageManager.INSTALL_ALLOW_TEST; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 507 | params.installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; |
Todd Kennedy | 78a7250 | 2017-07-19 12:49:30 -0700 | [diff] [blame] | 508 | if ((params.installFlags & PackageManager.INSTALL_VIRTUAL_PRELOAD) != 0 |
| 509 | && !mPm.isCallerVerifier(callingUid)) { |
| 510 | params.installFlags &= ~PackageManager.INSTALL_VIRTUAL_PRELOAD; |
| 511 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 514 | String originatingPackageName = null; |
| 515 | if (params.originatingUid != SessionParams.UID_UNKNOWN |
| 516 | && params.originatingUid != callingUid) { |
| 517 | String[] packages = mPm.getPackagesForUid(params.originatingUid); |
| 518 | if (packages != null && packages.length > 0) { |
| 519 | // Choose an arbitrary representative package in the case of a shared UID. |
| 520 | originatingPackageName = packages[0]; |
| 521 | } |
| 522 | } |
| 523 | |
Nikita Ioffe | 1962cb1 | 2019-03-27 22:59:28 +0000 | [diff] [blame] | 524 | if (Build.IS_DEBUGGABLE || isDowngradeAllowedForCaller(callingUid)) { |
Nikita Ioffe | b1d60f1 | 2019-03-06 18:56:49 +0000 | [diff] [blame] | 525 | params.installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE; |
Nikita Ioffe | a1111ff | 2019-03-04 12:26:14 +0000 | [diff] [blame] | 526 | } else { |
Nikita Ioffe | b1d60f1 | 2019-03-06 18:56:49 +0000 | [diff] [blame] | 527 | params.installFlags &= ~PackageManager.INSTALL_ALLOW_DOWNGRADE; |
Nikita Ioffe | 1962cb1 | 2019-03-27 22:59:28 +0000 | [diff] [blame] | 528 | params.installFlags &= ~PackageManager.INSTALL_REQUEST_DOWNGRADE; |
Nikita Ioffe | a1111ff | 2019-03-04 12:26:14 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Nikita Ioffe | a954349 | 2019-07-30 17:10:27 +0100 | [diff] [blame] | 531 | if (callingUid != Process.SYSTEM_UID) { |
| 532 | // Only system_server can use INSTALL_DISABLE_VERIFICATION. |
| 533 | params.installFlags &= ~PackageManager.INSTALL_DISABLE_VERIFICATION; |
| 534 | } |
| 535 | |
Nikita Ioffe | 4501c11 | 2019-02-22 11:51:09 +0000 | [diff] [blame] | 536 | boolean isApex = (params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 537 | if (params.isStaged || isApex) { |
Dario Freni | 77786d9 | 2019-02-08 17:26:05 +0000 | [diff] [blame] | 538 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, TAG); |
| 539 | } |
| 540 | |
Nikita Ioffe | 4501c11 | 2019-02-22 11:51:09 +0000 | [diff] [blame] | 541 | if (isApex) { |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 542 | if (!mApexManager.isApexSupported()) { |
| 543 | throw new IllegalArgumentException( |
| 544 | "This device doesn't support the installation of APEX files"); |
| 545 | } |
| 546 | if (!params.isStaged) { |
| 547 | throw new IllegalArgumentException( |
| 548 | "APEX files can only be installed as part of a staged session."); |
| 549 | } |
| 550 | } |
| 551 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 552 | if (!params.isMultiPackage) { |
| 553 | // Only system components can circumvent runtime permissions when installing. |
| 554 | if ((params.installFlags & PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0 |
| 555 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 556 | .INSTALL_GRANT_RUNTIME_PERMISSIONS) == PackageManager.PERMISSION_DENIED) { |
| 557 | throw new SecurityException("You need the " |
| 558 | + "android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS permission " |
| 559 | + "to use the PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS flag"); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 562 | // Defensively resize giant app icons |
| 563 | if (params.appIcon != null) { |
| 564 | final ActivityManager am = (ActivityManager) mContext.getSystemService( |
| 565 | Context.ACTIVITY_SERVICE); |
| 566 | final int iconSize = am.getLauncherLargeIconSize(); |
| 567 | if ((params.appIcon.getWidth() > iconSize * 2) |
| 568 | || (params.appIcon.getHeight() > iconSize * 2)) { |
| 569 | params.appIcon = Bitmap.createScaledBitmap(params.appIcon, iconSize, iconSize, |
| 570 | true); |
| 571 | } |
| 572 | } |
Jeff Sharkey | ab23409 | 2015-06-09 21:42:22 -0700 | [diff] [blame] | 573 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 574 | switch (params.mode) { |
| 575 | case SessionParams.MODE_FULL_INSTALL: |
| 576 | case SessionParams.MODE_INHERIT_EXISTING: |
| 577 | break; |
| 578 | default: |
| 579 | throw new IllegalArgumentException("Invalid install mode: " + params.mode); |
| 580 | } |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 581 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 582 | // If caller requested explicit location, sanity check it, otherwise |
| 583 | // resolve the best internal or adopted location. |
| 584 | if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 585 | if (!PackageHelper.fitsOnInternal(mContext, params)) { |
| 586 | throw new IOException("No suitable internal storage available"); |
| 587 | } |
Patrick Baumann | ff10c9c | 2018-12-11 15:17:38 -0800 | [diff] [blame] | 588 | } else if ((params.installFlags & PackageManager.INSTALL_FORCE_VOLUME_UUID) != 0) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 589 | // For now, installs to adopted media are treated as internal from |
| 590 | // an install flag point-of-view. |
Patrick Baumann | fc2851e | 2018-11-13 15:23:22 -0800 | [diff] [blame] | 591 | params.installFlags |= PackageManager.INSTALL_INTERNAL; |
Patrick Baumann | ff10c9c | 2018-12-11 15:17:38 -0800 | [diff] [blame] | 592 | } else { |
| 593 | params.installFlags |= PackageManager.INSTALL_INTERNAL; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 594 | |
| 595 | // Resolve best location for install, based on combination of |
| 596 | // requested install flags, delta size, and manifest settings. |
| 597 | final long ident = Binder.clearCallingIdentity(); |
| 598 | try { |
| 599 | params.volumeUuid = PackageHelper.resolveInstallVolume(mContext, params); |
| 600 | } finally { |
| 601 | Binder.restoreCallingIdentity(ident); |
| 602 | } |
Robin Lee | e812d90 | 2014-08-21 16:51:18 +0100 | [diff] [blame] | 603 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | final int sessionId; |
| 607 | final PackageInstallerSession session; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 608 | synchronized (mSessions) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 609 | // Sanity check that installer isn't going crazy |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 610 | final int activeCount = getSessionCount(mSessions, callingUid); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 611 | if (activeCount >= MAX_ACTIVE_SESSIONS) { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 612 | throw new IllegalStateException( |
| 613 | "Too many active sessions for UID " + callingUid); |
| 614 | } |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 615 | final int historicalCount = mHistoricalSessionsByInstaller.get(callingUid); |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 616 | if (historicalCount >= MAX_HISTORICAL_SESSIONS) { |
| 617 | throw new IllegalStateException( |
| 618 | "Too many historical sessions for UID " + callingUid); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 621 | sessionId = allocateSessionIdLocked(); |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 622 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 623 | |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 624 | final long createdMillis = System.currentTimeMillis(); |
| 625 | // We're staging to exactly one location |
| 626 | File stageDir = null; |
| 627 | String stageCid = null; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 628 | if (!params.isMultiPackage) { |
| 629 | if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) { |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 630 | stageDir = buildSessionDir(sessionId, params); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 631 | } else { |
| 632 | stageCid = buildExternalStageCid(sessionId); |
| 633 | } |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 634 | } |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 635 | InstallSource installSource = InstallSource.create(installerPackageName, |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 636 | originatingPackageName, requestedInstallerPackageName, false); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 637 | session = new PackageInstallerSession(mInternalCallback, mContext, mPm, this, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 638 | mInstallThread.getLooper(), mStagingManager, sessionId, userId, callingUid, |
| 639 | installSource, params, createdMillis, |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 640 | stageDir, stageCid, false, false, false, null, SessionInfo.INVALID_ID, |
| 641 | false, false, false, SessionInfo.STAGED_SESSION_NO_ERROR, ""); |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 642 | |
| 643 | synchronized (mSessions) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 644 | mSessions.put(sessionId, session); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 645 | } |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 646 | if (params.isStaged) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 647 | mStagingManager.createSession(session); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 648 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 649 | |
Richard Uhler | 1c9c5d2 | 2019-06-05 13:04:32 +0100 | [diff] [blame] | 650 | if ((session.params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
| 651 | mCallbacks.notifySessionCreated(session.sessionId, session.userId); |
| 652 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 653 | writeSessionsAsync(); |
| 654 | return sessionId; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Nikita Ioffe | 1962cb1 | 2019-03-27 22:59:28 +0000 | [diff] [blame] | 657 | private boolean isDowngradeAllowedForCaller(int callingUid) { |
| 658 | return callingUid == Process.SYSTEM_UID || callingUid == Process.ROOT_UID |
| 659 | || callingUid == Process.SHELL_UID; |
| 660 | } |
| 661 | |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 662 | @Override |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 663 | public void updateSessionAppIcon(int sessionId, Bitmap appIcon) { |
| 664 | synchronized (mSessions) { |
| 665 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 666 | if (session == null || !isCallingUidOwner(session)) { |
| 667 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 668 | } |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 669 | |
| 670 | // Defensively resize giant app icons |
| 671 | if (appIcon != null) { |
| 672 | final ActivityManager am = (ActivityManager) mContext.getSystemService( |
| 673 | Context.ACTIVITY_SERVICE); |
| 674 | final int iconSize = am.getLauncherLargeIconSize(); |
| 675 | if ((appIcon.getWidth() > iconSize * 2) |
| 676 | || (appIcon.getHeight() > iconSize * 2)) { |
| 677 | appIcon = Bitmap.createScaledBitmap(appIcon, iconSize, iconSize, true); |
| 678 | } |
| 679 | } |
| 680 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 681 | session.params.appIcon = appIcon; |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 682 | session.params.appIconLastModified = -1; |
| 683 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 684 | mInternalCallback.onSessionBadgingChanged(session); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | @Override |
| 689 | public void updateSessionAppLabel(int sessionId, String appLabel) { |
| 690 | synchronized (mSessions) { |
| 691 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 692 | if (session == null || !isCallingUidOwner(session)) { |
| 693 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 694 | } |
| 695 | session.params.appLabel = appLabel; |
| 696 | mInternalCallback.onSessionBadgingChanged(session); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | @Override |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 701 | public void abandonSession(int sessionId) { |
| 702 | synchronized (mSessions) { |
| 703 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 704 | if (session == null || !isCallingUidOwner(session)) { |
| 705 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 706 | } |
| 707 | session.abandon(); |
| 708 | } |
| 709 | } |
| 710 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 711 | @Override |
| 712 | public IPackageInstallerSession openSession(int sessionId) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 713 | try { |
| 714 | return openSessionInternal(sessionId); |
| 715 | } catch (IOException e) { |
| 716 | throw ExceptionUtils.wrap(e); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | private IPackageInstallerSession openSessionInternal(int sessionId) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 721 | synchronized (mSessions) { |
| 722 | final PackageInstallerSession session = mSessions.get(sessionId); |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 723 | if (session == null || !isCallingUidOwner(session)) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 724 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 725 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 726 | session.open(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 727 | return session; |
| 728 | } |
| 729 | } |
| 730 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 731 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 732 | private int allocateSessionIdLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 733 | int n = 0; |
| 734 | int sessionId; |
| 735 | do { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 736 | sessionId = mRandom.nextInt(Integer.MAX_VALUE - 1) + 1; |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 737 | if (!mAllocatedSessions.get(sessionId, false)) { |
| 738 | mAllocatedSessions.put(sessionId, true); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 739 | return sessionId; |
| 740 | } |
| 741 | } while (n++ < 32); |
| 742 | |
| 743 | throw new IllegalStateException("Failed to allocate session ID"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 746 | private File getTmpSessionDir(String volumeUuid) { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 747 | return Environment.getDataAppDirectory(volumeUuid); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 748 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 749 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 750 | private File buildTmpSessionDir(int sessionId, String volumeUuid) { |
| 751 | final File sessionStagingDir = getTmpSessionDir(volumeUuid); |
| 752 | return new File(sessionStagingDir, "vmdl" + sessionId + ".tmp"); |
| 753 | } |
| 754 | |
| 755 | private File buildSessionDir(int sessionId, SessionParams params) { |
| 756 | if (params.isStaged) { |
| 757 | final File sessionStagingDir = Environment.getDataStagingDirectory(params.volumeUuid); |
| 758 | return new File(sessionStagingDir, "session_" + sessionId); |
| 759 | } |
| 760 | return buildTmpSessionDir(sessionId, params.volumeUuid); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static void prepareStageDir(File stageDir) throws IOException { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 764 | if (stageDir.exists()) { |
| 765 | throw new IOException("Session dir already exists: " + stageDir); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | try { |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 769 | Os.mkdir(stageDir.getAbsolutePath(), 0775); |
| 770 | Os.chmod(stageDir.getAbsolutePath(), 0775); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 771 | } catch (ErrnoException e) { |
| 772 | // This purposefully throws if directory already exists |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 773 | throw new IOException("Failed to prepare session dir: " + stageDir, e); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 776 | if (!SELinux.restorecon(stageDir)) { |
| 777 | throw new IOException("Failed to restorecon session dir: " + stageDir); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 778 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 781 | private String buildExternalStageCid(int sessionId) { |
| 782 | return "smdl" + sessionId + ".tmp"; |
| 783 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 784 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 785 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 786 | public SessionInfo getSessionInfo(int sessionId) { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 787 | synchronized (mSessions) { |
| 788 | final PackageInstallerSession session = mSessions.get(sessionId); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 789 | return session != null ? session.generateInfo() : null; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | @Override |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 794 | public ParceledListSlice<SessionInfo> getStagedSessions() { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 795 | return mStagingManager.getSessions(); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | @Override |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 799 | public ParceledListSlice<SessionInfo> getAllSessions(int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 800 | mPermissionManager.enforceCrossUserPermission( |
| 801 | Binder.getCallingUid(), userId, true, false, "getAllSessions"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 802 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 803 | final List<SessionInfo> result = new ArrayList<>(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 804 | synchronized (mSessions) { |
| 805 | for (int i = 0; i < mSessions.size(); i++) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 806 | final PackageInstallerSession session = mSessions.valueAt(i); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 807 | if (session.userId == userId && !session.hasParentSessionId()) { |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 808 | result.add(session.generateInfo(false)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 812 | return new ParceledListSlice<>(result); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | @Override |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 816 | public ParceledListSlice<SessionInfo> getMySessions(String installerPackageName, int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 817 | mPermissionManager.enforceCrossUserPermission( |
| 818 | Binder.getCallingUid(), userId, true, false, "getMySessions"); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 819 | mAppOps.checkPackage(Binder.getCallingUid(), installerPackageName); |
| 820 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 821 | final List<SessionInfo> result = new ArrayList<>(); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 822 | synchronized (mSessions) { |
| 823 | for (int i = 0; i < mSessions.size(); i++) { |
| 824 | final PackageInstallerSession session = mSessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 825 | |
| 826 | SessionInfo info = session.generateInfo(false); |
| 827 | if (Objects.equals(info.getInstallerPackageName(), installerPackageName) |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 828 | && session.userId == userId && !session.hasParentSessionId()) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 829 | result.add(info); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | } |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 833 | return new ParceledListSlice<>(result); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | @Override |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 837 | public void uninstall(VersionedPackage versionedPackage, String callerPackageName, int flags, |
Patrick Baumann | 466ddb5 | 2019-04-11 12:43:05 -0700 | [diff] [blame] | 838 | IntentSender statusReceiver, int userId) { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 839 | final int callingUid = Binder.getCallingUid(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 840 | mPermissionManager.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall"); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 841 | if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) { |
| 842 | mAppOps.checkPackage(callingUid, callerPackageName); |
| 843 | } |
| 844 | |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 845 | // Check whether the caller is device owner or affiliated profile owner, in which case we do |
| 846 | // it silently. |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 847 | DevicePolicyManagerInternal dpmi = |
| 848 | LocalServices.getService(DevicePolicyManagerInternal.class); |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 849 | final boolean canSilentlyInstallPackage = |
| 850 | dpmi != null && dpmi.canSilentlyInstallPackage(callerPackageName, callingUid); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 851 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 852 | final PackageDeleteObserverAdapter adapter = new PackageDeleteObserverAdapter(mContext, |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 853 | statusReceiver, versionedPackage.getPackageName(), |
Rubin Xu | cb92415 | 2019-03-28 12:11:17 +0000 | [diff] [blame] | 854 | canSilentlyInstallPackage, userId); |
Sudheer Shanka | 72de4dd | 2016-07-22 15:46:37 -0700 | [diff] [blame] | 855 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DELETE_PACKAGES) |
| 856 | == PackageManager.PERMISSION_GRANTED) { |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 857 | // Sweet, call straight through! |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 858 | mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags); |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 859 | } else if (canSilentlyInstallPackage) { |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 860 | // Allow the device owner and affiliated profile owner to silently delete packages |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 861 | // Need to clear the calling identity to get DELETE_PACKAGES permission |
| 862 | long ident = Binder.clearCallingIdentity(); |
| 863 | try { |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 864 | mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 865 | } finally { |
| 866 | Binder.restoreCallingIdentity(ident); |
| 867 | } |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 868 | DevicePolicyEventLogger |
| 869 | .createEvent(DevicePolicyEnums.UNINSTALL_PACKAGE) |
| 870 | .setAdmin(callerPackageName) |
| 871 | .write(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 872 | } else { |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 873 | ApplicationInfo appInfo = mPm.getApplicationInfo(callerPackageName, 0, userId); |
| 874 | if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 875 | mContext.enforceCallingOrSelfPermission(Manifest.permission.REQUEST_DELETE_PACKAGES, |
| 876 | null); |
| 877 | } |
| 878 | |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 879 | // Take a short detour to confirm with user |
| 880 | final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 881 | intent.setData(Uri.fromParts("package", versionedPackage.getPackageName(), null)); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 882 | intent.putExtra(PackageInstaller.EXTRA_CALLBACK, adapter.getBinder().asBinder()); |
| 883 | adapter.onUserActionRequired(intent); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 884 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | @Override |
Chandan Nath | e8e463b | 2019-01-28 15:23:38 +0000 | [diff] [blame] | 888 | public void installExistingPackage(String packageName, int installFlags, int installReason, |
Philip P. Moltmann | a4bd150 | 2019-05-13 17:10:46 -0700 | [diff] [blame] | 889 | IntentSender statusReceiver, int userId, List<String> whiteListedPermissions) { |
Chandan Nath | e8e463b | 2019-01-28 15:23:38 +0000 | [diff] [blame] | 890 | mPm.installExistingPackageAsUser(packageName, userId, installFlags, installReason, |
Philip P. Moltmann | a4bd150 | 2019-05-13 17:10:46 -0700 | [diff] [blame] | 891 | whiteListedPermissions, statusReceiver); |
Chandan Nath | e8e463b | 2019-01-28 15:23:38 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | @Override |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 895 | public void setPermissionsResult(int sessionId, boolean accepted) { |
| 896 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, TAG); |
| 897 | |
| 898 | synchronized (mSessions) { |
Svet Ganov | 3baa876 | 2016-04-08 09:22:54 -0700 | [diff] [blame] | 899 | PackageInstallerSession session = mSessions.get(sessionId); |
| 900 | if (session != null) { |
| 901 | session.setPermissionsResult(accepted); |
| 902 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 907 | public void registerCallback(IPackageInstallerCallback callback, int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 908 | mPermissionManager.enforceCrossUserPermission( |
| 909 | Binder.getCallingUid(), userId, true, false, "registerCallback"); |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 910 | registerCallback(callback, eventUserId -> userId == eventUserId); |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Assume permissions already checked and caller's identity cleared |
| 915 | */ |
| 916 | public void registerCallback(IPackageInstallerCallback callback, IntPredicate userCheck) { |
| 917 | mCallbacks.register(callback, userCheck); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 921 | public void unregisterCallback(IPackageInstallerCallback callback) { |
| 922 | mCallbacks.unregister(callback); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 925 | @Override |
| 926 | public PackageInstallerSession getSession(int sessionId) { |
| 927 | synchronized (mSessions) { |
| 928 | return mSessions.get(sessionId); |
| 929 | } |
| 930 | } |
| 931 | |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 932 | private static int getSessionCount(SparseArray<PackageInstallerSession> sessions, |
| 933 | int installerUid) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 934 | int count = 0; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 935 | final int size = sessions.size(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 936 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 937 | final PackageInstallerSession session = sessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 938 | if (session.getInstallerUid() == installerUid) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 939 | count++; |
| 940 | } |
| 941 | } |
| 942 | return count; |
| 943 | } |
| 944 | |
| 945 | private boolean isCallingUidOwner(PackageInstallerSession session) { |
| 946 | final int callingUid = Binder.getCallingUid(); |
| 947 | if (callingUid == Process.ROOT_UID) { |
| 948 | return true; |
| 949 | } else { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 950 | return (session != null) && (callingUid == session.getInstallerUid()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 954 | static class PackageDeleteObserverAdapter extends PackageDeleteObserver { |
| 955 | private final Context mContext; |
| 956 | private final IntentSender mTarget; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 957 | private final String mPackageName; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 958 | private final Notification mNotification; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 959 | |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 960 | public PackageDeleteObserverAdapter(Context context, IntentSender target, |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 961 | String packageName, boolean showNotification, int userId) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 962 | mContext = context; |
| 963 | mTarget = target; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 964 | mPackageName = packageName; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 965 | if (showNotification) { |
| 966 | mNotification = buildSuccessNotification(mContext, |
| 967 | mContext.getResources().getString(R.string.package_deleted_device_owner), |
| 968 | packageName, |
| 969 | userId); |
| 970 | } else { |
| 971 | mNotification = null; |
| 972 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public void onUserActionRequired(Intent intent) { |
Patrick Baumann | 466ddb5 | 2019-04-11 12:43:05 -0700 | [diff] [blame] | 977 | if (mTarget == null) { |
| 978 | return; |
| 979 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 980 | final Intent fillIn = new Intent(); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 981 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, mPackageName); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 982 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 983 | PackageInstaller.STATUS_PENDING_USER_ACTION); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 984 | fillIn.putExtra(Intent.EXTRA_INTENT, intent); |
| 985 | try { |
| 986 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 987 | } catch (SendIntentException ignored) { |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | @Override |
| 992 | public void onPackageDeleted(String basePackageName, int returnCode, String msg) { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 993 | if (PackageManager.DELETE_SUCCEEDED == returnCode && mNotification != null) { |
| 994 | NotificationManager notificationManager = (NotificationManager) |
| 995 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 996 | notificationManager.notify(basePackageName, |
| 997 | SystemMessage.NOTE_PACKAGE_STATE, |
| 998 | mNotification); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 999 | } |
Patrick Baumann | 466ddb5 | 2019-04-11 12:43:05 -0700 | [diff] [blame] | 1000 | if (mTarget == null) { |
| 1001 | return; |
| 1002 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1003 | final Intent fillIn = new Intent(); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1004 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, mPackageName); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1005 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 1006 | PackageManager.deleteStatusToPublicStatus(returnCode)); |
| 1007 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
| 1008 | PackageManager.deleteStatusToString(returnCode, msg)); |
| 1009 | fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode); |
| 1010 | try { |
| 1011 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 1012 | } catch (SendIntentException ignored) { |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1017 | static void sendOnUserActionRequired(Context context, IntentSender target, int sessionId, |
| 1018 | Intent intent) { |
| 1019 | final Intent fillIn = new Intent(); |
| 1020 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 1021 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 1022 | PackageInstaller.STATUS_PENDING_USER_ACTION); |
| 1023 | fillIn.putExtra(Intent.EXTRA_INTENT, intent); |
| 1024 | try { |
| 1025 | target.sendIntent(context, 0, fillIn, null, null); |
| 1026 | } catch (SendIntentException ignored) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1027 | } |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1028 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1029 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1030 | static void sendOnPackageInstalled(Context context, IntentSender target, int sessionId, |
| 1031 | boolean showNotification, int userId, String basePackageName, int returnCode, |
| 1032 | String msg, Bundle extras) { |
| 1033 | if (PackageManager.INSTALL_SUCCEEDED == returnCode && showNotification) { |
| 1034 | boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING); |
| 1035 | Notification notification = buildSuccessNotification(context, |
| 1036 | context.getResources() |
| 1037 | .getString(update ? R.string.package_updated_device_owner : |
| 1038 | R.string.package_installed_device_owner), |
| 1039 | basePackageName, |
| 1040 | userId); |
| 1041 | if (notification != null) { |
| 1042 | NotificationManager notificationManager = (NotificationManager) |
| 1043 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 1044 | notificationManager.notify(basePackageName, |
| 1045 | SystemMessage.NOTE_PACKAGE_STATE, |
| 1046 | notification); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1049 | final Intent fillIn = new Intent(); |
| 1050 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName); |
| 1051 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 1052 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 1053 | PackageManager.installStatusToPublicStatus(returnCode)); |
| 1054 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
| 1055 | PackageManager.installStatusToString(returnCode, msg)); |
| 1056 | fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode); |
| 1057 | if (extras != null) { |
| 1058 | final String existing = extras.getString( |
| 1059 | PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE); |
| 1060 | if (!TextUtils.isEmpty(existing)) { |
| 1061 | fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1062 | } |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1063 | } |
| 1064 | try { |
| 1065 | target.sendIntent(context, 0, fillIn, null, null); |
| 1066 | } catch (SendIntentException ignored) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1070 | /** |
| 1071 | * Build a notification for package installation / deletion by device owners that is shown if |
| 1072 | * the operation succeeds. |
| 1073 | */ |
| 1074 | private static Notification buildSuccessNotification(Context context, String contentText, |
| 1075 | String basePackageName, int userId) { |
| 1076 | PackageInfo packageInfo = null; |
| 1077 | try { |
| 1078 | packageInfo = AppGlobals.getPackageManager().getPackageInfo( |
Svet Ganov | a5c867c | 2017-05-15 01:17:05 -0700 | [diff] [blame] | 1079 | basePackageName, PackageManager.MATCH_STATIC_SHARED_LIBRARIES, userId); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1080 | } catch (RemoteException ignored) { |
| 1081 | } |
| 1082 | if (packageInfo == null || packageInfo.applicationInfo == null) { |
| 1083 | Slog.w(TAG, "Notification not built for package: " + basePackageName); |
| 1084 | return null; |
| 1085 | } |
| 1086 | PackageManager pm = context.getPackageManager(); |
| 1087 | Bitmap packageIcon = ImageUtils.buildScaledBitmap( |
| 1088 | packageInfo.applicationInfo.loadIcon(pm), |
| 1089 | context.getResources().getDimensionPixelSize( |
| 1090 | android.R.dimen.notification_large_icon_width), |
| 1091 | context.getResources().getDimensionPixelSize( |
| 1092 | android.R.dimen.notification_large_icon_height)); |
| 1093 | CharSequence packageLabel = packageInfo.applicationInfo.loadLabel(pm); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1094 | return new Notification.Builder(context, SystemNotificationChannels.DEVICE_ADMIN) |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1095 | .setSmallIcon(R.drawable.ic_check_circle_24px) |
| 1096 | .setColor(context.getResources().getColor( |
| 1097 | R.color.system_notification_accent_color)) |
| 1098 | .setContentTitle(packageLabel) |
| 1099 | .setContentText(contentText) |
Benjamin Franz | 2e3e943 | 2015-04-17 15:28:17 +0100 | [diff] [blame] | 1100 | .setStyle(new Notification.BigTextStyle().bigText(contentText)) |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1101 | .setLargeIcon(packageIcon) |
| 1102 | .build(); |
| 1103 | } |
| 1104 | |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 1105 | public static <E> ArraySet<E> newArraySet(E... elements) { |
| 1106 | final ArraySet<E> set = new ArraySet<E>(); |
| 1107 | if (elements != null) { |
| 1108 | set.ensureCapacity(elements.length); |
| 1109 | Collections.addAll(set, elements); |
| 1110 | } |
| 1111 | return set; |
| 1112 | } |
| 1113 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1114 | private static class Callbacks extends Handler { |
| 1115 | private static final int MSG_SESSION_CREATED = 1; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1116 | private static final int MSG_SESSION_BADGING_CHANGED = 2; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1117 | private static final int MSG_SESSION_ACTIVE_CHANGED = 3; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1118 | private static final int MSG_SESSION_PROGRESS_CHANGED = 4; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1119 | private static final int MSG_SESSION_FINISHED = 5; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1120 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1121 | private final RemoteCallbackList<IPackageInstallerCallback> |
| 1122 | mCallbacks = new RemoteCallbackList<>(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1123 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1124 | public Callbacks(Looper looper) { |
| 1125 | super(looper); |
| 1126 | } |
| 1127 | |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1128 | public void register(IPackageInstallerCallback callback, IntPredicate userCheck) { |
| 1129 | mCallbacks.register(callback, userCheck); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | public void unregister(IPackageInstallerCallback callback) { |
| 1133 | mCallbacks.unregister(callback); |
| 1134 | } |
| 1135 | |
| 1136 | @Override |
| 1137 | public void handleMessage(Message msg) { |
| 1138 | final int userId = msg.arg2; |
| 1139 | final int n = mCallbacks.beginBroadcast(); |
| 1140 | for (int i = 0; i < n; i++) { |
| 1141 | final IPackageInstallerCallback callback = mCallbacks.getBroadcastItem(i); |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1142 | final IntPredicate userCheck = (IntPredicate) mCallbacks.getBroadcastCookie(i); |
| 1143 | if (userCheck.test(userId)) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1144 | try { |
| 1145 | invokeCallback(callback, msg); |
| 1146 | } catch (RemoteException ignored) { |
| 1147 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1150 | mCallbacks.finishBroadcast(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1151 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1152 | |
| 1153 | private void invokeCallback(IPackageInstallerCallback callback, Message msg) |
| 1154 | throws RemoteException { |
| 1155 | final int sessionId = msg.arg1; |
| 1156 | switch (msg.what) { |
| 1157 | case MSG_SESSION_CREATED: |
| 1158 | callback.onSessionCreated(sessionId); |
| 1159 | break; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1160 | case MSG_SESSION_BADGING_CHANGED: |
| 1161 | callback.onSessionBadgingChanged(sessionId); |
| 1162 | break; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1163 | case MSG_SESSION_ACTIVE_CHANGED: |
| 1164 | callback.onSessionActiveChanged(sessionId, (boolean) msg.obj); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1165 | break; |
| 1166 | case MSG_SESSION_PROGRESS_CHANGED: |
| 1167 | callback.onSessionProgressChanged(sessionId, (float) msg.obj); |
| 1168 | break; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1169 | case MSG_SESSION_FINISHED: |
| 1170 | callback.onSessionFinished(sessionId, (boolean) msg.obj); |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | private void notifySessionCreated(int sessionId, int userId) { |
| 1176 | obtainMessage(MSG_SESSION_CREATED, sessionId, userId).sendToTarget(); |
| 1177 | } |
| 1178 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1179 | private void notifySessionBadgingChanged(int sessionId, int userId) { |
| 1180 | obtainMessage(MSG_SESSION_BADGING_CHANGED, sessionId, userId).sendToTarget(); |
| 1181 | } |
| 1182 | |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1183 | private void notifySessionActiveChanged(int sessionId, int userId, boolean active) { |
| 1184 | obtainMessage(MSG_SESSION_ACTIVE_CHANGED, sessionId, userId, active).sendToTarget(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | private void notifySessionProgressChanged(int sessionId, int userId, float progress) { |
| 1188 | obtainMessage(MSG_SESSION_PROGRESS_CHANGED, sessionId, userId, progress).sendToTarget(); |
| 1189 | } |
| 1190 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1191 | public void notifySessionFinished(int sessionId, int userId, boolean success) { |
| 1192 | obtainMessage(MSG_SESSION_FINISHED, sessionId, userId, success).sendToTarget(); |
| 1193 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1197 | synchronized (mSessions) { |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1198 | pw.println("Active install sessions:"); |
| 1199 | pw.increaseIndent(); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 1200 | |
| 1201 | List<PackageInstallerSession> finalizedSessions = new ArrayList<>(); |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1202 | int N = mSessions.size(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1203 | for (int i = 0; i < N; i++) { |
| 1204 | final PackageInstallerSession session = mSessions.valueAt(i); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 1205 | if (session.isStagedAndInTerminalState()) { |
| 1206 | finalizedSessions.add(session); |
| 1207 | continue; |
| 1208 | } |
| 1209 | session.dump(pw); |
| 1210 | pw.println(); |
| 1211 | } |
| 1212 | pw.println(); |
| 1213 | pw.decreaseIndent(); |
| 1214 | |
| 1215 | pw.println("Finalized install sessions:"); |
| 1216 | pw.increaseIndent(); |
| 1217 | N = finalizedSessions.size(); |
| 1218 | for (int i = 0; i < N; i++) { |
| 1219 | final PackageInstallerSession session = finalizedSessions.get(i); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1220 | session.dump(pw); |
| 1221 | pw.println(); |
| 1222 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1223 | pw.println(); |
| 1224 | pw.decreaseIndent(); |
| 1225 | |
| 1226 | pw.println("Historical install sessions:"); |
| 1227 | pw.increaseIndent(); |
| 1228 | N = mHistoricalSessions.size(); |
| 1229 | for (int i = 0; i < N; i++) { |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 1230 | pw.print(mHistoricalSessions.get(i)); |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1231 | pw.println(); |
| 1232 | } |
| 1233 | pw.println(); |
| 1234 | pw.decreaseIndent(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1235 | |
| 1236 | pw.println("Legacy install sessions:"); |
| 1237 | pw.increaseIndent(); |
| 1238 | pw.println(mLegacySessions.toString()); |
| 1239 | pw.decreaseIndent(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1240 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1243 | class InternalCallback { |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1244 | public void onSessionBadgingChanged(PackageInstallerSession session) { |
Richard Uhler | 1c9c5d2 | 2019-06-05 13:04:32 +0100 | [diff] [blame] | 1245 | if ((session.params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
| 1246 | mCallbacks.notifySessionBadgingChanged(session.sessionId, session.userId); |
| 1247 | } |
| 1248 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1249 | writeSessionsAsync(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1252 | public void onSessionActiveChanged(PackageInstallerSession session, boolean active) { |
Richard Uhler | 1c9c5d2 | 2019-06-05 13:04:32 +0100 | [diff] [blame] | 1253 | if ((session.params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
| 1254 | mCallbacks.notifySessionActiveChanged(session.sessionId, session.userId, |
| 1255 | active); |
| 1256 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1259 | public void onSessionProgressChanged(PackageInstallerSession session, float progress) { |
Richard Uhler | 1c9c5d2 | 2019-06-05 13:04:32 +0100 | [diff] [blame] | 1260 | if ((session.params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
| 1261 | mCallbacks.notifySessionProgressChanged(session.sessionId, session.userId, |
| 1262 | progress); |
| 1263 | } |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 1266 | public void onStagedSessionChanged(PackageInstallerSession session) { |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 1267 | session.markUpdated(); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 1268 | writeSessionsAsync(); |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 1269 | if (mOkToSendBroadcasts) { |
Dario Freni | 9694b80 | 2019-01-27 23:26:06 +0000 | [diff] [blame] | 1270 | mPm.sendSessionUpdatedBroadcast(session.generateInfo(false), |
| 1271 | session.userId); |
| 1272 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1275 | public void onSessionFinished(final PackageInstallerSession session, boolean success) { |
Richard Uhler | 1c9c5d2 | 2019-06-05 13:04:32 +0100 | [diff] [blame] | 1276 | if ((session.params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
| 1277 | mCallbacks.notifySessionFinished(session.sessionId, session.userId, success); |
| 1278 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1279 | |
| 1280 | mInstallHandler.post(new Runnable() { |
| 1281 | @Override |
| 1282 | public void run() { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1283 | if (session.isStaged()) { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1284 | if (!success) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1285 | mStagingManager.abortSession(session); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1286 | } |
| 1287 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1288 | synchronized (mSessions) { |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 1289 | if (!session.isStaged() || !success) { |
| 1290 | mSessions.remove(session.sessionId); |
| 1291 | } |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 1292 | addHistoricalSessionLocked(session); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 1293 | |
| 1294 | final File appIconFile = buildAppIconFile(session.sessionId); |
| 1295 | if (appIconFile.exists()) { |
| 1296 | appIconFile.delete(); |
| 1297 | } |
| 1298 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1299 | writeSessionsLocked(); |
| 1300 | } |
| 1301 | } |
| 1302 | }); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1303 | } |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1304 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1305 | public void onSessionPrepared(PackageInstallerSession session) { |
| 1306 | // We prepared the destination to write into; we want to persist |
| 1307 | // this, but it's not critical enough to block for. |
| 1308 | writeSessionsAsync(); |
| 1309 | } |
| 1310 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1311 | public void onSessionSealedBlocking(PackageInstallerSession session) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1312 | // It's very important that we block until we've recorded the |
| 1313 | // session as being sealed, since we never want to allow mutation |
| 1314 | // after sealing. |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1315 | synchronized (mSessions) { |
| 1316 | writeSessionsLocked(); |
| 1317 | } |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1318 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |