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 | #ifndef ANDROID_SERVERS_CAMERA_ATTRIBUTION_AND_PERMISSION_UTILS_H |
| 17 | #define ANDROID_SERVERS_CAMERA_ATTRIBUTION_AND_PERMISSION_UTILS_H |
| 18 | |
| 19 | #include <android/content/AttributionSourceState.h> |
| 20 | #include <android/permission/PermissionChecker.h> |
| 21 | #include <binder/BinderService.h> |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame^] | 22 | #include <binder/IPermissionController.h> |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 23 | #include <private/android_filesystem_config.h> |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | class CameraService; |
| 28 | |
| 29 | using content::AttributionSourceState; |
| 30 | using permission::PermissionChecker; |
| 31 | |
| 32 | /** |
| 33 | * Utility class consolidating methods/data for verifying permissions and the identity of the |
| 34 | * caller. |
| 35 | */ |
| 36 | class AttributionAndPermissionUtils { |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 37 | public: |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 38 | AttributionAndPermissionUtils() { } |
| 39 | virtual ~AttributionAndPermissionUtils() {} |
| 40 | |
| 41 | void setCameraService(wp<CameraService> cameraService) { |
| 42 | mCameraService = cameraService; |
| 43 | } |
| 44 | |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 45 | static AttributionSourceState buildAttributionSource(int callingPid, int callingUid) { |
| 46 | AttributionSourceState attributionSource{}; |
| 47 | attributionSource.pid = callingPid; |
| 48 | attributionSource.uid = callingUid; |
| 49 | return attributionSource; |
| 50 | } |
| 51 | |
| 52 | static AttributionSourceState buildAttributionSource(int callingPid, int callingUid, |
| 53 | int32_t deviceId) { |
| 54 | AttributionSourceState attributionSource = buildAttributionSource(callingPid, callingUid); |
| 55 | attributionSource.deviceId = deviceId; |
| 56 | return attributionSource; |
| 57 | } |
| 58 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 59 | // Utilities handling Binder calling identities (previously in CameraThreadState) |
| 60 | virtual int getCallingUid(); |
| 61 | virtual int getCallingPid(); |
| 62 | virtual int64_t clearCallingIdentity(); |
| 63 | virtual void restoreCallingIdentity(int64_t token); |
| 64 | |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 65 | virtual bool resolveClientUid(/*inout*/ int& clientUid); |
| 66 | virtual bool resolveClientPid(/*inout*/ int& clientPid); |
| 67 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Pre-grants the permission if the attribution source uid is for an automotive |
| 70 | * privileged client. Otherwise uses system service permission checker to check |
| 71 | * for the appropriate permission. If this function is called for accessing a specific |
| 72 | * camera,then the cameraID must not be empty. CameraId is used only in case of automotive |
| 73 | * privileged client so that permission is pre-granted only to access system camera device |
| 74 | * which is located outside of the vehicle body frame because camera located inside the vehicle |
| 75 | * cabin would need user permission. |
| 76 | */ |
| 77 | virtual bool checkPermissionForPreflight(const std::string &cameraId, |
| 78 | const std::string &permission, const AttributionSourceState& attributionSource, |
| 79 | const std::string& message, int32_t attributedOpCode); |
| 80 | |
| 81 | // Can camera service trust the caller based on the calling UID? |
| 82 | virtual bool isTrustedCallingUid(uid_t uid); |
| 83 | |
| 84 | virtual bool isAutomotiveDevice(); |
| 85 | virtual bool isHeadlessSystemUserMode(); |
| 86 | |
| 87 | /** |
| 88 | * Returns true if the client has uid AID_AUTOMOTIVE_EVS and the device is an automotive device. |
| 89 | */ |
| 90 | virtual bool isAutomotivePrivilegedClient(int32_t uid); |
| 91 | |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame^] | 92 | // In some cases the calling code has no access to the package it runs under. |
| 93 | // For example, NDK camera API. |
| 94 | // In this case we will get the packages for the calling UID and pick the first one |
| 95 | // for attributing the app op. This will work correctly for runtime permissions |
| 96 | // as for legacy apps we will toggle the app op for all packages in the UID. |
| 97 | // The caveat is that the operation may be attributed to the wrong package and |
| 98 | // stats based on app ops may be slightly off. |
| 99 | virtual std::string getPackageNameFromUid(int clientUid) const; |
| 100 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 101 | virtual status_t getUidForPackage(const std::string &packageName, int userId, |
| 102 | /*inout*/uid_t& uid, int err); |
| 103 | virtual bool isCallerCameraServerNotDelegating(); |
| 104 | |
| 105 | // Utils for checking specific permissions |
| 106 | virtual bool hasPermissionsForCamera(const std::string& cameraId, |
| 107 | const AttributionSourceState& attributionSource); |
| 108 | virtual bool hasPermissionsForSystemCamera(const std::string& cameraId, |
| 109 | const AttributionSourceState& attributionSource, bool checkCameraPermissions = true); |
| 110 | virtual bool hasPermissionsForCameraHeadlessSystemUser(const std::string& cameraId, |
| 111 | const AttributionSourceState& attributionSource); |
| 112 | virtual bool hasPermissionsForCameraPrivacyAllowlist( |
| 113 | const AttributionSourceState& attributionSource); |
| 114 | virtual bool hasPermissionsForOpenCloseListener( |
| 115 | const AttributionSourceState& attributionSource); |
| 116 | |
| 117 | static const std::string sDumpPermission; |
| 118 | static const std::string sManageCameraPermission; |
| 119 | static const std::string sCameraPermission; |
| 120 | static const std::string sSystemCameraPermission; |
| 121 | static const std::string sCameraHeadlessSystemUserPermission; |
| 122 | static const std::string sCameraPrivacyAllowlistPermission; |
| 123 | static const std::string sCameraSendSystemEventsPermission; |
| 124 | static const std::string sCameraOpenCloseListenerPermission; |
| 125 | static const std::string sCameraInjectExternalCameraPermission; |
| 126 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 127 | protected: |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 128 | wp<CameraService> mCameraService; |
| 129 | |
| 130 | bool checkAutomotivePrivilegedClient(const std::string &cameraId, |
| 131 | const AttributionSourceState &attributionSource); |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 132 | |
| 133 | private: |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame^] | 134 | virtual const sp<IPermissionController>& getPermissionController() const; |
| 135 | |
Avichal Rakesh | 5788fec | 2024-03-15 14:39:20 -0700 | [diff] [blame] | 136 | std::unique_ptr<permission::PermissionChecker> mPermissionChecker = |
| 137 | std::make_unique<permission::PermissionChecker>(); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * Class to be inherited by classes encapsulating AttributionAndPermissionUtils. Provides an |
| 142 | * additional utility layer above AttributionAndPermissionUtils calls, and avoids verbosity |
| 143 | * in the encapsulating class's methods. |
| 144 | */ |
| 145 | class AttributionAndPermissionUtilsEncapsulator { |
| 146 | protected: |
| 147 | std::shared_ptr<AttributionAndPermissionUtils> mAttributionAndPermissionUtils; |
| 148 | |
| 149 | public: |
| 150 | AttributionAndPermissionUtilsEncapsulator( |
| 151 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils) |
| 152 | : mAttributionAndPermissionUtils(attributionAndPermissionUtils) { } |
| 153 | |
| 154 | static AttributionSourceState buildAttributionSource(int callingPid, int callingUid) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 155 | return AttributionAndPermissionUtils::buildAttributionSource(callingPid, callingUid); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static AttributionSourceState buildAttributionSource(int callingPid, int callingUid, |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 159 | int32_t deviceId) { |
Austin Borger | 65e6464 | 2024-06-11 15:58:23 -0700 | [diff] [blame] | 160 | return AttributionAndPermissionUtils::buildAttributionSource( |
| 161 | callingPid, callingUid, deviceId); |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static AttributionSourceState buildAttributionSource(int callingPid, int callingUid, |
| 165 | const std::string& packageName, int32_t deviceId) { |
| 166 | AttributionSourceState attributionSource = buildAttributionSource(callingPid, callingUid, |
| 167 | deviceId); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 168 | attributionSource.packageName = packageName; |
| 169 | return attributionSource; |
| 170 | } |
| 171 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 172 | int getCallingUid() const { |
| 173 | return mAttributionAndPermissionUtils->getCallingUid(); |
| 174 | } |
| 175 | |
| 176 | int getCallingPid() const { |
| 177 | return mAttributionAndPermissionUtils->getCallingPid(); |
| 178 | } |
| 179 | |
| 180 | int64_t clearCallingIdentity() const { |
| 181 | return mAttributionAndPermissionUtils->clearCallingIdentity(); |
| 182 | } |
| 183 | |
| 184 | void restoreCallingIdentity(int64_t token) const { |
| 185 | mAttributionAndPermissionUtils->restoreCallingIdentity(token); |
| 186 | } |
| 187 | |
Austin Borger | b01a870 | 2024-07-22 16:20:34 -0700 | [diff] [blame] | 188 | bool resolveClientUid(/*inout*/ int& clientUid) const { |
| 189 | return mAttributionAndPermissionUtils->resolveClientUid(clientUid); |
| 190 | } |
| 191 | |
| 192 | bool resolveClientPid(/*inout*/ int& clientPid) const { |
| 193 | return mAttributionAndPermissionUtils->resolveClientPid(clientPid); |
| 194 | } |
| 195 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 196 | // The word 'System' here does not refer to callers only on the system |
| 197 | // partition. They just need to have an android system uid. |
| 198 | bool callerHasSystemUid() const { |
| 199 | return (getCallingUid() < AID_APP_START); |
| 200 | } |
| 201 | |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 202 | bool hasPermissionsForCamera(int callingPid, int callingUid, int32_t deviceId) const { |
| 203 | return hasPermissionsForCamera(std::string(), callingPid, callingUid, deviceId); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool hasPermissionsForCamera(int callingPid, int callingUid, |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 207 | const std::string& packageName, int32_t deviceId) const { |
| 208 | return hasPermissionsForCamera(std::string(), callingPid, callingUid, packageName, |
| 209 | deviceId); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | bool hasPermissionsForCamera(const std::string& cameraId, int callingPid, |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 213 | int callingUid, int32_t deviceId) const { |
| 214 | auto attributionSource = buildAttributionSource(callingPid, callingUid, |
| 215 | deviceId); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 216 | return mAttributionAndPermissionUtils->hasPermissionsForCamera(cameraId, attributionSource); |
| 217 | } |
| 218 | |
| 219 | bool hasPermissionsForCamera(const std::string& cameraId, int callingPid, int callingUid, |
Biswarup Pal | e506e73 | 2024-03-27 13:46:20 +0000 | [diff] [blame] | 220 | const std::string& packageName, int32_t deviceId) const { |
| 221 | auto attributionSource = buildAttributionSource(callingPid, callingUid, packageName, |
| 222 | deviceId); |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 223 | return mAttributionAndPermissionUtils->hasPermissionsForCamera(cameraId, attributionSource); |
| 224 | } |
| 225 | |
| 226 | bool hasPermissionsForSystemCamera(const std::string& cameraId, int callingPid, int callingUid, |
| 227 | bool checkCameraPermissions = true) const { |
| 228 | auto attributionSource = buildAttributionSource(callingPid, callingUid); |
| 229 | return mAttributionAndPermissionUtils->hasPermissionsForSystemCamera( |
| 230 | cameraId, attributionSource, checkCameraPermissions); |
| 231 | } |
| 232 | |
| 233 | bool hasPermissionsForCameraHeadlessSystemUser(const std::string& cameraId, int callingPid, |
| 234 | int callingUid) const { |
| 235 | auto attributionSource = buildAttributionSource(callingPid, callingUid); |
| 236 | return mAttributionAndPermissionUtils->hasPermissionsForCameraHeadlessSystemUser( |
| 237 | cameraId, attributionSource); |
| 238 | } |
| 239 | |
| 240 | bool hasPermissionsForCameraPrivacyAllowlist(int callingPid, int callingUid) const { |
| 241 | auto attributionSource = buildAttributionSource(callingPid, callingUid); |
| 242 | return mAttributionAndPermissionUtils->hasPermissionsForCameraPrivacyAllowlist( |
| 243 | attributionSource); |
| 244 | } |
| 245 | |
| 246 | bool hasPermissionsForOpenCloseListener(int callingPid, int callingUid) const { |
| 247 | auto attributionSource = buildAttributionSource(callingPid, callingUid); |
| 248 | return mAttributionAndPermissionUtils->hasPermissionsForOpenCloseListener( |
| 249 | attributionSource); |
| 250 | } |
| 251 | |
| 252 | bool isAutomotiveDevice() const { |
| 253 | return mAttributionAndPermissionUtils->isAutomotiveDevice(); |
| 254 | } |
| 255 | |
| 256 | bool isAutomotivePrivilegedClient(int32_t uid) const { |
| 257 | return mAttributionAndPermissionUtils->isAutomotivePrivilegedClient(uid); |
| 258 | } |
| 259 | |
| 260 | bool isTrustedCallingUid(uid_t uid) const { |
| 261 | return mAttributionAndPermissionUtils->isTrustedCallingUid(uid); |
| 262 | } |
| 263 | |
| 264 | bool isHeadlessSystemUserMode() const { |
| 265 | return mAttributionAndPermissionUtils->isHeadlessSystemUserMode(); |
| 266 | } |
| 267 | |
| 268 | status_t getUidForPackage(const std::string &packageName, int userId, |
| 269 | /*inout*/uid_t& uid, int err) const { |
| 270 | return mAttributionAndPermissionUtils->getUidForPackage(packageName, userId, uid, err); |
| 271 | } |
| 272 | |
Austin Borger | ce55edb | 2024-09-23 15:12:56 -0700 | [diff] [blame^] | 273 | std::string getPackageNameFromUid(int clientUid) const { |
| 274 | return mAttributionAndPermissionUtils->getPackageNameFromUid(clientUid); |
| 275 | } |
| 276 | |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 277 | bool isCallerCameraServerNotDelegating() const { |
| 278 | return mAttributionAndPermissionUtils->isCallerCameraServerNotDelegating(); |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | } // namespace android |
| 283 | |
| 284 | #endif // ANDROID_SERVERS_CAMERA_ATTRIBUTION_AND_PERMISSION_UTILS_H |