Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 19 | import static android.os.Process.FIRST_APPLICATION_UID; |
Todd Kennedy | 7c4c55d | 2017-11-02 10:01:39 -0700 | [diff] [blame] | 20 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 21 | import android.Manifest; |
| 22 | import android.annotation.NonNull; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 25 | import android.app.AppOpsManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 26 | import android.app.DownloadManager; |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 27 | import android.app.SearchManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 28 | import android.app.admin.DevicePolicyManager; |
| 29 | import android.companion.CompanionDeviceManager; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 32 | import android.content.pm.ApplicationInfo; |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Jeff Sharkey | 7a80760 | 2018-10-18 13:21:55 -0600 | [diff] [blame] | 35 | import android.content.pm.PackageManager.NameNotFoundException; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 36 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManagerInternal.PackagesProvider; |
| 38 | import android.content.pm.PackageManagerInternal.SyncAdapterPackagesProvider; |
Jeff Sharkey | 7a80760 | 2018-10-18 13:21:55 -0600 | [diff] [blame] | 39 | import android.content.pm.PermissionInfo; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 40 | import android.content.pm.ProviderInfo; |
| 41 | import android.content.pm.ResolveInfo; |
| 42 | import android.media.RingtoneManager; |
| 43 | import android.net.Uri; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 44 | import android.os.Build; |
| 45 | import android.os.Environment; |
| 46 | import android.os.Handler; |
| 47 | import android.os.Looper; |
| 48 | import android.os.Message; |
Jeff Sharkey | 4aacd8b | 2018-07-24 15:24:21 -0600 | [diff] [blame] | 49 | import android.os.SystemProperties; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
| 51 | import android.os.storage.StorageManager; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 52 | import android.permission.PermissionManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 53 | import android.print.PrintManager; |
| 54 | import android.provider.CalendarContract; |
| 55 | import android.provider.ContactsContract; |
| 56 | import android.provider.MediaStore; |
| 57 | import android.provider.Telephony.Sms.Intents; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 58 | import android.security.Credentials; |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 59 | import android.speech.RecognitionService; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 60 | import android.telephony.TelephonyManager; |
Ye Wen | 8e8b2d5 | 2018-03-14 11:48:24 -0700 | [diff] [blame] | 61 | import android.text.TextUtils; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 62 | import android.util.ArrayMap; |
| 63 | import android.util.ArraySet; |
| 64 | import android.util.Log; |
| 65 | import android.util.Slog; |
| 66 | import android.util.Xml; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 67 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 68 | import com.android.internal.util.ArrayUtils; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 69 | import com.android.internal.util.XmlUtils; |
| 70 | import com.android.server.LocalServices; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 71 | |
| 72 | import org.xmlpull.v1.XmlPullParser; |
| 73 | import org.xmlpull.v1.XmlPullParserException; |
| 74 | |
| 75 | import java.io.BufferedInputStream; |
| 76 | import java.io.File; |
| 77 | import java.io.FileInputStream; |
| 78 | import java.io.IOException; |
| 79 | import java.io.InputStream; |
| 80 | import java.util.ArrayList; |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 81 | import java.util.Arrays; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 82 | import java.util.Collections; |
| 83 | import java.util.List; |
| 84 | import java.util.Map; |
| 85 | import java.util.Set; |
| 86 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 87 | /** |
| 88 | * This class is the policy for granting runtime permissions to |
| 89 | * platform components and default handlers in the system such |
| 90 | * that the device is usable out-of-the-box. For example, the |
| 91 | * shell UID is a part of the system and the Phone app should |
| 92 | * have phone related permission by default. |
| 93 | * <p> |
| 94 | * NOTE: This class is at the wrong abstraction level. It is a part of the package manager |
| 95 | * service but knows about lots of higher level subsystems. The correct way to do this is |
| 96 | * to have an interface defined in the package manager but have the impl next to other |
| 97 | * policy stuff like PhoneWindowManager |
| 98 | */ |
| 99 | public final class DefaultPermissionGrantPolicy { |
| 100 | private static final String TAG = "DefaultPermGrantPolicy"; // must be <= 23 chars |
| 101 | private static final boolean DEBUG = false; |
| 102 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 103 | @PackageManager.ResolveInfoFlags |
| 104 | private static final int DEFAULT_INTENT_QUERY_FLAGS = |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 105 | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 106 | | PackageManager.MATCH_UNINSTALLED_PACKAGES; |
| 107 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 108 | @PackageManager.PackageInfoFlags |
| 109 | private static final int DEFAULT_PACKAGE_INFO_QUERY_FLAGS = |
| 110 | PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.GET_PERMISSIONS; |
| 111 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 112 | private static final String AUDIO_MIME_TYPE = "audio/mpeg"; |
| 113 | |
| 114 | private static final String TAG_EXCEPTIONS = "exceptions"; |
| 115 | private static final String TAG_EXCEPTION = "exception"; |
| 116 | private static final String TAG_PERMISSION = "permission"; |
| 117 | private static final String ATTR_PACKAGE = "package"; |
| 118 | private static final String ATTR_NAME = "name"; |
| 119 | private static final String ATTR_FIXED = "fixed"; |
| 120 | |
| 121 | private static final Set<String> PHONE_PERMISSIONS = new ArraySet<>(); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 122 | |
| 123 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 124 | static { |
| 125 | PHONE_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE); |
| 126 | PHONE_PERMISSIONS.add(Manifest.permission.CALL_PHONE); |
| 127 | PHONE_PERMISSIONS.add(Manifest.permission.READ_CALL_LOG); |
| 128 | PHONE_PERMISSIONS.add(Manifest.permission.WRITE_CALL_LOG); |
| 129 | PHONE_PERMISSIONS.add(Manifest.permission.ADD_VOICEMAIL); |
| 130 | PHONE_PERMISSIONS.add(Manifest.permission.USE_SIP); |
| 131 | PHONE_PERMISSIONS.add(Manifest.permission.PROCESS_OUTGOING_CALLS); |
| 132 | } |
| 133 | |
| 134 | private static final Set<String> CONTACTS_PERMISSIONS = new ArraySet<>(); |
| 135 | static { |
| 136 | CONTACTS_PERMISSIONS.add(Manifest.permission.READ_CONTACTS); |
| 137 | CONTACTS_PERMISSIONS.add(Manifest.permission.WRITE_CONTACTS); |
| 138 | CONTACTS_PERMISSIONS.add(Manifest.permission.GET_ACCOUNTS); |
| 139 | } |
| 140 | |
| 141 | private static final Set<String> LOCATION_PERMISSIONS = new ArraySet<>(); |
| 142 | static { |
| 143 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_FINE_LOCATION); |
| 144 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 145 | } |
| 146 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 147 | private static final Set<String> COARSE_LOCATION_PERMISSIONS = new ArraySet<>(); |
| 148 | static { |
| 149 | COARSE_LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 150 | } |
| 151 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 152 | private static final Set<String> CALENDAR_PERMISSIONS = new ArraySet<>(); |
| 153 | static { |
| 154 | CALENDAR_PERMISSIONS.add(Manifest.permission.READ_CALENDAR); |
| 155 | CALENDAR_PERMISSIONS.add(Manifest.permission.WRITE_CALENDAR); |
| 156 | } |
| 157 | |
| 158 | private static final Set<String> SMS_PERMISSIONS = new ArraySet<>(); |
| 159 | static { |
| 160 | SMS_PERMISSIONS.add(Manifest.permission.SEND_SMS); |
| 161 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_SMS); |
| 162 | SMS_PERMISSIONS.add(Manifest.permission.READ_SMS); |
| 163 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_WAP_PUSH); |
| 164 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_MMS); |
| 165 | SMS_PERMISSIONS.add(Manifest.permission.READ_CELL_BROADCASTS); |
| 166 | } |
| 167 | |
| 168 | private static final Set<String> MICROPHONE_PERMISSIONS = new ArraySet<>(); |
| 169 | static { |
| 170 | MICROPHONE_PERMISSIONS.add(Manifest.permission.RECORD_AUDIO); |
| 171 | } |
| 172 | |
| 173 | private static final Set<String> CAMERA_PERMISSIONS = new ArraySet<>(); |
| 174 | static { |
| 175 | CAMERA_PERMISSIONS.add(Manifest.permission.CAMERA); |
| 176 | } |
| 177 | |
| 178 | private static final Set<String> SENSORS_PERMISSIONS = new ArraySet<>(); |
| 179 | static { |
| 180 | SENSORS_PERMISSIONS.add(Manifest.permission.BODY_SENSORS); |
| 181 | } |
| 182 | |
Jeff Sharkey | 4aacd8b | 2018-07-24 15:24:21 -0600 | [diff] [blame] | 183 | @Deprecated |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 184 | private static final Set<String> STORAGE_PERMISSIONS = new ArraySet<>(); |
| 185 | static { |
Jeff Sharkey | b162909 | 2018-08-24 10:33:12 -0600 | [diff] [blame] | 186 | // STOPSHIP(b/112545973): remove once feature enabled by default |
| 187 | if (!SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, false)) { |
| 188 | STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE); |
| 189 | STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 190 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jeff Sharkey | 4aacd8b | 2018-07-24 15:24:21 -0600 | [diff] [blame] | 193 | private static final Set<String> MEDIA_AURAL_PERMISSIONS = new ArraySet<>(); |
| 194 | static { |
| 195 | // STOPSHIP(b/112545973): remove once feature enabled by default |
| 196 | if (SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, false)) { |
| 197 | MEDIA_AURAL_PERMISSIONS.add(Manifest.permission.READ_MEDIA_AUDIO); |
| 198 | MEDIA_AURAL_PERMISSIONS.add(Manifest.permission.WRITE_MEDIA_AUDIO); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private static final Set<String> MEDIA_VISUAL_PERMISSIONS = new ArraySet<>(); |
| 203 | static { |
| 204 | // STOPSHIP(b/112545973): remove once feature enabled by default |
| 205 | if (SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, false)) { |
| 206 | MEDIA_VISUAL_PERMISSIONS.add(Manifest.permission.READ_MEDIA_IMAGES); |
| 207 | MEDIA_VISUAL_PERMISSIONS.add(Manifest.permission.WRITE_MEDIA_IMAGES); |
| 208 | MEDIA_VISUAL_PERMISSIONS.add(Manifest.permission.READ_MEDIA_VIDEO); |
| 209 | MEDIA_VISUAL_PERMISSIONS.add(Manifest.permission.WRITE_MEDIA_VIDEO); |
| 210 | } |
| 211 | } |
| 212 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 213 | private static final int MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS = 1; |
| 214 | |
| 215 | private static final String ACTION_TRACK = "com.android.fitness.TRACK"; |
| 216 | |
| 217 | private final Handler mHandler; |
| 218 | |
| 219 | private PackagesProvider mLocationPackagesProvider; |
| 220 | private PackagesProvider mVoiceInteractionPackagesProvider; |
| 221 | private PackagesProvider mSmsAppPackagesProvider; |
| 222 | private PackagesProvider mDialerAppPackagesProvider; |
| 223 | private PackagesProvider mSimCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 224 | private PackagesProvider mUseOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 225 | private SyncAdapterPackagesProvider mSyncAdapterPackagesProvider; |
| 226 | |
| 227 | private ArrayMap<String, List<DefaultPermissionGrant>> mGrantExceptions; |
| 228 | private final Context mContext; |
| 229 | private final Object mLock = new Object(); |
| 230 | private final PackageManagerInternal mServiceInternal; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 231 | private final PermissionManagerService mPermissionManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 232 | private final DefaultPermissionGrantedCallback mPermissionGrantedCallback; |
| 233 | public interface DefaultPermissionGrantedCallback { |
| 234 | /** Callback when permissions have been granted */ |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 235 | void onDefaultRuntimePermissionsGranted(int userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | public DefaultPermissionGrantPolicy(Context context, Looper looper, |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 239 | @Nullable DefaultPermissionGrantedCallback callback, |
| 240 | @NonNull PermissionManagerService permissionManager) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 241 | mContext = context; |
| 242 | mHandler = new Handler(looper) { |
| 243 | @Override |
| 244 | public void handleMessage(Message msg) { |
| 245 | if (msg.what == MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS) { |
| 246 | synchronized (mLock) { |
| 247 | if (mGrantExceptions == null) { |
| 248 | mGrantExceptions = readDefaultPermissionExceptionsLocked(); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | }; |
| 254 | mPermissionGrantedCallback = callback; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 255 | mPermissionManager = permissionManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 256 | mServiceInternal = LocalServices.getService(PackageManagerInternal.class); |
| 257 | } |
| 258 | |
| 259 | public void setLocationPackagesProvider(PackagesProvider provider) { |
| 260 | synchronized (mLock) { |
| 261 | mLocationPackagesProvider = provider; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | public void setVoiceInteractionPackagesProvider(PackagesProvider provider) { |
| 266 | synchronized (mLock) { |
| 267 | mVoiceInteractionPackagesProvider = provider; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | public void setSmsAppPackagesProvider(PackagesProvider provider) { |
| 272 | synchronized (mLock) { |
| 273 | mSmsAppPackagesProvider = provider; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public void setDialerAppPackagesProvider(PackagesProvider provider) { |
| 278 | synchronized (mLock) { |
| 279 | mDialerAppPackagesProvider = provider; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public void setSimCallManagerPackagesProvider(PackagesProvider provider) { |
| 284 | synchronized (mLock) { |
| 285 | mSimCallManagerPackagesProvider = provider; |
| 286 | } |
| 287 | } |
| 288 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 289 | public void setUseOpenWifiAppPackagesProvider(PackagesProvider provider) { |
| 290 | synchronized (mLock) { |
| 291 | mUseOpenWifiAppPackagesProvider = provider; |
| 292 | } |
| 293 | } |
| 294 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 295 | public void setSyncAdapterPackagesProvider(SyncAdapterPackagesProvider provider) { |
| 296 | synchronized (mLock) { |
| 297 | mSyncAdapterPackagesProvider = provider; |
| 298 | } |
| 299 | } |
| 300 | |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 301 | public void grantDefaultPermissions(int userId) { |
Ralph Nathan | bd11158 | 2018-03-21 14:53:23 -0700 | [diff] [blame] | 302 | grantPermissionsToSysComponentsAndPrivApps(userId); |
| 303 | grantDefaultSystemHandlerPermissions(userId); |
| 304 | grantDefaultPermissionExceptions(userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 307 | private void grantRuntimePermissionsForSystemPackage(int userId, PackageInfo pkg) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 308 | Set<String> permissions = new ArraySet<>(); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 309 | for (String permission : pkg.requestedPermissions) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 310 | final BasePermission bp = mPermissionManager.getPermission(permission); |
| 311 | if (bp == null) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 312 | continue; |
| 313 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 314 | if (bp.isRuntime()) { |
| 315 | permissions.add(permission); |
| 316 | } |
| 317 | } |
| 318 | if (!permissions.isEmpty()) { |
| 319 | grantRuntimePermissions(pkg, permissions, true, userId); |
| 320 | } |
| 321 | } |
| 322 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 323 | public void scheduleReadDefaultPermissionExceptions() { |
| 324 | mHandler.sendEmptyMessage(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS); |
| 325 | } |
| 326 | |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 327 | private void grantPermissionsToSysComponentsAndPrivApps(int userId) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 328 | Log.i(TAG, "Granting permissions to platform components for user " + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 329 | List<PackageInfo> packages = mContext.getPackageManager().getInstalledPackagesAsUser( |
| 330 | DEFAULT_PACKAGE_INFO_QUERY_FLAGS, UserHandle.USER_SYSTEM); |
| 331 | for (PackageInfo pkg : packages) { |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 332 | if (pkg == null) { |
| 333 | continue; |
| 334 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 335 | if (!isSysComponentOrPersistentPlatformSignedPrivApp(pkg) |
| 336 | || !doesPackageSupportRuntimePermissions(pkg) |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 337 | || ArrayUtils.isEmpty(pkg.requestedPermissions)) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 338 | continue; |
| 339 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 340 | grantRuntimePermissionsForSystemPackage(userId, pkg); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | @SafeVarargs |
| 345 | private final void grantIgnoringSystemPackage(String packageName, int userId, |
| 346 | Set<String>... permissionGroups) { |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 347 | grantPermissionsToPackage( |
| 348 | packageName, userId, true /* ignoreSystemPackage */, permissionGroups); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @SafeVarargs |
| 352 | private final void grantSystemFixedPermissionsToSystemPackage(String packageName, int userId, |
| 353 | Set<String>... permissionGroups) { |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 354 | grantPermissionsToSystemPackage( |
| 355 | packageName, userId, true /* systemFixed */, permissionGroups); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @SafeVarargs |
| 359 | private final void grantPermissionsToSystemPackage( |
| 360 | String packageName, int userId, Set<String>... permissionGroups) { |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 361 | grantPermissionsToSystemPackage( |
| 362 | packageName, userId, false /* systemFixed */, permissionGroups); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @SafeVarargs |
| 366 | private final void grantPermissionsToSystemPackage(String packageName, int userId, |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 367 | boolean systemFixed, Set<String>... permissionGroups) { |
| 368 | if (!isSystemPackage(packageName)) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 369 | return; |
| 370 | } |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 371 | grantPermissionsToPackage(getSystemPackageInfo(packageName), |
| 372 | userId, systemFixed, false /* ignoreSystemPackage */, permissionGroups); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @SafeVarargs |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 376 | private final void grantPermissionsToPackage(String packageName, int userId, |
| 377 | boolean ignoreSystemPackage, Set<String>... permissionGroups) { |
| 378 | grantPermissionsToPackage(getPackageInfo(packageName), |
| 379 | userId, false /* systemFixed */, ignoreSystemPackage, permissionGroups); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @SafeVarargs |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 383 | private final void grantPermissionsToPackage(PackageInfo packageName, int userId, |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 384 | boolean systemFixed, boolean ignoreSystemPackage, Set<String>... permissionGroups) { |
| 385 | if (packageName == null) return; |
| 386 | if (doesPackageSupportRuntimePermissions(packageName)) { |
| 387 | for (Set<String> permissionGroup : permissionGroups) { |
| 388 | grantRuntimePermissions(packageName, permissionGroup, systemFixed, |
| 389 | ignoreSystemPackage, userId); |
| 390 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | private void grantDefaultSystemHandlerPermissions(int userId) { |
| 395 | Log.i(TAG, "Granting permissions to default platform handlers for user " + userId); |
| 396 | |
| 397 | final PackagesProvider locationPackagesProvider; |
| 398 | final PackagesProvider voiceInteractionPackagesProvider; |
| 399 | final PackagesProvider smsAppPackagesProvider; |
| 400 | final PackagesProvider dialerAppPackagesProvider; |
| 401 | final PackagesProvider simCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 402 | final PackagesProvider useOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 403 | final SyncAdapterPackagesProvider syncAdapterPackagesProvider; |
| 404 | |
| 405 | synchronized (mLock) { |
| 406 | locationPackagesProvider = mLocationPackagesProvider; |
| 407 | voiceInteractionPackagesProvider = mVoiceInteractionPackagesProvider; |
| 408 | smsAppPackagesProvider = mSmsAppPackagesProvider; |
| 409 | dialerAppPackagesProvider = mDialerAppPackagesProvider; |
| 410 | simCallManagerPackagesProvider = mSimCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 411 | useOpenWifiAppPackagesProvider = mUseOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 412 | syncAdapterPackagesProvider = mSyncAdapterPackagesProvider; |
| 413 | } |
| 414 | |
| 415 | String[] voiceInteractPackageNames = (voiceInteractionPackagesProvider != null) |
| 416 | ? voiceInteractionPackagesProvider.getPackages(userId) : null; |
| 417 | String[] locationPackageNames = (locationPackagesProvider != null) |
| 418 | ? locationPackagesProvider.getPackages(userId) : null; |
| 419 | String[] smsAppPackageNames = (smsAppPackagesProvider != null) |
| 420 | ? smsAppPackagesProvider.getPackages(userId) : null; |
| 421 | String[] dialerAppPackageNames = (dialerAppPackagesProvider != null) |
| 422 | ? dialerAppPackagesProvider.getPackages(userId) : null; |
| 423 | String[] simCallManagerPackageNames = (simCallManagerPackagesProvider != null) |
| 424 | ? simCallManagerPackagesProvider.getPackages(userId) : null; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 425 | String[] useOpenWifiAppPackageNames = (useOpenWifiAppPackagesProvider != null) |
| 426 | ? useOpenWifiAppPackagesProvider.getPackages(userId) : null; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 427 | String[] contactsSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 428 | syncAdapterPackagesProvider.getPackages(ContactsContract.AUTHORITY, userId) : null; |
| 429 | String[] calendarSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 430 | syncAdapterPackagesProvider.getPackages(CalendarContract.AUTHORITY, userId) : null; |
| 431 | |
| 432 | // Installer |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 433 | grantSystemFixedPermissionsToSystemPackage( |
| 434 | getKnownPackage(PackageManagerInternal.PACKAGE_INSTALLER, userId), |
| 435 | userId, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 436 | |
| 437 | // Verifier |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 438 | final String verifier = getKnownPackage(PackageManagerInternal.PACKAGE_VERIFIER, userId); |
| 439 | grantSystemFixedPermissionsToSystemPackage(verifier, userId, STORAGE_PERMISSIONS); |
| 440 | grantPermissionsToSystemPackage(verifier, userId, PHONE_PERMISSIONS, SMS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 441 | |
| 442 | // SetupWizard |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 443 | grantPermissionsToSystemPackage( |
| 444 | getKnownPackage(PackageManagerInternal.PACKAGE_SETUP_WIZARD, userId), userId, |
| 445 | PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, LOCATION_PERMISSIONS, CAMERA_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 446 | |
| 447 | // Camera |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 448 | grantPermissionsToSystemPackage( |
| 449 | getDefaultSystemHandlerActivityPackage(MediaStore.ACTION_IMAGE_CAPTURE, userId), |
| 450 | userId, CAMERA_PERMISSIONS, MICROPHONE_PERMISSIONS, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 451 | |
| 452 | // Media provider |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 453 | grantSystemFixedPermissionsToSystemPackage( |
| 454 | getDefaultProviderAuthorityPackage(MediaStore.AUTHORITY, userId), userId, |
| 455 | STORAGE_PERMISSIONS, MEDIA_AURAL_PERMISSIONS, MEDIA_VISUAL_PERMISSIONS, |
| 456 | PHONE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 457 | |
| 458 | // Downloads provider |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 459 | grantSystemFixedPermissionsToSystemPackage( |
| 460 | getDefaultProviderAuthorityPackage("downloads", userId), userId, |
| 461 | STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 462 | |
| 463 | // Downloads UI |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 464 | grantSystemFixedPermissionsToSystemPackage( |
| 465 | getDefaultSystemHandlerActivityPackage( |
| 466 | DownloadManager.ACTION_VIEW_DOWNLOADS, userId), |
| 467 | userId, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 468 | |
| 469 | // Storage provider |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 470 | grantSystemFixedPermissionsToSystemPackage( |
| 471 | getDefaultProviderAuthorityPackage("com.android.externalstorage.documents", userId), |
| 472 | userId, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 473 | |
| 474 | // CertInstaller |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 475 | grantSystemFixedPermissionsToSystemPackage( |
| 476 | getDefaultSystemHandlerActivityPackage(Credentials.INSTALL_ACTION, userId), userId, |
| 477 | STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 478 | |
| 479 | // Dialer |
| 480 | if (dialerAppPackageNames == null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 481 | String dialerPackage = |
| 482 | getDefaultSystemHandlerActivityPackage(Intent.ACTION_DIAL, userId); |
| 483 | grantDefaultPermissionsToDefaultSystemDialerApp(dialerPackage, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 484 | } else { |
| 485 | for (String dialerAppPackageName : dialerAppPackageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 486 | grantDefaultPermissionsToDefaultSystemDialerApp(dialerAppPackageName, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | // Sim call manager |
| 491 | if (simCallManagerPackageNames != null) { |
| 492 | for (String simCallManagerPackageName : simCallManagerPackageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 493 | grantDefaultPermissionsToDefaultSystemSimCallManager( |
| 494 | simCallManagerPackageName, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 498 | // Use Open Wifi |
| 499 | if (useOpenWifiAppPackageNames != null) { |
| 500 | for (String useOpenWifiPackageName : useOpenWifiAppPackageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 501 | grantDefaultPermissionsToDefaultSystemUseOpenWifiApp( |
| 502 | useOpenWifiPackageName, userId); |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 506 | // SMS |
| 507 | if (smsAppPackageNames == null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 508 | String smsPackage = getDefaultSystemHandlerActivityPackageForCategory( |
| 509 | Intent.CATEGORY_APP_MESSAGING, userId); |
| 510 | grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 511 | } else { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 512 | for (String smsPackage : smsAppPackageNames) { |
| 513 | grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | // Cell Broadcast Receiver |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 518 | grantPermissionsToSystemPackage( |
| 519 | getDefaultSystemHandlerActivityPackage(Intents.SMS_CB_RECEIVED_ACTION, userId), |
| 520 | userId, SMS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 521 | |
| 522 | // Carrier Provisioning Service |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 523 | grantPermissionsToSystemPackage( |
| 524 | getDefaultSystemHandlerServicePackage(Intents.SMS_CARRIER_PROVISION_ACTION, userId), |
| 525 | userId, SMS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 526 | |
| 527 | // Calendar |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 528 | grantPermissionsToSystemPackage( |
| 529 | getDefaultSystemHandlerActivityPackageForCategory( |
| 530 | Intent.CATEGORY_APP_CALENDAR, userId), |
| 531 | userId, CALENDAR_PERMISSIONS, CONTACTS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 532 | |
| 533 | // Calendar provider |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 534 | String calendarProvider = |
| 535 | getDefaultProviderAuthorityPackage(CalendarContract.AUTHORITY, userId); |
| 536 | grantPermissionsToSystemPackage(calendarProvider, userId, |
| 537 | CONTACTS_PERMISSIONS, STORAGE_PERMISSIONS); |
| 538 | grantSystemFixedPermissionsToSystemPackage(calendarProvider, userId, CALENDAR_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 539 | |
| 540 | // Calendar provider sync adapters |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 541 | grantPermissionToEachSystemPackage( |
| 542 | getHeadlessSyncAdapterPackages(calendarSyncAdapterPackages, userId), |
| 543 | userId, CALENDAR_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 544 | |
| 545 | // Contacts |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 546 | grantPermissionsToSystemPackage( |
| 547 | getDefaultSystemHandlerActivityPackageForCategory( |
| 548 | Intent.CATEGORY_APP_CONTACTS, userId), |
| 549 | userId, CONTACTS_PERMISSIONS, PHONE_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 550 | |
| 551 | // Contacts provider sync adapters |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 552 | grantPermissionToEachSystemPackage( |
| 553 | getHeadlessSyncAdapterPackages(contactsSyncAdapterPackages, userId), |
| 554 | userId, CONTACTS_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 555 | |
| 556 | // Contacts provider |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 557 | String contactsProviderPackage = |
| 558 | getDefaultProviderAuthorityPackage(ContactsContract.AUTHORITY, userId); |
| 559 | grantSystemFixedPermissionsToSystemPackage(contactsProviderPackage, userId, |
| 560 | CONTACTS_PERMISSIONS, PHONE_PERMISSIONS); |
| 561 | grantPermissionsToSystemPackage(contactsProviderPackage, userId, STORAGE_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 562 | |
| 563 | // Device provisioning |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 564 | grantPermissionsToSystemPackage( |
| 565 | getDefaultSystemHandlerActivityPackage( |
| 566 | DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE, userId), |
| 567 | userId, CONTACTS_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 568 | |
| 569 | // Maps |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 570 | grantPermissionsToSystemPackage( |
| 571 | getDefaultSystemHandlerActivityPackageForCategory(Intent.CATEGORY_APP_MAPS, userId), |
| 572 | userId, LOCATION_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 573 | |
| 574 | // Gallery |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 575 | grantPermissionsToSystemPackage( |
| 576 | getDefaultSystemHandlerActivityPackageForCategory( |
| 577 | Intent.CATEGORY_APP_GALLERY, userId), |
| 578 | userId, STORAGE_PERMISSIONS, MEDIA_VISUAL_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 579 | |
| 580 | // Email |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 581 | grantPermissionsToSystemPackage( |
| 582 | getDefaultSystemHandlerActivityPackageForCategory( |
| 583 | Intent.CATEGORY_APP_EMAIL, userId), |
| 584 | userId, CONTACTS_PERMISSIONS, CALENDAR_PERMISSIONS); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 585 | |
| 586 | // Browser |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 587 | String browserPackage = getKnownPackage(PackageManagerInternal.PACKAGE_BROWSER, userId); |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 588 | if (browserPackage == null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 589 | browserPackage = getDefaultSystemHandlerActivityPackageForCategory( |
| 590 | Intent.CATEGORY_APP_BROWSER, userId); |
| 591 | if (!isSystemPackage(browserPackage)) { |
| 592 | browserPackage = null; |
| 593 | } |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 594 | } |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 595 | grantPermissionsToPackage(browserPackage, userId, |
| 596 | false /* ignoreSystemPackage */, LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 597 | |
| 598 | // Voice interaction |
| 599 | if (voiceInteractPackageNames != null) { |
| 600 | for (String voiceInteractPackageName : voiceInteractPackageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 601 | grantPermissionsToSystemPackage(voiceInteractPackageName, userId, |
| 602 | CONTACTS_PERMISSIONS, CALENDAR_PERMISSIONS, MICROPHONE_PERMISSIONS, |
| 603 | PHONE_PERMISSIONS, SMS_PERMISSIONS, LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 608 | // Allow voice search on low-ram devices |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 609 | grantPermissionsToSystemPackage( |
| 610 | getDefaultSystemHandlerActivityPackage( |
| 611 | SearchManager.INTENT_ACTION_GLOBAL_SEARCH, userId), |
| 612 | userId, MICROPHONE_PERMISSIONS, LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // Voice recognition |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 616 | Intent voiceRecoIntent = new Intent(RecognitionService.SERVICE_INTERFACE) |
| 617 | .addCategory(Intent.CATEGORY_DEFAULT); |
| 618 | grantPermissionsToSystemPackage( |
| 619 | getDefaultSystemHandlerServicePackage(voiceRecoIntent, userId), userId, |
| 620 | MICROPHONE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 621 | |
| 622 | // Location |
| 623 | if (locationPackageNames != null) { |
| 624 | for (String packageName : locationPackageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 625 | grantPermissionsToSystemPackage(packageName, userId, |
| 626 | CONTACTS_PERMISSIONS, CALENDAR_PERMISSIONS, MICROPHONE_PERMISSIONS, |
| 627 | PHONE_PERMISSIONS, SMS_PERMISSIONS, CAMERA_PERMISSIONS, |
| 628 | SENSORS_PERMISSIONS, STORAGE_PERMISSIONS); |
| 629 | grantSystemFixedPermissionsToSystemPackage(packageName, userId, |
| 630 | LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | // Music |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 635 | Intent musicIntent = new Intent(Intent.ACTION_VIEW) |
| 636 | .addCategory(Intent.CATEGORY_DEFAULT) |
| 637 | .setDataAndType(Uri.fromFile(new File("foo.mp3")), AUDIO_MIME_TYPE); |
| 638 | grantPermissionsToSystemPackage( |
| 639 | getDefaultSystemHandlerActivityPackage(musicIntent, userId), userId, |
| 640 | STORAGE_PERMISSIONS, MEDIA_AURAL_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 641 | |
| 642 | // Home |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 643 | Intent homeIntent = new Intent(Intent.ACTION_MAIN) |
| 644 | .addCategory(Intent.CATEGORY_HOME) |
| 645 | .addCategory(Intent.CATEGORY_LAUNCHER_APP); |
| 646 | grantPermissionsToSystemPackage( |
| 647 | getDefaultSystemHandlerActivityPackage(homeIntent, userId), userId, |
| 648 | LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 649 | |
| 650 | // Watches |
| 651 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH, 0)) { |
| 652 | // Home application on watches |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 653 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 654 | String wearPackage = getDefaultSystemHandlerActivityPackageForCategory( |
| 655 | Intent.CATEGORY_HOME_MAIN, userId); |
| 656 | grantPermissionsToSystemPackage(wearPackage, userId, |
| 657 | CONTACTS_PERMISSIONS, MICROPHONE_PERMISSIONS, LOCATION_PERMISSIONS); |
| 658 | grantSystemFixedPermissionsToSystemPackage(wearPackage, userId, PHONE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 659 | |
| 660 | // Fitness tracking on watches |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 661 | grantPermissionsToSystemPackage( |
| 662 | getDefaultSystemHandlerActivityPackage(ACTION_TRACK, userId), userId, |
| 663 | SENSORS_PERMISSIONS, LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // Print Spooler |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 667 | grantSystemFixedPermissionsToSystemPackage(PrintManager.PRINT_SPOOLER_PACKAGE_NAME, userId, |
| 668 | LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 669 | |
| 670 | // EmergencyInfo |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 671 | grantSystemFixedPermissionsToSystemPackage( |
| 672 | getDefaultSystemHandlerActivityPackage( |
| 673 | TelephonyManager.ACTION_EMERGENCY_ASSISTANCE, userId), |
| 674 | userId, CONTACTS_PERMISSIONS, PHONE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 675 | |
| 676 | // NFC Tag viewer |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 677 | Intent nfcTagIntent = new Intent(Intent.ACTION_VIEW) |
| 678 | .setType("vnd.android.cursor.item/ndef_msg"); |
| 679 | grantPermissionsToSystemPackage( |
| 680 | getDefaultSystemHandlerActivityPackage(nfcTagIntent, userId), userId, |
| 681 | CONTACTS_PERMISSIONS, PHONE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 682 | |
| 683 | // Storage Manager |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 684 | grantSystemFixedPermissionsToSystemPackage( |
| 685 | getDefaultSystemHandlerActivityPackage( |
| 686 | StorageManager.ACTION_MANAGE_STORAGE, userId), |
| 687 | userId, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 688 | |
| 689 | // Companion devices |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 690 | grantSystemFixedPermissionsToSystemPackage( |
| 691 | CompanionDeviceManager.COMPANION_DEVICE_DISCOVERY_PACKAGE_NAME, userId, |
| 692 | LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 693 | |
| 694 | // Ringtone Picker |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 695 | grantSystemFixedPermissionsToSystemPackage( |
| 696 | getDefaultSystemHandlerActivityPackage( |
| 697 | RingtoneManager.ACTION_RINGTONE_PICKER, userId), |
| 698 | userId, STORAGE_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 699 | |
Abodunrinwa Toki | 2c01b6c | 2018-02-13 17:47:48 +0000 | [diff] [blame] | 700 | // TextClassifier Service |
Ye Wen | 8e8b2d5 | 2018-03-14 11:48:24 -0700 | [diff] [blame] | 701 | String textClassifierPackageName = |
| 702 | mContext.getPackageManager().getSystemTextClassifierPackageName(); |
| 703 | if (!TextUtils.isEmpty(textClassifierPackageName)) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 704 | grantPermissionsToSystemPackage(textClassifierPackageName, userId, |
| 705 | PHONE_PERMISSIONS, SMS_PERMISSIONS, CALENDAR_PERMISSIONS, |
| 706 | LOCATION_PERMISSIONS, CONTACTS_PERMISSIONS); |
Abodunrinwa Toki | 2c01b6c | 2018-02-13 17:47:48 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Anton Philippov | 4b3a1f5 | 2018-05-04 14:46:44 +0100 | [diff] [blame] | 709 | // There is no real "marker" interface to identify the shared storage backup, it is |
| 710 | // hardcoded in BackupManagerService.SHARED_BACKUP_AGENT_PACKAGE. |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 711 | grantSystemFixedPermissionsToSystemPackage("com.android.sharedstoragebackup", userId, |
| 712 | STORAGE_PERMISSIONS); |
Anton Philippov | 4b3a1f5 | 2018-05-04 14:46:44 +0100 | [diff] [blame] | 713 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 714 | if (mPermissionGrantedCallback != null) { |
| 715 | mPermissionGrantedCallback.onDefaultRuntimePermissionsGranted(userId); |
| 716 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 719 | private String getDefaultSystemHandlerActivityPackageForCategory(String category, int userId) { |
| 720 | return getDefaultSystemHandlerActivityPackage( |
| 721 | new Intent(Intent.ACTION_MAIN).addCategory(category), userId); |
| 722 | } |
| 723 | |
| 724 | @SafeVarargs |
| 725 | private final void grantPermissionToEachSystemPackage( |
| 726 | ArrayList<String> packages, int userId, Set<String>... permissions) { |
| 727 | if (packages == null) return; |
| 728 | final int count = packages.size(); |
| 729 | for (int i = 0; i < count; i++) { |
| 730 | grantPermissionsToSystemPackage(packages.get(i), userId, permissions); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 731 | } |
Eugene Susla | 47e8820 | 2018-07-02 18:48:55 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 734 | private String getKnownPackage(int knownPkgId, int userId) { |
| 735 | return mServiceInternal.getKnownPackageName(knownPkgId, userId); |
| 736 | } |
| 737 | |
| 738 | private void grantDefaultPermissionsToDefaultSystemDialerApp( |
| 739 | String dialerPackage, int userId) { |
| 740 | if (dialerPackage == null) { |
| 741 | return; |
Philip P. Moltmann | fa89422 | 2018-09-18 21:24:00 +0000 | [diff] [blame] | 742 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 743 | boolean isPhonePermFixed = |
| 744 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH, 0); |
| 745 | if (isPhonePermFixed) { |
| 746 | grantSystemFixedPermissionsToSystemPackage(dialerPackage, userId, PHONE_PERMISSIONS); |
| 747 | } else { |
| 748 | grantPermissionsToSystemPackage(dialerPackage, userId, PHONE_PERMISSIONS); |
| 749 | } |
| 750 | grantPermissionsToSystemPackage(dialerPackage, userId, |
| 751 | CONTACTS_PERMISSIONS, SMS_PERMISSIONS, MICROPHONE_PERMISSIONS, CAMERA_PERMISSIONS); |
| 752 | } |
| 753 | |
| 754 | private void grantDefaultPermissionsToDefaultSystemSmsApp(String smsPackage, int userId) { |
| 755 | grantPermissionsToSystemPackage(smsPackage, userId, |
| 756 | PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, SMS_PERMISSIONS, |
| 757 | STORAGE_PERMISSIONS, MICROPHONE_PERMISSIONS, CAMERA_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 760 | private void grantDefaultPermissionsToDefaultSystemUseOpenWifiApp( |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 761 | String useOpenWifiPackage, int userId) { |
| 762 | grantPermissionsToSystemPackage( |
| 763 | useOpenWifiPackage, userId, COARSE_LOCATION_PERMISSIONS); |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 766 | public void grantDefaultPermissionsToDefaultSmsApp(String packageName, int userId) { |
| 767 | Log.i(TAG, "Granting permissions to default sms app for user:" + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 768 | grantIgnoringSystemPackage(packageName, userId, |
| 769 | PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, SMS_PERMISSIONS, STORAGE_PERMISSIONS, |
| 770 | MICROPHONE_PERMISSIONS, CAMERA_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | public void grantDefaultPermissionsToDefaultDialerApp(String packageName, int userId) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 774 | mServiceInternal.onDefaultDialerAppChanged(packageName, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 775 | Log.i(TAG, "Granting permissions to default dialer app for user:" + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 776 | grantIgnoringSystemPackage(packageName, userId, |
| 777 | PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, SMS_PERMISSIONS, |
| 778 | MICROPHONE_PERMISSIONS, CAMERA_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 781 | public void grantDefaultPermissionsToDefaultUseOpenWifiApp(String packageName, int userId) { |
| 782 | Log.i(TAG, "Granting permissions to default Use Open WiFi app for user:" + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 783 | grantIgnoringSystemPackage(packageName, userId, COARSE_LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | public void grantDefaultPermissionsToDefaultSimCallManager(String packageName, int userId) { |
| 787 | if (packageName == null) { |
| 788 | return; |
| 789 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 790 | Log.i(TAG, "Granting permissions to sim call manager for user:" + userId); |
Eugene Susla | e4240e7 | 2018-11-02 10:58:11 -0700 | [diff] [blame] | 791 | grantPermissionsToPackage(packageName, userId, false /* ignoreSystemPackage */, |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 792 | PHONE_PERMISSIONS, MICROPHONE_PERMISSIONS); |
| 793 | } |
| 794 | |
| 795 | private void grantDefaultPermissionsToDefaultSystemSimCallManager( |
| 796 | String packageName, int userId) { |
| 797 | if (isSystemPackage(packageName)) { |
| 798 | grantDefaultPermissionsToDefaultSimCallManager(packageName, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | public void grantDefaultPermissionsToEnabledCarrierApps(String[] packageNames, int userId) { |
| 803 | Log.i(TAG, "Granting permissions to enabled carrier apps for user:" + userId); |
| 804 | if (packageNames == null) { |
| 805 | return; |
| 806 | } |
| 807 | for (String packageName : packageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 808 | grantPermissionsToSystemPackage(packageName, userId, |
| 809 | PHONE_PERMISSIONS, LOCATION_PERMISSIONS, SMS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | public void grantDefaultPermissionsToEnabledImsServices(String[] packageNames, int userId) { |
| 814 | Log.i(TAG, "Granting permissions to enabled ImsServices for user:" + userId); |
| 815 | if (packageNames == null) { |
| 816 | return; |
| 817 | } |
| 818 | for (String packageName : packageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 819 | grantPermissionsToSystemPackage(packageName, userId, |
| 820 | PHONE_PERMISSIONS, MICROPHONE_PERMISSIONS, LOCATION_PERMISSIONS, |
| 821 | CAMERA_PERMISSIONS, CONTACTS_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
Nathan Harold | 76ad1a3 | 2018-02-20 14:31:09 -0800 | [diff] [blame] | 825 | public void grantDefaultPermissionsToEnabledTelephonyDataServices( |
| 826 | String[] packageNames, int userId) { |
| 827 | Log.i(TAG, "Granting permissions to enabled data services for user:" + userId); |
| 828 | if (packageNames == null) { |
| 829 | return; |
| 830 | } |
| 831 | for (String packageName : packageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 832 | // Grant these permissions as system-fixed, so that nobody can accidentally |
| 833 | // break cellular data. |
| 834 | grantSystemFixedPermissionsToSystemPackage(packageName, userId, |
| 835 | PHONE_PERMISSIONS, LOCATION_PERMISSIONS); |
Nathan Harold | 76ad1a3 | 2018-02-20 14:31:09 -0800 | [diff] [blame] | 836 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 837 | |
Nathan Harold | 76ad1a3 | 2018-02-20 14:31:09 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | public void revokeDefaultPermissionsFromDisabledTelephonyDataServices( |
| 841 | String[] packageNames, int userId) { |
| 842 | Log.i(TAG, "Revoking permissions from disabled data services for user:" + userId); |
| 843 | if (packageNames == null) { |
| 844 | return; |
| 845 | } |
| 846 | for (String packageName : packageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 847 | PackageInfo pkg = getSystemPackageInfo(packageName); |
| 848 | if (isSystemPackage(pkg) && doesPackageSupportRuntimePermissions(pkg)) { |
| 849 | revokeRuntimePermissions(packageName, PHONE_PERMISSIONS, true, userId); |
| 850 | revokeRuntimePermissions(packageName, LOCATION_PERMISSIONS, true, userId); |
Nathan Harold | 76ad1a3 | 2018-02-20 14:31:09 -0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Holly Jiuyu Sun | 349e214 | 2018-03-26 15:29:42 -0700 | [diff] [blame] | 855 | public void grantDefaultPermissionsToActiveLuiApp(String packageName, int userId) { |
| 856 | Log.i(TAG, "Granting permissions to active LUI app for user:" + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 857 | grantSystemFixedPermissionsToSystemPackage(packageName, userId, CAMERA_PERMISSIONS); |
Holly Jiuyu Sun | 349e214 | 2018-03-26 15:29:42 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | public void revokeDefaultPermissionsFromLuiApps(String[] packageNames, int userId) { |
| 861 | Log.i(TAG, "Revoke permissions from LUI apps for user:" + userId); |
| 862 | if (packageNames == null) { |
| 863 | return; |
| 864 | } |
| 865 | for (String packageName : packageNames) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 866 | PackageInfo pkg = getSystemPackageInfo(packageName); |
| 867 | if (isSystemPackage(pkg) && doesPackageSupportRuntimePermissions(pkg)) { |
| 868 | revokeRuntimePermissions(packageName, CAMERA_PERMISSIONS, true, userId); |
Holly Jiuyu Sun | 349e214 | 2018-03-26 15:29:42 -0700 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 873 | public void grantDefaultPermissionsToDefaultBrowser(String packageName, int userId) { |
| 874 | Log.i(TAG, "Granting permissions to default browser for user:" + userId); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 875 | grantPermissionsToSystemPackage(packageName, userId, LOCATION_PERMISSIONS); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 878 | private String getDefaultSystemHandlerActivityPackage(String intentAction, int userId) { |
| 879 | return getDefaultSystemHandlerActivityPackage(new Intent(intentAction), userId); |
| 880 | } |
| 881 | |
| 882 | private String getDefaultSystemHandlerActivityPackage(Intent intent, int userId) { |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 883 | ResolveInfo handler = mContext.getPackageManager().resolveActivityAsUser( |
| 884 | intent, DEFAULT_INTENT_QUERY_FLAGS, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 885 | if (handler == null || handler.activityInfo == null) { |
| 886 | return null; |
| 887 | } |
| 888 | if (mServiceInternal.isResolveActivityComponent(handler.activityInfo)) { |
| 889 | return null; |
| 890 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 891 | String packageName = handler.activityInfo.packageName; |
| 892 | return isSystemPackage(packageName) ? packageName : null; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 895 | private String getDefaultSystemHandlerServicePackage(String intentAction, int userId) { |
| 896 | return getDefaultSystemHandlerServicePackage(new Intent(intentAction), userId); |
| 897 | } |
| 898 | |
| 899 | private String getDefaultSystemHandlerServicePackage( |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 900 | Intent intent, int userId) { |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 901 | List<ResolveInfo> handlers = mContext.getPackageManager().queryIntentServicesAsUser( |
| 902 | intent, DEFAULT_INTENT_QUERY_FLAGS, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 903 | if (handlers == null) { |
| 904 | return null; |
| 905 | } |
| 906 | final int handlerCount = handlers.size(); |
| 907 | for (int i = 0; i < handlerCount; i++) { |
| 908 | ResolveInfo handler = handlers.get(i); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 909 | String handlerPackage = handler.serviceInfo.packageName; |
| 910 | if (isSystemPackage(handlerPackage)) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 911 | return handlerPackage; |
| 912 | } |
| 913 | } |
| 914 | return null; |
| 915 | } |
| 916 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 917 | private ArrayList<String> getHeadlessSyncAdapterPackages( |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 918 | String[] syncAdapterPackageNames, int userId) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 919 | ArrayList<String> syncAdapterPackages = new ArrayList<>(); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 920 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 921 | Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 922 | |
| 923 | for (String syncAdapterPackageName : syncAdapterPackageNames) { |
| 924 | homeIntent.setPackage(syncAdapterPackageName); |
| 925 | |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 926 | ResolveInfo homeActivity = mContext.getPackageManager().resolveActivityAsUser( |
| 927 | homeIntent, DEFAULT_INTENT_QUERY_FLAGS, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 928 | if (homeActivity != null) { |
| 929 | continue; |
| 930 | } |
| 931 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 932 | if (isSystemPackage(syncAdapterPackageName)) { |
| 933 | syncAdapterPackages.add(syncAdapterPackageName); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | return syncAdapterPackages; |
| 938 | } |
| 939 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 940 | private String getDefaultProviderAuthorityPackage(String authority, int userId) { |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 941 | ProviderInfo provider = mContext.getPackageManager().resolveContentProviderAsUser( |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 942 | authority, DEFAULT_INTENT_QUERY_FLAGS, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 943 | if (provider != null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 944 | return provider.packageName; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 945 | } |
| 946 | return null; |
| 947 | } |
| 948 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 949 | private boolean isSystemPackage(String packageName) { |
Philip P. Moltmann | c701e7e | 2018-09-18 16:22:54 -0700 | [diff] [blame] | 950 | return isSystemPackage(getPackageInfo(packageName)); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 953 | private boolean isSystemPackage(PackageInfo pkg) { |
| 954 | if (pkg == null) { |
| 955 | return false; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 956 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 957 | return pkg.applicationInfo.isSystemApp() |
| 958 | && !isSysComponentOrPersistentPlatformSignedPrivApp(pkg); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 961 | private void grantRuntimePermissions(PackageInfo pkg, Set<String> permissions, |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 962 | boolean systemFixed, int userId) { |
| 963 | grantRuntimePermissions(pkg, permissions, systemFixed, false, userId); |
| 964 | } |
| 965 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 966 | private void revokeRuntimePermissions(String packageName, Set<String> permissions, |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 967 | boolean systemFixed, int userId) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 968 | PackageInfo pkg = getSystemPackageInfo(packageName); |
| 969 | if (ArrayUtils.isEmpty(pkg.requestedPermissions)) { |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 970 | return; |
| 971 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 972 | Set<String> revokablePermissions = new ArraySet<>(Arrays.asList(pkg.requestedPermissions)); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 973 | |
| 974 | for (String permission : permissions) { |
| 975 | // We can't revoke what wasn't requested. |
| 976 | if (!revokablePermissions.contains(permission)) { |
| 977 | continue; |
| 978 | } |
| 979 | |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 980 | UserHandle user = UserHandle.of(userId); |
| 981 | final int flags = mContext.getPackageManager() |
| 982 | .getPermissionFlags(permission, packageName, user); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 983 | |
| 984 | // We didn't get this through the default grant policy. Move along. |
| 985 | if ((flags & PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT) == 0) { |
| 986 | continue; |
| 987 | } |
| 988 | // We aren't going to clobber device policy with a DefaultGrant. |
| 989 | if ((flags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0) { |
| 990 | continue; |
| 991 | } |
| 992 | // Do not revoke system fixed permissions unless caller set them that way; |
| 993 | // there is no refcount for the number of sources of this, so there |
| 994 | // should be at most one grantor doing SYSTEM_FIXED for any given package. |
| 995 | if ((flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0 && !systemFixed) { |
| 996 | continue; |
| 997 | } |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 998 | mContext.getPackageManager().revokeRuntimePermission(packageName, permission, user); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 999 | |
| 1000 | if (DEBUG) { |
| 1001 | Log.i(TAG, "revoked " + (systemFixed ? "fixed " : "not fixed ") |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1002 | + permission + " to " + packageName); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | // Remove the GRANTED_BY_DEFAULT flag without touching the others. |
| 1006 | // Note that we do not revoke FLAG_PERMISSION_SYSTEM_FIXED. That bit remains |
| 1007 | // sticky once set. |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 1008 | mContext.getPackageManager().updatePermissionFlags(permission, packageName, |
| 1009 | PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT, 0, user); |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1013 | /** |
| 1014 | * Check if a permission is already fixed or is set by the user. |
| 1015 | * |
| 1016 | * <p>A permission should not be set by the default policy if the user or other policies already |
| 1017 | * set the permission. |
| 1018 | * |
| 1019 | * @param flags The flags of the permission |
| 1020 | * |
| 1021 | * @return {@code true} iff the permission can be set without violating a policy of the users |
| 1022 | * intention |
| 1023 | */ |
| 1024 | private boolean isFixedOrUserSet(int flags) { |
| 1025 | return (flags & (PackageManager.FLAG_PERMISSION_USER_SET |
| 1026 | | PackageManager.FLAG_PERMISSION_USER_FIXED |
| 1027 | | PackageManager.FLAG_PERMISSION_POLICY_FIXED |
| 1028 | | PackageManager.FLAG_PERMISSION_SYSTEM_FIXED)) != 0; |
| 1029 | } |
| 1030 | |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1031 | /** |
| 1032 | * Return the background permission for a permission. |
| 1033 | * |
| 1034 | * @param permission The name of the foreground permission |
| 1035 | * |
| 1036 | * @return The name of the background permission or {@code null} if the permission has no |
| 1037 | * background permission |
| 1038 | */ |
| 1039 | private @Nullable String getBackgroundPermission(@NonNull String permission) { |
| 1040 | try { |
| 1041 | return mContext.getPackageManager().getPermissionInfo(permission, |
| 1042 | 0).backgroundPermission; |
| 1043 | } catch (NameNotFoundException e) { |
| 1044 | return null; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1048 | private void grantRuntimePermissions(PackageInfo pkg, |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1049 | Set<String> permissionsWithoutSplits, boolean systemFixed, boolean ignoreSystemPackage, |
| 1050 | int userId) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1051 | if (pkg == null) { |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | String[] requestedPermissions = pkg.requestedPermissions; |
| 1056 | if (ArrayUtils.isEmpty(requestedPermissions)) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1060 | PackageManager pm = mContext.getPackageManager(); |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1061 | final ArraySet<String> permissions = new ArraySet<>(permissionsWithoutSplits); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1062 | ApplicationInfo applicationInfo = pkg.applicationInfo; |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1063 | |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1064 | int newFlags = PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
| 1065 | if (systemFixed) { |
| 1066 | newFlags |= PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 1067 | } |
| 1068 | |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1069 | // Automatically attempt to grant split permissions to older APKs |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 1070 | final List<PermissionManager.SplitPermissionInfo> splitPermissions = |
| 1071 | mContext.getSystemService(PermissionManager.class).getSplitPermissions(); |
| 1072 | final int numSplitPerms = splitPermissions.size(); |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1073 | for (int splitPermNum = 0; splitPermNum < numSplitPerms; splitPermNum++) { |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 1074 | final PermissionManager.SplitPermissionInfo splitPerm = |
| 1075 | splitPermissions.get(splitPermNum); |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1076 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1077 | if (applicationInfo != null |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 1078 | && applicationInfo.targetSdkVersion < splitPerm.getTargetSdk() |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 1079 | && permissionsWithoutSplits.contains(splitPerm.getSplitPermission())) { |
| 1080 | permissions.addAll(splitPerm.getNewPermissions()); |
Philip P. Moltmann | 8b56003 | 2018-07-12 09:51:02 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1084 | Set<String> grantablePermissions = null; |
| 1085 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 1086 | // In some cases, like for the Phone or SMS app, we grant permissions regardless |
| 1087 | // of if the version on the system image declares the permission as used since |
| 1088 | // selecting the app as the default for that function the user makes a deliberate |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1089 | // choice to grant this app the permissions needed to function. For all other |
| 1090 | // apps, (default grants on first boot and user creation) we don't grant default |
| 1091 | // permissions if the version on the system image does not declare them. |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1092 | if (!ignoreSystemPackage |
| 1093 | && applicationInfo != null |
| 1094 | && applicationInfo.isUpdatedSystemApp()) { |
| 1095 | final PackageInfo disabledPkg = getSystemPackageInfo( |
| 1096 | mServiceInternal.getDisabledSystemPackageName(pkg.packageName)); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1097 | if (disabledPkg != null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1098 | if (ArrayUtils.isEmpty(disabledPkg.requestedPermissions)) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | if (!requestedPermissions.equals(disabledPkg.requestedPermissions)) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1102 | grantablePermissions = new ArraySet<>(Arrays.asList(requestedPermissions)); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1103 | requestedPermissions = disabledPkg.requestedPermissions; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1108 | final int numRequestedPermissions = requestedPermissions.length; |
| 1109 | |
| 1110 | // Sort requested permissions so that all permissions that are a foreground permission (i.e. |
| 1111 | // permisions that have background permission) are before their background permissions. |
| 1112 | final String[] sortedRequestedPermissions = new String[numRequestedPermissions]; |
| 1113 | int numForeground = 0; |
| 1114 | int numOther = 0; |
| 1115 | for (int i = 0; i < numRequestedPermissions; i++) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1116 | String permission = requestedPermissions[i]; |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1117 | if (getBackgroundPermission(permission) != null) { |
| 1118 | sortedRequestedPermissions[numForeground] = permission; |
| 1119 | numForeground++; |
| 1120 | } else { |
| 1121 | sortedRequestedPermissions[numRequestedPermissions - 1 - numOther] = |
| 1122 | permission; |
| 1123 | numOther++; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | for (int requestedPermissionNum = 0; requestedPermissionNum < numRequestedPermissions; |
| 1128 | requestedPermissionNum++) { |
| 1129 | String permission = requestedPermissions[requestedPermissionNum]; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1130 | |
| 1131 | // If there is a disabled system app it may request a permission the updated |
| 1132 | // version ot the data partition doesn't, In this case skip the permission. |
| 1133 | if (grantablePermissions != null && !grantablePermissions.contains(permission)) { |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | if (permissions.contains(permission)) { |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 1138 | UserHandle user = UserHandle.of(userId); |
| 1139 | final int flags = mContext.getPackageManager().getPermissionFlags( |
| 1140 | permission, pkg.packageName, user); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1141 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1142 | // Certain flags imply that the permission's current state by the system or |
| 1143 | // device/profile owner or the user. In these cases we do not want to clobber the |
| 1144 | // current state. |
| 1145 | // |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1146 | // Unless the caller wants to override user choices. The override is |
| 1147 | // to make sure we can grant the needed permission to the default |
| 1148 | // sms and phone apps after the user chooses this in the UI. |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1149 | if (!isFixedOrUserSet(flags) || ignoreSystemPackage) { |
Nathan Harold | d66b9f3 | 2018-03-14 19:55:38 -0700 | [diff] [blame] | 1150 | // Never clobber policy fixed permissions. |
| 1151 | // We must allow the grant of a system-fixed permission because |
| 1152 | // system-fixed is sticky, but the permission itself may be revoked. |
| 1153 | if ((flags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1154 | continue; |
| 1155 | } |
| 1156 | |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1157 | int uid = UserHandle.getUid(userId, |
| 1158 | UserHandle.getAppId(pkg.applicationInfo.uid)); |
| 1159 | String op = AppOpsManager.permissionToOp(permission); |
| 1160 | |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 1161 | mContext.getPackageManager() |
| 1162 | .grantRuntimePermission(pkg.packageName, permission, user); |
Philip P. Moltmann | 12d0ee0 | 2018-10-30 14:47:33 -0700 | [diff] [blame^] | 1163 | |
| 1164 | mContext.getPackageManager().updatePermissionFlags(permission, pkg.packageName, |
| 1165 | newFlags, newFlags, user); |
| 1166 | |
| 1167 | List<String> fgPerms = mPermissionManager.getBackgroundPermissions() |
| 1168 | .get(permission); |
| 1169 | if (fgPerms != null) { |
| 1170 | int numFgPerms = fgPerms.size(); |
| 1171 | for (int fgPermNum = 0; fgPermNum < numFgPerms; fgPermNum++) { |
| 1172 | String fgPerm = fgPerms.get(fgPermNum); |
| 1173 | |
| 1174 | if (pm.checkPermission(fgPerm, pkg.packageName) |
| 1175 | == PackageManager.PERMISSION_GRANTED) { |
| 1176 | // Upgrade the app-op state of the fg permission to allow bg access |
| 1177 | mContext.getSystemService(AppOpsManager.class).setMode( |
| 1178 | AppOpsManager.permissionToOp(fgPerm), uid, |
| 1179 | pkg.packageName, AppOpsManager.MODE_ALLOWED); |
| 1180 | |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | String bgPerm = getBackgroundPermission(permission); |
| 1187 | if (bgPerm == null) { |
| 1188 | if (op != null) { |
| 1189 | mContext.getSystemService(AppOpsManager.class).setMode(op, uid, |
| 1190 | pkg.packageName, AppOpsManager.MODE_ALLOWED); |
| 1191 | } |
| 1192 | } else { |
| 1193 | int mode; |
| 1194 | if (pm.checkPermission(bgPerm, pkg.packageName) |
| 1195 | == PackageManager.PERMISSION_GRANTED) { |
| 1196 | mode = AppOpsManager.MODE_ALLOWED; |
| 1197 | } else { |
| 1198 | mode = AppOpsManager.MODE_FOREGROUND; |
| 1199 | } |
| 1200 | |
| 1201 | mContext.getSystemService(AppOpsManager.class).setMode(op, uid, |
| 1202 | pkg.packageName, mode); |
| 1203 | } |
| 1204 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1205 | if (DEBUG) { |
| 1206 | Log.i(TAG, "Granted " + (systemFixed ? "fixed " : "not fixed ") |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1207 | + permission + " to default handler " + pkg); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1208 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // If a component gets a permission for being the default handler A |
| 1212 | // and also default handler B, we grant the weaker grant form. |
| 1213 | if ((flags & PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT) != 0 |
| 1214 | && (flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0 |
| 1215 | && !systemFixed) { |
| 1216 | if (DEBUG) { |
| 1217 | Log.i(TAG, "Granted not fixed " + permission + " to default handler " |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1218 | + pkg); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1219 | } |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 1220 | mContext.getPackageManager().updatePermissionFlags(permission, pkg.packageName, |
| 1221 | PackageManager.FLAG_PERMISSION_SYSTEM_FIXED, 0, user); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1227 | private PackageInfo getSystemPackageInfo(String pkg) { |
| 1228 | //TODO not MATCH_SYSTEM_ONLY? |
| 1229 | return getPackageInfo(pkg, PackageManager.MATCH_FACTORY_ONLY); |
| 1230 | } |
| 1231 | |
| 1232 | private PackageInfo getPackageInfo(String pkg) { |
| 1233 | return getPackageInfo(pkg, 0 /* extraFlags */); |
| 1234 | } |
| 1235 | |
| 1236 | private PackageInfo getPackageInfo(String pkg, |
| 1237 | @PackageManager.PackageInfoFlags int extraFlags) { |
Eugene Susla | 4abd2e6 | 2018-11-02 17:35:07 -0700 | [diff] [blame] | 1238 | try { |
| 1239 | return mContext.getPackageManager().getPackageInfo(pkg, |
| 1240 | DEFAULT_PACKAGE_INFO_QUERY_FLAGS | extraFlags); |
| 1241 | } catch (NameNotFoundException e) { |
| 1242 | return null; |
| 1243 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | private boolean isSysComponentOrPersistentPlatformSignedPrivApp(PackageInfo pkg) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1247 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 1248 | return true; |
| 1249 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1250 | if (!pkg.applicationInfo.isPrivilegedApp()) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1253 | final PackageInfo disabledPkg = getSystemPackageInfo( |
| 1254 | mServiceInternal.getDisabledSystemPackageName(pkg.applicationInfo.packageName)); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1255 | if (disabledPkg != null) { |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1256 | ApplicationInfo disabledPackageAppInfo = disabledPkg.applicationInfo; |
| 1257 | if (disabledPackageAppInfo != null |
| 1258 | && (disabledPackageAppInfo.flags & ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
| 1261 | } else if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_PERSISTENT) == 0) { |
| 1262 | return false; |
| 1263 | } |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1264 | return mServiceInternal.isPlatformSigned(pkg.packageName); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | private void grantDefaultPermissionExceptions(int userId) { |
| 1268 | mHandler.removeMessages(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS); |
| 1269 | |
| 1270 | synchronized (mLock) { |
| 1271 | // mGrantExceptions is null only before the first read and then |
| 1272 | // it serves as a cache of the default grants that should be |
| 1273 | // performed for every user. If there is an entry then the app |
| 1274 | // is on the system image and supports runtime permissions. |
| 1275 | if (mGrantExceptions == null) { |
| 1276 | mGrantExceptions = readDefaultPermissionExceptionsLocked(); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | Set<String> permissions = null; |
| 1281 | final int exceptionCount = mGrantExceptions.size(); |
| 1282 | for (int i = 0; i < exceptionCount; i++) { |
| 1283 | String packageName = mGrantExceptions.keyAt(i); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1284 | PackageInfo pkg = getSystemPackageInfo(packageName); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1285 | List<DefaultPermissionGrant> permissionGrants = mGrantExceptions.valueAt(i); |
| 1286 | final int permissionGrantCount = permissionGrants.size(); |
| 1287 | for (int j = 0; j < permissionGrantCount; j++) { |
| 1288 | DefaultPermissionGrant permissionGrant = permissionGrants.get(j); |
Jeff Sharkey | 7a80760 | 2018-10-18 13:21:55 -0600 | [diff] [blame] | 1289 | if (!isPermissionDangerous(permissionGrant.name)) { |
| 1290 | Log.w(TAG, "Ignoring permission " + permissionGrant.name |
| 1291 | + " which isn't dangerous"); |
| 1292 | continue; |
| 1293 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1294 | if (permissions == null) { |
| 1295 | permissions = new ArraySet<>(); |
| 1296 | } else { |
| 1297 | permissions.clear(); |
| 1298 | } |
Jeff Sharkey | 9d4654c | 2018-10-25 09:52:57 -0600 | [diff] [blame] | 1299 | permissions.add(permissionGrant.name); |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1300 | grantRuntimePermissions(pkg, permissions, permissionGrant.fixed, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | private File[] getDefaultPermissionFiles() { |
| 1306 | ArrayList<File> ret = new ArrayList<File>(); |
| 1307 | File dir = new File(Environment.getRootDirectory(), "etc/default-permissions"); |
| 1308 | if (dir.isDirectory() && dir.canRead()) { |
| 1309 | Collections.addAll(ret, dir.listFiles()); |
| 1310 | } |
| 1311 | dir = new File(Environment.getVendorDirectory(), "etc/default-permissions"); |
| 1312 | if (dir.isDirectory() && dir.canRead()) { |
| 1313 | Collections.addAll(ret, dir.listFiles()); |
| 1314 | } |
Jiyong Park | 0989e38 | 2018-03-13 10:26:47 +0900 | [diff] [blame] | 1315 | dir = new File(Environment.getOdmDirectory(), "etc/default-permissions"); |
| 1316 | if (dir.isDirectory() && dir.canRead()) { |
| 1317 | Collections.addAll(ret, dir.listFiles()); |
| 1318 | } |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1319 | dir = new File(Environment.getProductDirectory(), "etc/default-permissions"); |
| 1320 | if (dir.isDirectory() && dir.canRead()) { |
| 1321 | Collections.addAll(ret, dir.listFiles()); |
| 1322 | } |
Dario Freni | 1ae46d7 | 2018-08-17 15:56:43 +0100 | [diff] [blame] | 1323 | dir = new File(Environment.getProductServicesDirectory(), |
| 1324 | "etc/default-permissions"); |
| 1325 | if (dir.isDirectory() && dir.canRead()) { |
| 1326 | Collections.addAll(ret, dir.listFiles()); |
| 1327 | } |
Ralph Nathan | bd11158 | 2018-03-21 14:53:23 -0700 | [diff] [blame] | 1328 | // For IoT devices, we check the oem partition for default permissions for each app. |
| 1329 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED, 0)) { |
| 1330 | dir = new File(Environment.getOemDirectory(), "etc/default-permissions"); |
| 1331 | if (dir.isDirectory() && dir.canRead()) { |
| 1332 | Collections.addAll(ret, dir.listFiles()); |
| 1333 | } |
| 1334 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1335 | return ret.isEmpty() ? null : ret.toArray(new File[0]); |
| 1336 | } |
| 1337 | |
| 1338 | private @NonNull ArrayMap<String, List<DefaultPermissionGrant>> |
| 1339 | readDefaultPermissionExceptionsLocked() { |
| 1340 | File[] files = getDefaultPermissionFiles(); |
| 1341 | if (files == null) { |
| 1342 | return new ArrayMap<>(0); |
| 1343 | } |
| 1344 | |
| 1345 | ArrayMap<String, List<DefaultPermissionGrant>> grantExceptions = new ArrayMap<>(); |
| 1346 | |
| 1347 | // Iterate over the files in the directory and scan .xml files |
| 1348 | for (File file : files) { |
| 1349 | if (!file.getPath().endsWith(".xml")) { |
| 1350 | Slog.i(TAG, "Non-xml file " + file |
| 1351 | + " in " + file.getParent() + " directory, ignoring"); |
| 1352 | continue; |
| 1353 | } |
| 1354 | if (!file.canRead()) { |
| 1355 | Slog.w(TAG, "Default permissions file " + file + " cannot be read"); |
| 1356 | continue; |
| 1357 | } |
| 1358 | try ( |
| 1359 | InputStream str = new BufferedInputStream(new FileInputStream(file)) |
| 1360 | ) { |
| 1361 | XmlPullParser parser = Xml.newPullParser(); |
| 1362 | parser.setInput(str, null); |
| 1363 | parse(parser, grantExceptions); |
| 1364 | } catch (XmlPullParserException | IOException e) { |
| 1365 | Slog.w(TAG, "Error reading default permissions file " + file, e); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | return grantExceptions; |
| 1370 | } |
| 1371 | |
| 1372 | private void parse(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>> |
| 1373 | outGrantExceptions) throws IOException, XmlPullParserException { |
| 1374 | final int outerDepth = parser.getDepth(); |
| 1375 | int type; |
| 1376 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1377 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1378 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1379 | continue; |
| 1380 | } |
| 1381 | if (TAG_EXCEPTIONS.equals(parser.getName())) { |
| 1382 | parseExceptions(parser, outGrantExceptions); |
| 1383 | } else { |
| 1384 | Log.e(TAG, "Unknown tag " + parser.getName()); |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | private void parseExceptions(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>> |
| 1390 | outGrantExceptions) throws IOException, XmlPullParserException { |
| 1391 | final int outerDepth = parser.getDepth(); |
| 1392 | int type; |
| 1393 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1394 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1395 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1396 | continue; |
| 1397 | } |
| 1398 | if (TAG_EXCEPTION.equals(parser.getName())) { |
| 1399 | String packageName = parser.getAttributeValue(null, ATTR_PACKAGE); |
| 1400 | |
| 1401 | List<DefaultPermissionGrant> packageExceptions = |
| 1402 | outGrantExceptions.get(packageName); |
| 1403 | if (packageExceptions == null) { |
| 1404 | // The package must be on the system image |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1405 | if (!isSystemPackage(packageName)) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1406 | Log.w(TAG, "Unknown package:" + packageName); |
| 1407 | XmlUtils.skipCurrentTag(parser); |
| 1408 | continue; |
| 1409 | } |
| 1410 | |
| 1411 | // The package must support runtime permissions |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1412 | if (!doesPackageSupportRuntimePermissions(getSystemPackageInfo(packageName))) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1413 | Log.w(TAG, "Skipping non supporting runtime permissions package:" |
| 1414 | + packageName); |
| 1415 | XmlUtils.skipCurrentTag(parser); |
| 1416 | continue; |
| 1417 | } |
| 1418 | packageExceptions = new ArrayList<>(); |
| 1419 | outGrantExceptions.put(packageName, packageExceptions); |
| 1420 | } |
| 1421 | |
| 1422 | parsePermission(parser, packageExceptions); |
| 1423 | } else { |
| 1424 | Log.e(TAG, "Unknown tag " + parser.getName() + "under <exceptions>"); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | private void parsePermission(XmlPullParser parser, List<DefaultPermissionGrant> |
| 1430 | outPackageExceptions) throws IOException, XmlPullParserException { |
| 1431 | final int outerDepth = parser.getDepth(); |
| 1432 | int type; |
| 1433 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1434 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1435 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | if (TAG_PERMISSION.contains(parser.getName())) { |
| 1440 | String name = parser.getAttributeValue(null, ATTR_NAME); |
| 1441 | if (name == null) { |
| 1442 | Log.w(TAG, "Mandatory name attribute missing for permission tag"); |
| 1443 | XmlUtils.skipCurrentTag(parser); |
| 1444 | continue; |
| 1445 | } |
| 1446 | |
| 1447 | final boolean fixed = XmlUtils.readBooleanAttribute(parser, ATTR_FIXED); |
| 1448 | |
| 1449 | DefaultPermissionGrant exception = new DefaultPermissionGrant(name, fixed); |
| 1450 | outPackageExceptions.add(exception); |
| 1451 | } else { |
| 1452 | Log.e(TAG, "Unknown tag " + parser.getName() + "under <exception>"); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
Philip P. Moltmann | b0be05c | 2018-09-19 02:46:56 +0000 | [diff] [blame] | 1457 | private static boolean doesPackageSupportRuntimePermissions(PackageInfo pkg) { |
| 1458 | return pkg.applicationInfo != null |
| 1459 | && pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Jeff Sharkey | 7a80760 | 2018-10-18 13:21:55 -0600 | [diff] [blame] | 1462 | private boolean isPermissionDangerous(String name) { |
| 1463 | try { |
| 1464 | final PermissionInfo pi = mContext.getPackageManager().getPermissionInfo(name, 0); |
Jeff Sharkey | 9d4654c | 2018-10-25 09:52:57 -0600 | [diff] [blame] | 1465 | return (pi.getProtection() == PermissionInfo.PROTECTION_DANGEROUS); |
Jeff Sharkey | 7a80760 | 2018-10-18 13:21:55 -0600 | [diff] [blame] | 1466 | } catch (NameNotFoundException e) { |
| 1467 | // When unknown assume it's dangerous to be on the safe side |
| 1468 | return true; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1472 | private static final class DefaultPermissionGrant { |
| 1473 | final String name; |
| 1474 | final boolean fixed; |
| 1475 | |
| 1476 | public DefaultPermissionGrant(String name, boolean fixed) { |
| 1477 | this.name = name; |
| 1478 | this.fixed = fixed; |
| 1479 | } |
| 1480 | } |
| 1481 | } |