blob: 93b440bd542f283e6799cf1c5cc8e8b46b5522cd [file] [log] [blame]
Austin Borger249e6592024-03-10 22:28:11 -07001/*
2 * Copyright (C) 2024 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 "AttributionAndPermissionUtils.h"
18
19#include <binder/AppOpsManager.h>
20#include <binder/PermissionController.h>
Avichal Rakesh5788fec2024-03-15 14:39:20 -070021#include <com_android_internal_camera_flags.h>
Austin Borger249e6592024-03-10 22:28:11 -070022#include <cutils/properties.h>
23#include <private/android_filesystem_config.h>
24
25#include "CameraService.h"
Austin Borger22c5c852024-03-08 13:31:36 -080026
27#include <binder/IPCThreadState.h>
28#include <hwbinder/IPCThreadState.h>
29#include <binderthreadstate/CallerUtils.h>
Austin Borger249e6592024-03-10 22:28:11 -070030
31namespace android {
32
Avichal Rakesh5788fec2024-03-15 14:39:20 -070033namespace flags = com::android::internal::camera::flags;
34
Austin Borger249e6592024-03-10 22:28:11 -070035const std::string AttributionAndPermissionUtils::sDumpPermission("android.permission.DUMP");
36const std::string AttributionAndPermissionUtils::sManageCameraPermission(
37 "android.permission.MANAGE_CAMERA");
38const std::string AttributionAndPermissionUtils::sCameraPermission(
39 "android.permission.CAMERA");
40const std::string AttributionAndPermissionUtils::sSystemCameraPermission(
41 "android.permission.SYSTEM_CAMERA");
42const std::string AttributionAndPermissionUtils::sCameraHeadlessSystemUserPermission(
43 "android.permission.CAMERA_HEADLESS_SYSTEM_USER");
44const std::string AttributionAndPermissionUtils::sCameraPrivacyAllowlistPermission(
45 "android.permission.CAMERA_PRIVACY_ALLOWLIST");
46const std::string AttributionAndPermissionUtils::sCameraSendSystemEventsPermission(
47 "android.permission.CAMERA_SEND_SYSTEM_EVENTS");
48const std::string AttributionAndPermissionUtils::sCameraOpenCloseListenerPermission(
49 "android.permission.CAMERA_OPEN_CLOSE_LISTENER");
50const std::string AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission(
51 "android.permission.CAMERA_INJECT_EXTERNAL_CAMERA");
52
Austin Borger22c5c852024-03-08 13:31:36 -080053int AttributionAndPermissionUtils::getCallingUid() {
54 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
55 return hardware::IPCThreadState::self()->getCallingUid();
56 }
57 return IPCThreadState::self()->getCallingUid();
58}
59
60int AttributionAndPermissionUtils::getCallingPid() {
61 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
62 return hardware::IPCThreadState::self()->getCallingPid();
63 }
64 return IPCThreadState::self()->getCallingPid();
65}
66
67int64_t AttributionAndPermissionUtils::clearCallingIdentity() {
68 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
69 return hardware::IPCThreadState::self()->clearCallingIdentity();
70 }
71 return IPCThreadState::self()->clearCallingIdentity();
72}
73
74void AttributionAndPermissionUtils::restoreCallingIdentity(int64_t token) {
75 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
76 hardware::IPCThreadState::self()->restoreCallingIdentity(token);
77 } else {
78 IPCThreadState::self()->restoreCallingIdentity(token);
79 }
80 return;
81}
82
Austin Borger249e6592024-03-10 22:28:11 -070083bool AttributionAndPermissionUtils::checkAutomotivePrivilegedClient(const std::string &cameraId,
84 const AttributionSourceState &attributionSource) {
85 if (isAutomotivePrivilegedClient(attributionSource.uid)) {
86 // If cameraId is empty, then it means that this check is not used for the
87 // purpose of accessing a specific camera, hence grant permission just
88 // based on uid to the automotive privileged client.
89 if (cameraId.empty())
90 return true;
91
92 auto cameraService = mCameraService.promote();
93 if (cameraService == nullptr) {
94 ALOGE("%s: CameraService unavailable.", __FUNCTION__);
95 return false;
96 }
97
98 // If this call is used for accessing a specific camera then cam_id must be provided.
99 // In that case, only pre-grants the permission for accessing the exterior system only
100 // camera.
101 return cameraService->isAutomotiveExteriorSystemCamera(cameraId);
102 }
103
104 return false;
105}
106
107bool AttributionAndPermissionUtils::checkPermissionForPreflight(const std::string &cameraId,
108 const std::string &permission, const AttributionSourceState &attributionSource,
109 const std::string& message, int32_t attributedOpCode) {
110 if (checkAutomotivePrivilegedClient(cameraId, attributionSource)) {
111 return true;
112 }
113
Avichal Rakesh5788fec2024-03-15 14:39:20 -0700114 if (!flags::cache_permission_services()) {
115 PermissionChecker permissionChecker;
116 return permissionChecker.checkPermissionForPreflight(
117 toString16(permission), attributionSource, toString16(message),
118 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
119 } else {
120 return mPermissionChecker->checkPermissionForPreflight(
121 toString16(permission), attributionSource, toString16(message),
122 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
123 }
Austin Borger249e6592024-03-10 22:28:11 -0700124}
125
126// Can camera service trust the caller based on the calling UID?
127bool AttributionAndPermissionUtils::isTrustedCallingUid(uid_t uid) {
128 switch (uid) {
129 case AID_MEDIA: // mediaserver
130 case AID_CAMERASERVER: // cameraserver
131 case AID_RADIO: // telephony
132 return true;
133 default:
134 return false;
135 }
136}
137
138bool AttributionAndPermissionUtils::isAutomotiveDevice() {
139 // Checks the property ro.hardware.type and returns true if it is
140 // automotive.
141 char value[PROPERTY_VALUE_MAX] = {0};
142 property_get("ro.hardware.type", value, "");
143 return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0;
144}
145
146bool AttributionAndPermissionUtils::isHeadlessSystemUserMode() {
147 // Checks if the device is running in headless system user mode
148 // by checking the property ro.fw.mu.headless_system_user.
149 char value[PROPERTY_VALUE_MAX] = {0};
150 property_get("ro.fw.mu.headless_system_user", value, "");
151 return strncmp(value, "true", PROPERTY_VALUE_MAX) == 0;
152}
153
154bool AttributionAndPermissionUtils::isAutomotivePrivilegedClient(int32_t uid) {
155 // Returns false if this is not an automotive device type.
156 if (!isAutomotiveDevice())
157 return false;
158
159 // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a
160 // privileged client uid used for safety critical use cases such as
161 // rear view and surround view.
162 return uid == AID_AUTOMOTIVE_EVS;
163}
164
165status_t AttributionAndPermissionUtils::getUidForPackage(const std::string &packageName,
166 int userId, /*inout*/uid_t& uid, int err) {
167 PermissionController pc;
168 uid = pc.getPackageUid(toString16(packageName), 0);
169 if (uid <= 0) {
170 ALOGE("Unknown package: '%s'", packageName.c_str());
171 dprintf(err, "Unknown package: '%s'\n", packageName.c_str());
172 return BAD_VALUE;
173 }
174
175 if (userId < 0) {
176 ALOGE("Invalid user: %d", userId);
177 dprintf(err, "Invalid user: %d\n", userId);
178 return BAD_VALUE;
179 }
180
181 uid = multiuser_get_uid(userId, uid);
182 return NO_ERROR;
183}
184
185bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() {
Austin Borger22c5c852024-03-08 13:31:36 -0800186 return (getCallingPid() == getpid());
Austin Borger249e6592024-03-10 22:28:11 -0700187}
188
189bool AttributionAndPermissionUtils::hasPermissionsForCamera(const std::string& cameraId,
190 const AttributionSourceState& attributionSource) {
191 return checkPermissionForPreflight(cameraId, sCameraPermission,
192 attributionSource, std::string(), AppOpsManager::OP_NONE);
193}
194
195bool AttributionAndPermissionUtils::hasPermissionsForSystemCamera(const std::string& cameraId,
196 const AttributionSourceState& attributionSource, bool checkCameraPermissions) {
197 bool systemCameraPermission = checkPermissionForPreflight(cameraId,
198 sSystemCameraPermission, attributionSource, std::string(), AppOpsManager::OP_NONE);
199 return systemCameraPermission && (!checkCameraPermissions
200 || hasPermissionsForCamera(cameraId, attributionSource));
201}
202
203bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser(
204 const std::string& cameraId, const AttributionSourceState& attributionSource) {
205 return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission,
206 attributionSource, std::string(), AppOpsManager::OP_NONE);
207}
208
209bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist(
210 const AttributionSourceState& attributionSource) {
211 return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission,
212 attributionSource, std::string(), AppOpsManager::OP_NONE);
213}
214
215bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener(
216 const AttributionSourceState& attributionSource) {
217 return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission,
218 attributionSource, std::string(), AppOpsManager::OP_NONE);
219}
220
221} // namespace android