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