blob: 72f8c4b7fb074cd523506c9366c1f170c3a01353 [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 Borgerb01a8702024-07-22 16:20:34 -070083// TODO(362551824): Make USE_CALLING_UID more explicit with a scoped enum.
84bool AttributionAndPermissionUtils::resolveClientUid(/*inout*/ int& clientUid) {
85 int callingUid = getCallingUid();
86
87 if (clientUid == hardware::ICameraService::USE_CALLING_UID) {
88 clientUid = callingUid;
89 } else if (!isTrustedCallingUid(callingUid)) {
90 return false;
91 }
92
93 return true;
94}
95
96// TODO(362551824): Make USE_CALLING_UID more explicit with a scoped enum.
97bool AttributionAndPermissionUtils::resolveClientPid(/*inout*/ int& clientPid) {
98 int callingUid = getCallingUid();
99 int callingPid = getCallingPid();
100
101 if (clientPid == hardware::ICameraService::USE_CALLING_PID) {
102 clientPid = callingPid;
103 } else if (!isTrustedCallingUid(callingUid)) {
104 return false;
105 }
106
107 return true;
108}
109
Austin Borger249e6592024-03-10 22:28:11 -0700110bool AttributionAndPermissionUtils::checkAutomotivePrivilegedClient(const std::string &cameraId,
111 const AttributionSourceState &attributionSource) {
112 if (isAutomotivePrivilegedClient(attributionSource.uid)) {
113 // If cameraId is empty, then it means that this check is not used for the
114 // purpose of accessing a specific camera, hence grant permission just
115 // based on uid to the automotive privileged client.
116 if (cameraId.empty())
117 return true;
118
119 auto cameraService = mCameraService.promote();
120 if (cameraService == nullptr) {
121 ALOGE("%s: CameraService unavailable.", __FUNCTION__);
122 return false;
123 }
124
125 // If this call is used for accessing a specific camera then cam_id must be provided.
126 // In that case, only pre-grants the permission for accessing the exterior system only
127 // camera.
128 return cameraService->isAutomotiveExteriorSystemCamera(cameraId);
129 }
130
131 return false;
132}
133
134bool AttributionAndPermissionUtils::checkPermissionForPreflight(const std::string &cameraId,
135 const std::string &permission, const AttributionSourceState &attributionSource,
136 const std::string& message, int32_t attributedOpCode) {
137 if (checkAutomotivePrivilegedClient(cameraId, attributionSource)) {
138 return true;
139 }
140
Avichal Rakesh5788fec2024-03-15 14:39:20 -0700141 if (!flags::cache_permission_services()) {
142 PermissionChecker permissionChecker;
143 return permissionChecker.checkPermissionForPreflight(
144 toString16(permission), attributionSource, toString16(message),
145 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
146 } else {
147 return mPermissionChecker->checkPermissionForPreflight(
148 toString16(permission), attributionSource, toString16(message),
149 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
150 }
Austin Borger249e6592024-03-10 22:28:11 -0700151}
152
153// Can camera service trust the caller based on the calling UID?
154bool AttributionAndPermissionUtils::isTrustedCallingUid(uid_t uid) {
155 switch (uid) {
156 case AID_MEDIA: // mediaserver
157 case AID_CAMERASERVER: // cameraserver
158 case AID_RADIO: // telephony
159 return true;
160 default:
161 return false;
162 }
163}
164
165bool AttributionAndPermissionUtils::isAutomotiveDevice() {
166 // Checks the property ro.hardware.type and returns true if it is
167 // automotive.
168 char value[PROPERTY_VALUE_MAX] = {0};
169 property_get("ro.hardware.type", value, "");
170 return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0;
171}
172
173bool AttributionAndPermissionUtils::isHeadlessSystemUserMode() {
174 // Checks if the device is running in headless system user mode
175 // by checking the property ro.fw.mu.headless_system_user.
176 char value[PROPERTY_VALUE_MAX] = {0};
177 property_get("ro.fw.mu.headless_system_user", value, "");
178 return strncmp(value, "true", PROPERTY_VALUE_MAX) == 0;
179}
180
181bool AttributionAndPermissionUtils::isAutomotivePrivilegedClient(int32_t uid) {
182 // Returns false if this is not an automotive device type.
183 if (!isAutomotiveDevice())
184 return false;
185
186 // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a
187 // privileged client uid used for safety critical use cases such as
188 // rear view and surround view.
189 return uid == AID_AUTOMOTIVE_EVS;
190}
191
192status_t AttributionAndPermissionUtils::getUidForPackage(const std::string &packageName,
193 int userId, /*inout*/uid_t& uid, int err) {
194 PermissionController pc;
195 uid = pc.getPackageUid(toString16(packageName), 0);
196 if (uid <= 0) {
197 ALOGE("Unknown package: '%s'", packageName.c_str());
198 dprintf(err, "Unknown package: '%s'\n", packageName.c_str());
199 return BAD_VALUE;
200 }
201
202 if (userId < 0) {
203 ALOGE("Invalid user: %d", userId);
204 dprintf(err, "Invalid user: %d\n", userId);
205 return BAD_VALUE;
206 }
207
208 uid = multiuser_get_uid(userId, uid);
209 return NO_ERROR;
210}
211
212bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() {
Austin Borger22c5c852024-03-08 13:31:36 -0800213 return (getCallingPid() == getpid());
Austin Borger249e6592024-03-10 22:28:11 -0700214}
215
216bool AttributionAndPermissionUtils::hasPermissionsForCamera(const std::string& cameraId,
217 const AttributionSourceState& attributionSource) {
218 return checkPermissionForPreflight(cameraId, sCameraPermission,
219 attributionSource, std::string(), AppOpsManager::OP_NONE);
220}
221
222bool AttributionAndPermissionUtils::hasPermissionsForSystemCamera(const std::string& cameraId,
223 const AttributionSourceState& attributionSource, bool checkCameraPermissions) {
224 bool systemCameraPermission = checkPermissionForPreflight(cameraId,
225 sSystemCameraPermission, attributionSource, std::string(), AppOpsManager::OP_NONE);
226 return systemCameraPermission && (!checkCameraPermissions
227 || hasPermissionsForCamera(cameraId, attributionSource));
228}
229
230bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser(
231 const std::string& cameraId, const AttributionSourceState& attributionSource) {
232 return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission,
233 attributionSource, std::string(), AppOpsManager::OP_NONE);
234}
235
236bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist(
237 const AttributionSourceState& attributionSource) {
238 return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission,
239 attributionSource, std::string(), AppOpsManager::OP_NONE);
240}
241
242bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener(
243 const AttributionSourceState& attributionSource) {
244 return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission,
245 attributionSource, std::string(), AppOpsManager::OP_NONE);
246}
247
248} // namespace android