blob: db91dc5a8bc3b0e9de8fefaf71e32a7195dee311 [file] [log] [blame]
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001/*
2 * Copyright (C) 2014 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
17package com.android.server;
18
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070019import static com.android.internal.util.ArrayUtils.appendInt;
20
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -080021import android.app.ActivityManager;
Ruben Brunk98576cf2016-03-07 18:54:28 -080022import android.content.ComponentName;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070023import android.content.pm.FeatureInfo;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070024import android.content.pm.PackageManager;
Jiyong Parkfad99442018-03-12 10:39:07 +090025import android.os.Build;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070026import android.os.Environment;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070027import android.os.Process;
Steven Morelandd7827fd2018-12-03 10:27:01 -080028import android.os.SystemProperties;
Felipe Leme3eb78092019-10-22 09:22:20 -070029import android.os.Trace;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070030import android.os.storage.StorageManager;
Zimuzocc2932f2018-10-29 16:04:41 +000031import android.permission.PermissionManager.SplitPermissionInfo;
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -080032import android.text.TextUtils;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070033import android.util.ArrayMap;
34import android.util.ArraySet;
35import android.util.Slog;
36import android.util.SparseArray;
Felipe Leme3eb78092019-10-22 09:22:20 -070037import android.util.TimingsTraceLog;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070038import android.util.Xml;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080039
Bookatz04d7ae52019-08-05 14:07:12 -070040import com.android.internal.annotations.VisibleForTesting;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070041import com.android.internal.util.XmlUtils;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080042
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070043import libcore.io.IoUtils;
44
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070045import org.xmlpull.v1.XmlPullParser;
46import org.xmlpull.v1.XmlPullParserException;
47
48import java.io.File;
49import java.io.FileNotFoundException;
50import java.io.FileReader;
51import java.io.IOException;
Jeff Davidson24b9d962016-07-21 12:35:10 -070052import java.util.ArrayList;
Svet Ganov087dce22017-09-07 15:42:16 -070053import java.util.Collections;
Jeff Davidson24b9d962016-07-21 12:35:10 -070054import java.util.List;
Svet Ganov087dce22017-09-07 15:42:16 -070055import java.util.Map;
Bookatz04d7ae52019-08-05 14:07:12 -070056import java.util.Set;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070057
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070058/**
59 * Loads global system configuration info.
Anthony Hughde787d42019-08-22 15:35:48 -070060 * Note: Initializing this class hits the disk and is slow. This class should generally only be
61 * accessed by the system_server process.
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070062 */
63public class SystemConfig {
64 static final String TAG = "SystemConfig";
65
66 static SystemConfig sInstance;
67
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080068 // permission flag, determines which types of configuration are allowed to be read
69 private static final int ALLOW_FEATURES = 0x01;
70 private static final int ALLOW_LIBS = 0x02;
71 private static final int ALLOW_PERMISSIONS = 0x04;
72 private static final int ALLOW_APP_CONFIGS = 0x08;
Jaekyun Seoke3b6bf12017-02-16 13:48:30 +090073 private static final int ALLOW_PRIVAPP_PERMISSIONS = 0x10;
Svet Ganov087dce22017-09-07 15:42:16 -070074 private static final int ALLOW_OEM_PERMISSIONS = 0x20;
Mathew Inwood4693a752018-02-20 16:04:25 +000075 private static final int ALLOW_HIDDENAPI_WHITELISTING = 0x40;
Dianne Hackborn769b2e72018-12-05 08:51:20 -080076 private static final int ALLOW_ASSOCIATIONS = 0x80;
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080077 private static final int ALLOW_ALL = ~0;
78
Steven Morelandd7827fd2018-12-03 10:27:01 -080079 // property for runtime configuration differentiation
80 private static final String SKU_PROPERTY = "ro.boot.product.hardware.sku";
81
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070082 // Group-ids that are given to all packages as read from etc/permissions/*.xml.
83 int[] mGlobalGids;
84
85 // These are the built-in uid -> permission mappings that were read from the
86 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -070087 final SparseArray<ArraySet<String>> mSystemPermissions = new SparseArray<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070088
Zimuzocc2932f2018-10-29 16:04:41 +000089 final ArrayList<SplitPermissionInfo> mSplitPermissions = new ArrayList<>();
90
Ben Lin71c16d72018-12-06 18:34:51 -080091 public static final class SharedLibraryEntry {
92 public final String name;
93 public final String filename;
94 public final String[] dependencies;
95
96 SharedLibraryEntry(String name, String filename, String[] dependencies) {
97 this.name = name;
98 this.filename = filename;
99 this.dependencies = dependencies;
100 }
101 }
102
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700103 // These are the built-in shared libraries that were read from the
Ben Lin71c16d72018-12-06 18:34:51 -0800104 // system configuration files. Keys are the library names; values are
105 // the individual entries that contain information such as filename
106 // and dependencies.
107 final ArrayMap<String, SharedLibraryEntry> mSharedLibraries = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700108
109 // These are the features this devices supports that were read from the
110 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700111 final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700112
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800113 // These are the features which this device doesn't support; the OEM
114 // partition uses these to opt-out of features from the system image.
115 final ArraySet<String> mUnavailableFeatures = new ArraySet<>();
116
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700117 public static final class PermissionEntry {
118 public final String name;
119 public int[] gids;
Jeff Sharkey00f39042015-03-23 16:51:22 -0700120 public boolean perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700121
Jeff Sharkey00f39042015-03-23 16:51:22 -0700122 PermissionEntry(String name, boolean perUser) {
123 this.name = name;
124 this.perUser = perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700125 }
126 }
127
128 // These are the permission -> gid mappings that were read from the
129 // system configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700130 final ArrayMap<String, PermissionEntry> mPermissions = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700131
132 // These are the packages that are white-listed to be able to run in the
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700133 // background while in power save mode (but not whitelisted from device idle modes),
134 // as read from the configuration files.
135 final ArraySet<String> mAllowInPowerSaveExceptIdle = new ArraySet<>();
136
137 // These are the packages that are white-listed to be able to run in the
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700138 // background while in power save mode, as read from the configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700139 final ArraySet<String> mAllowInPowerSave = new ArraySet<>();
140
Felipe Lemea9505cc2016-02-26 10:28:41 -0800141 // These are the packages that are white-listed to be able to run in the
142 // background while in data-usage save mode, as read from the configuration files.
143 final ArraySet<String> mAllowInDataUsageSave = new ArraySet<>();
144
Soonil Nagarkar2b565df2017-02-14 13:33:23 -0800145 // These are the packages that are white-listed to be able to run background location
146 // without throttling, as read from the configuration files.
147 final ArraySet<String> mAllowUnthrottledLocation = new ArraySet<>();
148
Soonil Nagarkar397ad582019-01-23 22:47:57 -0800149 // These are the packages that are white-listed to be able to retrieve location even when user
150 // location settings are off, for emergency purposes, as read from the configuration files.
151 final ArraySet<String> mAllowIgnoreLocationSettings = new ArraySet<>();
152
Christopher Tate42a386b2016-11-07 12:21:21 -0800153 // These are the action strings of broadcasts which are whitelisted to
154 // be delivered anonymously even to apps which target O+.
155 final ArraySet<String> mAllowImplicitBroadcasts = new ArraySet<>();
156
Christopher Tate01e18642015-07-07 18:10:38 -0700157 // These are the package names of apps which should be in the 'always'
158 // URL-handling state upon factory reset.
159 final ArraySet<String> mLinkedApps = new ArraySet<>();
160
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700161 // These are the packages that are whitelisted to be able to run as system user
162 final ArraySet<String> mSystemUserWhitelistedApps = new ArraySet<>();
163
164 // These are the packages that should not run under system user
165 final ArraySet<String> mSystemUserBlacklistedApps = new ArraySet<>();
166
Ruben Brunk98576cf2016-03-07 18:54:28 -0800167 // These are the components that are enabled by default as VR mode listener services.
168 final ArraySet<ComponentName> mDefaultVrComponents = new ArraySet<>();
169
Christopher Tate494df792016-05-10 17:05:38 -0700170 // These are the permitted backup transport service components
171 final ArraySet<ComponentName> mBackupTransportWhitelist = new ArraySet<>();
172
Ryan Mitchell4fd8e6f2019-10-07 16:31:34 -0700173 // These are packages mapped to maps of component class name to default enabled state.
174 final ArrayMap<String, ArrayMap<String, Boolean>> mPackageComponentEnabledState =
175 new ArrayMap<>();
176
Mathew Inwood4693a752018-02-20 16:04:25 +0000177 // Package names that are exempted from private API blacklisting
178 final ArraySet<String> mHiddenApiPackageWhitelist = new ArraySet<>();
179
goneileb31cd42018-02-28 15:22:08 -0800180 // The list of carrier applications which should be disabled until used.
181 // This function suppresses update notifications for these pre-installed apps.
182 // In SubscriptionInfoUpdater, the listed applications are disabled until used when all of the
183 // following conditions are met.
184 // 1. Not currently carrier-privileged according to the inserted SIM
185 // 2. Pre-installed
186 // 3. In the default state (enabled but not explicitly)
187 // And SubscriptionInfoUpdater undoes this and marks the app enabled when a SIM is inserted
188 // that marks the app as carrier privileged. It also grants the app default permissions
189 // for Phone and Location. As such, apps MUST only ever be added to this list if they
190 // obtain user consent to access their location through other means.
191 final ArraySet<String> mDisabledUntilUsedPreinstalledCarrierApps = new ArraySet<>();
192
Jeff Davidson24b9d962016-07-21 12:35:10 -0700193 // These are the packages of carrier-associated apps which should be disabled until used until
194 // a SIM is inserted which grants carrier privileges to that carrier app.
195 final ArrayMap<String, List<String>> mDisabledUntilUsedPreinstalledCarrierAssociatedApps =
196 new ArrayMap<>();
197
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -0800198 final ArrayMap<String, ArraySet<String>> mPrivAppPermissions = new ArrayMap<>();
Todd Kennedy74629e32017-08-15 14:48:07 -0700199 final ArrayMap<String, ArraySet<String>> mPrivAppDenyPermissions = new ArrayMap<>();
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -0800200
Jiyong Park002fdbd2017-02-13 20:50:31 +0900201 final ArrayMap<String, ArraySet<String>> mVendorPrivAppPermissions = new ArrayMap<>();
202 final ArrayMap<String, ArraySet<String>> mVendorPrivAppDenyPermissions = new ArrayMap<>();
203
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900204 final ArrayMap<String, ArraySet<String>> mProductPrivAppPermissions = new ArrayMap<>();
205 final ArrayMap<String, ArraySet<String>> mProductPrivAppDenyPermissions = new ArrayMap<>();
206
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900207 final ArrayMap<String, ArraySet<String>> mSystemExtPrivAppPermissions = new ArrayMap<>();
208 final ArrayMap<String, ArraySet<String>> mSystemExtPrivAppDenyPermissions = new ArrayMap<>();
Dario Freni2bef1762018-06-01 14:02:08 +0100209
Svet Ganov087dce22017-09-07 15:42:16 -0700210 final ArrayMap<String, ArrayMap<String, Boolean>> mOemPermissions = new ArrayMap<>();
211
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800212 // Allowed associations between applications. If there are any entries
213 // for an app, those are the only associations allowed; otherwise, all associations
214 // are allowed. Allowing an association from app A to app B means app A can not
215 // associate with any other apps, but does not limit what apps B can associate with.
216 final ArrayMap<String, ArraySet<String>> mAllowedAssociations = new ArrayMap<>();
217
Nikita Ioffe07964b42019-02-28 21:35:02 +0000218 private final ArraySet<String> mBugreportWhitelistedPackages = new ArraySet<>();
219
Bookatz04d7ae52019-08-05 14:07:12 -0700220 // Map of packagesNames to userTypes. Stored temporarily until cleared by UserManagerService().
221 private ArrayMap<String, Set<String>> mPackageToUserTypeWhitelist = new ArrayMap<>();
222 private ArrayMap<String, Set<String>> mPackageToUserTypeBlacklist = new ArrayMap<>();
223
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700224 public static SystemConfig getInstance() {
Anthony Hughde787d42019-08-22 15:35:48 -0700225 if (!isSystemProcess()) {
226 Slog.wtf(TAG, "SystemConfig is being accessed by a process other than "
227 + "system_server.");
228 }
229
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700230 synchronized (SystemConfig.class) {
231 if (sInstance == null) {
232 sInstance = new SystemConfig();
233 }
234 return sInstance;
235 }
236 }
237
238 public int[] getGlobalGids() {
239 return mGlobalGids;
240 }
241
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700242 public SparseArray<ArraySet<String>> getSystemPermissions() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700243 return mSystemPermissions;
244 }
245
Zimuzocc2932f2018-10-29 16:04:41 +0000246 public ArrayList<SplitPermissionInfo> getSplitPermissions() {
247 return mSplitPermissions;
248 }
249
Ben Lin71c16d72018-12-06 18:34:51 -0800250 public ArrayMap<String, SharedLibraryEntry> getSharedLibraries() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700251 return mSharedLibraries;
252 }
253
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700254 public ArrayMap<String, FeatureInfo> getAvailableFeatures() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700255 return mAvailableFeatures;
256 }
257
258 public ArrayMap<String, PermissionEntry> getPermissions() {
259 return mPermissions;
260 }
261
Christopher Tate42a386b2016-11-07 12:21:21 -0800262 public ArraySet<String> getAllowImplicitBroadcasts() {
263 return mAllowImplicitBroadcasts;
264 }
265
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700266 public ArraySet<String> getAllowInPowerSaveExceptIdle() {
267 return mAllowInPowerSaveExceptIdle;
268 }
269
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700270 public ArraySet<String> getAllowInPowerSave() {
271 return mAllowInPowerSave;
272 }
273
Felipe Lemea9505cc2016-02-26 10:28:41 -0800274 public ArraySet<String> getAllowInDataUsageSave() {
275 return mAllowInDataUsageSave;
276 }
277
Soonil Nagarkar2b565df2017-02-14 13:33:23 -0800278 public ArraySet<String> getAllowUnthrottledLocation() {
279 return mAllowUnthrottledLocation;
280 }
281
Soonil Nagarkar397ad582019-01-23 22:47:57 -0800282 public ArraySet<String> getAllowIgnoreLocationSettings() {
283 return mAllowIgnoreLocationSettings;
284 }
285
Christopher Tate01e18642015-07-07 18:10:38 -0700286 public ArraySet<String> getLinkedApps() {
287 return mLinkedApps;
288 }
289
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700290 public ArraySet<String> getSystemUserWhitelistedApps() {
291 return mSystemUserWhitelistedApps;
292 }
293
294 public ArraySet<String> getSystemUserBlacklistedApps() {
295 return mSystemUserBlacklistedApps;
296 }
297
Mathew Inwood4693a752018-02-20 16:04:25 +0000298 public ArraySet<String> getHiddenApiWhitelistedApps() {
299 return mHiddenApiPackageWhitelist;
300 }
301
Ruben Brunk98576cf2016-03-07 18:54:28 -0800302 public ArraySet<ComponentName> getDefaultVrComponents() {
303 return mDefaultVrComponents;
304 }
305
Christopher Tate494df792016-05-10 17:05:38 -0700306 public ArraySet<ComponentName> getBackupTransportWhitelist() {
307 return mBackupTransportWhitelist;
308 }
309
Ryan Mitchell4fd8e6f2019-10-07 16:31:34 -0700310 public ArrayMap<String, Boolean> getComponentsEnabledStates(String packageName) {
311 return mPackageComponentEnabledState.get(packageName);
312 }
313
goneileb31cd42018-02-28 15:22:08 -0800314 public ArraySet<String> getDisabledUntilUsedPreinstalledCarrierApps() {
315 return mDisabledUntilUsedPreinstalledCarrierApps;
316 }
317
Jeff Davidson24b9d962016-07-21 12:35:10 -0700318 public ArrayMap<String, List<String>> getDisabledUntilUsedPreinstalledCarrierAssociatedApps() {
319 return mDisabledUntilUsedPreinstalledCarrierAssociatedApps;
320 }
321
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -0800322 public ArraySet<String> getPrivAppPermissions(String packageName) {
323 return mPrivAppPermissions.get(packageName);
324 }
325
Todd Kennedy74629e32017-08-15 14:48:07 -0700326 public ArraySet<String> getPrivAppDenyPermissions(String packageName) {
327 return mPrivAppDenyPermissions.get(packageName);
328 }
329
Jiyong Park002fdbd2017-02-13 20:50:31 +0900330 public ArraySet<String> getVendorPrivAppPermissions(String packageName) {
331 return mVendorPrivAppPermissions.get(packageName);
332 }
333
334 public ArraySet<String> getVendorPrivAppDenyPermissions(String packageName) {
335 return mVendorPrivAppDenyPermissions.get(packageName);
336 }
337
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900338 public ArraySet<String> getProductPrivAppPermissions(String packageName) {
339 return mProductPrivAppPermissions.get(packageName);
340 }
341
342 public ArraySet<String> getProductPrivAppDenyPermissions(String packageName) {
343 return mProductPrivAppDenyPermissions.get(packageName);
344 }
345
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900346 /**
347 * Read from "permission" tags in /system_ext/etc/permissions/*.xml
348 * @return Set of privileged permissions that are explicitly granted.
349 */
350 public ArraySet<String> getSystemExtPrivAppPermissions(String packageName) {
351 return mSystemExtPrivAppPermissions.get(packageName);
Dario Freni2bef1762018-06-01 14:02:08 +0100352 }
353
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900354 /**
355 * Read from "deny-permission" tags in /system_ext/etc/permissions/*.xml
356 * @return Set of privileged permissions that are explicitly denied.
357 */
358 public ArraySet<String> getSystemExtPrivAppDenyPermissions(String packageName) {
359 return mSystemExtPrivAppDenyPermissions.get(packageName);
Dario Freni2bef1762018-06-01 14:02:08 +0100360 }
361
Svet Ganov087dce22017-09-07 15:42:16 -0700362 public Map<String, Boolean> getOemPermissions(String packageName) {
363 final Map<String, Boolean> oemPermissions = mOemPermissions.get(packageName);
364 if (oemPermissions != null) {
365 return oemPermissions;
366 }
367 return Collections.emptyMap();
368 }
369
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800370 public ArrayMap<String, ArraySet<String>> getAllowedAssociations() {
371 return mAllowedAssociations;
372 }
373
Nikita Ioffe07964b42019-02-28 21:35:02 +0000374 public ArraySet<String> getBugreportWhitelistedPackages() {
375 return mBugreportWhitelistedPackages;
376 }
377
Bookatz04d7ae52019-08-05 14:07:12 -0700378 /**
379 * Gets map of packagesNames to userTypes, dictating on which user types each package should be
380 * initially installed, and then removes this map from SystemConfig.
381 * Called by UserManagerService when it is constructed.
382 */
383 public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeWhitelist() {
384 ArrayMap<String, Set<String>> r = mPackageToUserTypeWhitelist;
385 mPackageToUserTypeWhitelist = new ArrayMap<>(0);
386 return r;
387 }
388
389 /**
390 * Gets map of packagesNames to userTypes, dictating on which user types each package should NOT
391 * be initially installed, even if they are whitelisted, and then removes this map from
392 * SystemConfig.
393 * Called by UserManagerService when it is constructed.
394 */
395 public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeBlacklist() {
396 ArrayMap<String, Set<String>> r = mPackageToUserTypeBlacklist;
397 mPackageToUserTypeBlacklist = new ArrayMap<>(0);
398 return r;
399 }
400
401 /**
402 * Only use for testing. Do NOT use in production code.
403 * @param readPermissions false to create an empty SystemConfig; true to read the permissions.
404 */
405 @VisibleForTesting
406 protected SystemConfig(boolean readPermissions) {
407 if (readPermissions) {
408 Slog.w(TAG, "Constructing a test SystemConfig");
409 readAllPermissions();
410 } else {
411 Slog.w(TAG, "Constructing an empty test SystemConfig");
412 }
413 }
414
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700415 SystemConfig() {
Felipe Leme3eb78092019-10-22 09:22:20 -0700416 TimingsTraceLog log = new TimingsTraceLog(TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
417 log.traceBegin("readAllPermissions");
418 try {
419 readAllPermissions();
420 } finally {
421 log.traceEnd();
422 }
Bookatz04d7ae52019-08-05 14:07:12 -0700423 }
424
425 private void readAllPermissions() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700426 // Read configuration from system
427 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800428 Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Svet Ganov087dce22017-09-07 15:42:16 -0700429
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700430 // Read configuration from the old permissions dir
431 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800432 Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
Svet Ganov087dce22017-09-07 15:42:16 -0700433
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800434 // Vendors are only allowed to customize these
435 int vendorPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_PRIVAPP_PERMISSIONS
436 | ALLOW_ASSOCIATIONS;
Jiyong Parkfad99442018-03-12 10:39:07 +0900437 if (Build.VERSION.FIRST_SDK_INT <= Build.VERSION_CODES.O_MR1) {
438 // For backward compatibility
439 vendorPermissionFlag |= (ALLOW_PERMISSIONS | ALLOW_APP_CONFIGS);
440 }
Jaekyun Seoke3b6bf12017-02-16 13:48:30 +0900441 readPermissions(Environment.buildPath(
442 Environment.getVendorDirectory(), "etc", "sysconfig"), vendorPermissionFlag);
443 readPermissions(Environment.buildPath(
444 Environment.getVendorDirectory(), "etc", "permissions"), vendorPermissionFlag);
Svet Ganov087dce22017-09-07 15:42:16 -0700445
Jiyong Park090c6b12017-12-28 12:03:28 +0900446 // Allow ODM to customize system configs as much as Vendor, because /odm is another
447 // vendor partition other than /vendor.
448 int odmPermissionFlag = vendorPermissionFlag;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700449 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800450 Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700451 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800452 Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
Svet Ganov087dce22017-09-07 15:42:16 -0700453
Steven Morelandd7827fd2018-12-03 10:27:01 -0800454 String skuProperty = SystemProperties.get(SKU_PROPERTY, "");
455 if (!skuProperty.isEmpty()) {
456 String skuDir = "sku_" + skuProperty;
457
458 readPermissions(Environment.buildPath(
459 Environment.getOdmDirectory(), "etc", "sysconfig", skuDir), odmPermissionFlag);
460 readPermissions(Environment.buildPath(
461 Environment.getOdmDirectory(), "etc", "permissions", skuDir),
462 odmPermissionFlag);
463 }
464
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800465 // Allow OEM to customize these
466 int oemPermissionFlag = ALLOW_FEATURES | ALLOW_OEM_PERMISSIONS | ALLOW_ASSOCIATIONS;
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800467 readPermissions(Environment.buildPath(
Svet Ganov087dce22017-09-07 15:42:16 -0700468 Environment.getOemDirectory(), "etc", "sysconfig"), oemPermissionFlag);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800469 readPermissions(Environment.buildPath(
Svet Ganov087dce22017-09-07 15:42:16 -0700470 Environment.getOemDirectory(), "etc", "permissions"), oemPermissionFlag);
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900471
Hung-ying Tyan38cd31c2018-09-26 15:05:07 +0800472 // Allow Product to customize all system configs
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900473 readPermissions(Environment.buildPath(
Hung-ying Tyan38cd31c2018-09-26 15:05:07 +0800474 Environment.getProductDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Jaekyun Seok1713d9e2018-01-12 21:47:26 +0900475 readPermissions(Environment.buildPath(
Hung-ying Tyan38cd31c2018-09-26 15:05:07 +0800476 Environment.getProductDirectory(), "etc", "permissions"), ALLOW_ALL);
Dario Freni2bef1762018-06-01 14:02:08 +0100477
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900478 // Allow /system_ext to customize all system configs
Dario Freni2bef1762018-06-01 14:02:08 +0100479 readPermissions(Environment.buildPath(
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900480 Environment.getSystemExtDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Dario Freni2bef1762018-06-01 14:02:08 +0100481 readPermissions(Environment.buildPath(
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900482 Environment.getSystemExtDirectory(), "etc", "permissions"), ALLOW_ALL);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700483 }
484
Bookatz04d7ae52019-08-05 14:07:12 -0700485 @VisibleForTesting
486 public void readPermissions(File libraryDir, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700487 // Read permissions from given directory.
488 if (!libraryDir.exists() || !libraryDir.isDirectory()) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800489 if (permissionFlag == ALLOW_ALL) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700490 Slog.w(TAG, "No directory " + libraryDir + ", skipping");
491 }
492 return;
493 }
494 if (!libraryDir.canRead()) {
495 Slog.w(TAG, "Directory " + libraryDir + " cannot be read");
496 return;
497 }
498
499 // Iterate over the files in the directory and scan .xml files
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800500 File platformFile = null;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700501 for (File f : libraryDir.listFiles()) {
Steven Morelandd7827fd2018-12-03 10:27:01 -0800502 if (!f.isFile()) {
503 continue;
504 }
505
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700506 // We'll read platform.xml last
507 if (f.getPath().endsWith("etc/permissions/platform.xml")) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800508 platformFile = f;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700509 continue;
510 }
511
512 if (!f.getPath().endsWith(".xml")) {
513 Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");
514 continue;
515 }
516 if (!f.canRead()) {
517 Slog.w(TAG, "Permissions library file " + f + " cannot be read");
518 continue;
519 }
520
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800521 readPermissionsFromXml(f, permissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700522 }
523
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800524 // Read platform permissions last so it will take precedence
525 if (platformFile != null) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800526 readPermissionsFromXml(platformFile, permissionFlag);
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800527 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700528 }
529
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800530 private void logNotAllowedInPartition(String name, File permFile, XmlPullParser parser) {
531 Slog.w(TAG, "<" + name + "> not allowed in partition of "
532 + permFile + " at " + parser.getPositionDescription());
533 }
534
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800535 private void readPermissionsFromXml(File permFile, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700536 FileReader permReader = null;
537 try {
538 permReader = new FileReader(permFile);
539 } catch (FileNotFoundException e) {
540 Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
541 return;
542 }
543
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800544 final boolean lowRam = ActivityManager.isLowRamDeviceStatic();
545
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700546 try {
547 XmlPullParser parser = Xml.newPullParser();
548 parser.setInput(permReader);
549
550 int type;
551 while ((type=parser.next()) != parser.START_TAG
552 && type != parser.END_DOCUMENT) {
553 ;
554 }
555
556 if (type != parser.START_TAG) {
557 throw new XmlPullParserException("No start tag found");
558 }
559
560 if (!parser.getName().equals("permissions") && !parser.getName().equals("config")) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800561 throw new XmlPullParserException("Unexpected start tag in " + permFile
562 + ": found " + parser.getName() + ", expected 'permissions' or 'config'");
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700563 }
564
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800565 final boolean allowAll = permissionFlag == ALLOW_ALL;
566 final boolean allowLibs = (permissionFlag & ALLOW_LIBS) != 0;
567 final boolean allowFeatures = (permissionFlag & ALLOW_FEATURES) != 0;
568 final boolean allowPermissions = (permissionFlag & ALLOW_PERMISSIONS) != 0;
569 final boolean allowAppConfigs = (permissionFlag & ALLOW_APP_CONFIGS) != 0;
570 final boolean allowPrivappPermissions = (permissionFlag & ALLOW_PRIVAPP_PERMISSIONS)
571 != 0;
572 final boolean allowOemPermissions = (permissionFlag & ALLOW_OEM_PERMISSIONS) != 0;
573 final boolean allowApiWhitelisting = (permissionFlag & ALLOW_HIDDENAPI_WHITELISTING)
574 != 0;
575 final boolean allowAssociations = (permissionFlag & ALLOW_ASSOCIATIONS) != 0;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700576 while (true) {
577 XmlUtils.nextElement(parser);
578 if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
579 break;
580 }
581
582 String name = parser.getName();
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800583 if (name == null) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700584 XmlUtils.skipCurrentTag(parser);
585 continue;
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800586 }
587 switch (name) {
588 case "group": {
589 if (allowAll) {
590 String gidStr = parser.getAttributeValue(null, "gid");
591 if (gidStr != null) {
592 int gid = android.os.Process.getGidForName(gidStr);
593 mGlobalGids = appendInt(mGlobalGids, gid);
594 } else {
595 Slog.w(TAG, "<" + name + "> without gid in " + permFile + " at "
goneileb31cd42018-02-28 15:22:08 -0800596 + parser.getPositionDescription());
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800597 }
598 } else {
599 logNotAllowedInPartition(name, permFile, parser);
600 }
601 XmlUtils.skipCurrentTag(parser);
602 } break;
603 case "permission": {
604 if (allowPermissions) {
605 String perm = parser.getAttributeValue(null, "name");
606 if (perm == null) {
607 Slog.w(TAG, "<" + name + "> without name in " + permFile + " at "
608 + parser.getPositionDescription());
609 XmlUtils.skipCurrentTag(parser);
610 break;
611 }
612 perm = perm.intern();
613 readPermission(parser, perm);
614 } else {
615 logNotAllowedInPartition(name, permFile, parser);
616 XmlUtils.skipCurrentTag(parser);
617 }
618 } break;
619 case "assign-permission": {
620 if (allowPermissions) {
621 String perm = parser.getAttributeValue(null, "name");
622 if (perm == null) {
623 Slog.w(TAG, "<" + name + "> without name in " + permFile
624 + " at " + parser.getPositionDescription());
625 XmlUtils.skipCurrentTag(parser);
626 break;
627 }
628 String uidStr = parser.getAttributeValue(null, "uid");
629 if (uidStr == null) {
630 Slog.w(TAG, "<" + name + "> without uid in " + permFile
631 + " at " + parser.getPositionDescription());
632 XmlUtils.skipCurrentTag(parser);
633 break;
634 }
635 int uid = Process.getUidForName(uidStr);
636 if (uid < 0) {
637 Slog.w(TAG, "<" + name + "> with unknown uid \""
638 + uidStr + " in " + permFile + " at "
639 + parser.getPositionDescription());
640 XmlUtils.skipCurrentTag(parser);
641 break;
642 }
643 perm = perm.intern();
644 ArraySet<String> perms = mSystemPermissions.get(uid);
645 if (perms == null) {
646 perms = new ArraySet<String>();
647 mSystemPermissions.put(uid, perms);
648 }
649 perms.add(perm);
650 } else {
651 logNotAllowedInPartition(name, permFile, parser);
652 }
653 XmlUtils.skipCurrentTag(parser);
654 } break;
655 case "split-permission": {
656 if (allowPermissions) {
657 readSplitPermission(parser, permFile);
658 } else {
659 logNotAllowedInPartition(name, permFile, parser);
660 XmlUtils.skipCurrentTag(parser);
661 }
662 } break;
663 case "library": {
664 if (allowLibs) {
665 String lname = parser.getAttributeValue(null, "name");
666 String lfile = parser.getAttributeValue(null, "file");
667 String ldependency = parser.getAttributeValue(null, "dependency");
668 if (lname == null) {
669 Slog.w(TAG, "<" + name + "> without name in " + permFile + " at "
670 + parser.getPositionDescription());
671 } else if (lfile == null) {
672 Slog.w(TAG, "<" + name + "> without file in " + permFile + " at "
673 + parser.getPositionDescription());
674 } else {
675 //Log.i(TAG, "Got library " + lname + " in " + lfile);
676 SharedLibraryEntry entry = new SharedLibraryEntry(lname, lfile,
677 ldependency == null ? new String[0] : ldependency.split(":"));
678 mSharedLibraries.put(lname, entry);
679 }
680 } else {
681 logNotAllowedInPartition(name, permFile, parser);
682 }
683 XmlUtils.skipCurrentTag(parser);
684 } break;
685 case "feature": {
686 if (allowFeatures) {
687 String fname = parser.getAttributeValue(null, "name");
688 int fversion = XmlUtils.readIntAttribute(parser, "version", 0);
689 boolean allowed;
690 if (!lowRam) {
691 allowed = true;
692 } else {
693 String notLowRam = parser.getAttributeValue(null, "notLowRam");
694 allowed = !"true".equals(notLowRam);
695 }
696 if (fname == null) {
697 Slog.w(TAG, "<" + name + "> without name in " + permFile + " at "
698 + parser.getPositionDescription());
699 } else if (allowed) {
700 addFeature(fname, fversion);
701 }
702 } else {
703 logNotAllowedInPartition(name, permFile, parser);
704 }
705 XmlUtils.skipCurrentTag(parser);
706 } break;
707 case "unavailable-feature": {
708 if (allowFeatures) {
709 String fname = parser.getAttributeValue(null, "name");
710 if (fname == null) {
711 Slog.w(TAG, "<" + name + "> without name in " + permFile
712 + " at " + parser.getPositionDescription());
713 } else {
714 mUnavailableFeatures.add(fname);
715 }
716 } else {
717 logNotAllowedInPartition(name, permFile, parser);
718 }
719 XmlUtils.skipCurrentTag(parser);
720 } break;
721 case "allow-in-power-save-except-idle": {
722 if (allowAll) {
723 String pkgname = parser.getAttributeValue(null, "package");
724 if (pkgname == null) {
725 Slog.w(TAG, "<" + name + "> without package in "
726 + permFile + " at " + parser.getPositionDescription());
727 } else {
728 mAllowInPowerSaveExceptIdle.add(pkgname);
729 }
730 } else {
731 logNotAllowedInPartition(name, permFile, parser);
732 }
733 XmlUtils.skipCurrentTag(parser);
734 } break;
735 case "allow-in-power-save": {
736 if (allowAll) {
737 String pkgname = parser.getAttributeValue(null, "package");
738 if (pkgname == null) {
739 Slog.w(TAG, "<" + name + "> without package in "
740 + permFile + " at " + parser.getPositionDescription());
741 } else {
742 mAllowInPowerSave.add(pkgname);
743 }
744 } else {
745 logNotAllowedInPartition(name, permFile, parser);
746 }
747 XmlUtils.skipCurrentTag(parser);
748 } break;
749 case "allow-in-data-usage-save": {
750 if (allowAll) {
751 String pkgname = parser.getAttributeValue(null, "package");
752 if (pkgname == null) {
753 Slog.w(TAG, "<" + name + "> without package in "
754 + permFile + " at " + parser.getPositionDescription());
755 } else {
756 mAllowInDataUsageSave.add(pkgname);
757 }
758 } else {
759 logNotAllowedInPartition(name, permFile, parser);
760 }
761 XmlUtils.skipCurrentTag(parser);
762 } break;
763 case "allow-unthrottled-location": {
764 if (allowAll) {
765 String pkgname = parser.getAttributeValue(null, "package");
766 if (pkgname == null) {
767 Slog.w(TAG, "<" + name + "> without package in "
768 + permFile + " at " + parser.getPositionDescription());
769 } else {
770 mAllowUnthrottledLocation.add(pkgname);
771 }
772 } else {
773 logNotAllowedInPartition(name, permFile, parser);
774 }
775 XmlUtils.skipCurrentTag(parser);
776 } break;
Soonil Nagarkar397ad582019-01-23 22:47:57 -0800777 case "allow-ignore-location-settings": {
778 if (allowAll) {
779 String pkgname = parser.getAttributeValue(null, "package");
780 if (pkgname == null) {
781 Slog.w(TAG, "<" + name + "> without package in "
782 + permFile + " at " + parser.getPositionDescription());
783 } else {
784 mAllowIgnoreLocationSettings.add(pkgname);
785 }
786 } else {
787 logNotAllowedInPartition(name, permFile, parser);
788 }
789 XmlUtils.skipCurrentTag(parser);
790 } break;
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800791 case "allow-implicit-broadcast": {
792 if (allowAll) {
793 String action = parser.getAttributeValue(null, "action");
794 if (action == null) {
795 Slog.w(TAG, "<" + name + "> without action in "
796 + permFile + " at " + parser.getPositionDescription());
797 } else {
798 mAllowImplicitBroadcasts.add(action);
799 }
800 } else {
801 logNotAllowedInPartition(name, permFile, parser);
802 }
803 XmlUtils.skipCurrentTag(parser);
804 } break;
805 case "app-link": {
806 if (allowAppConfigs) {
807 String pkgname = parser.getAttributeValue(null, "package");
808 if (pkgname == null) {
809 Slog.w(TAG, "<" + name + "> without package in " + permFile
810 + " at " + parser.getPositionDescription());
811 } else {
812 mLinkedApps.add(pkgname);
813 }
814 } else {
815 logNotAllowedInPartition(name, permFile, parser);
816 }
817 XmlUtils.skipCurrentTag(parser);
818 } break;
819 case "system-user-whitelisted-app": {
820 if (allowAppConfigs) {
821 String pkgname = parser.getAttributeValue(null, "package");
822 if (pkgname == null) {
823 Slog.w(TAG, "<" + name + "> without package in "
824 + permFile + " at " + parser.getPositionDescription());
825 } else {
826 mSystemUserWhitelistedApps.add(pkgname);
827 }
828 } else {
829 logNotAllowedInPartition(name, permFile, parser);
830 }
831 XmlUtils.skipCurrentTag(parser);
832 } break;
833 case "system-user-blacklisted-app": {
834 if (allowAppConfigs) {
835 String pkgname = parser.getAttributeValue(null, "package");
836 if (pkgname == null) {
837 Slog.w(TAG, "<" + name + "> without package in "
838 + permFile + " at " + parser.getPositionDescription());
839 } else {
840 mSystemUserBlacklistedApps.add(pkgname);
841 }
842 } else {
843 logNotAllowedInPartition(name, permFile, parser);
844 }
845 XmlUtils.skipCurrentTag(parser);
846 } break;
847 case "default-enabled-vr-app": {
848 if (allowAppConfigs) {
849 String pkgname = parser.getAttributeValue(null, "package");
850 String clsname = parser.getAttributeValue(null, "class");
851 if (pkgname == null) {
852 Slog.w(TAG, "<" + name + "> without package in "
853 + permFile + " at " + parser.getPositionDescription());
854 } else if (clsname == null) {
855 Slog.w(TAG, "<" + name + "> without class in "
856 + permFile + " at " + parser.getPositionDescription());
857 } else {
858 mDefaultVrComponents.add(new ComponentName(pkgname, clsname));
859 }
860 } else {
861 logNotAllowedInPartition(name, permFile, parser);
862 }
863 XmlUtils.skipCurrentTag(parser);
864 } break;
Ryan Mitchell4fd8e6f2019-10-07 16:31:34 -0700865 case "component-override": {
Ryan Mitchell61a30322019-10-30 08:52:43 -0700866 readComponentOverrides(parser, permFile);
Ryan Mitchell4fd8e6f2019-10-07 16:31:34 -0700867 XmlUtils.skipCurrentTag(parser);
868 } break;
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800869 case "backup-transport-whitelisted-service": {
870 if (allowFeatures) {
871 String serviceName = parser.getAttributeValue(null, "service");
872 if (serviceName == null) {
873 Slog.w(TAG, "<" + name + "> without service in "
874 + permFile + " at " + parser.getPositionDescription());
875 } else {
876 ComponentName cn = ComponentName.unflattenFromString(serviceName);
877 if (cn == null) {
878 Slog.w(TAG, "<" + name + "> with invalid service name "
879 + serviceName + " in " + permFile
880 + " at " + parser.getPositionDescription());
881 } else {
882 mBackupTransportWhitelist.add(cn);
883 }
884 }
885 } else {
886 logNotAllowedInPartition(name, permFile, parser);
887 }
888 XmlUtils.skipCurrentTag(parser);
889 } break;
890 case "disabled-until-used-preinstalled-carrier-associated-app": {
891 if (allowAppConfigs) {
892 String pkgname = parser.getAttributeValue(null, "package");
893 String carrierPkgname = parser.getAttributeValue(null,
894 "carrierAppPackage");
895 if (pkgname == null || carrierPkgname == null) {
896 Slog.w(TAG, "<" + name
897 + "> without package or carrierAppPackage in " + permFile
898 + " at " + parser.getPositionDescription());
899 } else {
900 List<String> associatedPkgs =
901 mDisabledUntilUsedPreinstalledCarrierAssociatedApps.get(
902 carrierPkgname);
903 if (associatedPkgs == null) {
904 associatedPkgs = new ArrayList<>();
905 mDisabledUntilUsedPreinstalledCarrierAssociatedApps.put(
906 carrierPkgname, associatedPkgs);
907 }
908 associatedPkgs.add(pkgname);
909 }
910 } else {
911 logNotAllowedInPartition(name, permFile, parser);
912 }
913 XmlUtils.skipCurrentTag(parser);
914 } break;
915 case "disabled-until-used-preinstalled-carrier-app": {
916 if (allowAppConfigs) {
917 String pkgname = parser.getAttributeValue(null, "package");
918 if (pkgname == null) {
919 Slog.w(TAG,
920 "<" + name + "> without "
921 + "package in " + permFile + " at "
922 + parser.getPositionDescription());
923 } else {
924 mDisabledUntilUsedPreinstalledCarrierApps.add(pkgname);
925 }
926 } else {
927 logNotAllowedInPartition(name, permFile, parser);
928 }
929 XmlUtils.skipCurrentTag(parser);
930 } break;
931 case "privapp-permissions": {
932 if (allowPrivappPermissions) {
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900933 // privapp permissions from system, vendor, product and system_ext
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800934 // partitions are stored separately. This is to prevent xml files in
935 // the vendor partition from granting permissions to priv apps in the
936 // system partition and vice versa.
937 boolean vendor = permFile.toPath().startsWith(
938 Environment.getVendorDirectory().toPath() + "/")
939 || permFile.toPath().startsWith(
940 Environment.getOdmDirectory().toPath() + "/");
941 boolean product = permFile.toPath().startsWith(
942 Environment.getProductDirectory().toPath() + "/");
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900943 boolean systemExt = permFile.toPath().startsWith(
944 Environment.getSystemExtDirectory().toPath() + "/");
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800945 if (vendor) {
946 readPrivAppPermissions(parser, mVendorPrivAppPermissions,
947 mVendorPrivAppDenyPermissions);
948 } else if (product) {
949 readPrivAppPermissions(parser, mProductPrivAppPermissions,
950 mProductPrivAppDenyPermissions);
Jeongik Cha9ec059a2019-07-04 21:12:06 +0900951 } else if (systemExt) {
952 readPrivAppPermissions(parser, mSystemExtPrivAppPermissions,
953 mSystemExtPrivAppDenyPermissions);
Dianne Hackborn769b2e72018-12-05 08:51:20 -0800954 } else {
955 readPrivAppPermissions(parser, mPrivAppPermissions,
956 mPrivAppDenyPermissions);
957 }
958 } else {
959 logNotAllowedInPartition(name, permFile, parser);
960 XmlUtils.skipCurrentTag(parser);
961 }
962 } break;
963 case "oem-permissions": {
964 if (allowOemPermissions) {
965 readOemPermissions(parser);
966 } else {
967 logNotAllowedInPartition(name, permFile, parser);
968 XmlUtils.skipCurrentTag(parser);
969 }
970 } break;
971 case "hidden-api-whitelisted-app": {
972 if (allowApiWhitelisting) {
973 String pkgname = parser.getAttributeValue(null, "package");
974 if (pkgname == null) {
975 Slog.w(TAG, "<" + name + "> without package in "
976 + permFile + " at " + parser.getPositionDescription());
977 } else {
978 mHiddenApiPackageWhitelist.add(pkgname);
979 }
980 } else {
981 logNotAllowedInPartition(name, permFile, parser);
982 }
983 XmlUtils.skipCurrentTag(parser);
984 } break;
985 case "allow-association": {
986 if (allowAssociations) {
987 String target = parser.getAttributeValue(null, "target");
988 if (target == null) {
989 Slog.w(TAG, "<" + name + "> without target in " + permFile
990 + " at " + parser.getPositionDescription());
991 XmlUtils.skipCurrentTag(parser);
992 break;
993 }
994 String allowed = parser.getAttributeValue(null, "allowed");
995 if (allowed == null) {
996 Slog.w(TAG, "<" + name + "> without allowed in " + permFile
997 + " at " + parser.getPositionDescription());
998 XmlUtils.skipCurrentTag(parser);
999 break;
1000 }
1001 target = target.intern();
1002 allowed = allowed.intern();
1003 ArraySet<String> associations = mAllowedAssociations.get(target);
1004 if (associations == null) {
1005 associations = new ArraySet<>();
1006 mAllowedAssociations.put(target, associations);
1007 }
1008 Slog.i(TAG, "Adding association: " + target + " <- " + allowed);
1009 associations.add(allowed);
1010 } else {
1011 logNotAllowedInPartition(name, permFile, parser);
1012 }
1013 XmlUtils.skipCurrentTag(parser);
1014 } break;
Nikita Ioffe07964b42019-02-28 21:35:02 +00001015 case "bugreport-whitelisted": {
1016 String pkgname = parser.getAttributeValue(null, "package");
1017 if (pkgname == null) {
1018 Slog.w(TAG, "<" + name + "> without package in " + permFile
1019 + " at " + parser.getPositionDescription());
1020 } else {
1021 mBugreportWhitelistedPackages.add(pkgname);
1022 }
Nikita Ioffe08146ef2019-03-13 17:04:21 +00001023 XmlUtils.skipCurrentTag(parser);
Nikita Ioffe07964b42019-02-28 21:35:02 +00001024 } break;
Bookatz04d7ae52019-08-05 14:07:12 -07001025 case "install-in-user-type": {
1026 // NB: We allow any directory permission to declare install-in-user-type.
1027 readInstallInUserType(parser,
1028 mPackageToUserTypeWhitelist, mPackageToUserTypeBlacklist);
1029 } break;
Dianne Hackborn769b2e72018-12-05 08:51:20 -08001030 default: {
1031 Slog.w(TAG, "Tag " + name + " is unknown in "
1032 + permFile + " at " + parser.getPositionDescription());
1033 XmlUtils.skipCurrentTag(parser);
1034 } break;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001035 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001036 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001037 } catch (XmlPullParserException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -08001038 Slog.w(TAG, "Got exception parsing permissions.", e);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001039 } catch (IOException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -08001040 Slog.w(TAG, "Got exception parsing permissions.", e);
1041 } finally {
1042 IoUtils.closeQuietly(permReader);
1043 }
1044
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -07001045 // Some devices can be field-converted to FBE, so offer to splice in
1046 // those features if not already defined by the static config
Paul Lawrence20be5d62016-02-26 13:51:17 -08001047 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -07001048 addFeature(PackageManager.FEATURE_FILE_BASED_ENCRYPTION, 0);
1049 addFeature(PackageManager.FEATURE_SECURELY_REMOVES_USERS, 0);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -07001050 }
1051
Jeff Sharkey8eb783b2018-01-04 16:46:48 -07001052 // Help legacy devices that may not have updated their static config
1053 if (StorageManager.hasAdoptable()) {
1054 addFeature(PackageManager.FEATURE_ADOPTABLE_STORAGE, 0);
1055 }
1056
Dianne Hackborn2a103f12017-08-08 15:50:31 -07001057 if (ActivityManager.isLowRamDeviceStatic()) {
1058 addFeature(PackageManager.FEATURE_RAM_LOW, 0);
1059 } else {
1060 addFeature(PackageManager.FEATURE_RAM_NORMAL, 0);
1061 }
1062
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -07001063 for (String featureName : mUnavailableFeatures) {
1064 removeFeature(featureName);
1065 }
1066 }
1067
Jeff Sharkey115d2c12016-02-15 17:25:57 -07001068 private void addFeature(String name, int version) {
1069 FeatureInfo fi = mAvailableFeatures.get(name);
1070 if (fi == null) {
1071 fi = new FeatureInfo();
1072 fi.name = name;
1073 fi.version = version;
1074 mAvailableFeatures.put(name, fi);
1075 } else {
1076 fi.version = Math.max(fi.version, version);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -07001077 }
1078 }
1079
Jeff Sharkey115d2c12016-02-15 17:25:57 -07001080 private void removeFeature(String name) {
1081 if (mAvailableFeatures.remove(name) != null) {
1082 Slog.d(TAG, "Removed unavailable feature " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001083 }
1084 }
1085
1086 void readPermission(XmlPullParser parser, String name)
1087 throws IOException, XmlPullParserException {
Jeff Sharkey00f39042015-03-23 16:51:22 -07001088 if (mPermissions.containsKey(name)) {
1089 throw new IllegalStateException("Duplicate permission definition for " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001090 }
Jeff Sharkey00f39042015-03-23 16:51:22 -07001091
1092 final boolean perUser = XmlUtils.readBooleanAttribute(parser, "perUser", false);
1093 final PermissionEntry perm = new PermissionEntry(name, perUser);
1094 mPermissions.put(name, perm);
1095
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001096 int outerDepth = parser.getDepth();
1097 int type;
1098 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
1099 && (type != XmlPullParser.END_TAG
1100 || parser.getDepth() > outerDepth)) {
1101 if (type == XmlPullParser.END_TAG
1102 || type == XmlPullParser.TEXT) {
1103 continue;
1104 }
1105
1106 String tagName = parser.getName();
1107 if ("group".equals(tagName)) {
1108 String gidStr = parser.getAttributeValue(null, "gid");
1109 if (gidStr != null) {
1110 int gid = Process.getGidForName(gidStr);
1111 perm.gids = appendInt(perm.gids, gid);
1112 } else {
1113 Slog.w(TAG, "<group> without gid at "
1114 + parser.getPositionDescription());
1115 }
1116 }
1117 XmlUtils.skipCurrentTag(parser);
1118 }
1119 }
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001120
Jiyong Park002fdbd2017-02-13 20:50:31 +09001121 private void readPrivAppPermissions(XmlPullParser parser,
1122 ArrayMap<String, ArraySet<String>> grantMap,
1123 ArrayMap<String, ArraySet<String>> denyMap)
1124 throws IOException, XmlPullParserException {
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001125 String packageName = parser.getAttributeValue(null, "package");
1126 if (TextUtils.isEmpty(packageName)) {
1127 Slog.w(TAG, "package is required for <privapp-permissions> in "
1128 + parser.getPositionDescription());
1129 return;
1130 }
1131
Jiyong Park002fdbd2017-02-13 20:50:31 +09001132 ArraySet<String> permissions = grantMap.get(packageName);
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001133 if (permissions == null) {
1134 permissions = new ArraySet<>();
1135 }
Jiyong Park002fdbd2017-02-13 20:50:31 +09001136 ArraySet<String> denyPermissions = denyMap.get(packageName);
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001137 int depth = parser.getDepth();
1138 while (XmlUtils.nextElementWithin(parser, depth)) {
1139 String name = parser.getName();
1140 if ("permission".equals(name)) {
1141 String permName = parser.getAttributeValue(null, "name");
1142 if (TextUtils.isEmpty(permName)) {
1143 Slog.w(TAG, "name is required for <permission> in "
1144 + parser.getPositionDescription());
1145 continue;
1146 }
1147 permissions.add(permName);
Todd Kennedy74629e32017-08-15 14:48:07 -07001148 } else if ("deny-permission".equals(name)) {
1149 String permName = parser.getAttributeValue(null, "name");
1150 if (TextUtils.isEmpty(permName)) {
1151 Slog.w(TAG, "name is required for <deny-permission> in "
1152 + parser.getPositionDescription());
1153 continue;
1154 }
1155 if (denyPermissions == null) {
1156 denyPermissions = new ArraySet<>();
1157 }
1158 denyPermissions.add(permName);
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001159 }
1160 }
Jiyong Park002fdbd2017-02-13 20:50:31 +09001161 grantMap.put(packageName, permissions);
Todd Kennedy74629e32017-08-15 14:48:07 -07001162 if (denyPermissions != null) {
Jiyong Park002fdbd2017-02-13 20:50:31 +09001163 denyMap.put(packageName, denyPermissions);
Todd Kennedy74629e32017-08-15 14:48:07 -07001164 }
Fyodor Kupolov964d2eb2016-11-09 14:32:27 -08001165 }
Svet Ganov087dce22017-09-07 15:42:16 -07001166
Bookatz04d7ae52019-08-05 14:07:12 -07001167 private void readInstallInUserType(XmlPullParser parser,
1168 Map<String, Set<String>> doInstallMap,
1169 Map<String, Set<String>> nonInstallMap)
1170 throws IOException, XmlPullParserException {
1171 final String packageName = parser.getAttributeValue(null, "package");
1172 if (TextUtils.isEmpty(packageName)) {
1173 Slog.w(TAG, "package is required for <install-in-user-type> in "
1174 + parser.getPositionDescription());
1175 return;
1176 }
1177
1178 Set<String> userTypesYes = doInstallMap.get(packageName);
1179 Set<String> userTypesNo = nonInstallMap.get(packageName);
1180 final int depth = parser.getDepth();
1181 while (XmlUtils.nextElementWithin(parser, depth)) {
1182 final String name = parser.getName();
1183 if ("install-in".equals(name)) {
1184 final String userType = parser.getAttributeValue(null, "user-type");
1185 if (TextUtils.isEmpty(userType)) {
1186 Slog.w(TAG, "user-type is required for <install-in-user-type> in "
1187 + parser.getPositionDescription());
1188 continue;
1189 }
1190 if (userTypesYes == null) {
1191 userTypesYes = new ArraySet<>();
1192 doInstallMap.put(packageName, userTypesYes);
1193 }
1194 userTypesYes.add(userType);
1195 } else if ("do-not-install-in".equals(name)) {
1196 final String userType = parser.getAttributeValue(null, "user-type");
1197 if (TextUtils.isEmpty(userType)) {
1198 Slog.w(TAG, "user-type is required for <install-in-user-type> in "
1199 + parser.getPositionDescription());
1200 continue;
1201 }
1202 if (userTypesNo == null) {
1203 userTypesNo = new ArraySet<>();
1204 nonInstallMap.put(packageName, userTypesNo);
1205 }
1206 userTypesNo.add(userType);
1207 } else {
1208 Slog.w(TAG, "unrecognized tag in <install-in-user-type> in "
1209 + parser.getPositionDescription());
1210 }
1211 }
1212 }
1213
Svet Ganov087dce22017-09-07 15:42:16 -07001214 void readOemPermissions(XmlPullParser parser) throws IOException, XmlPullParserException {
1215 final String packageName = parser.getAttributeValue(null, "package");
1216 if (TextUtils.isEmpty(packageName)) {
1217 Slog.w(TAG, "package is required for <oem-permissions> in "
1218 + parser.getPositionDescription());
1219 return;
1220 }
1221
1222 ArrayMap<String, Boolean> permissions = mOemPermissions.get(packageName);
1223 if (permissions == null) {
1224 permissions = new ArrayMap<>();
1225 }
1226 final int depth = parser.getDepth();
1227 while (XmlUtils.nextElementWithin(parser, depth)) {
1228 final String name = parser.getName();
1229 if ("permission".equals(name)) {
1230 final String permName = parser.getAttributeValue(null, "name");
1231 if (TextUtils.isEmpty(permName)) {
1232 Slog.w(TAG, "name is required for <permission> in "
1233 + parser.getPositionDescription());
1234 continue;
1235 }
1236 permissions.put(permName, Boolean.TRUE);
1237 } else if ("deny-permission".equals(name)) {
1238 String permName = parser.getAttributeValue(null, "name");
1239 if (TextUtils.isEmpty(permName)) {
1240 Slog.w(TAG, "name is required for <deny-permission> in "
1241 + parser.getPositionDescription());
1242 continue;
1243 }
1244 permissions.put(permName, Boolean.FALSE);
1245 }
1246 }
1247 mOemPermissions.put(packageName, permissions);
1248 }
Zimuzocc2932f2018-10-29 16:04:41 +00001249
1250 private void readSplitPermission(XmlPullParser parser, File permFile)
1251 throws IOException, XmlPullParserException {
1252 String splitPerm = parser.getAttributeValue(null, "name");
1253 if (splitPerm == null) {
1254 Slog.w(TAG, "<split-permission> without name in " + permFile + " at "
1255 + parser.getPositionDescription());
1256 XmlUtils.skipCurrentTag(parser);
1257 return;
1258 }
1259 String targetSdkStr = parser.getAttributeValue(null, "targetSdk");
1260 int targetSdk = Build.VERSION_CODES.CUR_DEVELOPMENT + 1;
1261 if (!TextUtils.isEmpty(targetSdkStr)) {
1262 try {
1263 targetSdk = Integer.parseInt(targetSdkStr);
1264 } catch (NumberFormatException e) {
1265 Slog.w(TAG, "<split-permission> targetSdk not an integer in " + permFile + " at "
1266 + parser.getPositionDescription());
1267 XmlUtils.skipCurrentTag(parser);
1268 return;
1269 }
1270 }
1271 final int depth = parser.getDepth();
1272 List<String> newPermissions = new ArrayList<>();
1273 while (XmlUtils.nextElementWithin(parser, depth)) {
1274 String name = parser.getName();
1275 if ("new-permission".equals(name)) {
1276 final String newName = parser.getAttributeValue(null, "name");
1277 if (TextUtils.isEmpty(newName)) {
1278 Slog.w(TAG, "name is required for <new-permission> in "
1279 + parser.getPositionDescription());
1280 continue;
1281 }
1282 newPermissions.add(newName);
1283 } else {
1284 XmlUtils.skipCurrentTag(parser);
1285 }
1286 }
1287 if (!newPermissions.isEmpty()) {
1288 mSplitPermissions.add(new SplitPermissionInfo(splitPerm, newPermissions, targetSdk));
1289 }
1290 }
Anthony Hughde787d42019-08-22 15:35:48 -07001291
Ryan Mitchell4fd8e6f2019-10-07 16:31:34 -07001292 private void readComponentOverrides(XmlPullParser parser, File permFile)
1293 throws IOException, XmlPullParserException {
1294 String pkgname = parser.getAttributeValue(null, "package");
1295 if (pkgname == null) {
1296 Slog.w(TAG, "<component-override> without package in "
1297 + permFile + " at " + parser.getPositionDescription());
1298 return;
1299 }
1300
1301 pkgname = pkgname.intern();
1302
1303 final int depth = parser.getDepth();
1304 while (XmlUtils.nextElementWithin(parser, depth)) {
1305 String name = parser.getName();
1306 if ("component".equals(name)) {
1307 String clsname = parser.getAttributeValue(null, "class");
1308 String enabled = parser.getAttributeValue(null, "enabled");
1309 if (clsname == null) {
1310 Slog.w(TAG, "<component> without class in "
1311 + permFile + " at " + parser.getPositionDescription());
1312 return;
1313 } else if (enabled == null) {
1314 Slog.w(TAG, "<component> without enabled in "
1315 + permFile + " at " + parser.getPositionDescription());
1316 return;
1317 }
1318
1319 if (clsname.startsWith(".")) {
1320 clsname = pkgname + clsname;
1321 }
1322
1323 clsname = clsname.intern();
1324
1325 ArrayMap<String, Boolean> componentEnabledStates =
1326 mPackageComponentEnabledState.get(pkgname);
1327 if (componentEnabledStates == null) {
1328 componentEnabledStates = new ArrayMap<>();
1329 mPackageComponentEnabledState.put(pkgname,
1330 componentEnabledStates);
1331 }
1332
1333 componentEnabledStates.put(clsname, !"false".equals(enabled));
1334 } else {
1335 XmlUtils.skipCurrentTag(parser);
1336 }
1337 }
1338 }
1339
Anthony Hughde787d42019-08-22 15:35:48 -07001340 private static boolean isSystemProcess() {
1341 return Process.myUid() == Process.SYSTEM_UID;
1342 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001343}