Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -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 | |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 19 | import static android.app.AppOpsManager.MODE_ALLOWED; |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 20 | import static android.app.AppOpsManager.MODE_FOREGROUND; |
Philip P. Moltmann | faa788a | 2019-05-29 16:18:18 -0700 | [diff] [blame] | 21 | import static android.app.AppOpsManager.MODE_IGNORED; |
| 22 | import static android.app.AppOpsManager.OP_NONE; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.FLAG_PERMISSION_APPLY_RESTRICTION; |
Philip P. Moltmann | 4e369a7 | 2019-06-14 11:08:16 -0700 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED; |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.FLAG_PERMISSION_REVOKED_COMPAT; |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 26 | import static android.content.pm.PackageManager.GET_PERMISSIONS; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 27 | |
| 28 | import android.annotation.NonNull; |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 29 | import android.annotation.Nullable; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 30 | import android.annotation.UserIdInt; |
| 31 | import android.app.AppOpsManager; |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 32 | import android.app.AppOpsManagerInternal; |
Nate Myren | d15dcf9 | 2020-04-07 19:30:37 +0000 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 34 | import android.content.Context; |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 35 | import android.content.Intent; |
Eugene Susla | 4fce75d | 2019-12-10 09:51:58 -0800 | [diff] [blame] | 36 | import android.content.IntentFilter; |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
Svet Ganov | 38a0631 | 2019-04-09 23:55:05 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageInfo; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
Svet Ganov | 38a0631 | 2019-04-09 23:55:05 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageManager.NameNotFoundException; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageManagerInternal; |
| 42 | import android.content.pm.PackageManagerInternal.PackageListObserver; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 43 | import android.content.pm.PermissionInfo; |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 44 | import android.os.Build; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 45 | import android.os.Process; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 48 | import android.os.UserHandle; |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 49 | import android.os.UserManagerInternal; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 50 | import android.permission.PermissionControllerManager; |
Nate Myren | d15dcf9 | 2020-04-07 19:30:37 +0000 | [diff] [blame] | 51 | import android.provider.Settings; |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 52 | import android.provider.Telephony; |
Hai Zhang | 114afc3 | 2019-05-01 14:16:23 +0800 | [diff] [blame] | 53 | import android.telecom.TelecomManager; |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 54 | import android.util.ArrayMap; |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 55 | import android.util.ArraySet; |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 56 | import android.util.LongSparseLongArray; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 57 | import android.util.Pair; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 58 | import android.util.Slog; |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 59 | import android.util.SparseBooleanArray; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 60 | |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 61 | import com.android.internal.annotations.GuardedBy; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 62 | import com.android.internal.app.IAppOpsCallback; |
| 63 | import com.android.internal.app.IAppOpsService; |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 64 | import com.android.internal.infra.AndroidFuture; |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 65 | import com.android.internal.util.IntPair; |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 66 | import com.android.internal.util.function.pooled.PooledLambda; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 67 | import com.android.server.FgThread; |
| 68 | import com.android.server.LocalServices; |
| 69 | import com.android.server.SystemService; |
Winson | 5e0a1d5 | 2020-01-24 12:00:33 -0800 | [diff] [blame] | 70 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 71 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 72 | import com.android.server.policy.PermissionPolicyInternal.OnInitializedCallback; |
Eugene Susla | 2745554 | 2019-08-30 16:20:11 -0700 | [diff] [blame] | 73 | |
Svet Ganov | 38a0631 | 2019-04-09 23:55:05 -0700 | [diff] [blame] | 74 | import java.util.ArrayList; |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
Nate Myren | d15dcf9 | 2020-04-07 19:30:37 +0000 | [diff] [blame] | 76 | import java.util.HashMap; |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 77 | import java.util.List; |
Nate Myren | d15dcf9 | 2020-04-07 19:30:37 +0000 | [diff] [blame] | 78 | import java.util.Map; |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 79 | import java.util.concurrent.ExecutionException; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * This is a permission policy that governs over all permission mechanism |
| 83 | * such as permissions, app ops, etc. For example, the policy ensures that |
| 84 | * permission state and app ops is synchronized for cases where there is a |
| 85 | * dependency between permission state (permissions or permission flags) |
| 86 | * and app ops - and vise versa. |
| 87 | */ |
| 88 | public final class PermissionPolicyService extends SystemService { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 89 | private static final String LOG_TAG = PermissionPolicyService.class.getSimpleName(); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 90 | private static final boolean DEBUG = false; |
Nate Myren | 6b55e95 | 2020-05-14 13:48:44 -0700 | [diff] [blame] | 91 | private static final long USER_SENSITIVE_UPDATE_DELAY_MS = 10000; |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 92 | |
| 93 | private final Object mLock = new Object(); |
| 94 | |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 95 | private IAppOpsCallback mAppOpsCallback; |
| 96 | |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 97 | /** Whether the user is started but not yet stopped */ |
| 98 | @GuardedBy("mLock") |
| 99 | private final SparseBooleanArray mIsStarted = new SparseBooleanArray(); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 100 | |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 101 | /** Callbacks for when a user is initialized */ |
| 102 | @GuardedBy("mLock") |
| 103 | private OnInitializedCallback mOnInitializedCallback; |
| 104 | |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Whether an async {@link #synchronizePackagePermissionsAndAppOpsForUser} is currently |
| 107 | * scheduled for a package/user. |
| 108 | */ |
| 109 | @GuardedBy("mLock") |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 110 | private final ArraySet<Pair<String, Integer>> mIsPackageSyncsScheduled = new ArraySet<>(); |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 111 | |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Whether an async {@link #resetAppOpPermissionsIfNotRequestedForUid} is currently |
| 114 | * scheduled for a uid. |
| 115 | */ |
| 116 | @GuardedBy("mLock") |
| 117 | private final SparseBooleanArray mIsUidSyncScheduled = new SparseBooleanArray(); |
| 118 | |
| 119 | private List<String> mAppOpPermissions; |
| 120 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 121 | public PermissionPolicyService(@NonNull Context context) { |
| 122 | super(context); |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 123 | |
| 124 | LocalServices.addService(PermissionPolicyInternal.class, new Internal()); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onStart() { |
| 129 | final PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 130 | PackageManagerInternal.class); |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 131 | final PermissionManagerServiceInternal permissionManagerInternal = LocalServices.getService( |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 132 | PermissionManagerServiceInternal.class); |
| 133 | final IAppOpsService appOpsService = IAppOpsService.Stub.asInterface( |
| 134 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 135 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 136 | packageManagerInternal.getPackageList(new PackageListObserver() { |
| 137 | @Override |
| 138 | public void onPackageAdded(String packageName, int uid) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 139 | final int userId = UserHandle.getUserId(uid); |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 140 | if (isStarted(userId)) { |
| 141 | synchronizePackagePermissionsAndAppOpsForUser(packageName, userId); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 142 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 146 | public void onPackageChanged(String packageName, int uid) { |
| 147 | final int userId = UserHandle.getUserId(uid); |
| 148 | if (isStarted(userId)) { |
| 149 | synchronizePackagePermissionsAndAppOpsForUser(packageName, userId); |
| 150 | resetAppOpPermissionsIfNotRequestedForUid(uid); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 155 | public void onPackageRemoved(String packageName, int uid) { |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 156 | final int userId = UserHandle.getUserId(uid); |
| 157 | if (isStarted(userId)) { |
| 158 | resetAppOpPermissionsIfNotRequestedForUid(uid); |
| 159 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 160 | } |
| 161 | }); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 162 | |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 163 | permissionManagerInternal.addOnRuntimePermissionStateChangedListener( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 164 | this::synchronizePackagePermissionsAndAppOpsAsyncForUser); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 165 | |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 166 | mAppOpsCallback = new IAppOpsCallback.Stub() { |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 167 | public void opChanged(int op, int uid, String packageName) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 168 | synchronizePackagePermissionsAndAppOpsAsyncForUser(packageName, |
| 169 | UserHandle.getUserId(uid)); |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 170 | resetAppOpPermissionsIfNotRequestedForUidAsync(uid); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 171 | } |
| 172 | }; |
| 173 | |
| 174 | final ArrayList<PermissionInfo> dangerousPerms = |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 175 | permissionManagerInternal.getAllPermissionsWithProtection( |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 176 | PermissionInfo.PROTECTION_DANGEROUS); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 177 | try { |
| 178 | int numDangerousPerms = dangerousPerms.size(); |
| 179 | for (int i = 0; i < numDangerousPerms; i++) { |
| 180 | PermissionInfo perm = dangerousPerms.get(i); |
| 181 | |
Hai Zhang | ab6a682 | 2019-10-18 00:34:06 +0000 | [diff] [blame] | 182 | if (perm.isRuntime()) { |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 183 | appOpsService.startWatchingMode(getSwitchOp(perm.name), null, mAppOpsCallback); |
Hai Zhang | bcfb075 | 2019-10-07 14:52:30 -0700 | [diff] [blame] | 184 | } |
| 185 | if (perm.isSoftRestricted()) { |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 186 | SoftRestrictedPermissionPolicy policy = |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 187 | SoftRestrictedPermissionPolicy.forPermission(null, null, null, |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 188 | null, perm.name); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 189 | int extraAppOp = policy.getExtraAppOpCode(); |
| 190 | if (extraAppOp != OP_NONE) { |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 191 | appOpsService.startWatchingMode(extraAppOp, null, mAppOpsCallback); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | } catch (RemoteException doesNotHappen) { |
| 196 | Slog.wtf(LOG_TAG, "Cannot set up app-ops listener"); |
| 197 | } |
Eugene Susla | 4fce75d | 2019-12-10 09:51:58 -0800 | [diff] [blame] | 198 | |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 199 | final List<PermissionInfo> appOpPermissionInfos = |
| 200 | permissionManagerInternal.getAllPermissionsWithProtectionFlags( |
| 201 | PermissionInfo.PROTECTION_FLAG_APPOP); |
| 202 | mAppOpPermissions = new ArrayList<>(); |
| 203 | final int appOpPermissionInfosSize = appOpPermissionInfos.size(); |
| 204 | for (int i = 0; i < appOpPermissionInfosSize; i++) { |
| 205 | final PermissionInfo appOpPermissionInfo = appOpPermissionInfos.get(i); |
| 206 | |
Hai Zhang | 17ae954 | 2020-05-08 12:00:48 -0700 | [diff] [blame] | 207 | switch (appOpPermissionInfo.name) { |
| 208 | case android.Manifest.permission.ACCESS_NOTIFICATIONS: |
| 209 | case android.Manifest.permission.MANAGE_IPSEC_TUNNELS: |
| 210 | continue; |
Hai Zhang | 643f913 | 2020-05-28 14:12:20 -0700 | [diff] [blame] | 211 | case android.Manifest.permission.REQUEST_INSTALL_PACKAGES: |
| 212 | // Settings allows the user to control the app op if it's not in the default |
| 213 | // mode, regardless of whether the app has requested the permission, so we |
| 214 | // should not reset it. |
| 215 | continue; |
Hai Zhang | 17ae954 | 2020-05-08 12:00:48 -0700 | [diff] [blame] | 216 | default: |
| 217 | final int appOpCode = AppOpsManager.permissionToOpCode( |
| 218 | appOpPermissionInfo.name); |
| 219 | if (appOpCode != OP_NONE) { |
| 220 | mAppOpPermissions.add(appOpPermissionInfo.name); |
| 221 | try { |
| 222 | appOpsService.startWatchingMode(appOpCode, null, mAppOpsCallback); |
| 223 | } catch (RemoteException e) { |
| 224 | Slog.wtf(LOG_TAG, "Cannot set up app-ops listener", e); |
| 225 | } |
| 226 | } |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Eugene Susla | 4fce75d | 2019-12-10 09:51:58 -0800 | [diff] [blame] | 230 | IntentFilter intentFilter = new IntentFilter(); |
| 231 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 232 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 233 | intentFilter.addDataScheme("package"); |
| 234 | |
Eugene Susla | 4fce75d | 2019-12-10 09:51:58 -0800 | [diff] [blame] | 235 | getContext().registerReceiverAsUser(new BroadcastReceiver() { |
| 236 | final List<Integer> mUserSetupUids = new ArrayList<>(200); |
| 237 | final Map<UserHandle, PermissionControllerManager> mPermControllerManagers = |
| 238 | new HashMap<>(); |
| 239 | |
| 240 | @Override |
| 241 | public void onReceive(Context context, Intent intent) { |
| 242 | boolean hasSetupRun = true; |
| 243 | try { |
| 244 | hasSetupRun = Settings.Secure.getInt(getContext().getContentResolver(), |
| 245 | Settings.Secure.USER_SETUP_COMPLETE) != 0; |
| 246 | } catch (Settings.SettingNotFoundException e) { |
| 247 | // Ignore error, assume setup has run |
| 248 | } |
| 249 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 250 | // If there is no valid package for the given UID, return immediately |
| 251 | if (packageManagerInternal.getPackage(uid) == null) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (hasSetupRun) { |
| 256 | if (!mUserSetupUids.isEmpty()) { |
| 257 | synchronized (mUserSetupUids) { |
| 258 | for (int i = mUserSetupUids.size() - 1; i >= 0; i--) { |
| 259 | updateUid(mUserSetupUids.get(i)); |
| 260 | } |
| 261 | mUserSetupUids.clear(); |
| 262 | } |
| 263 | } |
| 264 | updateUid(uid); |
| 265 | } else { |
| 266 | synchronized (mUserSetupUids) { |
| 267 | if (!mUserSetupUids.contains(uid)) { |
| 268 | mUserSetupUids.add(uid); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private void updateUid(int uid) { |
| 275 | UserHandle user = UserHandle.getUserHandleForUid(uid); |
| 276 | PermissionControllerManager manager = mPermControllerManagers.get(user); |
| 277 | if (manager == null) { |
| 278 | manager = new PermissionControllerManager( |
| 279 | getUserContext(getContext(), user), FgThread.getHandler()); |
| 280 | mPermControllerManagers.put(user, manager); |
| 281 | } |
| 282 | manager.updateUserSensitiveForApp(uid); |
| 283 | } |
| 284 | }, UserHandle.ALL, intentFilter, null, null); |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Get op that controls the access related to the permission. |
| 289 | * |
| 290 | * <p>Usually the permission-op relationship is 1:1 but some permissions (e.g. fine location) |
| 291 | * {@link AppOpsManager#sOpToSwitch share an op} to control the access. |
| 292 | * |
| 293 | * @param permission The permission |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 294 | * @return The op that controls the access of the permission |
| 295 | */ |
| 296 | private static int getSwitchOp(@NonNull String permission) { |
| 297 | int op = AppOpsManager.permissionToOpCode(permission); |
| 298 | if (op == OP_NONE) { |
| 299 | return OP_NONE; |
| 300 | } |
| 301 | |
| 302 | return AppOpsManager.opToSwitch(op); |
| 303 | } |
| 304 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 305 | private void synchronizePackagePermissionsAndAppOpsAsyncForUser(@NonNull String packageName, |
| 306 | @UserIdInt int changedUserId) { |
| 307 | if (isStarted(changedUserId)) { |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 308 | synchronized (mLock) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 309 | if (mIsPackageSyncsScheduled.add(new Pair<>(packageName, changedUserId))) { |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 310 | FgThread.getHandler().sendMessage(PooledLambda.obtainMessage( |
| 311 | PermissionPolicyService |
| 312 | ::synchronizePackagePermissionsAndAppOpsForUser, |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 313 | this, packageName, changedUserId)); |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 314 | } else { |
| 315 | if (DEBUG) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 316 | Slog.v(LOG_TAG, "sync for " + packageName + "/" + changedUserId |
| 317 | + " already scheduled"); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 318 | } |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void onBootPhase(int phase) { |
| 326 | if (DEBUG) Slog.i(LOG_TAG, "onBootPhase(" + phase + ")"); |
| 327 | |
| 328 | if (phase == PHASE_ACTIVITY_MANAGER_READY) { |
| 329 | final UserManagerInternal um = LocalServices.getService(UserManagerInternal.class); |
| 330 | |
| 331 | // For some users we might not receive a onStartUser, hence force one here |
| 332 | for (int userId : um.getUserIds()) { |
| 333 | if (um.isUserRunning(userId)) { |
| 334 | onStartUser(userId); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @return Whether the user is started but not yet stopped |
| 342 | */ |
| 343 | private boolean isStarted(@UserIdInt int userId) { |
| 344 | synchronized (mLock) { |
| 345 | return mIsStarted.get(userId); |
| 346 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public void onStartUser(@UserIdInt int userId) { |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 351 | if (DEBUG) Slog.i(LOG_TAG, "onStartUser(" + userId + ")"); |
| 352 | |
| 353 | if (isStarted(userId)) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | grantOrUpgradeDefaultRuntimePermissionsIfNeeded(userId); |
| 358 | |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 359 | final OnInitializedCallback callback; |
| 360 | |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 361 | synchronized (mLock) { |
| 362 | mIsStarted.put(userId, true); |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 363 | callback = mOnInitializedCallback; |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Force synchronization as permissions might have changed |
| 367 | synchronizePermissionsAndAppOpsForUser(userId); |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 368 | |
| 369 | // Tell observers we are initialized for this user. |
| 370 | if (callback != null) { |
| 371 | callback.onInitialized(userId); |
| 372 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 375 | @Override |
| 376 | public void onStopUser(@UserIdInt int userId) { |
| 377 | if (DEBUG) Slog.i(LOG_TAG, "onStopUser(" + userId + ")"); |
| 378 | |
| 379 | synchronized (mLock) { |
| 380 | mIsStarted.delete(userId); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | private void grantOrUpgradeDefaultRuntimePermissionsIfNeeded(@UserIdInt int userId) { |
| 385 | if (DEBUG) Slog.i(LOG_TAG, "grantOrUpgradeDefaultPermsIfNeeded(" + userId + ")"); |
| 386 | |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 387 | final PackageManagerInternal packageManagerInternal = |
| 388 | LocalServices.getService(PackageManagerInternal.class); |
| 389 | final PermissionManagerServiceInternal permissionManagerInternal = |
| 390 | LocalServices.getService(PermissionManagerServiceInternal.class); |
Evan Severson | 0dc24cb | 2020-02-04 10:25:08 -0800 | [diff] [blame] | 391 | if (packageManagerInternal.isPermissionUpgradeNeeded(userId)) { |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 392 | if (DEBUG) Slog.i(LOG_TAG, "defaultPermsWereGrantedSinceBoot(" + userId + ")"); |
| 393 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 394 | // Now call into the permission controller to apply policy around permissions |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 395 | final AndroidFuture<Boolean> future = new AndroidFuture<>(); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 396 | |
| 397 | // We need to create a local manager that does not schedule work on the main |
| 398 | // there as we are on the main thread and want to block until the work is |
| 399 | // completed or we time out. |
| 400 | final PermissionControllerManager permissionControllerManager = |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 401 | new PermissionControllerManager( |
| 402 | getUserContext(getContext(), UserHandle.of(userId)), |
Philip P. Moltmann | 7870aa0 | 2019-05-10 15:01:32 -0700 | [diff] [blame] | 403 | FgThread.getHandler()); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 404 | permissionControllerManager.grantOrUpgradeDefaultRuntimePermissions( |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 405 | FgThread.getExecutor(), successful -> { |
| 406 | if (successful) { |
| 407 | future.complete(null); |
| 408 | } else { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 409 | // We are in an undefined state now, let us crash and have |
| 410 | // rescue party suggest a wipe to recover to a good one. |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 411 | final String message = "Error granting/upgrading runtime permissions" |
| 412 | + " for user " + userId; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 413 | Slog.wtf(LOG_TAG, message); |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 414 | future.completeExceptionally(new IllegalStateException(message)); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 415 | } |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 416 | }); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 417 | try { |
Hai Zhang | 72ddbff | 2020-01-09 17:35:07 -0800 | [diff] [blame] | 418 | future.get(); |
| 419 | } catch (InterruptedException | ExecutionException e) { |
| 420 | throw new IllegalStateException(e); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 421 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 422 | |
Nate Myren | 6b55e95 | 2020-05-14 13:48:44 -0700 | [diff] [blame] | 423 | FgThread.getHandler().postDelayed(permissionControllerManager::updateUserSensitive, |
| 424 | USER_SENSITIVE_UPDATE_DELAY_MS); |
Eugene Susla | 2745554 | 2019-08-30 16:20:11 -0700 | [diff] [blame] | 425 | |
Evan Severson | 0dc24cb | 2020-02-04 10:25:08 -0800 | [diff] [blame] | 426 | packageManagerInternal.updateRuntimePermissionsFingerprint(userId); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 427 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 430 | private static @Nullable Context getUserContext(@NonNull Context context, |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 431 | @Nullable UserHandle user) { |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 432 | if (context.getUser().equals(user)) { |
| 433 | return context; |
| 434 | } else { |
| 435 | try { |
| 436 | return context.createPackageContextAsUser(context.getPackageName(), 0, user); |
| 437 | } catch (NameNotFoundException e) { |
| 438 | Slog.e(LOG_TAG, "Cannot create context for user " + user, e); |
| 439 | return null; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Synchronize a single package. |
| 446 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 447 | private void synchronizePackagePermissionsAndAppOpsForUser(@NonNull String packageName, |
| 448 | @UserIdInt int userId) { |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 449 | synchronized (mLock) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 450 | mIsPackageSyncsScheduled.remove(new Pair<>(packageName, userId)); |
Philip P. Moltmann | 78c155b | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 453 | if (DEBUG) { |
| 454 | Slog.v(LOG_TAG, |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 455 | "synchronizePackagePermissionsAndAppOpsForUser(" + packageName + ", " |
| 456 | + userId + ")"); |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 459 | final PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 460 | PackageManagerInternal.class); |
| 461 | final PackageInfo pkg = packageManagerInternal.getPackageInfo(packageName, 0, |
| 462 | Process.SYSTEM_UID, userId); |
| 463 | if (pkg == null) { |
| 464 | return; |
| 465 | } |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 466 | final PermissionToOpSynchroniser synchroniser = new PermissionToOpSynchroniser( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 467 | getUserContext(getContext(), UserHandle.of(userId))); |
| 468 | synchroniser.addPackage(pkg.packageName); |
| 469 | final String[] sharedPkgNames = packageManagerInternal.getSharedUserPackagesForPackage( |
| 470 | pkg.packageName, userId); |
| 471 | |
| 472 | for (String sharedPkgName : sharedPkgNames) { |
| 473 | final AndroidPackage sharedPkg = packageManagerInternal |
| 474 | .getPackage(sharedPkgName); |
| 475 | if (sharedPkg != null) { |
| 476 | synchroniser.addPackage(sharedPkg.getPackageName()); |
| 477 | } |
| 478 | } |
| 479 | synchroniser.syncPackages(); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Synchronize all packages |
| 484 | */ |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 485 | private void synchronizePermissionsAndAppOpsForUser(@UserIdInt int userId) { |
| 486 | if (DEBUG) Slog.i(LOG_TAG, "synchronizePermissionsAndAppOpsForUser(" + userId + ")"); |
| 487 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 488 | final PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 489 | PackageManagerInternal.class); |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 490 | final PermissionToOpSynchroniser synchronizer = new PermissionToOpSynchroniser( |
Philip P. Moltmann | 5f5783e | 2019-05-22 14:57:18 -0700 | [diff] [blame] | 491 | getUserContext(getContext(), UserHandle.of(userId))); |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 492 | packageManagerInternal.forEachPackage( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 493 | (pkg) -> synchronizer.addPackage(pkg.getPackageName())); |
| 494 | synchronizer.syncPackages(); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 497 | private void resetAppOpPermissionsIfNotRequestedForUidAsync(int uid) { |
| 498 | if (isStarted(UserHandle.getUserId(uid))) { |
| 499 | synchronized (mLock) { |
| 500 | if (!mIsUidSyncScheduled.get(uid)) { |
| 501 | mIsUidSyncScheduled.put(uid, true); |
| 502 | FgThread.getHandler().sendMessage(PooledLambda.obtainMessage( |
| 503 | PermissionPolicyService::resetAppOpPermissionsIfNotRequestedForUid, |
| 504 | this, uid)); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | private void resetAppOpPermissionsIfNotRequestedForUid(int uid) { |
| 511 | synchronized (mLock) { |
| 512 | mIsUidSyncScheduled.delete(uid); |
| 513 | } |
| 514 | |
| 515 | final Context context = getContext(); |
| 516 | final PackageManager userPackageManager = getUserContext(context, |
| 517 | UserHandle.getUserHandleForUid(uid)).getPackageManager(); |
| 518 | final String[] packageNames = userPackageManager.getPackagesForUid(uid); |
| 519 | if (packageNames == null || packageNames.length == 0) { |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | final ArraySet<String> requestedPermissions = new ArraySet<>(); |
| 524 | for (String packageName : packageNames) { |
| 525 | final PackageInfo packageInfo; |
| 526 | try { |
| 527 | packageInfo = userPackageManager.getPackageInfo(packageName, GET_PERMISSIONS); |
| 528 | } catch (NameNotFoundException e) { |
| 529 | continue; |
| 530 | } |
| 531 | if (packageInfo == null || packageInfo.requestedPermissions == null) { |
| 532 | continue; |
| 533 | } |
| 534 | Collections.addAll(requestedPermissions, packageInfo.requestedPermissions); |
| 535 | } |
| 536 | |
| 537 | final AppOpsManager appOpsManager = context.getSystemService(AppOpsManager.class); |
| 538 | final AppOpsManagerInternal appOpsManagerInternal = LocalServices.getService( |
| 539 | AppOpsManagerInternal.class); |
| 540 | final int appOpPermissionsSize = mAppOpPermissions.size(); |
| 541 | for (int i = 0; i < appOpPermissionsSize; i++) { |
| 542 | final String appOpPermission = mAppOpPermissions.get(i); |
| 543 | |
| 544 | if (!requestedPermissions.contains(appOpPermission)) { |
| 545 | final int appOpCode = AppOpsManager.permissionToOpCode(appOpPermission); |
| 546 | final int defaultAppOpMode = AppOpsManager.opToDefaultMode(appOpCode); |
| 547 | for (String packageName : packageNames) { |
| 548 | final int appOpMode = appOpsManager.unsafeCheckOpRawNoThrow(appOpCode, uid, |
| 549 | packageName); |
| 550 | if (appOpMode != defaultAppOpMode) { |
| 551 | appOpsManagerInternal.setUidModeFromPermissionPolicy(appOpCode, uid, |
| 552 | defaultAppOpMode, mAppOpsCallback); |
| 553 | appOpsManagerInternal.setModeFromPermissionPolicy(appOpCode, uid, |
| 554 | packageName, defaultAppOpMode, mAppOpsCallback); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
Svet Ganov | 38a0631 | 2019-04-09 23:55:05 -0700 | [diff] [blame] | 561 | /** |
| 562 | * Synchronizes permission to app ops. You *must* always sync all packages |
| 563 | * in a shared UID at the same time to ensure proper synchronization. |
| 564 | */ |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 565 | private class PermissionToOpSynchroniser { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 566 | private final @NonNull Context mContext; |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 567 | private final @NonNull PackageManager mPackageManager; |
| 568 | private final @NonNull AppOpsManager mAppOpsManager; |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 569 | private final @NonNull AppOpsManagerInternal mAppOpsManagerInternal; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 570 | |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 571 | private final @NonNull ArrayMap<String, PermissionInfo> mRuntimePermissionInfos; |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
Philip P. Moltmann | 74065c8 | 2019-05-15 10:46:32 -0700 | [diff] [blame] | 574 | * All ops that need to be flipped to allow. |
| 575 | * |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 576 | * @see #syncPackages |
Philip P. Moltmann | 74065c8 | 2019-05-15 10:46:32 -0700 | [diff] [blame] | 577 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 578 | private final @NonNull ArrayList<OpToChange> mOpsToAllow = new ArrayList<>(); |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 579 | |
| 580 | /** |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 581 | * All ops that need to be flipped to ignore. |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 582 | * |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 583 | * @see #syncPackages |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 584 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 585 | private final @NonNull ArrayList<OpToChange> mOpsToIgnore = new ArrayList<>(); |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 586 | |
| 587 | /** |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 588 | * All ops that need to be flipped to ignore if not allowed. |
| 589 | * |
| 590 | * Currently, only used by soft restricted permissions logic. |
| 591 | * |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 592 | * @see #syncPackages |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 593 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 594 | private final @NonNull ArrayList<OpToChange> mOpsToIgnoreIfNotAllowed = new ArrayList<>(); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 595 | |
| 596 | /** |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 597 | * All ops that need to be flipped to foreground. |
| 598 | * |
| 599 | * Currently, only used by the foreground/background permissions logic. |
| 600 | * |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 601 | * @see #syncPackages |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 602 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 603 | private final @NonNull ArrayList<OpToChange> mOpsToForeground = new ArrayList<>(); |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 604 | |
Svet Ganov | 38a0631 | 2019-04-09 23:55:05 -0700 | [diff] [blame] | 605 | PermissionToOpSynchroniser(@NonNull Context context) { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 606 | mContext = context; |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 607 | mPackageManager = context.getPackageManager(); |
| 608 | mAppOpsManager = context.getSystemService(AppOpsManager.class); |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 609 | mAppOpsManagerInternal = LocalServices.getService(AppOpsManagerInternal.class); |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 610 | |
| 611 | mRuntimePermissionInfos = new ArrayMap<>(); |
| 612 | PermissionManagerServiceInternal permissionManagerInternal = LocalServices.getService( |
| 613 | PermissionManagerServiceInternal.class); |
| 614 | List<PermissionInfo> permissionInfos = |
Hai Zhang | a7c60c0 | 2020-03-05 16:03:35 -0800 | [diff] [blame] | 615 | permissionManagerInternal.getAllPermissionsWithProtection( |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 616 | PermissionInfo.PROTECTION_DANGEROUS); |
| 617 | int permissionInfosSize = permissionInfos.size(); |
| 618 | for (int i = 0; i < permissionInfosSize; i++) { |
| 619 | PermissionInfo permissionInfo = permissionInfos.get(i); |
| 620 | mRuntimePermissionInfos.put(permissionInfo.name, permissionInfo); |
| 621 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 624 | /** |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 625 | * Set app ops that were added in {@link #addPackage}. |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 626 | * |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 627 | * <p>This processes ops previously added by {@link #addAppOps(PackageInfo, String)} |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 628 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 629 | private void syncPackages() { |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 630 | // Remember which ops were already set. This makes sure that we always set the most |
| 631 | // permissive mode if two OpChanges are scheduled. This can e.g. happen if two |
| 632 | // permissions change the same op. See {@link #getSwitchOp}. |
| 633 | LongSparseLongArray alreadySetAppOps = new LongSparseLongArray(); |
| 634 | |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 635 | final int allowCount = mOpsToAllow.size(); |
| 636 | for (int i = 0; i < allowCount; i++) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 637 | final OpToChange op = mOpsToAllow.get(i); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 638 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 639 | setUidModeAllowed(op.code, op.uid, op.packageName); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 640 | alreadySetAppOps.put(IntPair.of(op.uid, op.code), 1); |
Philip P. Moltmann | 74065c8 | 2019-05-15 10:46:32 -0700 | [diff] [blame] | 641 | } |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 642 | |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 643 | final int foregroundCount = mOpsToForeground.size(); |
| 644 | for (int i = 0; i < foregroundCount; i++) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 645 | final OpToChange op = mOpsToForeground.get(i); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 646 | if (alreadySetAppOps.indexOfKey(IntPair.of(op.uid, op.code)) >= 0) { |
| 647 | continue; |
| 648 | } |
| 649 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 650 | setUidModeForeground(op.code, op.uid, op.packageName); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 651 | alreadySetAppOps.put(IntPair.of(op.uid, op.code), 1); |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 652 | } |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 653 | |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 654 | final int ignoreCount = mOpsToIgnore.size(); |
| 655 | for (int i = 0; i < ignoreCount; i++) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 656 | final OpToChange op = mOpsToIgnore.get(i); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 657 | if (alreadySetAppOps.indexOfKey(IntPair.of(op.uid, op.code)) >= 0) { |
| 658 | continue; |
| 659 | } |
| 660 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 661 | setUidModeIgnored(op.code, op.uid, op.packageName); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 662 | alreadySetAppOps.put(IntPair.of(op.uid, op.code), 1); |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 663 | } |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 664 | |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 665 | final int ignoreIfNotAllowedCount = mOpsToIgnoreIfNotAllowed.size(); |
| 666 | for (int i = 0; i < ignoreIfNotAllowedCount; i++) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 667 | final OpToChange op = mOpsToIgnoreIfNotAllowed.get(i); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 668 | if (alreadySetAppOps.indexOfKey(IntPair.of(op.uid, op.code)) >= 0) { |
| 669 | continue; |
| 670 | } |
| 671 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 672 | boolean wasSet = setUidModeIgnoredIfNotAllowed(op.code, op.uid, op.packageName); |
Philip P. Moltmann | 3b8c14c | 2019-06-26 10:18:34 -0700 | [diff] [blame] | 673 | if (wasSet) { |
| 674 | alreadySetAppOps.put(IntPair.of(op.uid, op.code), 1); |
| 675 | } |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 676 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 679 | /** |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 680 | * Note: Called with the package lock held. Do <u>not</u> call into app-op manager. |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 681 | */ |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 682 | private void addAppOps(@NonNull PackageInfo packageInfo, @NonNull AndroidPackage pkg, |
| 683 | @NonNull String permissionName) { |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 684 | PermissionInfo permissionInfo = mRuntimePermissionInfos.get(permissionName); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 685 | if (permissionInfo == null) { |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 686 | return; |
| 687 | } |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 688 | addPermissionAppOp(packageInfo, pkg, permissionInfo); |
| 689 | addExtraAppOp(packageInfo, pkg, permissionInfo); |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 692 | private void addPermissionAppOp(@NonNull PackageInfo packageInfo, |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 693 | @NonNull AndroidPackage pkg, @NonNull PermissionInfo permissionInfo) { |
Hai Zhang | ab6a682 | 2019-10-18 00:34:06 +0000 | [diff] [blame] | 694 | if (!permissionInfo.isRuntime()) { |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 695 | return; |
| 696 | } |
| 697 | |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 698 | String permissionName = permissionInfo.name; |
| 699 | String packageName = packageInfo.packageName; |
| 700 | int permissionFlags = mPackageManager.getPermissionFlags(permissionName, |
| 701 | packageName, mContext.getUser()); |
| 702 | boolean isReviewRequired = (permissionFlags & FLAG_PERMISSION_REVIEW_REQUIRED) != 0; |
| 703 | if (isReviewRequired) { |
Philip P. Moltmann | 4e369a7 | 2019-06-14 11:08:16 -0700 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 707 | // TODO: COARSE_LOCATION and FINE_LOCATION shares the same app op. We are solving this |
| 708 | // with switch op but once we start syncing single permission this won't work. |
| 709 | int appOpCode = getSwitchOp(permissionName); |
| 710 | if (appOpCode == OP_NONE) { |
| 711 | // Note that background permissions don't have an associated app op. |
| 712 | return; |
| 713 | } |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 714 | |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 715 | int appOpMode; |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 716 | boolean shouldGrantAppOp = shouldGrantAppOp(packageInfo, pkg, permissionInfo); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 717 | if (shouldGrantAppOp) { |
| 718 | if (permissionInfo.backgroundPermission != null) { |
Hai Zhang | 0c123fa | 2019-10-21 12:41:59 -0700 | [diff] [blame] | 719 | PermissionInfo backgroundPermissionInfo = mRuntimePermissionInfos.get( |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 720 | permissionInfo.backgroundPermission); |
| 721 | boolean shouldGrantBackgroundAppOp = backgroundPermissionInfo != null |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 722 | && shouldGrantAppOp(packageInfo, pkg, backgroundPermissionInfo); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 723 | appOpMode = shouldGrantBackgroundAppOp ? MODE_ALLOWED : MODE_FOREGROUND; |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 724 | } else { |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 725 | appOpMode = MODE_ALLOWED; |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 726 | } |
| 727 | } else { |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 728 | appOpMode = MODE_IGNORED; |
| 729 | } |
| 730 | |
| 731 | int uid = packageInfo.applicationInfo.uid; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 732 | OpToChange opToChange = new OpToChange(uid, packageName, appOpCode); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 733 | switch (appOpMode) { |
| 734 | case MODE_ALLOWED: |
| 735 | mOpsToAllow.add(opToChange); |
| 736 | break; |
| 737 | case MODE_FOREGROUND: |
| 738 | mOpsToForeground.add(opToChange); |
| 739 | break; |
| 740 | case MODE_IGNORED: |
| 741 | mOpsToIgnore.add(opToChange); |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 746 | private boolean shouldGrantAppOp(@NonNull PackageInfo packageInfo, |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 747 | @NonNull AndroidPackage pkg, @NonNull PermissionInfo permissionInfo) { |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 748 | String permissionName = permissionInfo.name; |
| 749 | String packageName = packageInfo.packageName; |
| 750 | boolean isGranted = mPackageManager.checkPermission(permissionName, packageName) |
| 751 | == PackageManager.PERMISSION_GRANTED; |
| 752 | if (!isGranted) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | int permissionFlags = mPackageManager.getPermissionFlags(permissionName, packageName, |
| 757 | mContext.getUser()); |
| 758 | boolean isRevokedCompat = (permissionFlags & FLAG_PERMISSION_REVOKED_COMPAT) |
| 759 | == FLAG_PERMISSION_REVOKED_COMPAT; |
| 760 | if (isRevokedCompat) { |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | if (permissionInfo.isHardRestricted()) { |
| 765 | boolean shouldApplyRestriction = |
| 766 | (permissionFlags & FLAG_PERMISSION_APPLY_RESTRICTION) |
| 767 | == FLAG_PERMISSION_APPLY_RESTRICTION; |
| 768 | return !shouldApplyRestriction; |
| 769 | } else if (permissionInfo.isSoftRestricted()) { |
| 770 | SoftRestrictedPermissionPolicy policy = |
| 771 | SoftRestrictedPermissionPolicy.forPermission(mContext, |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 772 | packageInfo.applicationInfo, pkg, mContext.getUser(), |
| 773 | permissionName); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 774 | return policy.mayGrantPermission(); |
| 775 | } else { |
| 776 | return true; |
| 777 | } |
| 778 | } |
| 779 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 780 | private void addExtraAppOp(@NonNull PackageInfo packageInfo, @NonNull AndroidPackage pkg, |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 781 | @NonNull PermissionInfo permissionInfo) { |
| 782 | if (!permissionInfo.isSoftRestricted()) { |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | String permissionName = permissionInfo.name; |
| 787 | SoftRestrictedPermissionPolicy policy = |
| 788 | SoftRestrictedPermissionPolicy.forPermission(mContext, |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 789 | packageInfo.applicationInfo, pkg, mContext.getUser(), permissionName); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 790 | int extraOpCode = policy.getExtraAppOpCode(); |
| 791 | if (extraOpCode == OP_NONE) { |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | int uid = packageInfo.applicationInfo.uid; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 796 | String packageName = packageInfo.packageName; |
| 797 | OpToChange extraOpToChange = new OpToChange(uid, packageName, extraOpCode); |
Hai Zhang | ea87b47 | 2019-09-27 15:48:07 -0700 | [diff] [blame] | 798 | if (policy.mayAllowExtraAppOp()) { |
| 799 | mOpsToAllow.add(extraOpToChange); |
| 800 | } else { |
| 801 | if (policy.mayDenyExtraAppOpIfGranted()) { |
| 802 | mOpsToIgnore.add(extraOpToChange); |
| 803 | } else { |
| 804 | mOpsToIgnoreIfNotAllowed.add(extraOpToChange); |
| 805 | } |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 806 | } |
Philip P. Moltmann | 9408f58 | 2019-04-10 16:58:24 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 809 | /** |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 810 | * Add a package for {@link #syncPackages() processing} later. |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 811 | * |
| 812 | * <p>Note: Called with the package lock held. Do <u>not</u> call into app-op manager. |
| 813 | * |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 814 | * @param pkgName The package to add for later processing. |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 815 | */ |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 816 | void addPackage(@NonNull String pkgName) { |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 817 | PackageManagerInternal pmInternal = |
| 818 | LocalServices.getService(PackageManagerInternal.class); |
| 819 | final PackageInfo pkgInfo; |
| 820 | final AndroidPackage pkg; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 821 | try { |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 822 | pkgInfo = mPackageManager.getPackageInfo(pkgName, GET_PERMISSIONS); |
| 823 | pkg = pmInternal.getPackage(pkgName); |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 824 | } catch (NameNotFoundException e) { |
Philip P. Moltmann | 4868772 | 2019-04-11 10:40:30 -0700 | [diff] [blame] | 825 | return; |
| 826 | } |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 827 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 828 | if (pkgInfo == null || pkg == null || pkgInfo.requestedPermissions == null) { |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 831 | |
Winson | 33eacc6 | 2020-01-24 12:02:58 -0800 | [diff] [blame] | 832 | for (String permission : pkgInfo.requestedPermissions) { |
| 833 | addAppOps(pkgInfo, pkg, permission); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 837 | private void setUidModeAllowed(int opCode, int uid, @NonNull String packageName) { |
| 838 | setUidMode(opCode, uid, MODE_ALLOWED, packageName); |
Philip P. Moltmann | 74065c8 | 2019-05-15 10:46:32 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 841 | private void setUidModeForeground(int opCode, int uid, @NonNull String packageName) { |
| 842 | setUidMode(opCode, uid, MODE_FOREGROUND, packageName); |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 845 | private void setUidModeIgnored(int opCode, int uid, @NonNull String packageName) { |
| 846 | setUidMode(opCode, uid, MODE_IGNORED, packageName); |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 849 | private boolean setUidModeIgnoredIfNotAllowed(int opCode, int uid, |
| 850 | @NonNull String packageName) { |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 851 | final int currentMode = mAppOpsManager.unsafeCheckOpRaw(AppOpsManager.opToPublicName( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 852 | opCode), uid, packageName); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 853 | if (currentMode != MODE_ALLOWED) { |
| 854 | if (currentMode != MODE_IGNORED) { |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 855 | mAppOpsManagerInternal.setUidModeFromPermissionPolicy(opCode, uid, MODE_IGNORED, |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 856 | mAppOpsCallback); |
Hai Zhang | fa29170 | 2019-09-19 16:35:44 -0700 | [diff] [blame] | 857 | } |
| 858 | return true; |
| 859 | } |
| 860 | return false; |
| 861 | } |
| 862 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 863 | private void setUidMode(int opCode, int uid, int mode, |
| 864 | @NonNull String packageName) { |
Hai Zhang | a7ed475 | 2019-12-10 13:45:54 -0800 | [diff] [blame] | 865 | final int oldMode = mAppOpsManager.unsafeCheckOpRaw(AppOpsManager.opToPublicName( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 866 | opCode), uid, packageName); |
Hai Zhang | a7ed475 | 2019-12-10 13:45:54 -0800 | [diff] [blame] | 867 | if (oldMode != mode) { |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 868 | mAppOpsManagerInternal.setUidModeFromPermissionPolicy(opCode, uid, mode, |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 869 | mAppOpsCallback); |
Hai Zhang | a7ed475 | 2019-12-10 13:45:54 -0800 | [diff] [blame] | 870 | final int newMode = mAppOpsManager.unsafeCheckOpRaw(AppOpsManager.opToPublicName( |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 871 | opCode), uid, packageName); |
Hai Zhang | a7ed475 | 2019-12-10 13:45:54 -0800 | [diff] [blame] | 872 | if (newMode != mode) { |
| 873 | // Work around incorrectly-set package mode. It never makes sense for app ops |
| 874 | // related to runtime permissions, but can get in the way and we have to reset |
| 875 | // it. |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 876 | mAppOpsManagerInternal.setModeFromPermissionPolicy(opCode, uid, packageName, |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 877 | AppOpsManager.opToDefaultMode(opCode), mAppOpsCallback); |
Hai Zhang | a7ed475 | 2019-12-10 13:45:54 -0800 | [diff] [blame] | 878 | } |
Philip P. Moltmann | fad1a8f | 2019-06-14 09:02:24 -0700 | [diff] [blame] | 879 | } |
Philip P. Moltmann | 404b51c | 2019-06-11 09:01:59 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Philip P. Moltmann | e9f4b5a | 2019-06-20 14:51:52 -0700 | [diff] [blame] | 882 | private class OpToChange { |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 883 | final int uid; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 884 | final @NonNull String packageName; |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 885 | final int code; |
| 886 | |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 887 | OpToChange(int uid, @NonNull String packageName, int code) { |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 888 | this.uid = uid; |
Philip P. Moltmann | f9486608 | 2020-02-18 17:05:16 +0000 | [diff] [blame] | 889 | this.packageName = packageName; |
Philip P. Moltmann | bd4943b | 2019-04-11 10:05:48 -0700 | [diff] [blame] | 890 | this.code = code; |
| 891 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 892 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 893 | } |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 894 | |
| 895 | private class Internal extends PermissionPolicyInternal { |
| 896 | |
| 897 | @Override |
| 898 | public boolean checkStartActivity(@NonNull Intent intent, int callingUid, |
Hai Zhang | 639360a | 2019-05-20 15:35:33 -0700 | [diff] [blame] | 899 | @Nullable String callingPackage) { |
Hai Zhang | 560aa57 | 2019-07-11 17:30:45 -0700 | [diff] [blame] | 900 | if (callingPackage != null && isActionRemovedForCallingPackage(intent, callingUid, |
Hai Zhang | 639360a | 2019-05-20 15:35:33 -0700 | [diff] [blame] | 901 | callingPackage)) { |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 902 | Slog.w(LOG_TAG, "Action Removed: starting " + intent.toString() + " from " |
| 903 | + callingPackage + " (uid=" + callingUid + ")"); |
| 904 | return false; |
| 905 | } |
| 906 | return true; |
| 907 | } |
| 908 | |
Svet Ganov | 3c499ea | 2019-07-26 17:45:56 -0700 | [diff] [blame] | 909 | @Override |
| 910 | public boolean isInitialized(int userId) { |
| 911 | return isStarted(userId); |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public void setOnInitializedCallback(@NonNull OnInitializedCallback callback) { |
| 916 | synchronized (mLock) { |
| 917 | mOnInitializedCallback = callback; |
| 918 | } |
| 919 | } |
| 920 | |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 921 | /** |
| 922 | * Check if the intent action is removed for the calling package (often based on target SDK |
| 923 | * version). If the action is removed, we'll silently cancel the activity launch. |
| 924 | */ |
Hai Zhang | 560aa57 | 2019-07-11 17:30:45 -0700 | [diff] [blame] | 925 | private boolean isActionRemovedForCallingPackage(@NonNull Intent intent, int callingUid, |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 926 | @NonNull String callingPackage) { |
Hai Zhang | 560aa57 | 2019-07-11 17:30:45 -0700 | [diff] [blame] | 927 | String action = intent.getAction(); |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 928 | if (action == null) { |
| 929 | return false; |
| 930 | } |
| 931 | switch (action) { |
Hai Zhang | 114afc3 | 2019-05-01 14:16:23 +0800 | [diff] [blame] | 932 | case TelecomManager.ACTION_CHANGE_DEFAULT_DIALER: |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 933 | case Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT: { |
| 934 | ApplicationInfo applicationInfo; |
| 935 | try { |
Hai Zhang | 560aa57 | 2019-07-11 17:30:45 -0700 | [diff] [blame] | 936 | applicationInfo = getContext().getPackageManager().getApplicationInfoAsUser( |
| 937 | callingPackage, 0, UserHandle.getUserId(callingUid)); |
| 938 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.Q) { |
| 939 | // Applications targeting Q or higher should use |
| 940 | // RoleManager.createRequestRoleIntent() instead. |
| 941 | return true; |
| 942 | } |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 943 | } catch (PackageManager.NameNotFoundException e) { |
| 944 | Slog.i(LOG_TAG, "Cannot find application info for " + callingPackage); |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 945 | } |
Hai Zhang | 560aa57 | 2019-07-11 17:30:45 -0700 | [diff] [blame] | 946 | // Make sure RequestRoleActivity can know the calling package if we allow it. |
| 947 | intent.putExtra(Intent.EXTRA_CALLING_PACKAGE, callingPackage); |
| 948 | return false; |
Hai Zhang | f4da9be | 2019-05-01 13:46:06 +0800 | [diff] [blame] | 949 | } |
| 950 | default: |
| 951 | return false; |
| 952 | } |
| 953 | } |
| 954 | } |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 955 | } |