2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.pm;
19 import android.content.pm.PackageManager;
20 import android.os.UserHandle;
21 import android.util.ArrayMap;
22 import android.util.ArraySet;
24 import android.util.SparseArray;
25 import android.util.SparseBooleanArray;
26 import com.android.internal.util.ArrayUtils;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
35 * This class encapsulates the permissions for a package or a shared user.
37 * There are two types of permissions: install (granted at installation)
38 * and runtime (granted at runtime). Install permissions are granted to
39 * all device users while runtime permissions are granted explicitly to
43 * The permissions are kept on a per device user basis. For example, an
44 * application may have some runtime permissions granted under the device
45 * owner but not granted under the secondary user.
47 * This class is also responsible for keeping track of the Linux gids per
48 * user for a package or a shared user. The gids are computed as a set of
49 * the gids for all granted permissions' gids on a per user basis.
52 public final class PermissionsState {
54 /** The permission operation failed. */
55 public static final int PERMISSION_OPERATION_FAILURE = -1;
57 /** The permission operation succeeded and no gids changed. */
58 public static final int PERMISSION_OPERATION_SUCCESS = 0;
60 /** The permission operation succeeded and gids changed. */
61 public static final int PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED = 1;
63 private static final int[] NO_GIDS = {};
65 private ArrayMap<String, PermissionData> mPermissions;
67 private int[] mGlobalGids = NO_GIDS;
69 private SparseBooleanArray mPermissionReviewRequired;
71 public PermissionsState() {
75 public PermissionsState(PermissionsState prototype) {
80 * Sets the global gids, applicable to all users.
82 * @param globalGids The global gids.
84 public void setGlobalGids(int[] globalGids) {
85 if (!ArrayUtils.isEmpty(globalGids)) {
86 mGlobalGids = Arrays.copyOf(globalGids, globalGids.length);
91 * Initialized this instance from another one.
93 * @param other The other instance.
95 public void copyFrom(PermissionsState other) {
99 if (mPermissions != null) {
100 if (other.mPermissions == null) {
103 mPermissions.clear();
106 if (other.mPermissions != null) {
107 if (mPermissions == null) {
108 mPermissions = new ArrayMap<>();
110 final int permissionCount = other.mPermissions.size();
111 for (int i = 0; i < permissionCount; i++) {
112 String name = other.mPermissions.keyAt(i);
113 PermissionData permissionData = other.mPermissions.valueAt(i);
114 mPermissions.put(name, new PermissionData(permissionData));
118 mGlobalGids = NO_GIDS;
119 if (other.mGlobalGids != NO_GIDS) {
120 mGlobalGids = Arrays.copyOf(other.mGlobalGids,
121 other.mGlobalGids.length);
124 if (mPermissionReviewRequired != null) {
125 if (other.mPermissionReviewRequired == null) {
126 mPermissionReviewRequired = null;
128 mPermissionReviewRequired.clear();
131 if (other.mPermissionReviewRequired != null) {
132 if (mPermissionReviewRequired == null) {
133 mPermissionReviewRequired = new SparseBooleanArray();
135 final int userCount = other.mPermissionReviewRequired.size();
136 for (int i = 0; i < userCount; i++) {
137 final boolean reviewRequired = other.mPermissionReviewRequired.valueAt(i);
138 mPermissionReviewRequired.put(i, reviewRequired);
143 public boolean isPermissionReviewRequired(int userId) {
144 return mPermissionReviewRequired != null && mPermissionReviewRequired.get(userId);
148 * Grant an install permission.
150 * @param permission The permission to grant.
151 * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
152 * or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
153 * #PERMISSION_OPERATION_FAILURE}.
155 public int grantInstallPermission(BasePermission permission) {
156 return grantPermission(permission, UserHandle.USER_ALL);
160 * Revoke an install permission.
162 * @param permission The permission to revoke.
163 * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
164 * or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
165 * #PERMISSION_OPERATION_FAILURE}.
167 public int revokeInstallPermission(BasePermission permission) {
168 return revokePermission(permission, UserHandle.USER_ALL);
172 * Grant a runtime permission for a given device user.
174 * @param permission The permission to grant.
175 * @param userId The device user id.
176 * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
177 * or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
178 * #PERMISSION_OPERATION_FAILURE}.
180 public int grantRuntimePermission(BasePermission permission, int userId) {
181 enforceValidUserId(userId);
182 if (userId == UserHandle.USER_ALL) {
183 return PERMISSION_OPERATION_FAILURE;
185 return grantPermission(permission, userId);
189 * Revoke a runtime permission for a given device user.
191 * @param permission The permission to revoke.
192 * @param userId The device user id.
193 * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
194 * or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
195 * #PERMISSION_OPERATION_FAILURE}.
197 public int revokeRuntimePermission(BasePermission permission, int userId) {
198 enforceValidUserId(userId);
199 if (userId == UserHandle.USER_ALL) {
200 return PERMISSION_OPERATION_FAILURE;
202 return revokePermission(permission, userId);
206 * Gets whether this state has a given runtime permission for a
207 * given device user id.
209 * @param name The permission name.
210 * @param userId The device user id.
211 * @return Whether this state has the permission.
213 public boolean hasRuntimePermission(String name, int userId) {
214 enforceValidUserId(userId);
215 return !hasInstallPermission(name) && hasPermission(name, userId);
219 * Gets whether this state has a given install permission.
221 * @param name The permission name.
222 * @return Whether this state has the permission.
224 public boolean hasInstallPermission(String name) {
225 return hasPermission(name, UserHandle.USER_ALL);
229 * Gets whether the state has a given permission for the specified
230 * user, regardless if this is an install or a runtime permission.
232 * @param name The permission name.
233 * @param userId The device user id.
234 * @return Whether the user has the permission.
236 public boolean hasPermission(String name, int userId) {
237 enforceValidUserId(userId);
239 if (mPermissions == null) {
243 PermissionData permissionData = mPermissions.get(name);
244 return permissionData != null && permissionData.isGranted(userId);
248 * Returns whether the state has any known request for the given permission name,
249 * whether or not it has been granted.
251 public boolean hasRequestedPermission(ArraySet<String> names) {
252 if (mPermissions == null) {
255 for (int i=names.size()-1; i>=0; i--) {
256 if (mPermissions.get(names.valueAt(i)) != null) {
264 * Gets all permissions for a given device user id regardless if they
265 * are install time or runtime permissions.
267 * @param userId The device user id.
268 * @return The permissions or an empty set.
270 public Set<String> getPermissions(int userId) {
271 enforceValidUserId(userId);
273 if (mPermissions == null) {
274 return Collections.emptySet();
277 Set<String> permissions = new ArraySet<>();
279 final int permissionCount = mPermissions.size();
280 for (int i = 0; i < permissionCount; i++) {
281 String permission = mPermissions.keyAt(i);
283 if (hasInstallPermission(permission)) {
284 permissions.add(permission);
287 if (userId != UserHandle.USER_ALL) {
288 if (hasRuntimePermission(permission, userId)) {
289 permissions.add(permission);
298 * Gets the state for an install permission or null if no such.
300 * @param name The permission name.
301 * @return The permission state.
303 public PermissionState getInstallPermissionState(String name) {
304 return getPermissionState(name, UserHandle.USER_ALL);
308 * Gets the state for a runtime permission or null if no such.
310 * @param name The permission name.
311 * @param userId The device user id.
312 * @return The permission state.
314 public PermissionState getRuntimePermissionState(String name, int userId) {
315 enforceValidUserId(userId);
316 return getPermissionState(name, userId);
320 * Gets all install permission states.
322 * @return The permission states or an empty set.
324 public List<PermissionState> getInstallPermissionStates() {
325 return getPermissionStatesInternal(UserHandle.USER_ALL);
329 * Gets all runtime permission states.
331 * @return The permission states or an empty set.
333 public List<PermissionState> getRuntimePermissionStates(int userId) {
334 enforceValidUserId(userId);
335 return getPermissionStatesInternal(userId);
339 * Gets the flags for a permission regardless if it is install or
340 * runtime permission.
342 * @param name The permission name.
343 * @return The permission state or null if no such.
345 public int getPermissionFlags(String name, int userId) {
346 PermissionState installPermState = getInstallPermissionState(name);
347 if (installPermState != null) {
348 return installPermState.getFlags();
350 PermissionState runtimePermState = getRuntimePermissionState(name, userId);
351 if (runtimePermState != null) {
352 return runtimePermState.getFlags();
358 * Update the flags associated with a given permission.
359 * @param permission The permission whose flags to update.
360 * @param userId The user for which to update.
361 * @param flagMask Mask for which flags to change.
362 * @param flagValues New values for the mask flags.
363 * @return Whether the permission flags changed.
365 public boolean updatePermissionFlags(BasePermission permission, int userId,
366 int flagMask, int flagValues) {
367 enforceValidUserId(userId);
369 final boolean mayChangeFlags = flagValues != 0 || flagMask != 0;
371 if (mPermissions == null) {
372 if (!mayChangeFlags) {
375 ensurePermissionData(permission);
378 PermissionData permissionData = mPermissions.get(permission.name);
379 if (permissionData == null) {
380 if (!mayChangeFlags) {
383 permissionData = ensurePermissionData(permission);
386 final int oldFlags = permissionData.getFlags(userId);
388 final boolean updated = permissionData.updateFlags(userId, flagMask, flagValues);
390 final int newFlags = permissionData.getFlags(userId);
391 if ((oldFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) == 0
392 && (newFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0) {
393 if (mPermissionReviewRequired == null) {
394 mPermissionReviewRequired = new SparseBooleanArray();
396 mPermissionReviewRequired.put(userId, true);
397 } else if ((oldFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0
398 && (newFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) == 0) {
399 if (mPermissionReviewRequired != null) {
400 mPermissionReviewRequired.delete(userId);
401 if (mPermissionReviewRequired.size() <= 0) {
402 mPermissionReviewRequired = null;
410 public boolean updatePermissionFlagsForAllPermissions(
411 int userId, int flagMask, int flagValues) {
412 enforceValidUserId(userId);
414 if (mPermissions == null) {
417 boolean changed = false;
418 final int permissionCount = mPermissions.size();
419 for (int i = 0; i < permissionCount; i++) {
420 PermissionData permissionData = mPermissions.valueAt(i);
421 changed |= permissionData.updateFlags(userId, flagMask, flagValues);
427 * Compute the Linux gids for a given device user from the permissions
428 * granted to this user. Note that these are computed to avoid additional
429 * state as they are rarely accessed.
431 * @param userId The device user id.
432 * @return The gids for the device user.
434 public int[] computeGids(int userId) {
435 enforceValidUserId(userId);
437 int[] gids = mGlobalGids;
439 if (mPermissions != null) {
440 final int permissionCount = mPermissions.size();
441 for (int i = 0; i < permissionCount; i++) {
442 String permission = mPermissions.keyAt(i);
443 if (!hasPermission(permission, userId)) {
446 PermissionData permissionData = mPermissions.valueAt(i);
447 final int[] permGids = permissionData.computeGids(userId);
448 if (permGids != NO_GIDS) {
449 gids = appendInts(gids, permGids);
458 * Compute the Linux gids for all device users from the permissions
459 * granted to these users.
461 * @return The gids for all device users.
463 public int[] computeGids(int[] userIds) {
464 int[] gids = mGlobalGids;
466 for (int userId : userIds) {
467 final int[] userGids = computeGids(userId);
468 gids = appendInts(gids, userGids);
475 * Resets the internal state of this object.
477 public void reset() {
478 mGlobalGids = NO_GIDS;
480 mPermissionReviewRequired = null;
483 private PermissionState getPermissionState(String name, int userId) {
484 if (mPermissions == null) {
487 PermissionData permissionData = mPermissions.get(name);
488 if (permissionData == null) {
491 return permissionData.getPermissionState(userId);
494 private List<PermissionState> getPermissionStatesInternal(int userId) {
495 enforceValidUserId(userId);
497 if (mPermissions == null) {
498 return Collections.emptyList();
501 List<PermissionState> permissionStates = new ArrayList<>();
503 final int permissionCount = mPermissions.size();
504 for (int i = 0; i < permissionCount; i++) {
505 PermissionData permissionData = mPermissions.valueAt(i);
507 PermissionState permissionState = permissionData.getPermissionState(userId);
508 if (permissionState != null) {
509 permissionStates.add(permissionState);
513 return permissionStates;
516 private int grantPermission(BasePermission permission, int userId) {
517 if (hasPermission(permission.name, userId)) {
518 return PERMISSION_OPERATION_FAILURE;
521 final boolean hasGids = !ArrayUtils.isEmpty(permission.computeGids(userId));
522 final int[] oldGids = hasGids ? computeGids(userId) : NO_GIDS;
524 PermissionData permissionData = ensurePermissionData(permission);
526 if (!permissionData.grant(userId)) {
527 return PERMISSION_OPERATION_FAILURE;
531 final int[] newGids = computeGids(userId);
532 if (oldGids.length != newGids.length) {
533 return PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED;
537 return PERMISSION_OPERATION_SUCCESS;
540 private int revokePermission(BasePermission permission, int userId) {
541 if (!hasPermission(permission.name, userId)) {
542 return PERMISSION_OPERATION_FAILURE;
545 final boolean hasGids = !ArrayUtils.isEmpty(permission.computeGids(userId));
546 final int[] oldGids = hasGids ? computeGids(userId) : NO_GIDS;
548 PermissionData permissionData = mPermissions.get(permission.name);
550 if (!permissionData.revoke(userId)) {
551 return PERMISSION_OPERATION_FAILURE;
554 if (permissionData.isDefault()) {
555 ensureNoPermissionData(permission.name);
559 final int[] newGids = computeGids(userId);
560 if (oldGids.length != newGids.length) {
561 return PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED;
565 return PERMISSION_OPERATION_SUCCESS;
568 private static int[] appendInts(int[] current, int[] added) {
569 if (current != null && added != null) {
570 for (int guid : added) {
571 current = ArrayUtils.appendInt(current, guid);
577 private static void enforceValidUserId(int userId) {
578 if (userId != UserHandle.USER_ALL && userId < 0) {
579 throw new IllegalArgumentException("Invalid userId:" + userId);
583 private PermissionData ensurePermissionData(BasePermission permission) {
584 if (mPermissions == null) {
585 mPermissions = new ArrayMap<>();
587 PermissionData permissionData = mPermissions.get(permission.name);
588 if (permissionData == null) {
589 permissionData = new PermissionData(permission);
590 mPermissions.put(permission.name, permissionData);
592 return permissionData;
595 private void ensureNoPermissionData(String name) {
596 if (mPermissions == null) {
599 mPermissions.remove(name);
600 if (mPermissions.isEmpty()) {
605 private static final class PermissionData {
606 private final BasePermission mPerm;
607 private SparseArray<PermissionState> mUserStates = new SparseArray<>();
609 public PermissionData(BasePermission perm) {
613 public PermissionData(PermissionData other) {
615 final int otherStateCount = other.mUserStates.size();
616 for (int i = 0; i < otherStateCount; i++) {
617 final int otherUserId = other.mUserStates.keyAt(i);
618 PermissionState otherState = other.mUserStates.valueAt(i);
619 mUserStates.put(otherUserId, new PermissionState(otherState));
623 public int[] computeGids(int userId) {
624 return mPerm.computeGids(userId);
627 public boolean isGranted(int userId) {
628 if (isInstallPermission()) {
629 userId = UserHandle.USER_ALL;
632 PermissionState userState = mUserStates.get(userId);
633 if (userState == null) {
637 return userState.mGranted;
640 public boolean grant(int userId) {
641 if (!isCompatibleUserId(userId)) {
645 if (isGranted(userId)) {
649 PermissionState userState = mUserStates.get(userId);
650 if (userState == null) {
651 userState = new PermissionState(mPerm.name);
652 mUserStates.put(userId, userState);
655 userState.mGranted = true;
660 public boolean revoke(int userId) {
661 if (!isCompatibleUserId(userId)) {
665 if (!isGranted(userId)) {
669 PermissionState userState = mUserStates.get(userId);
670 userState.mGranted = false;
672 if (userState.isDefault()) {
673 mUserStates.remove(userId);
679 public PermissionState getPermissionState(int userId) {
680 return mUserStates.get(userId);
683 public int getFlags(int userId) {
684 PermissionState userState = mUserStates.get(userId);
685 if (userState != null) {
686 return userState.mFlags;
691 public boolean isDefault() {
692 return mUserStates.size() <= 0;
695 public static boolean isInstallPermissionKey(int userId) {
696 return userId == UserHandle.USER_ALL;
699 public boolean updateFlags(int userId, int flagMask, int flagValues) {
700 if (isInstallPermission()) {
701 userId = UserHandle.USER_ALL;
704 if (!isCompatibleUserId(userId)) {
708 final int newFlags = flagValues & flagMask;
710 PermissionState userState = mUserStates.get(userId);
711 if (userState != null) {
712 final int oldFlags = userState.mFlags;
713 userState.mFlags = (userState.mFlags & ~flagMask) | newFlags;
714 if (userState.isDefault()) {
715 mUserStates.remove(userId);
717 return userState.mFlags != oldFlags;
718 } else if (newFlags != 0) {
719 userState = new PermissionState(mPerm.name);
720 userState.mFlags = newFlags;
721 mUserStates.put(userId, userState);
728 private boolean isCompatibleUserId(int userId) {
729 return isDefault() || !(isInstallPermission() ^ isInstallPermissionKey(userId));
732 private boolean isInstallPermission() {
733 return mUserStates.size() == 1
734 && mUserStates.get(UserHandle.USER_ALL) != null;
738 public static final class PermissionState {
739 private final String mName;
740 private boolean mGranted;
743 public PermissionState(String name) {
747 public PermissionState(PermissionState other) {
749 mGranted = other.mGranted;
750 mFlags = other.mFlags;
753 public boolean isDefault() {
754 return !mGranted && mFlags == 0;
757 public String getName() {
761 public boolean isGranted() {
765 public int getFlags() {