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