Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.policy; |
| 18 | |
| 19 | import static android.Manifest.permission.READ_EXTERNAL_STORAGE; |
| 20 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 21 | import static android.app.AppOpsManager.OP_LEGACY_STORAGE; |
| 22 | import static android.app.AppOpsManager.OP_NONE; |
| 23 | import static android.content.pm.PackageManager.FLAG_PERMISSION_APPLY_RESTRICTION; |
Philip P. Moltmann | 8625cdd | 2019-05-30 08:27:19 -0700 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_INSTALLER_EXEMPT; |
| 25 | import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_SYSTEM_EXEMPT; |
| 26 | import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_UPGRADE_EXEMPT; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 27 | |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 28 | import static java.lang.Integer.min; |
| 29 | |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 30 | import android.annotation.NonNull; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 31 | import android.annotation.Nullable; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 32 | import android.app.AppOpsManager; |
| 33 | import android.content.Context; |
| 34 | import android.content.pm.ApplicationInfo; |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Philip P. Moltmann | 8625cdd | 2019-05-30 08:27:19 -0700 | [diff] [blame] | 36 | import android.os.Build; |
Philip P. Moltmann | 69b645f | 2019-06-17 14:28:11 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * The behavior of soft restricted permissions is different for each permission. This class collects |
| 41 | * the policies in one place. |
Philip P. Moltmann | f4de94f | 2019-05-30 11:14:33 -0700 | [diff] [blame] | 42 | * |
| 43 | * This is the twin of |
| 44 | * {@link com.android.packageinstaller.permission.utils.SoftRestrictedPermissionPolicy} |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 45 | */ |
| 46 | public abstract class SoftRestrictedPermissionPolicy { |
Philip P. Moltmann | 8625cdd | 2019-05-30 08:27:19 -0700 | [diff] [blame] | 47 | private static final int FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT = |
| 48 | FLAG_PERMISSION_RESTRICTION_SYSTEM_EXEMPT |
| 49 | | FLAG_PERMISSION_RESTRICTION_UPGRADE_EXEMPT |
| 50 | | FLAG_PERMISSION_RESTRICTION_INSTALLER_EXEMPT; |
| 51 | |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 52 | private static final SoftRestrictedPermissionPolicy DUMMY_POLICY = |
| 53 | new SoftRestrictedPermissionPolicy() { |
| 54 | @Override |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 55 | public boolean mayGrantPermission() { |
Philip P. Moltmann | 8625cdd | 2019-05-30 08:27:19 -0700 | [diff] [blame] | 56 | return true; |
| 57 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /** |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 61 | * TargetSDK is per package. To make sure two apps int the same shared UID do not fight over |
| 62 | * what to set, always compute the combined targetSDK. |
| 63 | * |
| 64 | * @param context A context |
| 65 | * @param appInfo The app that is changed |
| 66 | * @param user The user the app belongs to |
| 67 | * |
| 68 | * @return The minimum targetSDK of all apps sharing the uid of the app |
| 69 | */ |
| 70 | private static int getMinimumTargetSDK(@NonNull Context context, |
| 71 | @NonNull ApplicationInfo appInfo, @NonNull UserHandle user) { |
| 72 | PackageManager pm = context.getPackageManager(); |
| 73 | |
| 74 | int minimumTargetSDK = appInfo.targetSdkVersion; |
| 75 | |
| 76 | String[] uidPkgs = pm.getPackagesForUid(appInfo.uid); |
| 77 | if (uidPkgs != null) { |
| 78 | for (String uidPkg : uidPkgs) { |
| 79 | if (!uidPkg.equals(appInfo.packageName)) { |
| 80 | ApplicationInfo uidPkgInfo; |
| 81 | try { |
| 82 | uidPkgInfo = pm.getApplicationInfoAsUser(uidPkg, 0, user); |
| 83 | } catch (PackageManager.NameNotFoundException e) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | minimumTargetSDK = min(minimumTargetSDK, uidPkgInfo.targetSdkVersion); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return minimumTargetSDK; |
| 93 | } |
| 94 | |
| 95 | /** |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 96 | * Get the policy for a soft restricted permission. |
| 97 | * |
| 98 | * @param context A context to use |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 99 | * @param appInfo The application the permission belongs to. |
| 100 | * @param user The user the app belongs to. |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 101 | * @param permission The name of the permission |
| 102 | * |
| 103 | * @return The policy for this permission |
| 104 | */ |
| 105 | public static @NonNull SoftRestrictedPermissionPolicy forPermission(@NonNull Context context, |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 106 | @Nullable ApplicationInfo appInfo, @Nullable UserHandle user, |
Philip P. Moltmann | 69b645f | 2019-06-17 14:28:11 -0700 | [diff] [blame] | 107 | @NonNull String permission) { |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 108 | switch (permission) { |
| 109 | // Storage uses a special app op to decide the mount state and supports soft restriction |
| 110 | // where the restricted state allows the permission but only for accessing the medial |
| 111 | // collections. |
Philip P. Moltmann | 64f20dd | 2019-06-25 08:41:51 -0700 | [diff] [blame] | 112 | case READ_EXTERNAL_STORAGE: { |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 113 | final boolean isWhiteListed; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 114 | boolean shouldApplyRestriction; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 115 | final int targetSDK; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 116 | final boolean hasRequestedLegacyExternalStorage; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 117 | |
| 118 | if (appInfo != null) { |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 119 | PackageManager pm = context.getPackageManager(); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 120 | int flags = pm.getPermissionFlags(permission, appInfo.packageName, user); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 121 | isWhiteListed = (flags & FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT) != 0; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 122 | shouldApplyRestriction = (flags & FLAG_PERMISSION_APPLY_RESTRICTION) != 0; |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 123 | targetSDK = getMinimumTargetSDK(context, appInfo, user); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 124 | hasRequestedLegacyExternalStorage = hasUidRequestedLegacyExternalStorage( |
| 125 | appInfo.uid, context); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 126 | } else { |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 127 | isWhiteListed = false; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 128 | shouldApplyRestriction = false; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 129 | targetSDK = 0; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 130 | hasRequestedLegacyExternalStorage = false; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 131 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 132 | |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 133 | // We have a check in PermissionPolicyService.PermissionToOpSynchroniser.setUidMode |
| 134 | // to prevent apps losing files in legacy storage, because we are holding the |
| 135 | // package manager lock here. If we ever remove this policy that check should be |
| 136 | // removed as well. |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 137 | return new SoftRestrictedPermissionPolicy() { |
| 138 | @Override |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 139 | public boolean mayGrantPermission() { |
| 140 | return isWhiteListed || targetSDK >= Build.VERSION_CODES.Q; |
| 141 | } |
| 142 | @Override |
| 143 | public int getExtraAppOpCode() { |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 144 | return OP_LEGACY_STORAGE; |
| 145 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 146 | @Override |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 147 | public boolean mayAllowExtraAppOp() { |
Nandana Dutt | 13ecad4 | 2020-01-06 08:12:57 +0000 | [diff] [blame] | 148 | return !shouldApplyRestriction && hasRequestedLegacyExternalStorage |
| 149 | && targetSDK <= Build.VERSION_CODES.Q; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 150 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 151 | @Override |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 152 | public boolean mayDenyExtraAppOpIfGranted() { |
| 153 | return shouldApplyRestriction; |
Philip P. Moltmann | 8625cdd | 2019-05-30 08:27:19 -0700 | [diff] [blame] | 154 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 155 | }; |
| 156 | } |
Philip P. Moltmann | 64f20dd | 2019-06-25 08:41:51 -0700 | [diff] [blame] | 157 | case WRITE_EXTERNAL_STORAGE: { |
| 158 | final boolean isWhiteListed; |
| 159 | final int targetSDK; |
| 160 | |
| 161 | if (appInfo != null) { |
| 162 | final int flags = context.getPackageManager().getPermissionFlags(permission, |
| 163 | appInfo.packageName, user); |
| 164 | isWhiteListed = (flags & FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT) != 0; |
Philip P. Moltmann | f75bb77 | 2019-07-17 08:35:47 -0700 | [diff] [blame] | 165 | targetSDK = getMinimumTargetSDK(context, appInfo, user); |
Philip P. Moltmann | 64f20dd | 2019-06-25 08:41:51 -0700 | [diff] [blame] | 166 | } else { |
| 167 | isWhiteListed = false; |
| 168 | targetSDK = 0; |
| 169 | } |
| 170 | |
| 171 | return new SoftRestrictedPermissionPolicy() { |
| 172 | @Override |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 173 | public boolean mayGrantPermission() { |
Philip P. Moltmann | 64f20dd | 2019-06-25 08:41:51 -0700 | [diff] [blame] | 174 | return isWhiteListed || targetSDK >= Build.VERSION_CODES.Q; |
| 175 | } |
| 176 | }; |
| 177 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 178 | default: |
| 179 | return DUMMY_POLICY; |
| 180 | } |
| 181 | } |
| 182 | |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 183 | private static boolean hasUidRequestedLegacyExternalStorage(int uid, @NonNull Context context) { |
| 184 | PackageManager packageManager = context.getPackageManager(); |
| 185 | String[] packageNames = packageManager.getPackagesForUid(uid); |
| 186 | if (packageNames == null) { |
| 187 | return false; |
Winson | 243e7ea | 2019-08-07 10:51:25 -0700 | [diff] [blame] | 188 | } |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 189 | UserHandle user = UserHandle.getUserHandleForUid(uid); |
| 190 | for (String packageName : packageNames) { |
| 191 | ApplicationInfo applicationInfo; |
| 192 | try { |
| 193 | applicationInfo = packageManager.getApplicationInfoAsUser(packageName, 0, user); |
| 194 | } catch (PackageManager.NameNotFoundException e) { |
| 195 | continue; |
| 196 | } |
| 197 | if (applicationInfo.hasRequestedLegacyExternalStorage()) { |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | return false; |
Winson | 243e7ea | 2019-08-07 10:51:25 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 205 | * @return If the permission can be granted |
| 206 | */ |
| 207 | public abstract boolean mayGrantPermission(); |
| 208 | |
| 209 | /** |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 210 | * @return An app op to be changed based on the state of the permission or |
| 211 | * {@link AppOpsManager#OP_NONE} if not app-op should be set. |
| 212 | */ |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 213 | public int getExtraAppOpCode() { |
| 214 | return OP_NONE; |
| 215 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 216 | |
| 217 | /** |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 218 | * @return Whether the {@link #getExtraAppOpCode() app op} may be granted. |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 219 | */ |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 220 | public boolean mayAllowExtraAppOp() { |
| 221 | return false; |
| 222 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 223 | |
| 224 | /** |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 225 | * @return Whether the {@link #getExtraAppOpCode() app op} may be denied if was granted. |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 226 | */ |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 227 | public boolean mayDenyExtraAppOpIfGranted() { |
| 228 | return false; |
| 229 | } |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 230 | } |