Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.permission; |
| 18 | |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 19 | import static android.Manifest.permission.ACCESS_BACKGROUND_LOCATION; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.READ_EXTERNAL_STORAGE; |
| 21 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 22 | import static android.app.AppOpsManager.MODE_ALLOWED; |
| 23 | import static android.app.AppOpsManager.MODE_DEFAULT; |
| 24 | import static android.app.AppOpsManager.MODE_ERRORED; |
| 25 | import static android.app.AppOpsManager.MODE_FOREGROUND; |
| 26 | import static android.app.AppOpsManager.MODE_IGNORED; |
| 27 | import static android.app.AppOpsManager.OP_NONE; |
| 28 | import static android.app.AppOpsManager.permissionToOp; |
| 29 | import static android.app.AppOpsManager.permissionToOpCode; |
| 30 | import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; |
| 31 | import static android.content.pm.PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
| 32 | import static android.content.pm.PackageManager.FLAG_PERMISSION_POLICY_FIXED; |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 33 | import static android.content.pm.PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED; |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 34 | import static android.content.pm.PackageManager.FLAG_PERMISSION_REVOKE_WHEN_REQUESTED; |
| 35 | import static android.content.pm.PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 36 | import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_FIXED; |
| 37 | import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 38 | import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER; |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 39 | import static android.os.UserHandle.getAppId; |
| 40 | import static android.os.UserHandle.getUid; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 41 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 42 | import static com.android.server.pm.PackageManagerService.DEBUG_INSTALL; |
| 43 | import static com.android.server.pm.PackageManagerService.DEBUG_PACKAGE_SCANNING; |
| 44 | import static com.android.server.pm.PackageManagerService.DEBUG_PERMISSIONS; |
| 45 | import static com.android.server.pm.PackageManagerService.DEBUG_REMOVE; |
| 46 | import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 47 | |
| 48 | import android.Manifest; |
| 49 | import android.annotation.NonNull; |
| 50 | import android.annotation.Nullable; |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 51 | import android.annotation.UserIdInt; |
| 52 | import android.app.AppOpsManager; |
| 53 | import android.app.AppOpsManagerInternal; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 54 | import android.content.Context; |
| 55 | import android.content.pm.PackageManager; |
| 56 | import android.content.pm.PackageManagerInternal; |
| 57 | import android.content.pm.PackageParser; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageParser.Package; |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 59 | import android.content.pm.PermissionGroupInfo; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 60 | import android.content.pm.PermissionInfo; |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 61 | import android.metrics.LogMaker; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 62 | import android.os.Binder; |
| 63 | import android.os.Build; |
| 64 | import android.os.Handler; |
| 65 | import android.os.HandlerThread; |
| 66 | import android.os.Process; |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 67 | import android.os.Trace; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 68 | import android.os.UserHandle; |
| 69 | import android.os.UserManager; |
| 70 | import android.os.UserManagerInternal; |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 71 | import android.os.storage.StorageManager; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 72 | import android.os.storage.StorageManagerInternal; |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 73 | import android.permission.PermissionManager; |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 74 | import android.text.TextUtils; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 75 | import android.util.ArrayMap; |
| 76 | import android.util.ArraySet; |
Philip P. Moltmann | fae8a528 | 2018-04-10 12:15:32 -0700 | [diff] [blame] | 77 | import android.util.EventLog; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 78 | import android.util.Log; |
| 79 | import android.util.Slog; |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 80 | import android.util.SparseArray; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 81 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 82 | import com.android.internal.annotations.GuardedBy; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 83 | import com.android.internal.logging.MetricsLogger; |
| 84 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 85 | import com.android.internal.os.RoSystemProperties; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 86 | import com.android.internal.util.ArrayUtils; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 87 | import com.android.server.LocalServices; |
| 88 | import com.android.server.ServiceThread; |
| 89 | import com.android.server.SystemConfig; |
| 90 | import com.android.server.Watchdog; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 91 | import com.android.server.pm.PackageManagerServiceUtils; |
| 92 | import com.android.server.pm.PackageSetting; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 93 | import com.android.server.pm.SharedUserSetting; |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 94 | import com.android.server.pm.UserManagerService; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 95 | import com.android.server.pm.permission.DefaultPermissionGrantPolicy |
| 96 | .DefaultPermissionGrantedCallback; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 97 | import com.android.server.pm.permission.PermissionManagerInternal.PermissionCallback; |
| 98 | import com.android.server.pm.permission.PermissionsState.PermissionState; |
| 99 | |
| 100 | import libcore.util.EmptyArray; |
| 101 | |
| 102 | import java.util.ArrayList; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 103 | import java.util.Collection; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 104 | import java.util.HashMap; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 105 | import java.util.Iterator; |
| 106 | import java.util.List; |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 107 | import java.util.Map; |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 108 | import java.util.Objects; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 109 | import java.util.Set; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Manages all permissions and handles permissions related tasks. |
| 113 | */ |
| 114 | public class PermissionManagerService { |
| 115 | private static final String TAG = "PackageManager"; |
| 116 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 117 | /** Permission grant: not grant the permission. */ |
| 118 | private static final int GRANT_DENIED = 1; |
| 119 | /** Permission grant: grant the permission as an install permission. */ |
| 120 | private static final int GRANT_INSTALL = 2; |
| 121 | /** Permission grant: grant the permission as a runtime one. */ |
| 122 | private static final int GRANT_RUNTIME = 3; |
| 123 | /** Permission grant: grant as runtime a permission that was granted as an install time one. */ |
| 124 | private static final int GRANT_UPGRADE = 4; |
| 125 | |
| 126 | /** Cap the size of permission trees that 3rd party apps can define; in characters of text */ |
| 127 | private static final int MAX_PERMISSION_TREE_FOOTPRINT = 32768; |
| 128 | /** Empty array to avoid allocations */ |
| 129 | private static final int[] EMPTY_INT_ARRAY = new int[0]; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 130 | |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 131 | /** If the permission of the value is granted, so is the key */ |
| 132 | private static final Map<String, String> FULLER_PERMISSION_MAP = new HashMap<>(); |
| 133 | |
| 134 | static { |
| 135 | FULLER_PERMISSION_MAP.put(Manifest.permission.ACCESS_COARSE_LOCATION, |
| 136 | Manifest.permission.ACCESS_FINE_LOCATION); |
| 137 | FULLER_PERMISSION_MAP.put(Manifest.permission.INTERACT_ACROSS_USERS, |
| 138 | Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 139 | } |
| 140 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 141 | /** Lock to protect internal data access */ |
| 142 | private final Object mLock; |
| 143 | |
| 144 | /** Internal connection to the package manager */ |
| 145 | private final PackageManagerInternal mPackageManagerInt; |
| 146 | |
| 147 | /** Internal connection to the user manager */ |
| 148 | private final UserManagerInternal mUserManagerInt; |
| 149 | |
| 150 | /** Default permission policy to provide proper behaviour out-of-the-box */ |
| 151 | private final DefaultPermissionGrantPolicy mDefaultPermissionGrantPolicy; |
| 152 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Built-in permissions. Read from system configuration files. Mapping is from |
| 155 | * UID to permission name. |
| 156 | */ |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 157 | private final SparseArray<ArraySet<String>> mSystemPermissions; |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 158 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 159 | /** Built-in group IDs given to all packages. Read from system configuration files. */ |
| 160 | private final int[] mGlobalGids; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 161 | |
| 162 | private final HandlerThread mHandlerThread; |
| 163 | private final Handler mHandler; |
| 164 | private final Context mContext; |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 165 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 166 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 167 | /** Internal storage for permissions and related settings */ |
| 168 | @GuardedBy("mLock") |
| 169 | private final PermissionSettings mSettings; |
| 170 | |
| 171 | @GuardedBy("mLock") |
| 172 | private ArraySet<String> mPrivappPermissionsViolations; |
| 173 | |
| 174 | @GuardedBy("mLock") |
| 175 | private boolean mSystemReady; |
| 176 | |
Philip P. Moltmann | e1b277a | 2018-11-01 16:22:50 -0700 | [diff] [blame] | 177 | /** |
| 178 | * For each foreground/background permission the mapping: |
| 179 | * Background permission -> foreground permissions |
| 180 | */ |
| 181 | @GuardedBy("mLock") |
| 182 | private ArrayMap<String, List<String>> mBackgroundPermissions; |
| 183 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 184 | PermissionManagerService(Context context, |
| 185 | @Nullable DefaultPermissionGrantedCallback defaultGrantCallback, |
| 186 | @NonNull Object externalLock) { |
| 187 | mContext = context; |
| 188 | mLock = externalLock; |
| 189 | mPackageManagerInt = LocalServices.getService(PackageManagerInternal.class); |
| 190 | mUserManagerInt = LocalServices.getService(UserManagerInternal.class); |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 191 | mSettings = new PermissionSettings(mLock); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 192 | |
| 193 | mHandlerThread = new ServiceThread(TAG, |
| 194 | Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/); |
| 195 | mHandlerThread.start(); |
| 196 | mHandler = new Handler(mHandlerThread.getLooper()); |
| 197 | Watchdog.getInstance().addThread(mHandler); |
| 198 | |
| 199 | mDefaultPermissionGrantPolicy = new DefaultPermissionGrantPolicy( |
| 200 | context, mHandlerThread.getLooper(), defaultGrantCallback, this); |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 201 | SystemConfig systemConfig = SystemConfig.getInstance(); |
| 202 | mSystemPermissions = systemConfig.getSystemPermissions(); |
| 203 | mGlobalGids = systemConfig.getGlobalGids(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 204 | |
| 205 | // propagate permission configuration |
| 206 | final ArrayMap<String, SystemConfig.PermissionEntry> permConfig = |
| 207 | SystemConfig.getInstance().getPermissions(); |
| 208 | synchronized (mLock) { |
| 209 | for (int i=0; i<permConfig.size(); i++) { |
| 210 | final SystemConfig.PermissionEntry perm = permConfig.valueAt(i); |
| 211 | BasePermission bp = mSettings.getPermissionLocked(perm.name); |
| 212 | if (bp == null) { |
| 213 | bp = new BasePermission(perm.name, "android", BasePermission.TYPE_BUILTIN); |
| 214 | mSettings.putPermissionLocked(perm.name, bp); |
| 215 | } |
| 216 | if (perm.gids != null) { |
| 217 | bp.setGids(perm.gids, perm.perUser); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | LocalServices.addService( |
| 223 | PermissionManagerInternal.class, new PermissionManagerInternalImpl()); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Creates and returns an initialized, internal service for use by other components. |
| 228 | * <p> |
| 229 | * The object returned is identical to the one returned by the LocalServices class using: |
| 230 | * {@code LocalServices.getService(PermissionManagerInternal.class);} |
| 231 | * <p> |
| 232 | * NOTE: The external lock is temporary and should be removed. This needs to be a |
| 233 | * lock created by the permission manager itself. |
| 234 | */ |
| 235 | public static PermissionManagerInternal create(Context context, |
| 236 | @Nullable DefaultPermissionGrantedCallback defaultGrantCallback, |
| 237 | @NonNull Object externalLock) { |
| 238 | final PermissionManagerInternal permMgrInt = |
| 239 | LocalServices.getService(PermissionManagerInternal.class); |
| 240 | if (permMgrInt != null) { |
| 241 | return permMgrInt; |
| 242 | } |
| 243 | new PermissionManagerService(context, defaultGrantCallback, externalLock); |
| 244 | return LocalServices.getService(PermissionManagerInternal.class); |
| 245 | } |
| 246 | |
| 247 | @Nullable BasePermission getPermission(String permName) { |
| 248 | synchronized (mLock) { |
| 249 | return mSettings.getPermissionLocked(permName); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | private int checkPermission(String permName, String pkgName, int callingUid, int userId) { |
| 254 | if (!mUserManagerInt.exists(userId)) { |
| 255 | return PackageManager.PERMISSION_DENIED; |
| 256 | } |
| 257 | |
Patrick Baumann | 97b9b53 | 2018-04-11 14:51:30 +0000 | [diff] [blame] | 258 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(pkgName); |
| 259 | if (pkg != null && pkg.mExtras != null) { |
| 260 | if (mPackageManagerInt.filterAppAccess(pkg, callingUid, userId)) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 261 | return PackageManager.PERMISSION_DENIED; |
| 262 | } |
Patrick Baumann | 97b9b53 | 2018-04-11 14:51:30 +0000 | [diff] [blame] | 263 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 264 | final boolean instantApp = ps.getInstantApp(userId); |
| 265 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 266 | if (permissionsState.hasPermission(permName, userId)) { |
| 267 | if (instantApp) { |
| 268 | synchronized (mLock) { |
| 269 | BasePermission bp = mSettings.getPermissionLocked(permName); |
| 270 | if (bp != null && bp.isInstant()) { |
| 271 | return PackageManager.PERMISSION_GRANTED; |
| 272 | } |
| 273 | } |
| 274 | } else { |
| 275 | return PackageManager.PERMISSION_GRANTED; |
| 276 | } |
| 277 | } |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 278 | if (isImpliedPermissionGranted(permissionsState, permName, userId)) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 279 | return PackageManager.PERMISSION_GRANTED; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return PackageManager.PERMISSION_DENIED; |
| 284 | } |
| 285 | |
Todd Kennedy | 3c71449 | 2017-10-27 09:12:50 -0700 | [diff] [blame] | 286 | private int checkUidPermission(String permName, PackageParser.Package pkg, int uid, |
| 287 | int callingUid) { |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 288 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 289 | final boolean isCallerInstantApp = |
| 290 | mPackageManagerInt.getInstantAppPackageName(callingUid) != null; |
| 291 | final boolean isUidInstantApp = |
| 292 | mPackageManagerInt.getInstantAppPackageName(uid) != null; |
| 293 | final int userId = UserHandle.getUserId(uid); |
| 294 | if (!mUserManagerInt.exists(userId)) { |
| 295 | return PackageManager.PERMISSION_DENIED; |
| 296 | } |
| 297 | |
Todd Kennedy | 3c71449 | 2017-10-27 09:12:50 -0700 | [diff] [blame] | 298 | if (pkg != null) { |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 299 | if (pkg.mSharedUserId != null) { |
| 300 | if (isCallerInstantApp) { |
| 301 | return PackageManager.PERMISSION_DENIED; |
| 302 | } |
Todd Kennedy | 3c71449 | 2017-10-27 09:12:50 -0700 | [diff] [blame] | 303 | } else if (mPackageManagerInt.filterAppAccess(pkg, callingUid, callingUserId)) { |
| 304 | return PackageManager.PERMISSION_DENIED; |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 305 | } |
| 306 | final PermissionsState permissionsState = |
| 307 | ((PackageSetting) pkg.mExtras).getPermissionsState(); |
| 308 | if (permissionsState.hasPermission(permName, userId)) { |
| 309 | if (isUidInstantApp) { |
| 310 | if (mSettings.isPermissionInstant(permName)) { |
| 311 | return PackageManager.PERMISSION_GRANTED; |
| 312 | } |
| 313 | } else { |
| 314 | return PackageManager.PERMISSION_GRANTED; |
| 315 | } |
| 316 | } |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 317 | if (isImpliedPermissionGranted(permissionsState, permName, userId)) { |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 318 | return PackageManager.PERMISSION_GRANTED; |
| 319 | } |
| 320 | } else { |
| 321 | ArraySet<String> perms = mSystemPermissions.get(uid); |
| 322 | if (perms != null) { |
| 323 | if (perms.contains(permName)) { |
| 324 | return PackageManager.PERMISSION_GRANTED; |
| 325 | } |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 326 | if (FULLER_PERMISSION_MAP.containsKey(permName) |
| 327 | && perms.contains(FULLER_PERMISSION_MAP.get(permName))) { |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 328 | return PackageManager.PERMISSION_GRANTED; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | return PackageManager.PERMISSION_DENIED; |
| 333 | } |
| 334 | |
Hongwei Wang | f391b55 | 2018-04-06 13:52:46 -0700 | [diff] [blame] | 335 | /** |
| 336 | * Returns {@code true} if the permission can be implied from another granted permission. |
| 337 | * <p>Some permissions, such as ACCESS_FINE_LOCATION, imply other permissions, |
| 338 | * such as ACCESS_COURSE_LOCATION. If the caller holds an umbrella permission, give |
| 339 | * it access to any implied permissions. |
| 340 | */ |
| 341 | private static boolean isImpliedPermissionGranted(PermissionsState permissionsState, |
| 342 | String permName, int userId) { |
| 343 | return FULLER_PERMISSION_MAP.containsKey(permName) |
| 344 | && permissionsState.hasPermission(FULLER_PERMISSION_MAP.get(permName), userId); |
| 345 | } |
| 346 | |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 347 | private PermissionGroupInfo getPermissionGroupInfo(String groupName, int flags, |
| 348 | int callingUid) { |
| 349 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 350 | return null; |
| 351 | } |
| 352 | synchronized (mLock) { |
| 353 | return PackageParser.generatePermissionGroupInfo( |
| 354 | mSettings.mPermissionGroups.get(groupName), flags); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | private List<PermissionGroupInfo> getAllPermissionGroups(int flags, int callingUid) { |
| 359 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 360 | return null; |
| 361 | } |
| 362 | synchronized (mLock) { |
| 363 | final int N = mSettings.mPermissionGroups.size(); |
| 364 | final ArrayList<PermissionGroupInfo> out |
| 365 | = new ArrayList<PermissionGroupInfo>(N); |
| 366 | for (PackageParser.PermissionGroup pg : mSettings.mPermissionGroups.values()) { |
| 367 | out.add(PackageParser.generatePermissionGroupInfo(pg, flags)); |
| 368 | } |
| 369 | return out; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | private PermissionInfo getPermissionInfo(String permName, String packageName, int flags, |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 374 | int callingUid) { |
| 375 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 376 | return null; |
| 377 | } |
| 378 | // reader |
| 379 | synchronized (mLock) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 380 | final BasePermission bp = mSettings.getPermissionLocked(permName); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 381 | if (bp == null) { |
| 382 | return null; |
| 383 | } |
| 384 | final int adjustedProtectionLevel = adjustPermissionProtectionFlagsLocked( |
| 385 | bp.getProtectionLevel(), packageName, callingUid); |
| 386 | return bp.generatePermissionInfo(adjustedProtectionLevel, flags); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private List<PermissionInfo> getPermissionInfoByGroup( |
| 391 | String groupName, int flags, int callingUid) { |
| 392 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 393 | return null; |
| 394 | } |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 395 | synchronized (mLock) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 396 | if (groupName != null && !mSettings.mPermissionGroups.containsKey(groupName)) { |
| 397 | return null; |
| 398 | } |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 399 | final ArrayList<PermissionInfo> out = new ArrayList<PermissionInfo>(10); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 400 | for (BasePermission bp : mSettings.mPermissions.values()) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 401 | final PermissionInfo pi = bp.generatePermissionInfo(groupName, flags); |
| 402 | if (pi != null) { |
| 403 | out.add(pi); |
| 404 | } |
| 405 | } |
| 406 | return out; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private int adjustPermissionProtectionFlagsLocked( |
| 411 | int protectionLevel, String packageName, int uid) { |
| 412 | // Signature permission flags area always reported |
| 413 | final int protectionLevelMasked = protectionLevel |
| 414 | & (PermissionInfo.PROTECTION_NORMAL |
| 415 | | PermissionInfo.PROTECTION_DANGEROUS |
| 416 | | PermissionInfo.PROTECTION_SIGNATURE); |
| 417 | if (protectionLevelMasked == PermissionInfo.PROTECTION_SIGNATURE) { |
| 418 | return protectionLevel; |
| 419 | } |
| 420 | // System sees all flags. |
| 421 | final int appId = UserHandle.getAppId(uid); |
| 422 | if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID |
| 423 | || appId == Process.SHELL_UID) { |
| 424 | return protectionLevel; |
| 425 | } |
| 426 | // Normalize package name to handle renamed packages and static libs |
| 427 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(packageName); |
| 428 | if (pkg == null) { |
| 429 | return protectionLevel; |
| 430 | } |
| 431 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.O) { |
| 432 | return protectionLevelMasked; |
| 433 | } |
| 434 | // Apps that target O see flags for all protection levels. |
| 435 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 436 | if (ps == null) { |
| 437 | return protectionLevel; |
| 438 | } |
| 439 | if (ps.getAppId() != appId) { |
| 440 | return protectionLevel; |
| 441 | } |
| 442 | return protectionLevel; |
| 443 | } |
| 444 | |
Philip P. Moltmann | fae8a528 | 2018-04-10 12:15:32 -0700 | [diff] [blame] | 445 | /** |
| 446 | * We might auto-grant permissions if any permission of the group is already granted. Hence if |
| 447 | * the group of a granted permission changes we need to revoke it to avoid having permissions of |
| 448 | * the new group auto-granted. |
| 449 | * |
| 450 | * @param newPackage The new package that was installed |
| 451 | * @param oldPackage The old package that was updated |
| 452 | * @param allPackageNames All package names |
| 453 | * @param permissionCallback Callback for permission changed |
| 454 | */ |
| 455 | private void revokeRuntimePermissionsIfGroupChanged( |
| 456 | @NonNull PackageParser.Package newPackage, |
| 457 | @NonNull PackageParser.Package oldPackage, |
| 458 | @NonNull ArrayList<String> allPackageNames, |
| 459 | @NonNull PermissionCallback permissionCallback) { |
| 460 | final int numOldPackagePermissions = oldPackage.permissions.size(); |
| 461 | final ArrayMap<String, String> oldPermissionNameToGroupName |
| 462 | = new ArrayMap<>(numOldPackagePermissions); |
| 463 | |
| 464 | for (int i = 0; i < numOldPackagePermissions; i++) { |
| 465 | final PackageParser.Permission permission = oldPackage.permissions.get(i); |
| 466 | |
| 467 | if (permission.group != null) { |
| 468 | oldPermissionNameToGroupName.put(permission.info.name, |
| 469 | permission.group.info.name); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | final int numNewPackagePermissions = newPackage.permissions.size(); |
| 474 | for (int newPermissionNum = 0; newPermissionNum < numNewPackagePermissions; |
| 475 | newPermissionNum++) { |
| 476 | final PackageParser.Permission newPermission = |
| 477 | newPackage.permissions.get(newPermissionNum); |
| 478 | final int newProtection = newPermission.info.getProtection(); |
| 479 | |
| 480 | if ((newProtection & PermissionInfo.PROTECTION_DANGEROUS) != 0) { |
| 481 | final String permissionName = newPermission.info.name; |
| 482 | final String newPermissionGroupName = |
| 483 | newPermission.group == null ? null : newPermission.group.info.name; |
| 484 | final String oldPermissionGroupName = oldPermissionNameToGroupName.get( |
| 485 | permissionName); |
| 486 | |
| 487 | if (newPermissionGroupName != null |
| 488 | && !newPermissionGroupName.equals(oldPermissionGroupName)) { |
| 489 | final int[] userIds = mUserManagerInt.getUserIds(); |
| 490 | final int numUserIds = userIds.length; |
| 491 | for (int userIdNum = 0; userIdNum < numUserIds; userIdNum++) { |
| 492 | final int userId = userIds[userIdNum]; |
| 493 | |
| 494 | final int numPackages = allPackageNames.size(); |
| 495 | for (int packageNum = 0; packageNum < numPackages; packageNum++) { |
| 496 | final String packageName = allPackageNames.get(packageNum); |
| 497 | |
| 498 | if (checkPermission(permissionName, packageName, UserHandle.USER_SYSTEM, |
| 499 | userId) == PackageManager.PERMISSION_GRANTED) { |
| 500 | EventLog.writeEvent(0x534e4554, "72710897", |
| 501 | newPackage.applicationInfo.uid, |
Koji Fukui | acae3ef | 2018-05-09 11:38:01 +0900 | [diff] [blame] | 502 | "Revoking permission " + permissionName + |
| 503 | " from package " + packageName + |
| 504 | " as the group changed from " + oldPermissionGroupName + |
| 505 | " to " + newPermissionGroupName); |
Philip P. Moltmann | fae8a528 | 2018-04-10 12:15:32 -0700 | [diff] [blame] | 506 | |
| 507 | try { |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 508 | revokeRuntimePermission(permissionName, packageName, false, |
| 509 | Process.SYSTEM_UID, userId, permissionCallback); |
Philip P. Moltmann | fae8a528 | 2018-04-10 12:15:32 -0700 | [diff] [blame] | 510 | } catch (IllegalArgumentException e) { |
| 511 | Slog.e(TAG, "Could not revoke " + permissionName + " from " |
| 512 | + packageName, e); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 522 | private void addAllPermissions(PackageParser.Package pkg, boolean chatty) { |
| 523 | final int N = pkg.permissions.size(); |
| 524 | for (int i=0; i<N; i++) { |
| 525 | PackageParser.Permission p = pkg.permissions.get(i); |
| 526 | |
| 527 | // Assume by default that we did not install this permission into the system. |
| 528 | p.info.flags &= ~PermissionInfo.FLAG_INSTALLED; |
| 529 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 530 | synchronized (PermissionManagerService.this.mLock) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 531 | // Now that permission groups have a special meaning, we ignore permission |
| 532 | // groups for legacy apps to prevent unexpected behavior. In particular, |
| 533 | // permissions for one app being granted to someone just because they happen |
| 534 | // to be in a group defined by another app (before this had no implications). |
| 535 | if (pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 536 | p.group = mSettings.mPermissionGroups.get(p.info.group); |
| 537 | // Warn for a permission in an unknown group. |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 538 | if (DEBUG_PERMISSIONS |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 539 | && p.info.group != null && p.group == null) { |
| 540 | Slog.i(TAG, "Permission " + p.info.name + " from package " |
| 541 | + p.info.packageName + " in an unknown group " + p.info.group); |
| 542 | } |
| 543 | } |
| 544 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 545 | if (p.tree) { |
| 546 | final BasePermission bp = BasePermission.createOrUpdate( |
| 547 | mSettings.getPermissionTreeLocked(p.info.name), p, pkg, |
| 548 | mSettings.getAllPermissionTreesLocked(), chatty); |
| 549 | mSettings.putPermissionTreeLocked(p.info.name, bp); |
| 550 | } else { |
| 551 | final BasePermission bp = BasePermission.createOrUpdate( |
| 552 | mSettings.getPermissionLocked(p.info.name), |
| 553 | p, pkg, mSettings.getAllPermissionTreesLocked(), chatty); |
| 554 | mSettings.putPermissionLocked(p.info.name, bp); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 560 | private void addAllPermissionGroups(PackageParser.Package pkg, boolean chatty) { |
| 561 | final int N = pkg.permissionGroups.size(); |
| 562 | StringBuilder r = null; |
| 563 | for (int i=0; i<N; i++) { |
| 564 | final PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i); |
| 565 | final PackageParser.PermissionGroup cur = mSettings.mPermissionGroups.get(pg.info.name); |
| 566 | final String curPackageName = (cur == null) ? null : cur.info.packageName; |
| 567 | final boolean isPackageUpdate = pg.info.packageName.equals(curPackageName); |
| 568 | if (cur == null || isPackageUpdate) { |
| 569 | mSettings.mPermissionGroups.put(pg.info.name, pg); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 570 | if (chatty && DEBUG_PACKAGE_SCANNING) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 571 | if (r == null) { |
| 572 | r = new StringBuilder(256); |
| 573 | } else { |
| 574 | r.append(' '); |
| 575 | } |
| 576 | if (isPackageUpdate) { |
| 577 | r.append("UPD:"); |
| 578 | } |
| 579 | r.append(pg.info.name); |
| 580 | } |
| 581 | } else { |
| 582 | Slog.w(TAG, "Permission group " + pg.info.name + " from package " |
| 583 | + pg.info.packageName + " ignored: original from " |
| 584 | + cur.info.packageName); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 585 | if (chatty && DEBUG_PACKAGE_SCANNING) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 586 | if (r == null) { |
| 587 | r = new StringBuilder(256); |
| 588 | } else { |
| 589 | r.append(' '); |
| 590 | } |
| 591 | r.append("DUP:"); |
| 592 | r.append(pg.info.name); |
| 593 | } |
| 594 | } |
| 595 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 596 | if (r != null && DEBUG_PACKAGE_SCANNING) { |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 597 | Log.d(TAG, " Permission Groups: " + r); |
| 598 | } |
| 599 | |
| 600 | } |
| 601 | |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 602 | private void removeAllPermissions(PackageParser.Package pkg, boolean chatty) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 603 | synchronized (mLock) { |
| 604 | int N = pkg.permissions.size(); |
| 605 | StringBuilder r = null; |
| 606 | for (int i=0; i<N; i++) { |
| 607 | PackageParser.Permission p = pkg.permissions.get(i); |
| 608 | BasePermission bp = (BasePermission) mSettings.mPermissions.get(p.info.name); |
| 609 | if (bp == null) { |
| 610 | bp = mSettings.mPermissionTrees.get(p.info.name); |
| 611 | } |
| 612 | if (bp != null && bp.isPermission(p)) { |
| 613 | bp.setPermission(null); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 614 | if (DEBUG_REMOVE && chatty) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 615 | if (r == null) { |
| 616 | r = new StringBuilder(256); |
| 617 | } else { |
| 618 | r.append(' '); |
| 619 | } |
| 620 | r.append(p.info.name); |
| 621 | } |
| 622 | } |
| 623 | if (p.isAppOp()) { |
| 624 | ArraySet<String> appOpPkgs = |
| 625 | mSettings.mAppOpPermissionPackages.get(p.info.name); |
| 626 | if (appOpPkgs != null) { |
| 627 | appOpPkgs.remove(pkg.packageName); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | if (r != null) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 632 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | N = pkg.requestedPermissions.size(); |
| 636 | r = null; |
| 637 | for (int i=0; i<N; i++) { |
| 638 | String perm = pkg.requestedPermissions.get(i); |
| 639 | if (mSettings.isPermissionAppOp(perm)) { |
| 640 | ArraySet<String> appOpPkgs = mSettings.mAppOpPermissionPackages.get(perm); |
| 641 | if (appOpPkgs != null) { |
| 642 | appOpPkgs.remove(pkg.packageName); |
| 643 | if (appOpPkgs.isEmpty()) { |
| 644 | mSettings.mAppOpPermissionPackages.remove(perm); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | if (r != null) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 650 | if (DEBUG_REMOVE) Log.d(TAG, " Permissions: " + r); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | private boolean addDynamicPermission( |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 656 | PermissionInfo info, int callingUid, PermissionCallback callback) { |
| 657 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 658 | throw new SecurityException("Instant apps can't add permissions"); |
| 659 | } |
| 660 | if (info.labelRes == 0 && info.nonLocalizedLabel == null) { |
| 661 | throw new SecurityException("Label must be specified in permission"); |
| 662 | } |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 663 | final BasePermission tree = mSettings.enforcePermissionTree(info.name, callingUid); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 664 | final boolean added; |
| 665 | final boolean changed; |
| 666 | synchronized (mLock) { |
| 667 | BasePermission bp = mSettings.getPermissionLocked(info.name); |
| 668 | added = bp == null; |
| 669 | int fixedLevel = PermissionInfo.fixProtectionLevel(info.protectionLevel); |
| 670 | if (added) { |
| 671 | enforcePermissionCapLocked(info, tree); |
| 672 | bp = new BasePermission(info.name, tree.getSourcePackageName(), |
| 673 | BasePermission.TYPE_DYNAMIC); |
Svet Ganov | 2808cbc | 2018-05-09 15:27:43 -0700 | [diff] [blame] | 674 | } else if (!bp.isDynamic()) { |
| 675 | throw new SecurityException("Not allowed to modify non-dynamic permission " |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 676 | + info.name); |
| 677 | } |
| 678 | changed = bp.addToTree(fixedLevel, info, tree); |
| 679 | if (added) { |
| 680 | mSettings.putPermissionLocked(info.name, bp); |
| 681 | } |
| 682 | } |
| 683 | if (changed && callback != null) { |
| 684 | callback.onPermissionChanged(); |
| 685 | } |
| 686 | return added; |
| 687 | } |
| 688 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 689 | private void removeDynamicPermission( |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 690 | String permName, int callingUid, PermissionCallback callback) { |
| 691 | if (mPackageManagerInt.getInstantAppPackageName(callingUid) != null) { |
| 692 | throw new SecurityException("Instant applications don't have access to this method"); |
| 693 | } |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 694 | final BasePermission tree = mSettings.enforcePermissionTree(permName, callingUid); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 695 | synchronized (mLock) { |
| 696 | final BasePermission bp = mSettings.getPermissionLocked(permName); |
| 697 | if (bp == null) { |
| 698 | return; |
| 699 | } |
| 700 | if (bp.isDynamic()) { |
Jeff Sharkey | 4dc5052 | 2017-10-17 15:29:41 -0600 | [diff] [blame] | 701 | // TODO: switch this back to SecurityException |
| 702 | Slog.wtf(TAG, "Not allowed to modify non-dynamic permission " |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 703 | + permName); |
| 704 | } |
| 705 | mSettings.removePermissionLocked(permName); |
| 706 | if (callback != null) { |
| 707 | callback.onPermissionRemoved(); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
Philip P. Moltmann | e0f00ea | 2018-10-30 10:43:15 -0700 | [diff] [blame] | 712 | /** |
| 713 | * Restore the permission state for a package. |
| 714 | * |
| 715 | * <ul> |
| 716 | * <li>During boot the state gets restored from the disk</li> |
| 717 | * <li>During app update the state gets restored from the last version of the app</li> |
| 718 | * </ul> |
| 719 | * |
| 720 | * <p>This restores the permission state for all users. |
| 721 | * |
| 722 | * @param pkg the package the permissions belong to |
| 723 | * @param replace if the package is getting replaced (this might change the requested |
| 724 | * permissions of this package) |
| 725 | * @param packageOfInterest If this is the name of {@code pkg} add extra logging |
| 726 | * @param callback Result call back |
| 727 | */ |
| 728 | private void restorePermissionState(@NonNull PackageParser.Package pkg, boolean replace, |
| 729 | @Nullable String packageOfInterest, @Nullable PermissionCallback callback) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 730 | // IMPORTANT: There are two types of permissions: install and runtime. |
| 731 | // Install time permissions are granted when the app is installed to |
| 732 | // all device users and users added in the future. Runtime permissions |
| 733 | // are granted at runtime explicitly to specific users. Normal and signature |
| 734 | // protected permissions are install time permissions. Dangerous permissions |
| 735 | // are install permissions if the app's target SDK is Lollipop MR1 or older, |
| 736 | // otherwise they are runtime permissions. This function does not manage |
| 737 | // runtime permissions except for the case an app targeting Lollipop MR1 |
| 738 | // being upgraded to target a newer SDK, in which case dangerous permissions |
| 739 | // are transformed from install time to runtime ones. |
| 740 | |
| 741 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 742 | if (ps == null) { |
| 743 | return; |
| 744 | } |
| 745 | final boolean isLegacySystemApp = mPackageManagerInt.isLegacySystemApp(pkg); |
| 746 | |
| 747 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 748 | PermissionsState origPermissions = permissionsState; |
| 749 | |
| 750 | final int[] currentUserIds = UserManagerService.getInstance().getUserIds(); |
| 751 | |
| 752 | boolean runtimePermissionsRevoked = false; |
| 753 | int[] updatedUserIds = EMPTY_INT_ARRAY; |
| 754 | |
| 755 | boolean changedInstallPermission = false; |
| 756 | |
| 757 | if (replace) { |
| 758 | ps.setInstallPermissionsFixed(false); |
| 759 | if (!ps.isSharedUser()) { |
| 760 | origPermissions = new PermissionsState(permissionsState); |
| 761 | permissionsState.reset(); |
| 762 | } else { |
| 763 | // We need to know only about runtime permission changes since the |
| 764 | // calling code always writes the install permissions state but |
| 765 | // the runtime ones are written only if changed. The only cases of |
| 766 | // changed runtime permissions here are promotion of an install to |
| 767 | // runtime and revocation of a runtime from a shared user. |
| 768 | synchronized (mLock) { |
| 769 | updatedUserIds = revokeUnusedSharedUserPermissionsLocked( |
| 770 | ps.getSharedUser(), UserManagerService.getInstance().getUserIds()); |
| 771 | if (!ArrayUtils.isEmpty(updatedUserIds)) { |
| 772 | runtimePermissionsRevoked = true; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | permissionsState.setGlobalGids(mGlobalGids); |
| 779 | |
| 780 | synchronized (mLock) { |
| 781 | final int N = pkg.requestedPermissions.size(); |
| 782 | for (int i = 0; i < N; i++) { |
| 783 | final String permName = pkg.requestedPermissions.get(i); |
| 784 | final BasePermission bp = mSettings.getPermissionLocked(permName); |
| 785 | final boolean appSupportsRuntimePermissions = |
| 786 | pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.M; |
| 787 | |
| 788 | if (DEBUG_INSTALL) { |
| 789 | Log.i(TAG, "Package " + pkg.packageName + " checking " + permName + ": " + bp); |
| 790 | } |
| 791 | |
| 792 | if (bp == null || bp.getSourcePackageSetting() == null) { |
| 793 | if (packageOfInterest == null || packageOfInterest.equals(pkg.packageName)) { |
| 794 | if (DEBUG_PERMISSIONS) { |
| 795 | Slog.i(TAG, "Unknown permission " + permName |
| 796 | + " in package " + pkg.packageName); |
| 797 | } |
| 798 | } |
| 799 | continue; |
| 800 | } |
| 801 | |
| 802 | // Limit ephemeral apps to ephemeral allowed permissions. |
| 803 | if (pkg.applicationInfo.isInstantApp() && !bp.isInstant()) { |
| 804 | if (DEBUG_PERMISSIONS) { |
| 805 | Log.i(TAG, "Denying non-ephemeral permission " + bp.getName() |
| 806 | + " for package " + pkg.packageName); |
| 807 | } |
| 808 | continue; |
| 809 | } |
| 810 | |
| 811 | if (bp.isRuntimeOnly() && !appSupportsRuntimePermissions) { |
| 812 | if (DEBUG_PERMISSIONS) { |
| 813 | Log.i(TAG, "Denying runtime-only permission " + bp.getName() |
| 814 | + " for package " + pkg.packageName); |
| 815 | } |
| 816 | continue; |
| 817 | } |
| 818 | |
| 819 | final String perm = bp.getName(); |
| 820 | boolean allowedSig = false; |
| 821 | int grant = GRANT_DENIED; |
| 822 | |
| 823 | // Keep track of app op permissions. |
| 824 | if (bp.isAppOp()) { |
| 825 | mSettings.addAppOpPackage(perm, pkg.packageName); |
| 826 | } |
| 827 | |
| 828 | if (bp.isNormal()) { |
| 829 | // For all apps normal permissions are install time ones. |
| 830 | grant = GRANT_INSTALL; |
| 831 | } else if (bp.isRuntime()) { |
| 832 | // If a permission review is required for legacy apps we represent |
| 833 | // their permissions as always granted runtime ones since we need |
| 834 | // to keep the review required permission flag per user while an |
| 835 | // install permission's state is shared across all users. |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 836 | if (origPermissions.hasInstallPermission(bp.getName())) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 837 | // For legacy apps that became modern, install becomes runtime. |
| 838 | grant = GRANT_UPGRADE; |
| 839 | } else if (isLegacySystemApp) { |
| 840 | // For legacy system apps, install becomes runtime. |
| 841 | // We cannot check hasInstallPermission() for system apps since those |
| 842 | // permissions were granted implicitly and not persisted pre-M. |
| 843 | grant = GRANT_UPGRADE; |
| 844 | } else { |
| 845 | // For modern apps keep runtime permissions unchanged. |
| 846 | grant = GRANT_RUNTIME; |
| 847 | } |
| 848 | } else if (bp.isSignature()) { |
| 849 | // For all apps signature permissions are install time ones. |
| 850 | allowedSig = grantSignaturePermission(perm, pkg, bp, origPermissions); |
| 851 | if (allowedSig) { |
| 852 | grant = GRANT_INSTALL; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | if (DEBUG_PERMISSIONS) { |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 857 | Slog.i(TAG, "Considering granting permission " + perm + " to package " |
| 858 | + pkg.packageName); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | if (grant != GRANT_DENIED) { |
| 862 | if (!ps.isSystem() && ps.areInstallPermissionsFixed()) { |
| 863 | // If this is an existing, non-system package, then |
| 864 | // we can't add any new permissions to it. |
| 865 | if (!allowedSig && !origPermissions.hasInstallPermission(perm)) { |
| 866 | // Except... if this is a permission that was added |
| 867 | // to the platform (note: need to only do this when |
| 868 | // updating the platform). |
| 869 | if (!isNewPlatformPermissionForPackage(perm, pkg)) { |
| 870 | grant = GRANT_DENIED; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | switch (grant) { |
| 876 | case GRANT_INSTALL: { |
| 877 | // Revoke this as runtime permission to handle the case of |
| 878 | // a runtime permission being downgraded to an install one. |
| 879 | // Also in permission review mode we keep dangerous permissions |
| 880 | // for legacy apps |
| 881 | for (int userId : UserManagerService.getInstance().getUserIds()) { |
| 882 | if (origPermissions.getRuntimePermissionState( |
| 883 | perm, userId) != null) { |
| 884 | // Revoke the runtime permission and clear the flags. |
| 885 | origPermissions.revokeRuntimePermission(bp, userId); |
| 886 | origPermissions.updatePermissionFlags(bp, userId, |
| 887 | PackageManager.MASK_PERMISSION_FLAGS, 0); |
| 888 | // If we revoked a permission permission, we have to write. |
| 889 | updatedUserIds = ArrayUtils.appendInt( |
| 890 | updatedUserIds, userId); |
| 891 | } |
| 892 | } |
| 893 | // Grant an install permission. |
| 894 | if (permissionsState.grantInstallPermission(bp) != |
| 895 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 896 | changedInstallPermission = true; |
| 897 | } |
| 898 | } break; |
| 899 | |
| 900 | case GRANT_RUNTIME: { |
| 901 | // Grant previously granted runtime permissions. |
| 902 | for (int userId : UserManagerService.getInstance().getUserIds()) { |
| 903 | final PermissionState permissionState = origPermissions |
| 904 | .getRuntimePermissionState(perm, userId); |
| 905 | int flags = permissionState != null |
| 906 | ? permissionState.getFlags() : 0; |
| 907 | if (origPermissions.hasRuntimePermission(perm, userId)) { |
| 908 | // Don't propagate the permission in a permission review |
| 909 | // mode if the former was revoked, i.e. marked to not |
| 910 | // propagate on upgrade. Note that in a permission review |
| 911 | // mode install permissions are represented as constantly |
| 912 | // granted runtime ones since we need to keep a per user |
| 913 | // state associated with the permission. Also the revoke |
| 914 | // on upgrade flag is no longer applicable and is reset. |
| 915 | final boolean revokeOnUpgrade = (flags & PackageManager |
| 916 | .FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0; |
| 917 | if (revokeOnUpgrade) { |
| 918 | flags &= ~PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE; |
| 919 | // Since we changed the flags, we have to write. |
| 920 | updatedUserIds = ArrayUtils.appendInt( |
| 921 | updatedUserIds, userId); |
| 922 | } |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 923 | if (!revokeOnUpgrade) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 924 | if (permissionsState.grantRuntimePermission(bp, userId) == |
| 925 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 926 | // If we cannot put the permission as it was, |
| 927 | // we have to write. |
| 928 | updatedUserIds = ArrayUtils.appendInt( |
| 929 | updatedUserIds, userId); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // If the app supports runtime permissions no need for a review. |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 934 | if (appSupportsRuntimePermissions |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 935 | && (flags & PackageManager |
| 936 | .FLAG_PERMISSION_REVIEW_REQUIRED) != 0) { |
| 937 | flags &= ~PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED; |
| 938 | // Since we changed the flags, we have to write. |
| 939 | updatedUserIds = ArrayUtils.appendInt( |
| 940 | updatedUserIds, userId); |
| 941 | } |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 942 | } else if (!appSupportsRuntimePermissions) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 943 | // For legacy apps that need a permission review, every new |
| 944 | // runtime permission is granted but it is pending a review. |
| 945 | // We also need to review only platform defined runtime |
| 946 | // permissions as these are the only ones the platform knows |
| 947 | // how to disable the API to simulate revocation as legacy |
| 948 | // apps don't expect to run with revoked permissions. |
| 949 | if (PLATFORM_PACKAGE_NAME.equals(bp.getSourcePackageName())) { |
| 950 | if ((flags & FLAG_PERMISSION_REVIEW_REQUIRED) == 0) { |
| 951 | flags |= FLAG_PERMISSION_REVIEW_REQUIRED; |
| 952 | // We changed the flags, hence have to write. |
| 953 | updatedUserIds = ArrayUtils.appendInt( |
| 954 | updatedUserIds, userId); |
| 955 | } |
| 956 | } |
| 957 | if (permissionsState.grantRuntimePermission(bp, userId) |
| 958 | != PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 959 | // We changed the permission, hence have to write. |
| 960 | updatedUserIds = ArrayUtils.appendInt( |
| 961 | updatedUserIds, userId); |
| 962 | } |
| 963 | } |
| 964 | // Propagate the permission flags. |
| 965 | permissionsState.updatePermissionFlags(bp, userId, flags, flags); |
| 966 | } |
| 967 | } break; |
| 968 | |
| 969 | case GRANT_UPGRADE: { |
| 970 | // Grant runtime permissions for a previously held install permission. |
| 971 | final PermissionState permissionState = origPermissions |
| 972 | .getInstallPermissionState(perm); |
| 973 | final int flags = |
| 974 | (permissionState != null) ? permissionState.getFlags() : 0; |
| 975 | |
| 976 | if (origPermissions.revokeInstallPermission(bp) |
| 977 | != PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 978 | // We will be transferring the permission flags, so clear them. |
| 979 | origPermissions.updatePermissionFlags(bp, UserHandle.USER_ALL, |
| 980 | PackageManager.MASK_PERMISSION_FLAGS, 0); |
| 981 | changedInstallPermission = true; |
| 982 | } |
| 983 | |
| 984 | // If the permission is not to be promoted to runtime we ignore it and |
| 985 | // also its other flags as they are not applicable to install permissions. |
| 986 | if ((flags & PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE) == 0) { |
| 987 | for (int userId : currentUserIds) { |
| 988 | if (permissionsState.grantRuntimePermission(bp, userId) != |
| 989 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 990 | // Transfer the permission flags. |
| 991 | permissionsState.updatePermissionFlags(bp, userId, |
| 992 | flags, flags); |
| 993 | // If we granted the permission, we have to write. |
| 994 | updatedUserIds = ArrayUtils.appendInt( |
| 995 | updatedUserIds, userId); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | } break; |
| 1000 | |
| 1001 | default: { |
| 1002 | if (packageOfInterest == null |
| 1003 | || packageOfInterest.equals(pkg.packageName)) { |
| 1004 | if (DEBUG_PERMISSIONS) { |
| 1005 | Slog.i(TAG, "Not granting permission " + perm |
| 1006 | + " to package " + pkg.packageName |
| 1007 | + " because it was previously installed without"); |
| 1008 | } |
| 1009 | } |
| 1010 | } break; |
| 1011 | } |
| 1012 | } else { |
| 1013 | if (permissionsState.revokeInstallPermission(bp) != |
| 1014 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 1015 | // Also drop the permission flags. |
| 1016 | permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL, |
| 1017 | PackageManager.MASK_PERMISSION_FLAGS, 0); |
| 1018 | changedInstallPermission = true; |
| 1019 | Slog.i(TAG, "Un-granting permission " + perm |
| 1020 | + " from package " + pkg.packageName |
| 1021 | + " (protectionLevel=" + bp.getProtectionLevel() |
| 1022 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 1023 | + ")"); |
| 1024 | } else if (bp.isAppOp()) { |
| 1025 | // Don't print warning for app op permissions, since it is fine for them |
| 1026 | // not to be granted, there is a UI for the user to decide. |
| 1027 | if (DEBUG_PERMISSIONS |
| 1028 | && (packageOfInterest == null |
| 1029 | || packageOfInterest.equals(pkg.packageName))) { |
| 1030 | Slog.i(TAG, "Not granting permission " + perm |
| 1031 | + " to package " + pkg.packageName |
| 1032 | + " (protectionLevel=" + bp.getProtectionLevel() |
| 1033 | + " flags=0x" + Integer.toHexString(pkg.applicationInfo.flags) |
| 1034 | + ")"); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | if ((changedInstallPermission || replace) && !ps.areInstallPermissionsFixed() && |
| 1041 | !ps.isSystem() || ps.isUpdatedSystem()) { |
| 1042 | // This is the first that we have heard about this package, so the |
| 1043 | // permissions we have now selected are fixed until explicitly |
| 1044 | // changed. |
| 1045 | ps.setInstallPermissionsFixed(true); |
| 1046 | } |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1047 | |
| 1048 | updatedUserIds = revokePermissionsNoLongerImplicitLocked(permissionsState, pkg, |
| 1049 | updatedUserIds); |
| 1050 | updatedUserIds = setInitialGrantForNewImplicitPermissionsLocked(origPermissions, |
| 1051 | permissionsState, pkg, updatedUserIds); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | // Persist the runtime permissions state for users with changes. If permissions |
| 1055 | // were revoked because no app in the shared user declares them we have to |
| 1056 | // write synchronously to avoid losing runtime permissions state. |
| 1057 | if (callback != null) { |
| 1058 | callback.onPermissionUpdated(updatedUserIds, runtimePermissionsRevoked); |
| 1059 | } |
| 1060 | } |
| 1061 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1062 | /** |
| 1063 | * Set app op for a app-op related to a permission. |
| 1064 | * |
| 1065 | * @param permission The permission the app-op belongs to |
| 1066 | * @param pkg The package the permission belongs to |
| 1067 | * @param userId The user to be changed |
| 1068 | * @param mode The new mode to set |
| 1069 | */ |
| 1070 | private void setAppOpMode(@NonNull String permission, @NonNull PackageParser.Package pkg, |
| 1071 | @UserIdInt int userId, int mode) { |
| 1072 | AppOpsManagerInternal appOpsInternal = LocalServices.getService( |
| 1073 | AppOpsManagerInternal.class); |
| 1074 | |
| 1075 | appOpsInternal.setMode(permissionToOpCode(permission), |
| 1076 | getUid(userId, getAppId(pkg.applicationInfo.uid)), pkg.packageName, mode, |
| 1077 | (pkg.applicationInfo.privateFlags & PRIVATE_FLAG_PRIVILEGED) != 0); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Revoke permissions that are not implicit anymore and that have |
| 1082 | * {@link PackageManager#FLAG_PERMISSION_REVOKE_WHEN_REQUESTED} set. |
| 1083 | * |
| 1084 | * @param ps The state of the permissions of the package |
| 1085 | * @param pkg The package that is currently looked at |
| 1086 | * @param updatedUserIds a list of user ids that needs to be amended if the permission state |
| 1087 | * for a user is changed. |
| 1088 | * |
| 1089 | * @return The updated value of the {@code updatedUserIds} parameter |
| 1090 | */ |
| 1091 | private @NonNull int[] revokePermissionsNoLongerImplicitLocked( |
| 1092 | @NonNull PermissionsState ps, @NonNull PackageParser.Package pkg, |
| 1093 | @NonNull int[] updatedUserIds) { |
| 1094 | AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class); |
| 1095 | |
| 1096 | String pkgName = pkg.packageName; |
| 1097 | |
| 1098 | int[] users = UserManagerService.getInstance().getUserIds(); |
| 1099 | int numUsers = users.length; |
| 1100 | for (int i = 0; i < numUsers; i++) { |
| 1101 | int userId = users[i]; |
| 1102 | |
| 1103 | for (String permission : ps.getPermissions(userId)) { |
| 1104 | if (!pkg.implicitPermissions.contains(permission)) { |
| 1105 | if (!ps.hasInstallPermission(permission)) { |
| 1106 | int flags = ps.getRuntimePermissionState(permission, userId).getFlags(); |
| 1107 | |
| 1108 | if ((flags & FLAG_PERMISSION_REVOKE_WHEN_REQUESTED) != 0) { |
| 1109 | BasePermission bp = mSettings.getPermissionLocked(permission); |
| 1110 | |
| 1111 | ps.updatePermissionFlags(bp, userId, |
| 1112 | FLAG_PERMISSION_REVOKE_WHEN_REQUESTED |
| 1113 | | FLAG_PERMISSION_USER_FIXED | FLAG_PERMISSION_USER_SET, |
| 1114 | 0); |
| 1115 | updatedUserIds = ArrayUtils.appendInt(updatedUserIds, |
| 1116 | userId); |
| 1117 | |
| 1118 | if ((flags & (FLAG_PERMISSION_GRANTED_BY_DEFAULT |
| 1119 | | FLAG_PERMISSION_POLICY_FIXED | FLAG_PERMISSION_SYSTEM_FIXED)) |
| 1120 | == 0) { |
| 1121 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M) { |
| 1122 | if (permissionToOpCode(permission) != OP_NONE) { |
| 1123 | setAppOpMode(permission, pkg, userId, MODE_IGNORED); |
| 1124 | |
| 1125 | if (DEBUG_PERMISSIONS) { |
| 1126 | Slog.i(TAG, "Revoking app-op " |
| 1127 | + permissionToOp(permission) + " for " + pkgName |
| 1128 | + " as it is now requested"); |
| 1129 | } |
| 1130 | } |
| 1131 | } else { |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 1132 | int revokeResult = ps.revokeRuntimePermission(bp, userId); |
| 1133 | if (revokeResult |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1134 | != PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 1135 | |
| 1136 | if (DEBUG_PERMISSIONS) { |
| 1137 | Slog.i(TAG, "Revoking runtime permission " + permission |
| 1138 | + " for " + pkgName |
| 1139 | + " as it is now requested"); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | List<String> fgPerms = mBackgroundPermissions.get(permission); |
| 1145 | if (fgPerms != null) { |
| 1146 | int numFgPerms = fgPerms.size(); |
| 1147 | for (int fgPermNum = 0; fgPermNum < numFgPerms; fgPermNum++) { |
| 1148 | String fgPerm = fgPerms.get(fgPermNum); |
| 1149 | |
| 1150 | int mode = appOpsManager.unsafeCheckOpRaw( |
| 1151 | permissionToOp(fgPerm), |
| 1152 | getUid(userId, getAppId(pkg.applicationInfo.uid)), |
| 1153 | pkgName); |
| 1154 | |
| 1155 | if (mode == MODE_ALLOWED) { |
| 1156 | setAppOpMode(fgPerm, pkg, userId, MODE_FOREGROUND); |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return updatedUserIds; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * {@code newPerm} is newly added; Inherit the state from {@code sourcePerms}. |
| 1172 | * |
| 1173 | * <p>A single new permission can be split off from several source permissions. In this case |
| 1174 | * the most leniant state is inherited. |
| 1175 | * |
| 1176 | * <p>Warning: This does not handle foreground / background permissions |
| 1177 | * |
| 1178 | * @param sourcePerms The permissions to inherit from |
| 1179 | * @param newPerm The permission to inherit to |
| 1180 | * @param ps The permission state of the package |
| 1181 | * @param pkg The package requesting the permissions |
| 1182 | * @param userId The user the permission belongs to |
| 1183 | */ |
| 1184 | private void inheritPermissionStateToNewImplicitPermissionLocked( |
| 1185 | @NonNull ArraySet<String> sourcePerms, @NonNull String newPerm, |
| 1186 | @NonNull PermissionsState ps, @NonNull PackageParser.Package pkg, |
| 1187 | @UserIdInt int userId) { |
| 1188 | AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class); |
| 1189 | String pkgName = pkg.packageName; |
| 1190 | |
| 1191 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M) { |
| 1192 | if (permissionToOp(newPerm) != null) { |
| 1193 | int mostLenientSourceMode = MODE_ERRORED; |
| 1194 | |
| 1195 | // Find most lenient source permission state. |
| 1196 | int numSourcePerms = sourcePerms.size(); |
| 1197 | for (int i = 0; i < numSourcePerms; i++) { |
| 1198 | String sourcePerm = sourcePerms.valueAt(i); |
| 1199 | |
| 1200 | if (ps.hasRuntimePermission(sourcePerm, userId)) { |
| 1201 | String sourceOp = permissionToOp(sourcePerm); |
| 1202 | |
| 1203 | if (sourceOp != null) { |
| 1204 | int mode = appOpsManager.unsafeCheckOpRaw(sourceOp, |
| 1205 | getUid(userId, getAppId(pkg.applicationInfo.uid)), pkgName); |
| 1206 | |
| 1207 | if (mode == MODE_FOREGROUND) { |
| 1208 | throw new IllegalArgumentException("split permission" + sourcePerm |
| 1209 | + " has app-op state " + AppOpsManager.MODE_NAMES[mode]); |
| 1210 | } |
| 1211 | |
| 1212 | // Leniency order: allowed > ignored > default |
| 1213 | if (mode == MODE_ALLOWED) { |
| 1214 | mostLenientSourceMode = MODE_ALLOWED; |
| 1215 | break; |
| 1216 | } else if (mode == MODE_IGNORED) { |
| 1217 | mostLenientSourceMode = MODE_IGNORED; |
| 1218 | } else if (mode == MODE_DEFAULT |
| 1219 | && mostLenientSourceMode != MODE_IGNORED) { |
| 1220 | mostLenientSourceMode = MODE_DEFAULT; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (mostLenientSourceMode != MODE_ERRORED) { |
| 1227 | if (DEBUG_PERMISSIONS) { |
| 1228 | Slog.i(TAG, newPerm + " inherits app-ops state " + mostLenientSourceMode |
| 1229 | + " from " + sourcePerms + " for " + pkgName); |
| 1230 | } |
| 1231 | |
| 1232 | setAppOpMode(newPerm, pkg, userId, mostLenientSourceMode); |
| 1233 | } |
| 1234 | } |
| 1235 | } else { |
| 1236 | boolean isGranted = false; |
| 1237 | |
| 1238 | int numSourcePerm = sourcePerms.size(); |
| 1239 | for (int i = 0; i < numSourcePerm; i++) { |
| 1240 | String sourcePerm = sourcePerms.valueAt(i); |
| 1241 | if (ps.hasRuntimePermission(sourcePerm, userId) |
| 1242 | && ps.getRuntimePermissionState(sourcePerm, userId).isGranted()) { |
| 1243 | isGranted = true; |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | if (isGranted) { |
| 1249 | if (DEBUG_PERMISSIONS) { |
| 1250 | Slog.i(TAG, newPerm + " inherits runtime perm grant from " + sourcePerms |
| 1251 | + " for " + pkgName); |
| 1252 | } |
| 1253 | |
| 1254 | ps.grantRuntimePermission(mSettings.getPermissionLocked(newPerm), userId); |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * Set the state of a implicit permission that is seen for the first time. |
| 1261 | * |
| 1262 | * @param origPs The permission state of the package before the split |
| 1263 | * @param ps The new permission state |
| 1264 | * @param pkg The package the permission belongs to |
| 1265 | * @param updatedUserIds List of users for which the permission state has already been changed |
| 1266 | * |
| 1267 | * @return List of users for which the permission state has been changed |
| 1268 | */ |
| 1269 | private @NonNull int[] setInitialGrantForNewImplicitPermissionsLocked( |
| 1270 | @NonNull PermissionsState origPs, |
| 1271 | @NonNull PermissionsState ps, @NonNull PackageParser.Package pkg, |
| 1272 | @NonNull int[] updatedUserIds) { |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 1273 | AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class); |
| 1274 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1275 | String pkgName = pkg.packageName; |
| 1276 | ArraySet<String> newImplicitPermissions = new ArraySet<>(); |
| 1277 | |
| 1278 | int numRequestedPerms = pkg.requestedPermissions.size(); |
| 1279 | for (int i = 0; i < numRequestedPerms; i++) { |
| 1280 | BasePermission bp = mSettings.getPermissionLocked(pkg.requestedPermissions.get(i)); |
| 1281 | if (bp != null) { |
| 1282 | String perm = bp.getName(); |
| 1283 | |
| 1284 | if (!origPs.hasRequestedPermission(perm) && pkg.implicitPermissions.contains( |
| 1285 | perm)) { |
| 1286 | newImplicitPermissions.add(perm); |
| 1287 | |
| 1288 | if (DEBUG_PERMISSIONS) { |
| 1289 | Slog.i(TAG, perm + " is newly added for " + pkgName); |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | ArrayMap<String, ArraySet<String>> newToSplitPerms = new ArrayMap<>(); |
| 1296 | |
| 1297 | int numSplitPerms = PermissionManager.SPLIT_PERMISSIONS.size(); |
| 1298 | for (int splitPermNum = 0; splitPermNum < numSplitPerms; splitPermNum++) { |
| 1299 | PermissionManager.SplitPermissionInfo spi = |
| 1300 | PermissionManager.SPLIT_PERMISSIONS.get(splitPermNum); |
| 1301 | |
| 1302 | List<String> newPerms = spi.getNewPermissions(); |
| 1303 | int numNewPerms = newPerms.size(); |
| 1304 | for (int newPermNum = 0; newPermNum < numNewPerms; newPermNum++) { |
| 1305 | String newPerm = newPerms.get(newPermNum); |
| 1306 | |
| 1307 | ArraySet<String> splitPerms = newToSplitPerms.get(newPerm); |
| 1308 | if (splitPerms == null) { |
| 1309 | splitPerms = new ArraySet<>(); |
| 1310 | newToSplitPerms.put(newPerm, splitPerms); |
| 1311 | } |
| 1312 | |
| 1313 | splitPerms.add(spi.getSplitPermission()); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | int numNewImplicitPerms = newImplicitPermissions.size(); |
| 1318 | for (int newImplicitPermNum = 0; newImplicitPermNum < numNewImplicitPerms; |
| 1319 | newImplicitPermNum++) { |
| 1320 | String newPerm = newImplicitPermissions.valueAt(newImplicitPermNum); |
| 1321 | ArraySet<String> sourcePerms = newToSplitPerms.get(newPerm); |
| 1322 | |
| 1323 | if (sourcePerms != null) { |
| 1324 | if (!ps.hasInstallPermission(newPerm)) { |
| 1325 | BasePermission bp = mSettings.getPermissionLocked(newPerm); |
| 1326 | |
| 1327 | int[] users = UserManagerService.getInstance().getUserIds(); |
| 1328 | int numUsers = users.length; |
| 1329 | for (int userNum = 0; userNum < numUsers; userNum++) { |
| 1330 | int userId = users[userNum]; |
| 1331 | |
| 1332 | ps.updatePermissionFlags(bp, userId, |
| 1333 | FLAG_PERMISSION_REVOKE_WHEN_REQUESTED, |
| 1334 | FLAG_PERMISSION_REVOKE_WHEN_REQUESTED); |
| 1335 | updatedUserIds = ArrayUtils.appendInt(updatedUserIds, userId); |
| 1336 | |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 1337 | // SPECIAL BEHAVIOR for background location. Foreground only by default. |
| 1338 | if (newPerm.equals(ACCESS_BACKGROUND_LOCATION)) { |
| 1339 | int numSourcePerms = sourcePerms.size(); |
| 1340 | for (int sourcePermNum = 0; sourcePermNum < numSourcePerms; |
| 1341 | sourcePermNum++) { |
| 1342 | String sourcePerm = sourcePerms.valueAt(sourcePermNum); |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1343 | |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 1344 | if (appOpsManager.unsafeCheckOpNoThrow(permissionToOp(sourcePerm), |
| 1345 | getUid(userId, getAppId(pkg.applicationInfo.uid)), pkgName) |
| 1346 | == MODE_ALLOWED) { |
| 1347 | setAppOpMode(sourcePerm, pkg, userId, MODE_FOREGROUND); |
| 1348 | } |
| 1349 | } |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1350 | } else { |
Philip P. Moltmann | d82bdaf | 2018-10-30 12:46:40 -0700 | [diff] [blame^] | 1351 | if (!origPs.hasRequestedPermission(sourcePerms)) { |
| 1352 | // Both permissions are new, do nothing |
| 1353 | if (DEBUG_PERMISSIONS) { |
| 1354 | Slog.i(TAG, newPerm + " does not inherit from " + sourcePerms |
| 1355 | + " for " + pkgName |
| 1356 | + " as split permission is also new"); |
| 1357 | } |
| 1358 | |
| 1359 | break; |
| 1360 | } else { |
| 1361 | inheritPermissionStateToNewImplicitPermissionLocked(sourcePerms, |
| 1362 | newPerm, ps, pkg, userId); |
| 1363 | } |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | return updatedUserIds; |
| 1371 | } |
| 1372 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1373 | private boolean isNewPlatformPermissionForPackage(String perm, PackageParser.Package pkg) { |
| 1374 | boolean allowed = false; |
| 1375 | final int NP = PackageParser.NEW_PERMISSIONS.length; |
| 1376 | for (int ip=0; ip<NP; ip++) { |
| 1377 | final PackageParser.NewPermissionInfo npi |
| 1378 | = PackageParser.NEW_PERMISSIONS[ip]; |
| 1379 | if (npi.name.equals(perm) |
| 1380 | && pkg.applicationInfo.targetSdkVersion < npi.sdkVersion) { |
| 1381 | allowed = true; |
| 1382 | Log.i(TAG, "Auto-granting " + perm + " to old pkg " |
| 1383 | + pkg.packageName); |
| 1384 | break; |
| 1385 | } |
| 1386 | } |
| 1387 | return allowed; |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * Determines whether a package is whitelisted for a particular privapp permission. |
| 1392 | * |
| 1393 | * <p>Does NOT check whether the package is a privapp, just whether it's whitelisted. |
| 1394 | * |
| 1395 | * <p>This handles parent/child apps. |
| 1396 | */ |
| 1397 | private boolean hasPrivappWhitelistEntry(String perm, PackageParser.Package pkg) { |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1398 | ArraySet<String> wlPermissions = null; |
| 1399 | if (pkg.isVendor()) { |
| 1400 | wlPermissions = |
| 1401 | SystemConfig.getInstance().getVendorPrivAppPermissions(pkg.packageName); |
| 1402 | } else if (pkg.isProduct()) { |
| 1403 | wlPermissions = |
| 1404 | SystemConfig.getInstance().getProductPrivAppPermissions(pkg.packageName); |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 1405 | } else if (pkg.isProductServices()) { |
| 1406 | wlPermissions = |
| 1407 | SystemConfig.getInstance().getProductServicesPrivAppPermissions( |
| 1408 | pkg.packageName); |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1409 | } else { |
| 1410 | wlPermissions = SystemConfig.getInstance().getPrivAppPermissions(pkg.packageName); |
| 1411 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1412 | // Let's check if this package is whitelisted... |
| 1413 | boolean whitelisted = wlPermissions != null && wlPermissions.contains(perm); |
| 1414 | // If it's not, we'll also tail-recurse to the parent. |
| 1415 | return whitelisted || |
| 1416 | pkg.parentPackage != null && hasPrivappWhitelistEntry(perm, pkg.parentPackage); |
| 1417 | } |
| 1418 | |
| 1419 | private boolean grantSignaturePermission(String perm, PackageParser.Package pkg, |
| 1420 | BasePermission bp, PermissionsState origPermissions) { |
| 1421 | boolean oemPermission = bp.isOEM(); |
Jiyong Park | 002fdbd | 2017-02-13 20:50:31 +0900 | [diff] [blame] | 1422 | boolean vendorPrivilegedPermission = bp.isVendorPrivileged(); |
| 1423 | boolean privilegedPermission = bp.isPrivileged() || bp.isVendorPrivileged(); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1424 | boolean privappPermissionsDisable = |
| 1425 | RoSystemProperties.CONTROL_PRIVAPP_PERMISSIONS_DISABLE; |
| 1426 | boolean platformPermission = PLATFORM_PACKAGE_NAME.equals(bp.getSourcePackageName()); |
| 1427 | boolean platformPackage = PLATFORM_PACKAGE_NAME.equals(pkg.packageName); |
| 1428 | if (!privappPermissionsDisable && privilegedPermission && pkg.isPrivileged() |
| 1429 | && !platformPackage && platformPermission) { |
| 1430 | if (!hasPrivappWhitelistEntry(perm, pkg)) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1431 | // Only report violations for apps on system image |
| 1432 | if (!mSystemReady && !pkg.isUpdatedSystemApp()) { |
| 1433 | // it's only a reportable violation if the permission isn't explicitly denied |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1434 | ArraySet<String> deniedPermissions = null; |
| 1435 | if (pkg.isVendor()) { |
| 1436 | deniedPermissions = SystemConfig.getInstance() |
| 1437 | .getVendorPrivAppDenyPermissions(pkg.packageName); |
| 1438 | } else if (pkg.isProduct()) { |
| 1439 | deniedPermissions = SystemConfig.getInstance() |
| 1440 | .getProductPrivAppDenyPermissions(pkg.packageName); |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 1441 | } else if (pkg.isProductServices()) { |
| 1442 | deniedPermissions = SystemConfig.getInstance() |
| 1443 | .getProductServicesPrivAppDenyPermissions(pkg.packageName); |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1444 | } else { |
| 1445 | deniedPermissions = SystemConfig.getInstance() |
| 1446 | .getPrivAppDenyPermissions(pkg.packageName); |
| 1447 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1448 | final boolean permissionViolation = |
| 1449 | deniedPermissions == null || !deniedPermissions.contains(perm); |
Fyodor Kupolov | f5e600d | 2017-10-25 17:03:50 -0700 | [diff] [blame] | 1450 | if (permissionViolation) { |
| 1451 | Slog.w(TAG, "Privileged permission " + perm + " for package " |
| 1452 | + pkg.packageName + " - not in privapp-permissions whitelist"); |
| 1453 | |
| 1454 | if (RoSystemProperties.CONTROL_PRIVAPP_PERMISSIONS_ENFORCE) { |
| 1455 | if (mPrivappPermissionsViolations == null) { |
| 1456 | mPrivappPermissionsViolations = new ArraySet<>(); |
| 1457 | } |
| 1458 | mPrivappPermissionsViolations.add(pkg.packageName + ": " + perm); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1459 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1460 | } else { |
| 1461 | return false; |
| 1462 | } |
| 1463 | } |
| 1464 | if (RoSystemProperties.CONTROL_PRIVAPP_PERMISSIONS_ENFORCE) { |
| 1465 | return false; |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | final String systemPackageName = mPackageManagerInt.getKnownPackageName( |
| 1470 | PackageManagerInternal.PACKAGE_SYSTEM, UserHandle.USER_SYSTEM); |
| 1471 | final PackageParser.Package systemPackage = |
| 1472 | mPackageManagerInt.getPackage(systemPackageName); |
Dan Cashman | 1dbe6d0 | 2018-01-23 11:18:28 -0800 | [diff] [blame] | 1473 | |
| 1474 | // check if the package is allow to use this signature permission. A package is allowed to |
| 1475 | // use a signature permission if: |
| 1476 | // - it has the same set of signing certificates as the source package |
| 1477 | // - or its signing certificate was rotated from the source package's certificate |
| 1478 | // - or its signing certificate is a previous signing certificate of the defining |
| 1479 | // package, and the defining package still trusts the old certificate for permissions |
| 1480 | // - or it shares the above relationships with the system package |
| 1481 | boolean allowed = |
| 1482 | pkg.mSigningDetails.hasAncestorOrSelf( |
| 1483 | bp.getSourcePackageSetting().getSigningDetails()) |
| 1484 | || bp.getSourcePackageSetting().getSigningDetails().checkCapability( |
| 1485 | pkg.mSigningDetails, |
| 1486 | PackageParser.SigningDetails.CertCapabilities.PERMISSION) |
| 1487 | || pkg.mSigningDetails.hasAncestorOrSelf(systemPackage.mSigningDetails) |
| 1488 | || systemPackage.mSigningDetails.checkCapability( |
| 1489 | pkg.mSigningDetails, |
| 1490 | PackageParser.SigningDetails.CertCapabilities.PERMISSION); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1491 | if (!allowed && (privilegedPermission || oemPermission)) { |
| 1492 | if (pkg.isSystem()) { |
| 1493 | // For updated system applications, a privileged/oem permission |
| 1494 | // is granted only if it had been defined by the original application. |
| 1495 | if (pkg.isUpdatedSystemApp()) { |
| 1496 | final PackageParser.Package disabledPkg = |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1497 | mPackageManagerInt.getDisabledSystemPackage(pkg.packageName); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1498 | final PackageSetting disabledPs = |
| 1499 | (disabledPkg != null) ? (PackageSetting) disabledPkg.mExtras : null; |
| 1500 | if (disabledPs != null |
| 1501 | && disabledPs.getPermissionsState().hasInstallPermission(perm)) { |
| 1502 | // If the original was granted this permission, we take |
| 1503 | // that grant decision as read and propagate it to the |
| 1504 | // update. |
| 1505 | if ((privilegedPermission && disabledPs.isPrivileged()) |
| 1506 | || (oemPermission && disabledPs.isOem() |
| 1507 | && canGrantOemPermission(disabledPs, perm))) { |
| 1508 | allowed = true; |
| 1509 | } |
| 1510 | } else { |
| 1511 | // The system apk may have been updated with an older |
| 1512 | // version of the one on the data partition, but which |
| 1513 | // granted a new system permission that it didn't have |
| 1514 | // before. In this case we do want to allow the app to |
| 1515 | // now get the new permission if the ancestral apk is |
| 1516 | // privileged to get it. |
Todd Kennedy | 1efb833 | 2017-10-25 15:51:36 -0700 | [diff] [blame] | 1517 | if (disabledPs != null && disabledPkg != null |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1518 | && isPackageRequestingPermission(disabledPkg, perm) |
| 1519 | && ((privilegedPermission && disabledPs.isPrivileged()) |
| 1520 | || (oemPermission && disabledPs.isOem() |
| 1521 | && canGrantOemPermission(disabledPs, perm)))) { |
| 1522 | allowed = true; |
| 1523 | } |
| 1524 | // Also if a privileged parent package on the system image or any of |
| 1525 | // its children requested a privileged/oem permission, the updated child |
| 1526 | // packages can also get the permission. |
| 1527 | if (pkg.parentPackage != null) { |
| 1528 | final PackageParser.Package disabledParentPkg = mPackageManagerInt |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1529 | .getDisabledSystemPackage(pkg.parentPackage.packageName); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1530 | final PackageSetting disabledParentPs = (disabledParentPkg != null) |
| 1531 | ? (PackageSetting) disabledParentPkg.mExtras : null; |
| 1532 | if (disabledParentPkg != null |
| 1533 | && ((privilegedPermission && disabledParentPs.isPrivileged()) |
| 1534 | || (oemPermission && disabledParentPs.isOem()))) { |
| 1535 | if (isPackageRequestingPermission(disabledParentPkg, perm) |
| 1536 | && canGrantOemPermission(disabledParentPs, perm)) { |
| 1537 | allowed = true; |
| 1538 | } else if (disabledParentPkg.childPackages != null) { |
| 1539 | for (PackageParser.Package disabledChildPkg |
| 1540 | : disabledParentPkg.childPackages) { |
| 1541 | final PackageSetting disabledChildPs = |
| 1542 | (disabledChildPkg != null) |
| 1543 | ? (PackageSetting) disabledChildPkg.mExtras |
| 1544 | : null; |
| 1545 | if (isPackageRequestingPermission(disabledChildPkg, perm) |
| 1546 | && canGrantOemPermission( |
| 1547 | disabledChildPs, perm)) { |
| 1548 | allowed = true; |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | } else { |
| 1557 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 1558 | allowed = (privilegedPermission && pkg.isPrivileged()) |
| 1559 | || (oemPermission && pkg.isOem() |
| 1560 | && canGrantOemPermission(ps, perm)); |
| 1561 | } |
Jiyong Park | 002fdbd | 2017-02-13 20:50:31 +0900 | [diff] [blame] | 1562 | // In any case, don't grant a privileged permission to privileged vendor apps, if |
| 1563 | // the permission's protectionLevel does not have the extra 'vendorPrivileged' |
| 1564 | // flag. |
| 1565 | if (allowed && privilegedPermission && |
| 1566 | !vendorPrivilegedPermission && pkg.isVendor()) { |
| 1567 | Slog.w(TAG, "Permission " + perm + " cannot be granted to privileged vendor apk " |
| 1568 | + pkg.packageName + " because it isn't a 'vendorPrivileged' permission."); |
| 1569 | allowed = false; |
| 1570 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | if (!allowed) { |
| 1574 | if (!allowed |
| 1575 | && bp.isPre23() |
| 1576 | && pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M) { |
| 1577 | // If this was a previously normal/dangerous permission that got moved |
| 1578 | // to a system permission as part of the runtime permission redesign, then |
| 1579 | // we still want to blindly grant it to old apps. |
| 1580 | allowed = true; |
| 1581 | } |
Philip P. Moltmann | 8943ad6 | 2018-07-25 12:12:30 -0700 | [diff] [blame] | 1582 | // TODO (moltmann): The installer now shares the platforms signature. Hence it does not |
| 1583 | // need a separate flag anymore. Hence we need to check which |
| 1584 | // permissions are needed by the permission controller |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1585 | if (!allowed && bp.isInstaller() |
Philip P. Moltmann | 8943ad6 | 2018-07-25 12:12:30 -0700 | [diff] [blame] | 1586 | && (pkg.packageName.equals(mPackageManagerInt.getKnownPackageName( |
| 1587 | PackageManagerInternal.PACKAGE_INSTALLER, UserHandle.USER_SYSTEM)) |
| 1588 | || pkg.packageName.equals(mPackageManagerInt.getKnownPackageName( |
| 1589 | PackageManagerInternal.PACKAGE_PERMISSION_CONTROLLER, |
| 1590 | UserHandle.USER_SYSTEM)))) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1591 | // If this permission is to be granted to the system installer and |
| 1592 | // this app is an installer, then it gets the permission. |
| 1593 | allowed = true; |
| 1594 | } |
| 1595 | if (!allowed && bp.isVerifier() |
| 1596 | && pkg.packageName.equals(mPackageManagerInt.getKnownPackageName( |
| 1597 | PackageManagerInternal.PACKAGE_VERIFIER, UserHandle.USER_SYSTEM))) { |
| 1598 | // If this permission is to be granted to the system verifier and |
| 1599 | // this app is a verifier, then it gets the permission. |
| 1600 | allowed = true; |
| 1601 | } |
| 1602 | if (!allowed && bp.isPreInstalled() |
| 1603 | && pkg.isSystem()) { |
| 1604 | // Any pre-installed system app is allowed to get this permission. |
| 1605 | allowed = true; |
| 1606 | } |
| 1607 | if (!allowed && bp.isDevelopment()) { |
| 1608 | // For development permissions, a development permission |
| 1609 | // is granted only if it was already granted. |
| 1610 | allowed = origPermissions.hasInstallPermission(perm); |
| 1611 | } |
| 1612 | if (!allowed && bp.isSetup() |
| 1613 | && pkg.packageName.equals(mPackageManagerInt.getKnownPackageName( |
| 1614 | PackageManagerInternal.PACKAGE_SETUP_WIZARD, UserHandle.USER_SYSTEM))) { |
| 1615 | // If this permission is to be granted to the system setup wizard and |
| 1616 | // this app is a setup wizard, then it gets the permission. |
| 1617 | allowed = true; |
| 1618 | } |
Makoto Onuki | 700feef | 2018-02-15 10:59:41 -0800 | [diff] [blame] | 1619 | if (!allowed && bp.isSystemTextClassifier() |
| 1620 | && pkg.packageName.equals(mPackageManagerInt.getKnownPackageName( |
| 1621 | PackageManagerInternal.PACKAGE_SYSTEM_TEXT_CLASSIFIER, |
| 1622 | UserHandle.USER_SYSTEM))) { |
| 1623 | // Special permissions for the system default text classifier. |
| 1624 | allowed = true; |
| 1625 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1626 | } |
| 1627 | return allowed; |
| 1628 | } |
| 1629 | |
| 1630 | private static boolean canGrantOemPermission(PackageSetting ps, String permission) { |
| 1631 | if (!ps.isOem()) { |
| 1632 | return false; |
| 1633 | } |
| 1634 | // all oem permissions must explicitly be granted or denied |
| 1635 | final Boolean granted = |
| 1636 | SystemConfig.getInstance().getOemPermissions(ps.name).get(permission); |
| 1637 | if (granted == null) { |
| 1638 | throw new IllegalStateException("OEM permission" + permission + " requested by package " |
| 1639 | + ps.name + " must be explicitly declared granted or not"); |
| 1640 | } |
| 1641 | return Boolean.TRUE == granted; |
| 1642 | } |
| 1643 | |
| 1644 | private boolean isPermissionsReviewRequired(PackageParser.Package pkg, int userId) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1645 | // Permission review applies only to apps not supporting the new permission model. |
| 1646 | if (pkg.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.M) { |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
| 1650 | // Legacy apps have the permission and get user consent on launch. |
| 1651 | if (pkg == null || pkg.mExtras == null) { |
| 1652 | return false; |
| 1653 | } |
| 1654 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 1655 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 1656 | return permissionsState.isPermissionReviewRequired(userId); |
| 1657 | } |
| 1658 | |
| 1659 | private boolean isPackageRequestingPermission(PackageParser.Package pkg, String permission) { |
| 1660 | final int permCount = pkg.requestedPermissions.size(); |
| 1661 | for (int j = 0; j < permCount; j++) { |
| 1662 | String requestedPermission = pkg.requestedPermissions.get(j); |
| 1663 | if (permission.equals(requestedPermission)) { |
| 1664 | return true; |
| 1665 | } |
| 1666 | } |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1670 | @GuardedBy("mLock") |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1671 | private void grantRuntimePermissionsGrantedToDisabledPackageLocked( |
| 1672 | PackageParser.Package pkg, int callingUid, PermissionCallback callback) { |
| 1673 | if (pkg.parentPackage == null) { |
| 1674 | return; |
| 1675 | } |
| 1676 | if (pkg.requestedPermissions == null) { |
| 1677 | return; |
| 1678 | } |
| 1679 | final PackageParser.Package disabledPkg = |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1680 | mPackageManagerInt.getDisabledSystemPackage(pkg.parentPackage.packageName); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1681 | if (disabledPkg == null || disabledPkg.mExtras == null) { |
| 1682 | return; |
| 1683 | } |
| 1684 | final PackageSetting disabledPs = (PackageSetting) disabledPkg.mExtras; |
| 1685 | if (!disabledPs.isPrivileged() || disabledPs.hasChildPackages()) { |
| 1686 | return; |
| 1687 | } |
| 1688 | final int permCount = pkg.requestedPermissions.size(); |
| 1689 | for (int i = 0; i < permCount; i++) { |
| 1690 | String permission = pkg.requestedPermissions.get(i); |
| 1691 | BasePermission bp = mSettings.getPermissionLocked(permission); |
| 1692 | if (bp == null || !(bp.isRuntime() || bp.isDevelopment())) { |
| 1693 | continue; |
| 1694 | } |
| 1695 | for (int userId : mUserManagerInt.getUserIds()) { |
| 1696 | if (disabledPs.getPermissionsState().hasRuntimePermission(permission, userId)) { |
| 1697 | grantRuntimePermission( |
| 1698 | permission, pkg.packageName, false, callingUid, userId, callback); |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | private void grantRequestedRuntimePermissions(PackageParser.Package pkg, int[] userIds, |
| 1705 | String[] grantedPermissions, int callingUid, PermissionCallback callback) { |
| 1706 | for (int userId : userIds) { |
| 1707 | grantRequestedRuntimePermissionsForUser(pkg, userId, grantedPermissions, callingUid, |
| 1708 | callback); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | private void grantRequestedRuntimePermissionsForUser(PackageParser.Package pkg, int userId, |
| 1713 | String[] grantedPermissions, int callingUid, PermissionCallback callback) { |
| 1714 | PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 1715 | if (ps == null) { |
| 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | PermissionsState permissionsState = ps.getPermissionsState(); |
| 1720 | |
| 1721 | final int immutableFlags = PackageManager.FLAG_PERMISSION_SYSTEM_FIXED |
| 1722 | | PackageManager.FLAG_PERMISSION_POLICY_FIXED; |
| 1723 | |
| 1724 | final boolean supportsRuntimePermissions = pkg.applicationInfo.targetSdkVersion |
| 1725 | >= Build.VERSION_CODES.M; |
| 1726 | |
| 1727 | final boolean instantApp = mPackageManagerInt.isInstantApp(pkg.packageName, userId); |
| 1728 | |
| 1729 | for (String permission : pkg.requestedPermissions) { |
| 1730 | final BasePermission bp; |
| 1731 | synchronized (mLock) { |
| 1732 | bp = mSettings.getPermissionLocked(permission); |
| 1733 | } |
| 1734 | if (bp != null && (bp.isRuntime() || bp.isDevelopment()) |
| 1735 | && (!instantApp || bp.isInstant()) |
| 1736 | && (supportsRuntimePermissions || !bp.isRuntimeOnly()) |
| 1737 | && (grantedPermissions == null |
| 1738 | || ArrayUtils.contains(grantedPermissions, permission))) { |
| 1739 | final int flags = permissionsState.getPermissionFlags(permission, userId); |
| 1740 | if (supportsRuntimePermissions) { |
| 1741 | // Installer cannot change immutable permissions. |
| 1742 | if ((flags & immutableFlags) == 0) { |
| 1743 | grantRuntimePermission(permission, pkg.packageName, false, callingUid, |
| 1744 | userId, callback); |
| 1745 | } |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 1746 | } else { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1747 | // In permission review mode we clear the review flag when we |
| 1748 | // are asked to install the app with all permissions granted. |
| 1749 | if ((flags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0) { |
| 1750 | updatePermissionFlags(permission, pkg.packageName, |
| 1751 | PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED, 0, callingUid, |
| 1752 | userId, callback); |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | private void grantRuntimePermission(String permName, String packageName, boolean overridePolicy, |
| 1760 | int callingUid, final int userId, PermissionCallback callback) { |
| 1761 | if (!mUserManagerInt.exists(userId)) { |
| 1762 | Log.e(TAG, "No such user:" + userId); |
| 1763 | return; |
| 1764 | } |
| 1765 | |
| 1766 | mContext.enforceCallingOrSelfPermission( |
| 1767 | android.Manifest.permission.GRANT_RUNTIME_PERMISSIONS, |
| 1768 | "grantRuntimePermission"); |
| 1769 | |
| 1770 | enforceCrossUserPermission(callingUid, userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 1771 | true, // requireFullPermission |
| 1772 | true, // checkShell |
| 1773 | false, // requirePermissionWhenSameUser |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1774 | "grantRuntimePermission"); |
| 1775 | |
| 1776 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(packageName); |
| 1777 | if (pkg == null || pkg.mExtras == null) { |
| 1778 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 1779 | } |
| 1780 | final BasePermission bp; |
| 1781 | synchronized(mLock) { |
| 1782 | bp = mSettings.getPermissionLocked(permName); |
| 1783 | } |
| 1784 | if (bp == null) { |
| 1785 | throw new IllegalArgumentException("Unknown permission: " + permName); |
| 1786 | } |
| 1787 | if (mPackageManagerInt.filterAppAccess(pkg, callingUid, userId)) { |
| 1788 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 1789 | } |
| 1790 | |
| 1791 | bp.enforceDeclaredUsedAndRuntimeOrDevelopment(pkg); |
| 1792 | |
| 1793 | // If a permission review is required for legacy apps we represent |
| 1794 | // their permissions as always granted runtime ones since we need |
| 1795 | // to keep the review required permission flag per user while an |
| 1796 | // install permission's state is shared across all users. |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 1797 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1798 | && bp.isRuntime()) { |
| 1799 | return; |
| 1800 | } |
| 1801 | |
| 1802 | final int uid = UserHandle.getUid(userId, pkg.applicationInfo.uid); |
| 1803 | |
| 1804 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 1805 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 1806 | |
| 1807 | final int flags = permissionsState.getPermissionFlags(permName, userId); |
| 1808 | if ((flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0) { |
| 1809 | throw new SecurityException("Cannot grant system fixed permission " |
| 1810 | + permName + " for package " + packageName); |
| 1811 | } |
| 1812 | if (!overridePolicy && (flags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0) { |
| 1813 | throw new SecurityException("Cannot grant policy fixed permission " |
| 1814 | + permName + " for package " + packageName); |
| 1815 | } |
| 1816 | |
| 1817 | if (bp.isDevelopment()) { |
| 1818 | // Development permissions must be handled specially, since they are not |
| 1819 | // normal runtime permissions. For now they apply to all users. |
| 1820 | if (permissionsState.grantInstallPermission(bp) != |
| 1821 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 1822 | if (callback != null) { |
| 1823 | callback.onInstallPermissionGranted(); |
| 1824 | } |
| 1825 | } |
| 1826 | return; |
| 1827 | } |
| 1828 | |
| 1829 | if (ps.getInstantApp(userId) && !bp.isInstant()) { |
| 1830 | throw new SecurityException("Cannot grant non-ephemeral permission" |
| 1831 | + permName + " for package " + packageName); |
| 1832 | } |
| 1833 | |
| 1834 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M) { |
| 1835 | Slog.w(TAG, "Cannot grant runtime permission to a legacy app"); |
| 1836 | return; |
| 1837 | } |
| 1838 | |
| 1839 | final int result = permissionsState.grantRuntimePermission(bp, userId); |
| 1840 | switch (result) { |
| 1841 | case PermissionsState.PERMISSION_OPERATION_FAILURE: { |
| 1842 | return; |
| 1843 | } |
| 1844 | |
| 1845 | case PermissionsState.PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED: { |
| 1846 | if (callback != null) { |
| 1847 | callback.onGidsChanged(UserHandle.getAppId(pkg.applicationInfo.uid), userId); |
| 1848 | } |
| 1849 | } |
| 1850 | break; |
| 1851 | } |
| 1852 | |
| 1853 | if (bp.isRuntime()) { |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 1854 | logPermission(MetricsEvent.ACTION_PERMISSION_GRANTED, permName, packageName); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | if (callback != null) { |
| 1858 | callback.onPermissionGranted(uid, userId); |
| 1859 | } |
| 1860 | |
| 1861 | // Only need to do this if user is initialized. Otherwise it's a new user |
| 1862 | // and there are no processes running as the user yet and there's no need |
| 1863 | // to make an expensive call to remount processes for the changed permissions. |
| 1864 | if (READ_EXTERNAL_STORAGE.equals(permName) |
| 1865 | || WRITE_EXTERNAL_STORAGE.equals(permName)) { |
| 1866 | final long token = Binder.clearCallingIdentity(); |
| 1867 | try { |
| 1868 | if (mUserManagerInt.isUserInitialized(userId)) { |
| 1869 | StorageManagerInternal storageManagerInternal = LocalServices.getService( |
| 1870 | StorageManagerInternal.class); |
| 1871 | storageManagerInternal.onExternalStoragePolicyChanged(uid, packageName); |
| 1872 | } |
| 1873 | } finally { |
| 1874 | Binder.restoreCallingIdentity(token); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | } |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 1879 | |
| 1880 | private void revokeRuntimePermission(String permName, String packageName, |
| 1881 | boolean overridePolicy, int callingUid, int userId, PermissionCallback callback) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1882 | if (!mUserManagerInt.exists(userId)) { |
| 1883 | Log.e(TAG, "No such user:" + userId); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| 1887 | mContext.enforceCallingOrSelfPermission( |
| 1888 | android.Manifest.permission.REVOKE_RUNTIME_PERMISSIONS, |
| 1889 | "revokeRuntimePermission"); |
| 1890 | |
| 1891 | enforceCrossUserPermission(Binder.getCallingUid(), userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 1892 | true, // requireFullPermission |
| 1893 | true, // checkShell |
| 1894 | false, // requirePermissionWhenSameUser |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1895 | "revokeRuntimePermission"); |
| 1896 | |
| 1897 | final int appId; |
| 1898 | |
| 1899 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(packageName); |
| 1900 | if (pkg == null || pkg.mExtras == null) { |
| 1901 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 1902 | } |
| 1903 | if (mPackageManagerInt.filterAppAccess(pkg, Binder.getCallingUid(), userId)) { |
| 1904 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 1905 | } |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 1906 | final BasePermission bp = mSettings.getPermissionLocked(permName); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1907 | if (bp == null) { |
| 1908 | throw new IllegalArgumentException("Unknown permission: " + permName); |
| 1909 | } |
| 1910 | |
| 1911 | bp.enforceDeclaredUsedAndRuntimeOrDevelopment(pkg); |
| 1912 | |
| 1913 | // If a permission review is required for legacy apps we represent |
| 1914 | // their permissions as always granted runtime ones since we need |
| 1915 | // to keep the review required permission flag per user while an |
| 1916 | // install permission's state is shared across all users. |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 1917 | if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.M |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1918 | && bp.isRuntime()) { |
| 1919 | return; |
| 1920 | } |
| 1921 | |
| 1922 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 1923 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 1924 | |
| 1925 | final int flags = permissionsState.getPermissionFlags(permName, userId); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 1926 | // Only the system may revoke SYSTEM_FIXED permissions. |
| 1927 | if ((flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0 |
| 1928 | && UserHandle.getCallingAppId() != Process.SYSTEM_UID) { |
| 1929 | throw new SecurityException("Non-System UID cannot revoke system fixed permission " |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1930 | + permName + " for package " + packageName); |
| 1931 | } |
| 1932 | if (!overridePolicy && (flags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0) { |
| 1933 | throw new SecurityException("Cannot revoke policy fixed permission " |
| 1934 | + permName + " for package " + packageName); |
| 1935 | } |
| 1936 | |
| 1937 | if (bp.isDevelopment()) { |
| 1938 | // Development permissions must be handled specially, since they are not |
| 1939 | // normal runtime permissions. For now they apply to all users. |
| 1940 | if (permissionsState.revokeInstallPermission(bp) != |
| 1941 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 1942 | if (callback != null) { |
| 1943 | callback.onInstallPermissionRevoked(); |
| 1944 | } |
| 1945 | } |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | if (permissionsState.revokeRuntimePermission(bp, userId) == |
| 1950 | PermissionsState.PERMISSION_OPERATION_FAILURE) { |
| 1951 | return; |
| 1952 | } |
| 1953 | |
| 1954 | if (bp.isRuntime()) { |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 1955 | logPermission(MetricsEvent.ACTION_PERMISSION_REVOKED, permName, packageName); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | if (callback != null) { |
| 1959 | final int uid = UserHandle.getUid(userId, pkg.applicationInfo.uid); |
| 1960 | callback.onPermissionRevoked(pkg.applicationInfo.uid, userId); |
| 1961 | } |
| 1962 | } |
| 1963 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1964 | @GuardedBy("mLock") |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1965 | private int[] revokeUnusedSharedUserPermissionsLocked( |
| 1966 | SharedUserSetting suSetting, int[] allUserIds) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1967 | // Collect all used permissions in the UID |
| 1968 | final ArraySet<String> usedPermissions = new ArraySet<>(); |
| 1969 | final List<PackageParser.Package> pkgList = suSetting.getPackages(); |
| 1970 | if (pkgList == null || pkgList.size() == 0) { |
| 1971 | return EmptyArray.INT; |
| 1972 | } |
| 1973 | for (PackageParser.Package pkg : pkgList) { |
Svet Ganov | d830807 | 2018-03-24 00:04:38 -0700 | [diff] [blame] | 1974 | if (pkg.requestedPermissions == null) { |
| 1975 | continue; |
| 1976 | } |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 1977 | final int requestedPermCount = pkg.requestedPermissions.size(); |
| 1978 | for (int j = 0; j < requestedPermCount; j++) { |
| 1979 | String permission = pkg.requestedPermissions.get(j); |
| 1980 | BasePermission bp = mSettings.getPermissionLocked(permission); |
| 1981 | if (bp != null) { |
| 1982 | usedPermissions.add(permission); |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | PermissionsState permissionsState = suSetting.getPermissionsState(); |
| 1988 | // Prune install permissions |
| 1989 | List<PermissionState> installPermStates = permissionsState.getInstallPermissionStates(); |
| 1990 | final int installPermCount = installPermStates.size(); |
| 1991 | for (int i = installPermCount - 1; i >= 0; i--) { |
| 1992 | PermissionState permissionState = installPermStates.get(i); |
| 1993 | if (!usedPermissions.contains(permissionState.getName())) { |
| 1994 | BasePermission bp = mSettings.getPermissionLocked(permissionState.getName()); |
| 1995 | if (bp != null) { |
| 1996 | permissionsState.revokeInstallPermission(bp); |
| 1997 | permissionsState.updatePermissionFlags(bp, UserHandle.USER_ALL, |
| 1998 | PackageManager.MASK_PERMISSION_FLAGS, 0); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | int[] runtimePermissionChangedUserIds = EmptyArray.INT; |
| 2004 | |
| 2005 | // Prune runtime permissions |
| 2006 | for (int userId : allUserIds) { |
| 2007 | List<PermissionState> runtimePermStates = permissionsState |
| 2008 | .getRuntimePermissionStates(userId); |
| 2009 | final int runtimePermCount = runtimePermStates.size(); |
| 2010 | for (int i = runtimePermCount - 1; i >= 0; i--) { |
| 2011 | PermissionState permissionState = runtimePermStates.get(i); |
| 2012 | if (!usedPermissions.contains(permissionState.getName())) { |
| 2013 | BasePermission bp = mSettings.getPermissionLocked(permissionState.getName()); |
| 2014 | if (bp != null) { |
| 2015 | permissionsState.revokeRuntimePermission(bp, userId); |
| 2016 | permissionsState.updatePermissionFlags(bp, userId, |
| 2017 | PackageManager.MASK_PERMISSION_FLAGS, 0); |
| 2018 | runtimePermissionChangedUserIds = ArrayUtils.appendInt( |
| 2019 | runtimePermissionChangedUserIds, userId); |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | return runtimePermissionChangedUserIds; |
| 2026 | } |
| 2027 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2028 | private String[] getAppOpPermissionPackages(String permName) { |
| 2029 | if (mPackageManagerInt.getInstantAppPackageName(Binder.getCallingUid()) != null) { |
| 2030 | return null; |
| 2031 | } |
| 2032 | synchronized (mLock) { |
| 2033 | final ArraySet<String> pkgs = mSettings.mAppOpPermissionPackages.get(permName); |
| 2034 | if (pkgs == null) { |
| 2035 | return null; |
| 2036 | } |
| 2037 | return pkgs.toArray(new String[pkgs.size()]); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | private int getPermissionFlags( |
| 2042 | String permName, String packageName, int callingUid, int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2043 | if (!mUserManagerInt.exists(userId)) { |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | enforceGrantRevokeRuntimePermissionPermissions("getPermissionFlags"); |
| 2048 | |
| 2049 | enforceCrossUserPermission(callingUid, userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2050 | true, // requireFullPermission |
| 2051 | false, // checkShell |
| 2052 | false, // requirePermissionWhenSameUser |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2053 | "getPermissionFlags"); |
| 2054 | |
| 2055 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(packageName); |
| 2056 | if (pkg == null || pkg.mExtras == null) { |
| 2057 | return 0; |
| 2058 | } |
| 2059 | synchronized (mLock) { |
| 2060 | if (mSettings.getPermissionLocked(permName) == null) { |
| 2061 | return 0; |
| 2062 | } |
| 2063 | } |
| 2064 | if (mPackageManagerInt.filterAppAccess(pkg, callingUid, userId)) { |
| 2065 | return 0; |
| 2066 | } |
| 2067 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2068 | PermissionsState permissionsState = ps.getPermissionsState(); |
| 2069 | return permissionsState.getPermissionFlags(permName, userId); |
| 2070 | } |
| 2071 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2072 | private static final int UPDATE_PERMISSIONS_ALL = 1<<0; |
| 2073 | private static final int UPDATE_PERMISSIONS_REPLACE_PKG = 1<<1; |
| 2074 | private static final int UPDATE_PERMISSIONS_REPLACE_ALL = 1<<2; |
| 2075 | |
| 2076 | private void updatePermissions(String packageName, PackageParser.Package pkg, |
| 2077 | boolean replaceGrant, Collection<PackageParser.Package> allPackages, |
| 2078 | PermissionCallback callback) { |
| 2079 | final int flags = (pkg != null ? UPDATE_PERMISSIONS_ALL : 0) | |
| 2080 | (replaceGrant ? UPDATE_PERMISSIONS_REPLACE_PKG : 0); |
| 2081 | updatePermissions( |
| 2082 | packageName, pkg, getVolumeUuidForPackage(pkg), flags, allPackages, callback); |
| 2083 | if (pkg != null && pkg.childPackages != null) { |
| 2084 | for (PackageParser.Package childPkg : pkg.childPackages) { |
| 2085 | updatePermissions(childPkg.packageName, childPkg, |
| 2086 | getVolumeUuidForPackage(childPkg), flags, allPackages, callback); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | private void updateAllPermissions(String volumeUuid, boolean sdkUpdated, |
| 2092 | Collection<PackageParser.Package> allPackages, PermissionCallback callback) { |
| 2093 | final int flags = UPDATE_PERMISSIONS_ALL | |
| 2094 | (sdkUpdated |
| 2095 | ? UPDATE_PERMISSIONS_REPLACE_PKG | UPDATE_PERMISSIONS_REPLACE_ALL |
| 2096 | : 0); |
| 2097 | updatePermissions(null, null, volumeUuid, flags, allPackages, callback); |
| 2098 | } |
| 2099 | |
| 2100 | private void updatePermissions(String changingPkgName, PackageParser.Package changingPkg, |
| 2101 | String replaceVolumeUuid, int flags, Collection<PackageParser.Package> allPackages, |
| 2102 | PermissionCallback callback) { |
| 2103 | // TODO: Most of the methods exposing BasePermission internals [source package name, |
| 2104 | // etc..] shouldn't be needed. Instead, when we've parsed a permission that doesn't |
| 2105 | // have package settings, we should make note of it elsewhere [map between |
| 2106 | // source package name and BasePermission] and cycle through that here. Then we |
| 2107 | // define a single method on BasePermission that takes a PackageSetting, changing |
| 2108 | // package name and a package. |
| 2109 | // NOTE: With this approach, we also don't need to tree trees differently than |
| 2110 | // normal permissions. Today, we need two separate loops because these BasePermission |
| 2111 | // objects are stored separately. |
| 2112 | // Make sure there are no dangling permission trees. |
| 2113 | flags = updatePermissionTrees(changingPkgName, changingPkg, flags); |
| 2114 | |
| 2115 | // Make sure all dynamic permissions have been assigned to a package, |
| 2116 | // and make sure there are no dangling permissions. |
| 2117 | flags = updatePermissions(changingPkgName, changingPkg, flags); |
| 2118 | |
Philip P. Moltmann | e1b277a | 2018-11-01 16:22:50 -0700 | [diff] [blame] | 2119 | synchronized (mLock) { |
| 2120 | if (mBackgroundPermissions == null) { |
| 2121 | // Cache background -> foreground permission mapping. |
| 2122 | // Only system declares background permissions, hence mapping does never change. |
| 2123 | mBackgroundPermissions = new ArrayMap<>(); |
| 2124 | for (BasePermission bp : mSettings.getAllPermissionsLocked()) { |
| 2125 | if (bp.perm.info != null && bp.perm.info.backgroundPermission != null) { |
| 2126 | String fgPerm = bp.name; |
| 2127 | String bgPerm = bp.perm.info.backgroundPermission; |
| 2128 | |
| 2129 | List<String> fgPerms = mBackgroundPermissions.get(bgPerm); |
| 2130 | if (fgPerms == null) { |
| 2131 | fgPerms = new ArrayList<>(); |
| 2132 | mBackgroundPermissions.put(bgPerm, fgPerms); |
| 2133 | } |
| 2134 | |
| 2135 | fgPerms.add(fgPerm); |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
Philip P. Moltmann | e0f00ea | 2018-10-30 10:43:15 -0700 | [diff] [blame] | 2141 | Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "restorePermissionState"); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2142 | // Now update the permissions for all packages, in particular |
| 2143 | // replace the granted permissions of the system packages. |
| 2144 | if ((flags & UPDATE_PERMISSIONS_ALL) != 0) { |
| 2145 | for (PackageParser.Package pkg : allPackages) { |
| 2146 | if (pkg != changingPkg) { |
| 2147 | // Only replace for packages on requested volume |
| 2148 | final String volumeUuid = getVolumeUuidForPackage(pkg); |
| 2149 | final boolean replace = ((flags & UPDATE_PERMISSIONS_REPLACE_ALL) != 0) |
| 2150 | && Objects.equals(replaceVolumeUuid, volumeUuid); |
Philip P. Moltmann | e0f00ea | 2018-10-30 10:43:15 -0700 | [diff] [blame] | 2151 | restorePermissionState(pkg, replace, changingPkgName, callback); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | if (changingPkg != null) { |
| 2157 | // Only replace for packages on requested volume |
| 2158 | final String volumeUuid = getVolumeUuidForPackage(changingPkg); |
| 2159 | final boolean replace = ((flags & UPDATE_PERMISSIONS_REPLACE_PKG) != 0) |
| 2160 | && Objects.equals(replaceVolumeUuid, volumeUuid); |
Philip P. Moltmann | e0f00ea | 2018-10-30 10:43:15 -0700 | [diff] [blame] | 2161 | restorePermissionState(changingPkg, replace, changingPkgName, callback); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2162 | } |
| 2163 | Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER); |
| 2164 | } |
| 2165 | |
| 2166 | private int updatePermissions(String packageName, PackageParser.Package pkg, int flags) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2167 | Set<BasePermission> needsUpdate = null; |
| 2168 | synchronized (mLock) { |
| 2169 | final Iterator<BasePermission> it = mSettings.mPermissions.values().iterator(); |
| 2170 | while (it.hasNext()) { |
| 2171 | final BasePermission bp = it.next(); |
| 2172 | if (bp.isDynamic()) { |
| 2173 | bp.updateDynamicPermission(mSettings.mPermissionTrees.values()); |
| 2174 | } |
| 2175 | if (bp.getSourcePackageSetting() != null) { |
| 2176 | if (packageName != null && packageName.equals(bp.getSourcePackageName()) |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2177 | && (pkg == null || !hasPermission(pkg, bp.getName()))) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2178 | Slog.i(TAG, "Removing old permission tree: " + bp.getName() |
| 2179 | + " from package " + bp.getSourcePackageName()); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2180 | flags |= UPDATE_PERMISSIONS_ALL; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2181 | it.remove(); |
| 2182 | } |
| 2183 | continue; |
| 2184 | } |
| 2185 | if (needsUpdate == null) { |
| 2186 | needsUpdate = new ArraySet<>(mSettings.mPermissions.size()); |
| 2187 | } |
| 2188 | needsUpdate.add(bp); |
| 2189 | } |
| 2190 | } |
| 2191 | if (needsUpdate != null) { |
| 2192 | for (final BasePermission bp : needsUpdate) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2193 | final PackageParser.Package sourcePkg = |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2194 | mPackageManagerInt.getPackage(bp.getSourcePackageName()); |
| 2195 | synchronized (mLock) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2196 | if (sourcePkg != null && sourcePkg.mExtras != null) { |
| 2197 | final PackageSetting sourcePs = (PackageSetting) sourcePkg.mExtras; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2198 | if (bp.getSourcePackageSetting() == null) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2199 | bp.setSourcePackageSetting(sourcePs); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2200 | } |
| 2201 | continue; |
| 2202 | } |
| 2203 | Slog.w(TAG, "Removing dangling permission: " + bp.getName() |
| 2204 | + " from package " + bp.getSourcePackageName()); |
| 2205 | mSettings.removePermissionLocked(bp.getName()); |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | return flags; |
| 2210 | } |
| 2211 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2212 | private int updatePermissionTrees(String packageName, PackageParser.Package pkg, |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2213 | int flags) { |
| 2214 | Set<BasePermission> needsUpdate = null; |
| 2215 | synchronized (mLock) { |
| 2216 | final Iterator<BasePermission> it = mSettings.mPermissionTrees.values().iterator(); |
| 2217 | while (it.hasNext()) { |
| 2218 | final BasePermission bp = it.next(); |
| 2219 | if (bp.getSourcePackageSetting() != null) { |
| 2220 | if (packageName != null && packageName.equals(bp.getSourcePackageName()) |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2221 | && (pkg == null || !hasPermission(pkg, bp.getName()))) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2222 | Slog.i(TAG, "Removing old permission tree: " + bp.getName() |
| 2223 | + " from package " + bp.getSourcePackageName()); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2224 | flags |= UPDATE_PERMISSIONS_ALL; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2225 | it.remove(); |
| 2226 | } |
| 2227 | continue; |
| 2228 | } |
| 2229 | if (needsUpdate == null) { |
| 2230 | needsUpdate = new ArraySet<>(mSettings.mPermissionTrees.size()); |
| 2231 | } |
| 2232 | needsUpdate.add(bp); |
| 2233 | } |
| 2234 | } |
| 2235 | if (needsUpdate != null) { |
| 2236 | for (final BasePermission bp : needsUpdate) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2237 | final PackageParser.Package sourcePkg = |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2238 | mPackageManagerInt.getPackage(bp.getSourcePackageName()); |
| 2239 | synchronized (mLock) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2240 | if (sourcePkg != null && sourcePkg.mExtras != null) { |
| 2241 | final PackageSetting sourcePs = (PackageSetting) sourcePkg.mExtras; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2242 | if (bp.getSourcePackageSetting() == null) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2243 | bp.setSourcePackageSetting(sourcePs); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2244 | } |
| 2245 | continue; |
| 2246 | } |
| 2247 | Slog.w(TAG, "Removing dangling permission tree: " + bp.getName() |
| 2248 | + " from package " + bp.getSourcePackageName()); |
| 2249 | mSettings.removePermissionLocked(bp.getName()); |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | return flags; |
| 2254 | } |
| 2255 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2256 | private void updatePermissionFlags(String permName, String packageName, int flagMask, |
| 2257 | int flagValues, int callingUid, int userId, PermissionCallback callback) { |
| 2258 | if (!mUserManagerInt.exists(userId)) { |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | enforceGrantRevokeRuntimePermissionPermissions("updatePermissionFlags"); |
| 2263 | |
| 2264 | enforceCrossUserPermission(callingUid, userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2265 | true, // requireFullPermission |
| 2266 | true, // checkShell |
| 2267 | false, // requirePermissionWhenSameUser |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2268 | "updatePermissionFlags"); |
| 2269 | |
| 2270 | // Only the system can change these flags and nothing else. |
| 2271 | if (callingUid != Process.SYSTEM_UID) { |
| 2272 | flagMask &= ~PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 2273 | flagValues &= ~PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 2274 | flagMask &= ~PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
| 2275 | flagValues &= ~PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
| 2276 | flagValues &= ~PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED; |
| 2277 | } |
| 2278 | |
| 2279 | final PackageParser.Package pkg = mPackageManagerInt.getPackage(packageName); |
| 2280 | if (pkg == null || pkg.mExtras == null) { |
| 2281 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2282 | } |
| 2283 | if (mPackageManagerInt.filterAppAccess(pkg, callingUid, userId)) { |
| 2284 | throw new IllegalArgumentException("Unknown package: " + packageName); |
| 2285 | } |
| 2286 | |
| 2287 | final BasePermission bp; |
| 2288 | synchronized (mLock) { |
| 2289 | bp = mSettings.getPermissionLocked(permName); |
| 2290 | } |
| 2291 | if (bp == null) { |
| 2292 | throw new IllegalArgumentException("Unknown permission: " + permName); |
| 2293 | } |
| 2294 | |
| 2295 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2296 | final PermissionsState permissionsState = ps.getPermissionsState(); |
| 2297 | final boolean hadState = |
| 2298 | permissionsState.getRuntimePermissionState(permName, userId) != null; |
| 2299 | final boolean permissionUpdated = |
| 2300 | permissionsState.updatePermissionFlags(bp, userId, flagMask, flagValues); |
| 2301 | if (permissionUpdated && callback != null) { |
| 2302 | // Install and runtime permissions are stored in different places, |
| 2303 | // so figure out what permission changed and persist the change. |
| 2304 | if (permissionsState.getInstallPermissionState(permName) != null) { |
| 2305 | callback.onInstallPermissionUpdated(); |
| 2306 | } else if (permissionsState.getRuntimePermissionState(permName, userId) != null |
| 2307 | || hadState) { |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2308 | callback.onPermissionUpdated(new int[] { userId }, false); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | private boolean updatePermissionFlagsForAllApps(int flagMask, int flagValues, int callingUid, |
| 2314 | int userId, Collection<Package> packages, PermissionCallback callback) { |
| 2315 | if (!mUserManagerInt.exists(userId)) { |
| 2316 | return false; |
| 2317 | } |
| 2318 | |
| 2319 | enforceGrantRevokeRuntimePermissionPermissions( |
| 2320 | "updatePermissionFlagsForAllApps"); |
| 2321 | enforceCrossUserPermission(callingUid, userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2322 | true, // requireFullPermission |
| 2323 | true, // checkShell |
| 2324 | false, // requirePermissionWhenSameUser |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2325 | "updatePermissionFlagsForAllApps"); |
| 2326 | |
| 2327 | // Only the system can change system fixed flags. |
| 2328 | if (callingUid != Process.SYSTEM_UID) { |
| 2329 | flagMask &= ~PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 2330 | flagValues &= ~PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 2331 | } |
| 2332 | |
| 2333 | boolean changed = false; |
| 2334 | for (PackageParser.Package pkg : packages) { |
| 2335 | final PackageSetting ps = (PackageSetting) pkg.mExtras; |
| 2336 | if (ps == null) { |
| 2337 | continue; |
| 2338 | } |
| 2339 | PermissionsState permissionsState = ps.getPermissionsState(); |
| 2340 | changed |= permissionsState.updatePermissionFlagsForAllPermissions( |
| 2341 | userId, flagMask, flagValues); |
| 2342 | } |
| 2343 | return changed; |
| 2344 | } |
| 2345 | |
| 2346 | private void enforceGrantRevokeRuntimePermissionPermissions(String message) { |
| 2347 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.GRANT_RUNTIME_PERMISSIONS) |
| 2348 | != PackageManager.PERMISSION_GRANTED |
| 2349 | && mContext.checkCallingOrSelfPermission(Manifest.permission.REVOKE_RUNTIME_PERMISSIONS) |
| 2350 | != PackageManager.PERMISSION_GRANTED) { |
| 2351 | throw new SecurityException(message + " requires " |
| 2352 | + Manifest.permission.GRANT_RUNTIME_PERMISSIONS + " or " |
| 2353 | + Manifest.permission.REVOKE_RUNTIME_PERMISSIONS); |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | /** |
| 2358 | * Checks if the request is from the system or an app that has INTERACT_ACROSS_USERS |
| 2359 | * or INTERACT_ACROSS_USERS_FULL permissions, if the userid is not for the caller. |
| 2360 | * @param checkShell whether to prevent shell from access if there's a debugging restriction |
| 2361 | * @param message the message to log on security exception |
| 2362 | */ |
| 2363 | private void enforceCrossUserPermission(int callingUid, int userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2364 | boolean requireFullPermission, boolean checkShell, |
| 2365 | boolean requirePermissionWhenSameUser, String message) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2366 | if (userId < 0) { |
| 2367 | throw new IllegalArgumentException("Invalid userId " + userId); |
| 2368 | } |
| 2369 | if (checkShell) { |
| 2370 | PackageManagerServiceUtils.enforceShellRestriction( |
| 2371 | UserManager.DISALLOW_DEBUGGING_FEATURES, callingUid, userId); |
| 2372 | } |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2373 | if (!requirePermissionWhenSameUser && userId == UserHandle.getUserId(callingUid)) return; |
Suprabh Shukla | 151b21b | 2018-04-27 19:30:30 -0700 | [diff] [blame] | 2374 | if (callingUid != Process.SYSTEM_UID && callingUid != Process.ROOT_UID) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2375 | if (requireFullPermission) { |
| 2376 | mContext.enforceCallingOrSelfPermission( |
| 2377 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, message); |
| 2378 | } else { |
| 2379 | try { |
| 2380 | mContext.enforceCallingOrSelfPermission( |
| 2381 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, message); |
| 2382 | } catch (SecurityException se) { |
| 2383 | mContext.enforceCallingOrSelfPermission( |
| 2384 | android.Manifest.permission.INTERACT_ACROSS_USERS, message); |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | |
Andreas Gampe | a71bee8 | 2018-07-20 12:55:36 -0700 | [diff] [blame] | 2390 | @GuardedBy({"mSettings.mLock", "mLock"}) |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2391 | private int calculateCurrentPermissionFootprintLocked(BasePermission tree) { |
| 2392 | int size = 0; |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2393 | for (BasePermission perm : mSettings.mPermissions.values()) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2394 | size += tree.calculateFootprint(perm); |
| 2395 | } |
| 2396 | return size; |
| 2397 | } |
| 2398 | |
Andreas Gampe | a71bee8 | 2018-07-20 12:55:36 -0700 | [diff] [blame] | 2399 | @GuardedBy({"mSettings.mLock", "mLock"}) |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2400 | private void enforcePermissionCapLocked(PermissionInfo info, BasePermission tree) { |
| 2401 | // We calculate the max size of permissions defined by this uid and throw |
| 2402 | // if that plus the size of 'info' would exceed our stated maximum. |
| 2403 | if (tree.getUid() != Process.SYSTEM_UID) { |
| 2404 | final int curTreeSize = calculateCurrentPermissionFootprintLocked(tree); |
| 2405 | if (curTreeSize + info.calculateFootprint() > MAX_PERMISSION_TREE_FOOTPRINT) { |
| 2406 | throw new SecurityException("Permission tree size cap exceeded"); |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2411 | private void systemReady() { |
| 2412 | mSystemReady = true; |
| 2413 | if (mPrivappPermissionsViolations != null) { |
| 2414 | throw new IllegalStateException("Signature|privileged permissions not in " |
| 2415 | + "privapp-permissions whitelist: " + mPrivappPermissionsViolations); |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | private static String getVolumeUuidForPackage(PackageParser.Package pkg) { |
| 2420 | if (pkg == null) { |
| 2421 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 2422 | } |
| 2423 | if (pkg.isExternal()) { |
| 2424 | if (TextUtils.isEmpty(pkg.volumeUuid)) { |
| 2425 | return StorageManager.UUID_PRIMARY_PHYSICAL; |
| 2426 | } else { |
| 2427 | return pkg.volumeUuid; |
| 2428 | } |
| 2429 | } else { |
| 2430 | return StorageManager.UUID_PRIVATE_INTERNAL; |
| 2431 | } |
| 2432 | } |
| 2433 | |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2434 | private static boolean hasPermission(PackageParser.Package pkgInfo, String permName) { |
| 2435 | for (int i=pkgInfo.permissions.size()-1; i>=0; i--) { |
| 2436 | if (pkgInfo.permissions.get(i).info.name.equals(permName)) { |
| 2437 | return true; |
| 2438 | } |
| 2439 | } |
| 2440 | return false; |
| 2441 | } |
| 2442 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2443 | /** |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 2444 | * Log that a permission request was granted/revoked. |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2445 | * |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 2446 | * @param action the action performed |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2447 | * @param name name of the permission |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 2448 | * @param packageName package permission is for |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2449 | */ |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 2450 | private void logPermission(int action, @NonNull String name, @NonNull String packageName) { |
| 2451 | final LogMaker log = new LogMaker(action); |
| 2452 | log.setPackageName(packageName); |
| 2453 | log.addTaggedData(MetricsEvent.FIELD_PERMISSION, name); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2454 | |
Philip P. Moltmann | 8cff8b9 | 2017-10-25 14:32:41 -0700 | [diff] [blame] | 2455 | mMetricsLogger.write(log); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Philip P. Moltmann | e1b277a | 2018-11-01 16:22:50 -0700 | [diff] [blame] | 2458 | /** |
| 2459 | * Get the mapping of background permissions to their foreground permissions. |
| 2460 | * |
| 2461 | * <p>Only initialized in the system server. |
| 2462 | * |
| 2463 | * @return the map <bg permission -> list<fg perm>> |
| 2464 | */ |
| 2465 | public @Nullable ArrayMap<String, List<String>> getBackgroundPermissions() { |
| 2466 | return mBackgroundPermissions; |
| 2467 | } |
| 2468 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2469 | private class PermissionManagerInternalImpl extends PermissionManagerInternal { |
| 2470 | @Override |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2471 | public void systemReady() { |
| 2472 | PermissionManagerService.this.systemReady(); |
| 2473 | } |
| 2474 | @Override |
| 2475 | public boolean isPermissionsReviewRequired(Package pkg, int userId) { |
| 2476 | return PermissionManagerService.this.isPermissionsReviewRequired(pkg, userId); |
| 2477 | } |
| 2478 | @Override |
Philip P. Moltmann | fae8a528 | 2018-04-10 12:15:32 -0700 | [diff] [blame] | 2479 | public void revokeRuntimePermissionsIfGroupChanged( |
| 2480 | @NonNull PackageParser.Package newPackage, |
| 2481 | @NonNull PackageParser.Package oldPackage, |
| 2482 | @NonNull ArrayList<String> allPackageNames, |
| 2483 | @NonNull PermissionCallback permissionCallback) { |
| 2484 | PermissionManagerService.this.revokeRuntimePermissionsIfGroupChanged(newPackage, |
| 2485 | oldPackage, allPackageNames, permissionCallback); |
| 2486 | } |
| 2487 | @Override |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2488 | public void addAllPermissions(Package pkg, boolean chatty) { |
| 2489 | PermissionManagerService.this.addAllPermissions(pkg, chatty); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2490 | } |
| 2491 | @Override |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 2492 | public void addAllPermissionGroups(Package pkg, boolean chatty) { |
| 2493 | PermissionManagerService.this.addAllPermissionGroups(pkg, chatty); |
| 2494 | } |
| 2495 | @Override |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 2496 | public void removeAllPermissions(Package pkg, boolean chatty) { |
| 2497 | PermissionManagerService.this.removeAllPermissions(pkg, chatty); |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2498 | } |
| 2499 | @Override |
| 2500 | public boolean addDynamicPermission(PermissionInfo info, boolean async, int callingUid, |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2501 | PermissionCallback callback) { |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2502 | return PermissionManagerService.this.addDynamicPermission(info, callingUid, callback); |
| 2503 | } |
| 2504 | @Override |
| 2505 | public void removeDynamicPermission(String permName, int callingUid, |
| 2506 | PermissionCallback callback) { |
| 2507 | PermissionManagerService.this.removeDynamicPermission(permName, callingUid, callback); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2508 | } |
| 2509 | @Override |
| 2510 | public void grantRuntimePermission(String permName, String packageName, |
| 2511 | boolean overridePolicy, int callingUid, int userId, |
| 2512 | PermissionCallback callback) { |
| 2513 | PermissionManagerService.this.grantRuntimePermission( |
| 2514 | permName, packageName, overridePolicy, callingUid, userId, callback); |
| 2515 | } |
| 2516 | @Override |
| 2517 | public void grantRequestedRuntimePermissions(PackageParser.Package pkg, int[] userIds, |
| 2518 | String[] grantedPermissions, int callingUid, PermissionCallback callback) { |
| 2519 | PermissionManagerService.this.grantRequestedRuntimePermissions( |
| 2520 | pkg, userIds, grantedPermissions, callingUid, callback); |
| 2521 | } |
| 2522 | @Override |
| 2523 | public void grantRuntimePermissionsGrantedToDisabledPackage(PackageParser.Package pkg, |
| 2524 | int callingUid, PermissionCallback callback) { |
| 2525 | PermissionManagerService.this.grantRuntimePermissionsGrantedToDisabledPackageLocked( |
| 2526 | pkg, callingUid, callback); |
| 2527 | } |
| 2528 | @Override |
| 2529 | public void revokeRuntimePermission(String permName, String packageName, |
| 2530 | boolean overridePolicy, int callingUid, int userId, |
| 2531 | PermissionCallback callback) { |
| 2532 | PermissionManagerService.this.revokeRuntimePermission(permName, packageName, |
Hongming Jin | ae750fb | 2018-09-27 23:00:20 +0000 | [diff] [blame] | 2533 | overridePolicy, callingUid, userId, callback); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2534 | } |
| 2535 | @Override |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 2536 | public void updatePermissions(String packageName, Package pkg, boolean replaceGrant, |
| 2537 | Collection<PackageParser.Package> allPackages, PermissionCallback callback) { |
| 2538 | PermissionManagerService.this.updatePermissions( |
| 2539 | packageName, pkg, replaceGrant, allPackages, callback); |
| 2540 | } |
| 2541 | @Override |
| 2542 | public void updateAllPermissions(String volumeUuid, boolean sdkUpdated, |
| 2543 | Collection<PackageParser.Package> allPackages, PermissionCallback callback) { |
| 2544 | PermissionManagerService.this.updateAllPermissions( |
| 2545 | volumeUuid, sdkUpdated, allPackages, callback); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2546 | } |
| 2547 | @Override |
Todd Kennedy | c842393 | 2017-10-05 08:58:36 -0700 | [diff] [blame] | 2548 | public String[] getAppOpPermissionPackages(String permName) { |
| 2549 | return PermissionManagerService.this.getAppOpPermissionPackages(permName); |
| 2550 | } |
| 2551 | @Override |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2552 | public int getPermissionFlags(String permName, String packageName, int callingUid, |
| 2553 | int userId) { |
| 2554 | return PermissionManagerService.this.getPermissionFlags(permName, packageName, |
| 2555 | callingUid, userId); |
| 2556 | } |
| 2557 | @Override |
| 2558 | public void updatePermissionFlags(String permName, String packageName, int flagMask, |
| 2559 | int flagValues, int callingUid, int userId, PermissionCallback callback) { |
| 2560 | PermissionManagerService.this.updatePermissionFlags( |
| 2561 | permName, packageName, flagMask, flagValues, callingUid, userId, callback); |
| 2562 | } |
| 2563 | @Override |
| 2564 | public boolean updatePermissionFlagsForAllApps(int flagMask, int flagValues, int callingUid, |
| 2565 | int userId, Collection<Package> packages, PermissionCallback callback) { |
| 2566 | return PermissionManagerService.this.updatePermissionFlagsForAllApps( |
| 2567 | flagMask, flagValues, callingUid, userId, packages, callback); |
| 2568 | } |
| 2569 | @Override |
| 2570 | public void enforceCrossUserPermission(int callingUid, int userId, |
| 2571 | boolean requireFullPermission, boolean checkShell, String message) { |
| 2572 | PermissionManagerService.this.enforceCrossUserPermission(callingUid, userId, |
Todd Kennedy | ef9acb6 | 2018-05-29 15:18:06 -0700 | [diff] [blame] | 2573 | requireFullPermission, checkShell, false, message); |
| 2574 | } |
| 2575 | @Override |
| 2576 | public void enforceCrossUserPermission(int callingUid, int userId, |
| 2577 | boolean requireFullPermission, boolean checkShell, |
| 2578 | boolean requirePermissionWhenSameUser, String message) { |
| 2579 | PermissionManagerService.this.enforceCrossUserPermission(callingUid, userId, |
| 2580 | requireFullPermission, checkShell, requirePermissionWhenSameUser, message); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2581 | } |
| 2582 | @Override |
| 2583 | public void enforceGrantRevokeRuntimePermissionPermissions(String message) { |
| 2584 | PermissionManagerService.this.enforceGrantRevokeRuntimePermissionPermissions(message); |
| 2585 | } |
| 2586 | @Override |
| 2587 | public int checkPermission(String permName, String packageName, int callingUid, |
| 2588 | int userId) { |
| 2589 | return PermissionManagerService.this.checkPermission( |
| 2590 | permName, packageName, callingUid, userId); |
| 2591 | } |
| 2592 | @Override |
Todd Kennedy | 3c71449 | 2017-10-27 09:12:50 -0700 | [diff] [blame] | 2593 | public int checkUidPermission(String permName, PackageParser.Package pkg, int uid, |
| 2594 | int callingUid) { |
| 2595 | return PermissionManagerService.this.checkUidPermission(permName, pkg, uid, callingUid); |
Todd Kennedy | 3bc9472 | 2017-10-10 09:55:53 -0700 | [diff] [blame] | 2596 | } |
| 2597 | @Override |
Todd Kennedy | 460f28c | 2017-10-06 13:46:22 -0700 | [diff] [blame] | 2598 | public PermissionGroupInfo getPermissionGroupInfo(String groupName, int flags, |
| 2599 | int callingUid) { |
| 2600 | return PermissionManagerService.this.getPermissionGroupInfo( |
| 2601 | groupName, flags, callingUid); |
| 2602 | } |
| 2603 | @Override |
| 2604 | public List<PermissionGroupInfo> getAllPermissionGroups(int flags, int callingUid) { |
| 2605 | return PermissionManagerService.this.getAllPermissionGroups(flags, callingUid); |
| 2606 | } |
| 2607 | @Override |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2608 | public PermissionInfo getPermissionInfo(String permName, String packageName, int flags, |
| 2609 | int callingUid) { |
| 2610 | return PermissionManagerService.this.getPermissionInfo( |
| 2611 | permName, packageName, flags, callingUid); |
| 2612 | } |
| 2613 | @Override |
| 2614 | public List<PermissionInfo> getPermissionInfoByGroup(String group, int flags, |
| 2615 | int callingUid) { |
| 2616 | return PermissionManagerService.this.getPermissionInfoByGroup(group, flags, callingUid); |
| 2617 | } |
| 2618 | @Override |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2619 | public PermissionSettings getPermissionSettings() { |
| 2620 | return mSettings; |
| 2621 | } |
| 2622 | @Override |
| 2623 | public DefaultPermissionGrantPolicy getDefaultPermissionGrantPolicy() { |
| 2624 | return mDefaultPermissionGrantPolicy; |
| 2625 | } |
| 2626 | @Override |
| 2627 | public BasePermission getPermissionTEMP(String permName) { |
| 2628 | synchronized (PermissionManagerService.this.mLock) { |
| 2629 | return mSettings.getPermissionLocked(permName); |
| 2630 | } |
| 2631 | } |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 2632 | } |
| 2633 | } |