• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #ifndef __ANDROID_VNDK__
20 
21 #include <stdint.h>
22 #include <unistd.h>
23 
24 #include <utils/String16.h>
25 #include <utils/Singleton.h>
26 #include <utils/SortedVector.h>
27 
28 namespace android {
29 // ---------------------------------------------------------------------------
30 
31 /*
32  * PermissionCache caches permission checks for a given uid.
33  *
34  * Currently the cache is not updated when there is a permission change,
35  * for instance when an application is uninstalled.
36  *
37  * IMPORTANT: for the reason stated above, only system permissions are safe
38  * to cache. This restriction may be lifted at a later time.
39  *
40  */
41 
42 class PermissionCache : Singleton<PermissionCache> {
43     struct Entry {
44         String16    name;
45         uid_t       uid;
46         bool        granted;
47         inline bool operator < (const Entry& e) const {
48             return (uid == e.uid) ? (name < e.name) : (uid < e.uid);
49         }
50     };
51     mutable Mutex mLock;
52     // we pool all the permission names we see, as many permissions checks
53     // will have identical names
54     SortedVector< String16 > mPermissionNamesPool;
55     // this is our cache per say. it stores pooled names.
56     SortedVector< Entry > mCache;
57 
58     // free the whole cache, but keep the permission name pool
59     void purge();
60 
61     status_t check(bool* granted,
62             const String16& permission, uid_t uid) const;
63 
64     void cache(const String16& permission, uid_t uid, bool granted);
65 
66 public:
67     PermissionCache();
68 
69     static bool checkCallingPermission(const String16& permission);
70 
71     static bool checkCallingPermission(const String16& permission,
72                                 int32_t* outPid, int32_t* outUid);
73 
74     static bool checkPermission(const String16& permission,
75             pid_t pid, uid_t uid);
76 
77     static void purgeCache();
78 };
79 
80 // ---------------------------------------------------------------------------
81 } // namespace android
82 
83 #else // __ANDROID_VNDK__
84 #error "This header is not visible to vendors"
85 #endif // __ANDROID_VNDK__
86