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 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AttributionAndPermissionUtils" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 20 | #include "AttributionAndPermissionUtils.h" |
| 21 | |
| 22 | #include <binder/AppOpsManager.h> |
| 23 | #include <binder/PermissionController.h> |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 24 | #include <com_android_internal_camera_flags.h> |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include "CameraService.h" |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 29 | |
| 30 | #include <binder/IPCThreadState.h> |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 31 | #include <binderthreadstate/CallerUtils.h> |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 32 | #include <hwbinder/IPCThreadState.h> |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 33 | |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame] | 34 | namespace { |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 35 | |
| 36 | using android::content::AttributionSourceState; |
| 37 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 38 | static const std::string kPermissionServiceName = "permission"; |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 39 | |
| 40 | static std::string getAttributionString(const AttributionSourceState& attributionSource) { |
| 41 | std::ostringstream ret; |
| 42 | const AttributionSourceState* current = &attributionSource; |
| 43 | while (current != nullptr) { |
| 44 | if (current != &attributionSource) { |
| 45 | ret << ", "; |
| 46 | } |
| 47 | |
| 48 | ret << "[uid " << current->uid << ", pid " << current->pid; |
| 49 | ret << ", packageName \"" << current->packageName.value_or("<unknown>"); |
| 50 | ret << "\"]"; |
| 51 | |
| 52 | if (!current->next.empty()) { |
| 53 | current = ¤t->next[0]; |
| 54 | } else { |
| 55 | current = nullptr; |
| 56 | } |
| 57 | } |
| 58 | return ret.str(); |
| 59 | } |
| 60 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 61 | } // namespace |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame] | 62 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 63 | namespace android { |
| 64 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 65 | namespace flags = com::android::internal::camera::flags; |
| 66 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 67 | const std::string AttributionAndPermissionUtils::sDumpPermission("android.permission.DUMP"); |
| 68 | const std::string AttributionAndPermissionUtils::sManageCameraPermission( |
| 69 | "android.permission.MANAGE_CAMERA"); |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 70 | const std::string AttributionAndPermissionUtils::sCameraPermission("android.permission.CAMERA"); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 71 | const std::string AttributionAndPermissionUtils::sSystemCameraPermission( |
| 72 | "android.permission.SYSTEM_CAMERA"); |
| 73 | const std::string AttributionAndPermissionUtils::sCameraHeadlessSystemUserPermission( |
| 74 | "android.permission.CAMERA_HEADLESS_SYSTEM_USER"); |
| 75 | const std::string AttributionAndPermissionUtils::sCameraPrivacyAllowlistPermission( |
| 76 | "android.permission.CAMERA_PRIVACY_ALLOWLIST"); |
| 77 | const std::string AttributionAndPermissionUtils::sCameraSendSystemEventsPermission( |
| 78 | "android.permission.CAMERA_SEND_SYSTEM_EVENTS"); |
| 79 | const std::string AttributionAndPermissionUtils::sCameraOpenCloseListenerPermission( |
| 80 | "android.permission.CAMERA_OPEN_CLOSE_LISTENER"); |
| 81 | const std::string AttributionAndPermissionUtils::sCameraInjectExternalCameraPermission( |
| 82 | "android.permission.CAMERA_INJECT_EXTERNAL_CAMERA"); |
| 83 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 84 | int AttributionAndPermissionUtils::getCallingUid() const { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 85 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
| 86 | return hardware::IPCThreadState::self()->getCallingUid(); |
| 87 | } |
| 88 | return IPCThreadState::self()->getCallingUid(); |
| 89 | } |
| 90 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 91 | int AttributionAndPermissionUtils::getCallingPid() const { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 92 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
| 93 | return hardware::IPCThreadState::self()->getCallingPid(); |
| 94 | } |
| 95 | return IPCThreadState::self()->getCallingPid(); |
| 96 | } |
| 97 | |
| 98 | int64_t AttributionAndPermissionUtils::clearCallingIdentity() { |
| 99 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
| 100 | return hardware::IPCThreadState::self()->clearCallingIdentity(); |
| 101 | } |
| 102 | return IPCThreadState::self()->clearCallingIdentity(); |
| 103 | } |
| 104 | |
| 105 | void AttributionAndPermissionUtils::restoreCallingIdentity(int64_t token) { |
| 106 | if (getCurrentServingCall() == BinderCallType::HWBINDER) { |
| 107 | hardware::IPCThreadState::self()->restoreCallingIdentity(token); |
| 108 | } else { |
| 109 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 110 | } |
| 111 | return; |
| 112 | } |
| 113 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 114 | binder::Status AttributionAndPermissionUtils::resolveAttributionSource( |
| 115 | /*inout*/ AttributionSourceState& resolvedAttributionSource, const std::string& methodName, |
| 116 | const std::optional<std::string>& cameraIdMaybe) { |
| 117 | // Check if we can trust clientUid |
| 118 | if (!resolveClientUid(resolvedAttributionSource.uid)) { |
| 119 | return errorNotTrusted(resolvedAttributionSource.pid, resolvedAttributionSource.uid, |
| 120 | methodName, cameraIdMaybe, *resolvedAttributionSource.packageName, |
| 121 | /* isPid= */ false); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 124 | resolveAttributionPackage(resolvedAttributionSource); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 125 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 126 | if (!resolveClientPid(resolvedAttributionSource.pid)) { |
| 127 | return errorNotTrusted(resolvedAttributionSource.pid, resolvedAttributionSource.uid, |
| 128 | methodName, cameraIdMaybe, *resolvedAttributionSource.packageName, |
| 129 | /* isPid= */ true); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 132 | return binder::Status::ok(); |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 135 | PermissionChecker::PermissionResult AttributionAndPermissionUtils::checkPermission( |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 136 | const std::string& cameraId, const std::string& permission, |
| 137 | const AttributionSourceState& attributionSource, const std::string& message, |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 138 | int32_t attributedOpCode, bool forDataDelivery, bool startDataDelivery, |
| 139 | bool checkAutomotive) { |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 140 | AttributionSourceState clientAttribution = attributionSource; |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame^] | 141 | if (!flags::data_delivery_permission_checks() && !clientAttribution.next.empty()) { |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 142 | clientAttribution.next.clear(); |
| 143 | } |
| 144 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 145 | if (checkAutomotive && checkAutomotivePrivilegedClient(cameraId, clientAttribution)) { |
| 146 | return PermissionChecker::PERMISSION_GRANTED; |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 149 | PermissionChecker::PermissionResult result; |
| 150 | if (forDataDelivery) { |
| 151 | if (startDataDelivery) { |
| 152 | result = mPermissionChecker->checkPermissionForStartDataDeliveryFromDatasource( |
| 153 | toString16(permission), clientAttribution, toString16(message), |
| 154 | attributedOpCode); |
| 155 | } else { |
| 156 | result = mPermissionChecker->checkPermissionForDataDeliveryFromDatasource( |
| 157 | toString16(permission), clientAttribution, toString16(message), |
| 158 | attributedOpCode); |
| 159 | } |
| 160 | } else { |
| 161 | result = mPermissionChecker->checkPermissionForPreflight( |
| 162 | toString16(permission), clientAttribution, toString16(message), attributedOpCode); |
| 163 | } |
| 164 | |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 165 | if (result == PermissionChecker::PERMISSION_HARD_DENIED) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 166 | ALOGI("%s (forDataDelivery %d startDataDelivery %d): Permission hard denied " |
| 167 | "for client attribution %s", |
| 168 | __FUNCTION__, forDataDelivery, startDataDelivery, |
| 169 | getAttributionString(clientAttribution).c_str()); |
| 170 | } else if (result == PermissionChecker::PERMISSION_SOFT_DENIED) { |
| 171 | ALOGI("%s checkPermission (forDataDelivery %d startDataDelivery %d): Permission soft " |
| 172 | "denied " |
| 173 | "for client attribution %s", |
| 174 | __FUNCTION__, forDataDelivery, startDataDelivery, |
Austin Borger | 2369443 | 2024-10-07 19:28:01 -0700 | [diff] [blame] | 175 | getAttributionString(clientAttribution).c_str()); |
| 176 | } |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 177 | return result; |
| 178 | } |
| 179 | |
| 180 | bool AttributionAndPermissionUtils::checkPermissionForPreflight( |
| 181 | const std::string& cameraId, const std::string& permission, |
| 182 | const AttributionSourceState& attributionSource, const std::string& message, |
| 183 | int32_t attributedOpCode) { |
| 184 | return checkPermission(cameraId, permission, attributionSource, message, attributedOpCode, |
| 185 | /* forDataDelivery */ false, /* startDataDelivery */ false, |
| 186 | /* checkAutomotive */ true) != PermissionChecker::PERMISSION_HARD_DENIED; |
| 187 | } |
| 188 | |
| 189 | bool AttributionAndPermissionUtils::checkPermissionForDataDelivery( |
| 190 | const std::string& cameraId, const std::string& permission, |
| 191 | const AttributionSourceState& attributionSource, const std::string& message, |
| 192 | int32_t attributedOpCode) { |
| 193 | return checkPermission(cameraId, permission, attributionSource, message, attributedOpCode, |
| 194 | /* forDataDelivery */ true, /* startDataDelivery */ false, |
| 195 | /* checkAutomotive */ false) != |
| 196 | PermissionChecker::PERMISSION_HARD_DENIED; |
| 197 | } |
| 198 | |
| 199 | PermissionChecker::PermissionResult |
| 200 | AttributionAndPermissionUtils::checkPermissionForStartDataDelivery( |
| 201 | const std::string& cameraId, const std::string& permission, |
| 202 | const AttributionSourceState& attributionSource, const std::string& message, |
| 203 | int32_t attributedOpCode) { |
| 204 | return checkPermission(cameraId, permission, attributionSource, message, attributedOpCode, |
| 205 | /* forDataDelivery */ true, /* startDataDelivery */ true, |
| 206 | /* checkAutomotive */ false); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Can camera service trust the caller based on the calling UID? |
| 210 | bool AttributionAndPermissionUtils::isTrustedCallingUid(uid_t uid) { |
| 211 | switch (uid) { |
| 212 | case AID_MEDIA: // mediaserver |
| 213 | case AID_CAMERASERVER: // cameraserver |
| 214 | case AID_RADIO: // telephony |
| 215 | return true; |
| 216 | default: |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | bool AttributionAndPermissionUtils::isAutomotiveDevice() { |
| 222 | // Checks the property ro.hardware.type and returns true if it is |
| 223 | // automotive. |
| 224 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 225 | property_get("ro.hardware.type", value, ""); |
| 226 | return strncmp(value, "automotive", PROPERTY_VALUE_MAX) == 0; |
| 227 | } |
| 228 | |
| 229 | bool AttributionAndPermissionUtils::isHeadlessSystemUserMode() { |
| 230 | // Checks if the device is running in headless system user mode |
| 231 | // by checking the property ro.fw.mu.headless_system_user. |
| 232 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 233 | property_get("ro.fw.mu.headless_system_user", value, ""); |
| 234 | return strncmp(value, "true", PROPERTY_VALUE_MAX) == 0; |
| 235 | } |
| 236 | |
| 237 | bool AttributionAndPermissionUtils::isAutomotivePrivilegedClient(int32_t uid) { |
| 238 | // Returns false if this is not an automotive device type. |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 239 | if (!isAutomotiveDevice()) return false; |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 240 | |
| 241 | // Returns true if the uid is AID_AUTOMOTIVE_EVS which is a |
| 242 | // privileged client uid used for safety critical use cases such as |
| 243 | // rear view and surround view. |
| 244 | return uid == AID_AUTOMOTIVE_EVS; |
| 245 | } |
| 246 | |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame] | 247 | std::string AttributionAndPermissionUtils::getPackageNameFromUid(int clientUid) const { |
| 248 | std::string packageName(""); |
| 249 | |
| 250 | sp<IPermissionController> permCtrl = getPermissionController(); |
| 251 | if (permCtrl == nullptr) { |
| 252 | // Return empty package name and the further interaction |
| 253 | // with camera will likely fail |
| 254 | return packageName; |
| 255 | } |
| 256 | |
| 257 | Vector<String16> packages; |
| 258 | |
| 259 | permCtrl->getPackagesForUid(clientUid, packages); |
| 260 | |
| 261 | if (packages.isEmpty()) { |
| 262 | ALOGE("No packages for calling UID %d", clientUid); |
| 263 | // Return empty package name and the further interaction |
| 264 | // with camera will likely fail |
| 265 | return packageName; |
| 266 | } |
| 267 | |
| 268 | // Arbitrarily pick the first name in the list |
| 269 | packageName = toStdString(packages[0]); |
| 270 | |
| 271 | return packageName; |
| 272 | } |
| 273 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 274 | status_t AttributionAndPermissionUtils::getUidForPackage(const std::string& packageName, int userId, |
| 275 | /*inout*/ uid_t& uid, int err) { |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 276 | PermissionController pc; |
| 277 | uid = pc.getPackageUid(toString16(packageName), 0); |
| 278 | if (uid <= 0) { |
| 279 | ALOGE("Unknown package: '%s'", packageName.c_str()); |
| 280 | dprintf(err, "Unknown package: '%s'\n", packageName.c_str()); |
| 281 | return BAD_VALUE; |
| 282 | } |
| 283 | |
| 284 | if (userId < 0) { |
| 285 | ALOGE("Invalid user: %d", userId); |
| 286 | dprintf(err, "Invalid user: %d\n", userId); |
| 287 | return BAD_VALUE; |
| 288 | } |
| 289 | |
| 290 | uid = multiuser_get_uid(userId, uid); |
| 291 | return NO_ERROR; |
| 292 | } |
| 293 | |
| 294 | bool AttributionAndPermissionUtils::isCallerCameraServerNotDelegating() { |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 295 | return (getCallingPid() == getpid()); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 298 | bool AttributionAndPermissionUtils::hasPermissionsForCamera( |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 299 | const std::string& cameraId, const AttributionSourceState& attributionSource, |
| 300 | bool forDataDelivery, bool checkAutomotive) { |
| 301 | return checkPermission(cameraId, sCameraPermission, attributionSource, std::string(), |
| 302 | AppOpsManager::OP_NONE, forDataDelivery, /* startDataDelivery */ false, |
| 303 | checkAutomotive) != PermissionChecker::PERMISSION_HARD_DENIED; |
| 304 | } |
| 305 | |
| 306 | PermissionChecker::PermissionResult |
| 307 | AttributionAndPermissionUtils::checkPermissionsForCameraForPreflight( |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 308 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 309 | return checkPermission(cameraId, sCameraPermission, attributionSource, std::string(), |
| 310 | AppOpsManager::OP_NONE, /* forDataDelivery */ false, |
| 311 | /* startDataDelivery */ false, /* checkAutomotive */ false); |
| 312 | } |
| 313 | |
| 314 | PermissionChecker::PermissionResult |
| 315 | AttributionAndPermissionUtils::checkPermissionsForCameraForDataDelivery( |
| 316 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
| 317 | return checkPermission(cameraId, sCameraPermission, attributionSource, std::string(), |
| 318 | AppOpsManager::OP_NONE, /* forDataDelivery */ true, |
| 319 | /* startDataDelivery */ false, /* checkAutomotive */ false); |
| 320 | } |
| 321 | |
| 322 | PermissionChecker::PermissionResult |
| 323 | AttributionAndPermissionUtils::checkPermissionsForCameraForStartDataDelivery( |
| 324 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
| 325 | return checkPermission(cameraId, sCameraPermission, attributionSource, std::string(), |
| 326 | AppOpsManager::OP_NONE, /* forDataDelivery */ true, |
| 327 | /* startDataDelivery */ true, /* checkAutomotive */ false); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 330 | bool AttributionAndPermissionUtils::hasPermissionsForSystemCamera( |
| 331 | const std::string& cameraId, const AttributionSourceState& attributionSource, |
| 332 | bool checkCameraPermissions) { |
| 333 | bool systemCameraPermission = |
| 334 | checkPermissionForPreflight(cameraId, sSystemCameraPermission, attributionSource, |
| 335 | std::string(), AppOpsManager::OP_NONE); |
| 336 | return systemCameraPermission && |
| 337 | (!checkCameraPermissions || hasPermissionsForCamera(cameraId, attributionSource)); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool AttributionAndPermissionUtils::hasPermissionsForCameraHeadlessSystemUser( |
| 341 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
| 342 | return checkPermissionForPreflight(cameraId, sCameraHeadlessSystemUserPermission, |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 343 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool AttributionAndPermissionUtils::hasPermissionsForCameraPrivacyAllowlist( |
| 347 | const AttributionSourceState& attributionSource) { |
| 348 | return checkPermissionForPreflight(std::string(), sCameraPrivacyAllowlistPermission, |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 349 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | bool AttributionAndPermissionUtils::hasPermissionsForOpenCloseListener( |
| 353 | const AttributionSourceState& attributionSource) { |
| 354 | return checkPermissionForPreflight(std::string(), sCameraOpenCloseListenerPermission, |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 355 | attributionSource, std::string(), AppOpsManager::OP_NONE); |
| 356 | } |
| 357 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame] | 358 | void AttributionAndPermissionUtils::finishDataDelivery( |
| 359 | const AttributionSourceState& attributionSource) { |
| 360 | mPermissionChecker->finishDataDeliveryFromDatasource(AppOpsManager::OP_CAMERA, |
| 361 | attributionSource); |
| 362 | } |
| 363 | |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 364 | bool AttributionAndPermissionUtils::checkAutomotivePrivilegedClient( |
| 365 | const std::string& cameraId, const AttributionSourceState& attributionSource) { |
| 366 | if (isAutomotivePrivilegedClient(attributionSource.uid)) { |
| 367 | // If cameraId is empty, then it means that this check is not used for the |
| 368 | // purpose of accessing a specific camera, hence grant permission just |
| 369 | // based on uid to the automotive privileged client. |
| 370 | if (cameraId.empty()) return true; |
| 371 | |
| 372 | auto cameraService = mCameraService.promote(); |
| 373 | if (cameraService == nullptr) { |
| 374 | ALOGE("%s: CameraService unavailable.", __FUNCTION__); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // If this call is used for accessing a specific camera then cam_id must be provided. |
| 379 | // In that case, only pre-grants the permission for accessing the exterior system only |
| 380 | // camera. |
| 381 | return cameraService->isAutomotiveExteriorSystemCamera(cameraId); |
| 382 | } |
| 383 | |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | void AttributionAndPermissionUtils::resolveAttributionPackage( |
| 388 | AttributionSourceState& resolvedAttributionSource) { |
| 389 | if (resolvedAttributionSource.packageName.has_value() && |
| 390 | resolvedAttributionSource.packageName->size() > 0) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // NDK calls don't come with package names, but we need one for various cases. |
| 395 | // Generally, there's a 1:1 mapping between UID and package name, but shared UIDs |
| 396 | // do exist. For all authentication cases, all packages under the same UID get the |
| 397 | // same permissions, so picking any associated package name is sufficient. For some |
| 398 | // other cases, this may give inaccurate names for clients in logs. |
| 399 | resolvedAttributionSource.packageName = getPackageNameFromUid(resolvedAttributionSource.uid); |
| 400 | } |
| 401 | |
| 402 | // TODO(362551824): Make USE_CALLING_UID more explicit with a scoped enum. |
| 403 | bool AttributionAndPermissionUtils::resolveClientUid(/*inout*/ int& clientUid) { |
| 404 | int callingUid = getCallingUid(); |
| 405 | |
| 406 | bool validUid = true; |
| 407 | if (clientUid == hardware::ICameraService::USE_CALLING_UID) { |
| 408 | clientUid = callingUid; |
| 409 | } else { |
| 410 | validUid = isTrustedCallingUid(callingUid); |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame^] | 411 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 412 | validUid = validUid || (clientUid == callingUid); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return validUid; |
| 417 | } |
| 418 | |
| 419 | // TODO(362551824): Make USE_CALLING_UID more explicit with a scoped enum. |
| 420 | bool AttributionAndPermissionUtils::resolveClientPid(/*inout*/ int& clientPid) { |
| 421 | int callingUid = getCallingUid(); |
| 422 | int callingPid = getCallingPid(); |
| 423 | |
| 424 | bool validPid = true; |
| 425 | if (clientPid == hardware::ICameraService::USE_CALLING_PID) { |
| 426 | clientPid = callingPid; |
| 427 | } else { |
| 428 | validPid = isTrustedCallingUid(callingUid); |
Austin Borger | 2a3f9a7 | 2024-11-07 12:24:49 -0800 | [diff] [blame^] | 429 | if (flags::data_delivery_permission_checks()) { |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 430 | validPid = validPid || (clientPid == callingPid); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return validPid; |
| 435 | } |
| 436 | |
| 437 | binder::Status AttributionAndPermissionUtils::errorNotTrusted( |
| 438 | int clientPid, int clientUid, const std::string& methodName, |
| 439 | const std::optional<std::string>& cameraIdMaybe, const std::string& clientName, |
| 440 | bool isPid) const { |
| 441 | int callingPid = getCallingPid(); |
| 442 | int callingUid = getCallingUid(); |
| 443 | ALOGE("CameraService::%s X (calling PID %d, calling UID %d) rejected " |
| 444 | "(don't trust %s %d)", |
| 445 | methodName.c_str(), callingPid, callingUid, isPid ? "clientPid" : "clientUid", |
| 446 | isPid ? clientPid : clientUid); |
| 447 | return STATUS_ERROR_FMT(hardware::ICameraService::ERROR_PERMISSION_DENIED, |
| 448 | "Untrusted caller (calling PID %d, UID %d) trying to " |
| 449 | "forward camera access to camera %s for client %s (PID %d, UID %d)", |
| 450 | getCallingPid(), getCallingUid(), cameraIdMaybe.value_or("N/A").c_str(), |
| 451 | clientName.c_str(), clientPid, clientUid); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame] | 454 | const sp<IPermissionController>& AttributionAndPermissionUtils::getPermissionController() const { |
| 455 | static const char* kPermissionControllerService = "permission"; |
| 456 | static thread_local sp<IPermissionController> sPermissionController = nullptr; |
| 457 | |
| 458 | if (sPermissionController == nullptr || |
Austin Borger | 95a0015 | 2024-09-23 17:20:24 -0700 | [diff] [blame] | 459 | !IInterface::asBinder(sPermissionController)->isBinderAlive()) { |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame] | 460 | sp<IServiceManager> sm = defaultServiceManager(); |
| 461 | sp<IBinder> binder = sm->checkService(toString16(kPermissionControllerService)); |
| 462 | if (binder == nullptr) { |
| 463 | ALOGE("%s: Could not get permission service", __FUNCTION__); |
| 464 | sPermissionController = nullptr; |
| 465 | } else { |
| 466 | sPermissionController = interface_cast<IPermissionController>(binder); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return sPermissionController; |
| 471 | } |
| 472 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 473 | } // namespace android |