blob: e8301c191ff63d271a9b468f205db0f42be4be86 [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"
26#include "CameraThreadState.h"
27
28namespace android {
29
Avichal Rakesh5788fec2024-03-15 14:39:20 -070030namespace flags = com::android::internal::camera::flags;
31
Austin Borger249e6592024-03-10 22:28:11 -070032const std::string AttributionAndPermissionUtils::sDumpPermission("android.permission.DUMP");
33const std::string AttributionAndPermissionUtils::sManageCameraPermission(
34 "android.permission.MANAGE_CAMERA");
35const std::string AttributionAndPermissionUtils::sCameraPermission(
36 "android.permission.CAMERA");
37const std::string AttributionAndPermissionUtils::sSystemCameraPermission(
38 "android.permission.SYSTEM_CAMERA");
39const std::string AttributionAndPermissionUtils::sCameraHeadlessSystemUserPermission(
40 "android.permission.CAMERA_HEADLESS_SYSTEM_USER");
41const std::string AttributionAndPermissionUtils::sCameraPrivacyAllowlistPermission(
42 "android.permission.CAMERA_PRIVACY_ALLOWLIST");
43const std::string AttributionAndPermissionUtils::sCameraSendSystemEventsPermission(
44 "android.permission.CAMERA_SEND_SYSTEM_EVENTS");
45const std::string AttributionAndPermissionUtils::sCameraOpenCloseListenerPermission(
46 "android.permission.CAMERA_OPEN_CLOSE_LISTENER");
47const std::string AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission(
48 "android.permission.CAMERA_INJECT_EXTERNAL_CAMERA");
49
50bool AttributionAndPermissionUtils::checkAutomotivePrivilegedClient(const std::string &cameraId,
51 const AttributionSourceState &attributionSource) {
52 if (isAutomotivePrivilegedClient(attributionSource.uid)) {
53 // If cameraId is empty, then it means that this check is not used for the
54 // purpose of accessing a specific camera, hence grant permission just
55 // based on uid to the automotive privileged client.
56 if (cameraId.empty())
57 return true;
58
59 auto cameraService = mCameraService.promote();
60 if (cameraService == nullptr) {
61 ALOGE("%s: CameraService unavailable.", __FUNCTION__);
62 return false;
63 }
64
65 // If this call is used for accessing a specific camera then cam_id must be provided.
66 // In that case, only pre-grants the permission for accessing the exterior system only
67 // camera.
68 return cameraService->isAutomotiveExteriorSystemCamera(cameraId);
69 }
70
71 return false;
72}
73
74bool AttributionAndPermissionUtils::checkPermissionForPreflight(const std::string &cameraId,
75 const std::string &permission, const AttributionSourceState &attributionSource,
76 const std::string& message, int32_t attributedOpCode) {
77 if (checkAutomotivePrivilegedClient(cameraId, attributionSource)) {
78 return true;
79 }
80
Avichal Rakesh5788fec2024-03-15 14:39:20 -070081 if (!flags::cache_permission_services()) {
82 PermissionChecker permissionChecker;
83 return permissionChecker.checkPermissionForPreflight(
84 toString16(permission), attributionSource, toString16(message),
85 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
86 } else {
87 return mPermissionChecker->checkPermissionForPreflight(
88 toString16(permission), attributionSource, toString16(message),
89 attributedOpCode) != PermissionChecker::PERMISSION_HARD_DENIED;
90 }
Austin Borger249e6592024-03-10 22:28:11 -070091}
92
93// Can camera service trust the caller based on the calling UID?
94bool AttributionAndPermissionUtils::isTrustedCallingUid(uid_t uid) {
95 switch (uid) {
96 case AID_MEDIA: // mediaserver
97 case AID_CAMERASERVER: // cameraserver
98 case AID_RADIO: // telephony
99 return true;
100 default:
101 return false;
102 }
103}
104
105bool AttributionAndPermissionUtils::isAutomotiveDevice() {
106 // Checks the property ro.hardware.type and returns true if it is
107 // automotive.
108 char value[PROPERTY_VALUE_MAX] = {0};
109 property_get("ro.hardware.type", value, "");
110 return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0;
111}
112
113bool AttributionAndPermissionUtils::isHeadlessSystemUserMode() {
114 // Checks if the device is running in headless system user mode
115 // by checking the property ro.fw.mu.headless_system_user.
116 char value[PROPERTY_VALUE_MAX] = {0};
117 property_get("ro.fw.mu.headless_system_user", value, "");
118 return strncmp(value, "true", PROPERTY_VALUE_MAX) == 0;
119}
120
121bool AttributionAndPermissionUtils::isAutomotivePrivilegedClient(int32_t uid) {
122 // Returns false if this is not an automotive device type.
123 if (!isAutomotiveDevice())
124 return false;
125
126 // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a
127 // privileged client uid used for safety critical use cases such as
128 // rear view and surround view.
129 return uid == AID_AUTOMOTIVE_EVS;
130}
131
132status_t AttributionAndPermissionUtils::getUidForPackage(const std::string &packageName,
133 int userId, /*inout*/uid_t& uid, int err) {
134 PermissionController pc;
135 uid = pc.getPackageUid(toString16(packageName), 0);
136 if (uid <= 0) {
137 ALOGE("Unknown package: '%s'", packageName.c_str());
138 dprintf(err, "Unknown package: '%s'\n", packageName.c_str());
139 return BAD_VALUE;
140 }
141
142 if (userId < 0) {
143 ALOGE("Invalid user: %d", userId);
144 dprintf(err, "Invalid user: %d\n", userId);
145 return BAD_VALUE;
146 }
147
148 uid = multiuser_get_uid(userId, uid);
149 return NO_ERROR;
150}
151
152bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() {
153 return CameraThreadState::getCallingPid() == getpid();
154}
155
156bool AttributionAndPermissionUtils::hasPermissionsForCamera(const std::string& cameraId,
157 const AttributionSourceState& attributionSource) {
158 return checkPermissionForPreflight(cameraId, sCameraPermission,
159 attributionSource, std::string(), AppOpsManager::OP_NONE);
160}
161
162bool AttributionAndPermissionUtils::hasPermissionsForSystemCamera(const std::string& cameraId,
163 const AttributionSourceState& attributionSource, bool checkCameraPermissions) {
164 bool systemCameraPermission = checkPermissionForPreflight(cameraId,
165 sSystemCameraPermission, attributionSource, std::string(), AppOpsManager::OP_NONE);
166 return systemCameraPermission && (!checkCameraPermissions
167 || hasPermissionsForCamera(cameraId, attributionSource));
168}
169
170bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser(
171 const std::string& cameraId, const AttributionSourceState& attributionSource) {
172 return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission,
173 attributionSource, std::string(), AppOpsManager::OP_NONE);
174}
175
176bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist(
177 const AttributionSourceState& attributionSource) {
178 return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission,
179 attributionSource, std::string(), AppOpsManager::OP_NONE);
180}
181
182bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener(
183 const AttributionSourceState& attributionSource) {
184 return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission,
185 attributionSource, std::string(), AppOpsManager::OP_NONE);
186}
187
188} // namespace android