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
17package com.android.server.pm;
18
19import android.content.pm.PackageManager;
20import android.os.UserHandle;
21import android.util.ArrayMap;
22import android.util.ArraySet;
23
24import android.util.SparseArray;
25import android.util.SparseBooleanArray;
26import com.android.internal.util.ArrayUtils;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.Collections;
31import java.util.List;
32import java.util.Set;
33
34/**
35 * This class encapsulates the permissions for a package or a shared user.
36 * <p>
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
40 * specific users.
41 * </p>
42 * <p>
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.
46 * <p>
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.
50 * </p>
51 */
52public final class PermissionsState {
53
54    /** The permission operation failed. */
55    public static final int PERMISSION_OPERATION_FAILURE = -1;
56
57    /** The permission operation succeeded and no gids changed. */
58    public static final int PERMISSION_OPERATION_SUCCESS = 0;
59
60    /** The permission operation succeeded and gids changed. */
61    public static final int PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED = 1;
62
63    private static final int[] NO_GIDS = {};
64
65    private ArrayMap<String, PermissionData> mPermissions;
66
67    private int[] mGlobalGids = NO_GIDS;
68
69    private SparseBooleanArray mPermissionReviewRequired;
70
71    public PermissionsState() {
72        /* do nothing */
73    }
74
75    public PermissionsState(PermissionsState prototype) {
76        copyFrom(prototype);
77    }
78
79    /**
80     * Sets the global gids, applicable to all users.
81     *
82     * @param globalGids The global gids.
83     */
84    public void setGlobalGids(int[] globalGids) {
85        if (!ArrayUtils.isEmpty(globalGids)) {
86            mGlobalGids = Arrays.copyOf(globalGids, globalGids.length);
87        }
88    }
89
90    /**
91     * Initialized this instance from another one.
92     *
93     * @param other The other instance.
94     */
95    public void copyFrom(PermissionsState other) {
96        if (other == this) {
97            return;
98        }
99        if (mPermissions != null) {
100            if (other.mPermissions == null) {
101                mPermissions = null;
102            } else {
103                mPermissions.clear();
104            }
105        }
106        if (other.mPermissions != null) {
107            if (mPermissions == null) {
108                mPermissions = new ArrayMap<>();
109            }
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));
115            }
116        }
117
118        mGlobalGids = NO_GIDS;
119        if (other.mGlobalGids != NO_GIDS) {
120            mGlobalGids = Arrays.copyOf(other.mGlobalGids,
121                    other.mGlobalGids.length);
122        }
123
124        if (mPermissionReviewRequired != null) {
125            if (other.mPermissionReviewRequired == null) {
126                mPermissionReviewRequired = null;
127            } else {
128                mPermissionReviewRequired.clear();
129            }
130        }
131        if (other.mPermissionReviewRequired != null) {
132            if (mPermissionReviewRequired == null) {
133                mPermissionReviewRequired = new SparseBooleanArray();
134            }
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);
139            }
140        }
141    }
142
143    @Override
144    public boolean equals(Object obj) {
145        if (this == obj) {
146            return true;
147        }
148        if (obj == null) {
149            return false;
150        }
151        if (getClass() != obj.getClass()) {
152            return false;
153        }
154        final PermissionsState other = (PermissionsState) obj;
155
156        if (mPermissions == null) {
157            if (other.mPermissions != null) {
158                return false;
159            }
160        } else if (!mPermissions.equals(other.mPermissions)) {
161            return false;
162        }
163        if (mPermissionReviewRequired == null) {
164            if (other.mPermissionReviewRequired != null) {
165                return false;
166            }
167        } else if (!mPermissionReviewRequired.equals(other.mPermissionReviewRequired)) {
168            return false;
169        }
170        return Arrays.equals(mGlobalGids, other.mGlobalGids);
171    }
172
173    public boolean isPermissionReviewRequired(int userId) {
174        return mPermissionReviewRequired != null && mPermissionReviewRequired.get(userId);
175    }
176
177    /**
178     * Grant an install permission.
179     *
180     * @param permission The permission to grant.
181     * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
182     *     or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
183     *     #PERMISSION_OPERATION_FAILURE}.
184     */
185    public int grantInstallPermission(BasePermission permission) {
186        return grantPermission(permission, UserHandle.USER_ALL);
187    }
188
189    /**
190     * Revoke an install permission.
191     *
192     * @param permission The permission to revoke.
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}.
196     */
197    public int revokeInstallPermission(BasePermission permission) {
198        return revokePermission(permission, UserHandle.USER_ALL);
199    }
200
201    /**
202     * Grant a runtime permission for a given device user.
203     *
204     * @param permission The permission to grant.
205     * @param userId The device user id.
206     * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
207     *     or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
208     *     #PERMISSION_OPERATION_FAILURE}.
209     */
210    public int grantRuntimePermission(BasePermission permission, int userId) {
211        enforceValidUserId(userId);
212        if (userId == UserHandle.USER_ALL) {
213            return PERMISSION_OPERATION_FAILURE;
214        }
215        return grantPermission(permission, userId);
216    }
217
218    /**
219     *  Revoke a runtime permission for a given device user.
220     *
221     * @param permission The permission to revoke.
222     * @param userId The device user id.
223     * @return The operation result which is either {@link #PERMISSION_OPERATION_SUCCESS},
224     *     or {@link #PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED}, or {@link
225     *     #PERMISSION_OPERATION_FAILURE}.
226     */
227    public int revokeRuntimePermission(BasePermission permission, int userId) {
228        enforceValidUserId(userId);
229        if (userId == UserHandle.USER_ALL) {
230            return PERMISSION_OPERATION_FAILURE;
231        }
232        return revokePermission(permission, userId);
233    }
234
235    /**
236     * Gets whether this state has a given runtime permission for a
237     * given device user id.
238     *
239     * @param name The permission name.
240     * @param userId The device user id.
241     * @return Whether this state has the permission.
242     */
243    public boolean hasRuntimePermission(String name, int userId) {
244        enforceValidUserId(userId);
245        return !hasInstallPermission(name) && hasPermission(name, userId);
246    }
247
248    /**
249     * Gets whether this state has a given install permission.
250     *
251     * @param name The permission name.
252     * @return Whether this state has the permission.
253     */
254    public boolean hasInstallPermission(String name) {
255        return hasPermission(name, UserHandle.USER_ALL);
256    }
257
258    /**
259     * Gets whether the state has a given permission for the specified
260     * user, regardless if this is an install or a runtime permission.
261     *
262     * @param name The permission name.
263     * @param userId The device user id.
264     * @return Whether the user has the permission.
265     */
266    public boolean hasPermission(String name, int userId) {
267        enforceValidUserId(userId);
268
269        if (mPermissions == null) {
270            return false;
271        }
272
273        PermissionData permissionData = mPermissions.get(name);
274        return permissionData != null && permissionData.isGranted(userId);
275    }
276
277    /**
278     * Returns whether the state has any known request for the given permission name,
279     * whether or not it has been granted.
280     */
281    public boolean hasRequestedPermission(ArraySet<String> names) {
282        if (mPermissions == null) {
283            return false;
284        }
285        for (int i=names.size()-1; i>=0; i--) {
286            if (mPermissions.get(names.valueAt(i)) != null) {
287                return true;
288            }
289        }
290        return false;
291    }
292
293    /**
294     * Gets all permissions for a given device user id regardless if they
295     * are install time or runtime permissions.
296     *
297     * @param userId The device user id.
298     * @return The permissions or an empty set.
299     */
300    public Set<String> getPermissions(int userId) {
301        enforceValidUserId(userId);
302
303        if (mPermissions == null) {
304            return Collections.emptySet();
305        }
306
307        Set<String> permissions = new ArraySet<>(mPermissions.size());
308
309        final int permissionCount = mPermissions.size();
310        for (int i = 0; i < permissionCount; i++) {
311            String permission = mPermissions.keyAt(i);
312
313            if (hasInstallPermission(permission)) {
314                permissions.add(permission);
315                continue;
316            }
317
318            if (userId != UserHandle.USER_ALL) {
319                if (hasRuntimePermission(permission, userId)) {
320                    permissions.add(permission);
321                }
322            }
323        }
324
325        return permissions;
326    }
327
328    /**
329     * Gets the state for an install permission or null if no such.
330     *
331     * @param name The permission name.
332     * @return The permission state.
333     */
334    public PermissionState getInstallPermissionState(String name) {
335        return getPermissionState(name, UserHandle.USER_ALL);
336    }
337
338    /**
339     * Gets the state for a runtime permission or null if no such.
340     *
341     * @param name The permission name.
342     * @param userId The device user id.
343     * @return The permission state.
344     */
345    public PermissionState getRuntimePermissionState(String name, int userId) {
346        enforceValidUserId(userId);
347        return getPermissionState(name, userId);
348    }
349
350    /**
351     * Gets all install permission states.
352     *
353     * @return The permission states or an empty set.
354     */
355    public List<PermissionState> getInstallPermissionStates() {
356        return getPermissionStatesInternal(UserHandle.USER_ALL);
357    }
358
359    /**
360     * Gets all runtime permission states.
361     *
362     * @return The permission states or an empty set.
363     */
364    public List<PermissionState> getRuntimePermissionStates(int userId) {
365        enforceValidUserId(userId);
366        return getPermissionStatesInternal(userId);
367    }
368
369    /**
370     * Gets the flags for a permission regardless if it is install or
371     * runtime permission.
372     *
373     * @param name The permission name.
374     * @return The permission state or null if no such.
375     */
376    public int getPermissionFlags(String name, int userId) {
377        PermissionState installPermState = getInstallPermissionState(name);
378        if (installPermState != null) {
379            return installPermState.getFlags();
380        }
381        PermissionState runtimePermState = getRuntimePermissionState(name, userId);
382        if (runtimePermState != null) {
383            return runtimePermState.getFlags();
384        }
385        return 0;
386    }
387
388    /**
389     * Update the flags associated with a given permission.
390     * @param permission The permission whose flags to update.
391     * @param userId The user for which to update.
392     * @param flagMask Mask for which flags to change.
393     * @param flagValues New values for the mask flags.
394     * @return Whether the permission flags changed.
395     */
396    public boolean updatePermissionFlags(BasePermission permission, int userId,
397            int flagMask, int flagValues) {
398        enforceValidUserId(userId);
399
400        final boolean mayChangeFlags = flagValues != 0 || flagMask != 0;
401
402        if (mPermissions == null) {
403            if (!mayChangeFlags) {
404                return false;
405            }
406            ensurePermissionData(permission);
407        }
408
409        PermissionData permissionData = mPermissions.get(permission.name);
410        if (permissionData == null) {
411            if (!mayChangeFlags) {
412                return false;
413            }
414            permissionData = ensurePermissionData(permission);
415        }
416
417        final int oldFlags = permissionData.getFlags(userId);
418
419        final boolean updated = permissionData.updateFlags(userId, flagMask, flagValues);
420        if (updated) {
421            final int newFlags = permissionData.getFlags(userId);
422            if ((oldFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) == 0
423                    && (newFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0) {
424                if (mPermissionReviewRequired == null) {
425                    mPermissionReviewRequired = new SparseBooleanArray();
426                }
427                mPermissionReviewRequired.put(userId, true);
428            } else if ((oldFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0
429                    && (newFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) == 0) {
430                if (mPermissionReviewRequired != null) {
431                    mPermissionReviewRequired.delete(userId);
432                    if (mPermissionReviewRequired.size() <= 0) {
433                        mPermissionReviewRequired = null;
434                    }
435                }
436            }
437        }
438        return updated;
439    }
440
441    public boolean updatePermissionFlagsForAllPermissions(
442            int userId, int flagMask, int flagValues) {
443        enforceValidUserId(userId);
444
445        if (mPermissions == null) {
446            return false;
447        }
448        boolean changed = false;
449        final int permissionCount = mPermissions.size();
450        for (int i = 0; i < permissionCount; i++) {
451            PermissionData permissionData = mPermissions.valueAt(i);
452            changed |= permissionData.updateFlags(userId, flagMask, flagValues);
453        }
454        return changed;
455    }
456
457    /**
458     * Compute the Linux gids for a given device user from the permissions
459     * granted to this user. Note that these are computed to avoid additional
460     * state as they are rarely accessed.
461     *
462     * @param userId The device user id.
463     * @return The gids for the device user.
464     */
465    public int[] computeGids(int userId) {
466        enforceValidUserId(userId);
467
468        int[] gids = mGlobalGids;
469
470        if (mPermissions != null) {
471            final int permissionCount = mPermissions.size();
472            for (int i = 0; i < permissionCount; i++) {
473                String permission = mPermissions.keyAt(i);
474                if (!hasPermission(permission, userId)) {
475                    continue;
476                }
477                PermissionData permissionData = mPermissions.valueAt(i);
478                final int[] permGids = permissionData.computeGids(userId);
479                if (permGids != NO_GIDS) {
480                    gids = appendInts(gids, permGids);
481                }
482            }
483        }
484
485        return gids;
486    }
487
488    /**
489     * Compute the Linux gids for all device users from the permissions
490     * granted to these users.
491     *
492     * @return The gids for all device users.
493     */
494    public int[] computeGids(int[] userIds) {
495        int[] gids = mGlobalGids;
496
497        for (int userId : userIds) {
498            final int[] userGids = computeGids(userId);
499            gids = appendInts(gids, userGids);
500        }
501
502        return gids;
503    }
504
505    /**
506     * Resets the internal state of this object.
507     */
508    public void reset() {
509        mGlobalGids = NO_GIDS;
510        mPermissions = null;
511        mPermissionReviewRequired = null;
512    }
513
514    private PermissionState getPermissionState(String name, int userId) {
515        if (mPermissions == null) {
516            return null;
517        }
518        PermissionData permissionData = mPermissions.get(name);
519        if (permissionData == null) {
520            return null;
521        }
522        return permissionData.getPermissionState(userId);
523    }
524
525    private List<PermissionState> getPermissionStatesInternal(int userId) {
526        enforceValidUserId(userId);
527
528        if (mPermissions == null) {
529            return Collections.emptyList();
530        }
531
532        List<PermissionState> permissionStates = new ArrayList<>();
533
534        final int permissionCount = mPermissions.size();
535        for (int i = 0; i < permissionCount; i++) {
536            PermissionData permissionData = mPermissions.valueAt(i);
537
538            PermissionState permissionState = permissionData.getPermissionState(userId);
539            if (permissionState != null) {
540                permissionStates.add(permissionState);
541            }
542        }
543
544        return permissionStates;
545    }
546
547    private int grantPermission(BasePermission permission, int userId) {
548        if (hasPermission(permission.name, userId)) {
549            return PERMISSION_OPERATION_FAILURE;
550        }
551
552        final boolean hasGids = !ArrayUtils.isEmpty(permission.computeGids(userId));
553        final int[] oldGids = hasGids ? computeGids(userId) : NO_GIDS;
554
555        PermissionData permissionData = ensurePermissionData(permission);
556
557        if (!permissionData.grant(userId)) {
558            return PERMISSION_OPERATION_FAILURE;
559        }
560
561        if (hasGids) {
562            final int[] newGids = computeGids(userId);
563            if (oldGids.length != newGids.length) {
564                return PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED;
565            }
566        }
567
568        return PERMISSION_OPERATION_SUCCESS;
569    }
570
571    private int revokePermission(BasePermission permission, int userId) {
572        if (!hasPermission(permission.name, userId)) {
573            return PERMISSION_OPERATION_FAILURE;
574        }
575
576        final boolean hasGids = !ArrayUtils.isEmpty(permission.computeGids(userId));
577        final int[] oldGids = hasGids ? computeGids(userId) : NO_GIDS;
578
579        PermissionData permissionData = mPermissions.get(permission.name);
580
581        if (!permissionData.revoke(userId)) {
582            return PERMISSION_OPERATION_FAILURE;
583        }
584
585        if (permissionData.isDefault()) {
586            ensureNoPermissionData(permission.name);
587        }
588
589        if (hasGids) {
590            final int[] newGids = computeGids(userId);
591            if (oldGids.length != newGids.length) {
592                return PERMISSION_OPERATION_SUCCESS_GIDS_CHANGED;
593            }
594        }
595
596        return PERMISSION_OPERATION_SUCCESS;
597    }
598
599    // TODO: fix this to use arraycopy and append all ints in one go
600    private static int[] appendInts(int[] current, int[] added) {
601        if (current != null && added != null) {
602            for (int guid : added) {
603                current = ArrayUtils.appendInt(current, guid);
604            }
605        }
606        return current;
607    }
608
609    private static void enforceValidUserId(int userId) {
610        if (userId != UserHandle.USER_ALL && userId < 0) {
611            throw new IllegalArgumentException("Invalid userId:" + userId);
612        }
613    }
614
615    private PermissionData ensurePermissionData(BasePermission permission) {
616        if (mPermissions == null) {
617            mPermissions = new ArrayMap<>();
618        }
619        PermissionData permissionData = mPermissions.get(permission.name);
620        if (permissionData == null) {
621            permissionData = new PermissionData(permission);
622            mPermissions.put(permission.name, permissionData);
623        }
624        return permissionData;
625    }
626
627    private void ensureNoPermissionData(String name) {
628        if (mPermissions == null) {
629            return;
630        }
631        mPermissions.remove(name);
632        if (mPermissions.isEmpty()) {
633            mPermissions = null;
634        }
635    }
636
637    private static final class PermissionData {
638        private final BasePermission mPerm;
639        private SparseArray<PermissionState> mUserStates = new SparseArray<>();
640
641        public PermissionData(BasePermission perm) {
642            mPerm = perm;
643        }
644
645        public PermissionData(PermissionData other) {
646            this(other.mPerm);
647            final int otherStateCount = other.mUserStates.size();
648            for (int i = 0; i < otherStateCount; i++) {
649                final int otherUserId = other.mUserStates.keyAt(i);
650                PermissionState otherState = other.mUserStates.valueAt(i);
651                mUserStates.put(otherUserId, new PermissionState(otherState));
652            }
653        }
654
655        public int[] computeGids(int userId) {
656            return mPerm.computeGids(userId);
657        }
658
659        public boolean isGranted(int userId) {
660            if (isInstallPermission()) {
661                userId = UserHandle.USER_ALL;
662            }
663
664            PermissionState userState = mUserStates.get(userId);
665            if (userState == null) {
666                return false;
667            }
668
669            return userState.mGranted;
670        }
671
672        public boolean grant(int userId) {
673            if (!isCompatibleUserId(userId)) {
674                return false;
675            }
676
677            if (isGranted(userId)) {
678                return false;
679            }
680
681            PermissionState userState = mUserStates.get(userId);
682            if (userState == null) {
683                userState = new PermissionState(mPerm.name);
684                mUserStates.put(userId, userState);
685            }
686
687            userState.mGranted = true;
688
689            return true;
690        }
691
692        public boolean revoke(int userId) {
693            if (!isCompatibleUserId(userId)) {
694                return false;
695            }
696
697            if (!isGranted(userId)) {
698                return false;
699            }
700
701            PermissionState userState = mUserStates.get(userId);
702            userState.mGranted = false;
703
704            if (userState.isDefault()) {
705                mUserStates.remove(userId);
706            }
707
708            return true;
709        }
710
711        public PermissionState getPermissionState(int userId) {
712            return mUserStates.get(userId);
713        }
714
715        public int getFlags(int userId) {
716            PermissionState userState = mUserStates.get(userId);
717            if (userState != null) {
718                return userState.mFlags;
719            }
720            return 0;
721        }
722
723        public boolean isDefault() {
724            return mUserStates.size() <= 0;
725        }
726
727        public static boolean isInstallPermissionKey(int userId) {
728            return userId == UserHandle.USER_ALL;
729        }
730
731        public boolean updateFlags(int userId, int flagMask, int flagValues) {
732            if (isInstallPermission()) {
733                userId = UserHandle.USER_ALL;
734            }
735
736            if (!isCompatibleUserId(userId)) {
737                return false;
738            }
739
740            final int newFlags = flagValues & flagMask;
741
742            PermissionState userState = mUserStates.get(userId);
743            if (userState != null) {
744                final int oldFlags = userState.mFlags;
745                userState.mFlags = (userState.mFlags & ~flagMask) | newFlags;
746                if (userState.isDefault()) {
747                    mUserStates.remove(userId);
748                }
749                return userState.mFlags != oldFlags;
750            } else if (newFlags != 0) {
751                userState = new PermissionState(mPerm.name);
752                userState.mFlags = newFlags;
753                mUserStates.put(userId, userState);
754                return true;
755            }
756
757            return false;
758        }
759
760        private boolean isCompatibleUserId(int userId) {
761            return isDefault() || !(isInstallPermission() ^ isInstallPermissionKey(userId));
762        }
763
764        private boolean isInstallPermission() {
765            return mUserStates.size() == 1
766                    && mUserStates.get(UserHandle.USER_ALL) != null;
767        }
768    }
769
770    public static final class PermissionState {
771        private final String mName;
772        private boolean mGranted;
773        private int mFlags;
774
775        public PermissionState(String name) {
776            mName = name;
777        }
778
779        public PermissionState(PermissionState other) {
780            mName = other.mName;
781            mGranted = other.mGranted;
782            mFlags = other.mFlags;
783        }
784
785        public boolean isDefault() {
786            return !mGranted && mFlags == 0;
787        }
788
789        public String getName() {
790            return mName;
791        }
792
793        public boolean isGranted() {
794            return mGranted;
795        }
796
797        public int getFlags() {
798            return mFlags;
799        }
800    }
801}
802