1 /*
2  * Copyright (C) 2023 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 #include <binder/IPCThreadState.h>
18 #include <gui/LayerStatePermissions.h>
19 #include <private/android_filesystem_config.h>
20 #ifndef __ANDROID_VNDK__
21 #include <binder/PermissionCache.h>
22 #endif // __ANDROID_VNDK__
23 #include <gui/LayerState.h>
24 
25 namespace android {
26 std::vector<std::pair<String16, int>> LayerStatePermissions::mPermissionMap = {
27         // If caller has ACCESS_SURFACE_FLINGER, they automatically get ROTATE_SURFACE_FLINGER
28         // permission, as well
29         {String16("android.permission.ACCESS_SURFACE_FLINGER"),
30          layer_state_t::Permission::ACCESS_SURFACE_FLINGER |
31                  layer_state_t::Permission::ROTATE_SURFACE_FLINGER},
32         {String16("android.permission.ROTATE_SURFACE_FLINGER"),
33          layer_state_t::Permission::ROTATE_SURFACE_FLINGER},
34         {String16("android.permission.INTERNAL_SYSTEM_WINDOW"),
35          layer_state_t::Permission::INTERNAL_SYSTEM_WINDOW},
36 };
37 
callingThreadHasPermission(const String16 & permission,int pid,int uid)38 static bool callingThreadHasPermission(const String16& permission __attribute__((unused)),
39                                        int pid __attribute__((unused)),
40                                        int uid __attribute__((unused))) {
41 #ifndef __ANDROID_VNDK__
42     return uid == AID_GRAPHICS || uid == AID_SYSTEM ||
43             PermissionCache::checkPermission(permission, pid, uid);
44 #endif // __ANDROID_VNDK__
45     return false;
46 }
47 
getTransactionPermissions(int pid,int uid)48 uint32_t LayerStatePermissions::getTransactionPermissions(int pid, int uid) {
49     uint32_t permissions = 0;
50     for (const auto& [permissionName, permissionVal] : mPermissionMap) {
51         if (callingThreadHasPermission(permissionName, pid, uid)) {
52             permissions |= permissionVal;
53         }
54     }
55 
56     return permissions;
57 }
58 } // namespace android
59