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