Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 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> |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame^] | 21 | #include <com_android_internal_camera_flags.h> |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
| 23 | #include <private/android_filesystem_config.h> |
| 24 | |
| 25 | #include "CameraService.h" |
| 26 | #include "CameraThreadState.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame^] | 30 | namespace flags = com::android::internal::camera::flags; |
| 31 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 32 | const std::string AttributionAndPermissionUtils::sDumpPermission("android.permission.DUMP"); |
| 33 | const std::string AttributionAndPermissionUtils::sManageCameraPermission( |
| 34 | "android.permission.MANAGE_CAMERA"); |
| 35 | const std::string AttributionAndPermissionUtils::sCameraPermission( |
| 36 | "android.permission.CAMERA"); |
| 37 | const std::string AttributionAndPermissionUtils::sSystemCameraPermission( |
| 38 | "android.permission.SYSTEM_CAMERA"); |
| 39 | const std::string AttributionAndPermissionUtils::sCameraHeadlessSystemUserPermission( |
| 40 | "android.permission.CAMERA_HEADLESS_SYSTEM_USER"); |
| 41 | const std::string AttributionAndPermissionUtils::sCameraPrivacyAllowlistPermission( |
| 42 | "android.permission.CAMERA_PRIVACY_ALLOWLIST"); |
| 43 | const std::string AttributionAndPermissionUtils::sCameraSendSystemEventsPermission( |
| 44 | "android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
| 45 | const std::string AttributionAndPermissionUtils::sCameraOpenCloseListenerPermission( |
| 46 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
| 47 | const std::string AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission( |
| 48 | "android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
| 49 | |
| 50 | bool 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 | |
| 74 | bool 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 Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame^] | 81 | 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 Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Can camera service trust the caller based on the calling UID? |
| 94 | bool 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 | |
| 105 | bool 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 | |
| 113 | bool 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 | |
| 121 | bool 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 | |
| 132 | status_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 | |
| 152 | bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() { |
| 153 | return CameraThreadState::getCallingPid() == getpid(); |
| 154 | } |
| 155 | |
| 156 | bool AttributionAndPermissionUtils::hasPermissionsForCamera(const std::string& cameraId, |
| 157 | const AttributionSourceState& attributionSource) { |
| 158 | return checkPermissionForPreflight(cameraId, sCameraPermission, |
| 159 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 160 | } |
| 161 | |
| 162 | bool 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 | |
| 170 | bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser( |
| 171 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
| 172 | return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission, |
| 173 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 174 | } |
| 175 | |
| 176 | bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist( |
| 177 | const AttributionSourceState& attributionSource) { |
| 178 | return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission, |
| 179 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 180 | } |
| 181 | |
| 182 | bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener( |
| 183 | const AttributionSourceState& attributionSource) { |
| 184 | return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission, |
| 185 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 186 | } |
| 187 | |
| 188 | } // namespace android |