1 /*
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>
21 #include <com_android_internal_camera_flags.h>
22 #include <cutils/properties.h>
23 #include <private/android_filesystem_config.h>
24
25 #include "CameraService.h"
26
27 #include <binder/IPCThreadState.h>
28 #include <hwbinder/IPCThreadState.h>
29 #include <binderthreadstate/CallerUtils.h>
30
31 namespace android {
32
33 namespace flags = com::android::internal::camera::flags;
34
35 const std::string AttributionAndPermissionUtils::sDumpPermission("android.permission.DUMP");
36 const std::string AttributionAndPermissionUtils::sManageCameraPermission(
37 "android.permission.MANAGE_CAMERA");
38 const std::string AttributionAndPermissionUtils::sCameraPermission(
39 "android.permission.CAMERA");
40 const std::string AttributionAndPermissionUtils::sSystemCameraPermission(
41 "android.permission.SYSTEM_CAMERA");
42 const std::string AttributionAndPermissionUtils::sCameraHeadlessSystemUserPermission(
43 "android.permission.CAMERA_HEADLESS_SYSTEM_USER");
44 const std::string AttributionAndPermissionUtils::sCameraPrivacyAllowlistPermission(
45 "android.permission.CAMERA_PRIVACY_ALLOWLIST");
46 const std::string AttributionAndPermissionUtils::sCameraSendSystemEventsPermission(
47 "android.permission.CAMERA_SEND_SYSTEM_EVENTS");
48 const std::string AttributionAndPermissionUtils::sCameraOpenCloseListenerPermission(
49 "android.permission.CAMERA_OPEN_CLOSE_LISTENER");
50 const std::string AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission(
51 "android.permission.CAMERA_INJECT_EXTERNAL_CAMERA");
52
getCallingUid()53 int AttributionAndPermissionUtils::getCallingUid() {
54 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
55 return hardware::IPCThreadState::self()->getCallingUid();
56 }
57 return IPCThreadState::self()->getCallingUid();
58 }
59
getCallingPid()60 int AttributionAndPermissionUtils::getCallingPid() {
61 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
62 return hardware::IPCThreadState::self()->getCallingPid();
63 }
64 return IPCThreadState::self()->getCallingPid();
65 }
66
clearCallingIdentity()67 int64_t AttributionAndPermissionUtils::clearCallingIdentity() {
68 if (getCurrentServingCall() == BinderCallType::HWBINDER) {
69 return hardware::IPCThreadState::self()->clearCallingIdentity();
70 }
71 return IPCThreadState::self()->clearCallingIdentity();
72 }
73
restoreCallingIdentity(int64_t token)74 void 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
checkAutomotivePrivilegedClient(const std::string & cameraId,const AttributionSourceState & attributionSource)83 bool 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
checkPermissionForPreflight(const std::string & cameraId,const std::string & permission,const AttributionSourceState & attributionSource,const std::string & message,int32_t attributedOpCode)107 bool 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
114 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 }
124 }
125
126 // Can camera service trust the caller based on the calling UID?
isTrustedCallingUid(uid_t uid)127 bool 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
isAutomotiveDevice()138 bool 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
isHeadlessSystemUserMode()146 bool 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
isAutomotivePrivilegedClient(int32_t uid)154 bool 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
getUidForPackage(const std::string & packageName,int userId,uid_t & uid,int err)165 status_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
isCallerCameraServerNotDelegating()185 bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() {
186 return (getCallingPid() == getpid());
187 }
188
hasPermissionsForCamera(const std::string & cameraId,const AttributionSourceState & attributionSource)189 bool AttributionAndPermissionUtils::hasPermissionsForCamera(const std::string& cameraId,
190 const AttributionSourceState& attributionSource) {
191 return checkPermissionForPreflight(cameraId, sCameraPermission,
192 attributionSource, std::string(), AppOpsManager::OP_NONE);
193 }
194
hasPermissionsForSystemCamera(const std::string & cameraId,const AttributionSourceState & attributionSource,bool checkCameraPermissions)195 bool 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
hasPermissionsForCameraHeadlessSystemUser(const std::string & cameraId,const AttributionSourceState & attributionSource)203 bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser(
204 const std::string& cameraId, const AttributionSourceState& attributionSource) {
205 return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission,
206 attributionSource, std::string(), AppOpsManager::OP_NONE);
207 }
208
hasPermissionsForCameraPrivacyAllowlist(const AttributionSourceState & attributionSource)209 bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist(
210 const AttributionSourceState& attributionSource) {
211 return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission,
212 attributionSource, std::string(), AppOpsManager::OP_NONE);
213 }
214
hasPermissionsForOpenCloseListener(const AttributionSourceState & attributionSource)215 bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener(
216 const AttributionSourceState& attributionSource) {
217 return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission,
218 attributionSource, std::string(), AppOpsManager::OP_NONE);
219 }
220
221 } // namespace android
222