blob: a23fba77a4f65c3e7dd811f4da7896447cbd5055 [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#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 Borger22c5c852024-03-08 13:31:36 -080022#include <private/android_filesystem_config.h>
Austin Borger249e6592024-03-10 22:28:11 -070023
24namespace android {
25
26class CameraService;
27
28using content::AttributionSourceState;
29using permission::PermissionChecker;
30
31/**
32 * Utility class consolidating methods/data for verifying permissions and the identity of the
33 * caller.
34 */
35class AttributionAndPermissionUtils {
Avichal Rakesh5788fec2024-03-15 14:39:20 -070036 public:
Austin Borger249e6592024-03-10 22:28:11 -070037 AttributionAndPermissionUtils() { }
38 virtual ~AttributionAndPermissionUtils() {}
39
40 void setCameraService(wp<CameraService> cameraService) {
41 mCameraService = cameraService;
42 }
43
Austin Borger65e64642024-06-11 15:58:23 -070044 static AttributionSourceState buildAttributionSource(int callingPid, int callingUid) {
45 AttributionSourceState attributionSource{};
46 attributionSource.pid = callingPid;
47 attributionSource.uid = callingUid;
48 return attributionSource;
49 }
50
51 static AttributionSourceState buildAttributionSource(int callingPid, int callingUid,
52 int32_t deviceId) {
53 AttributionSourceState attributionSource = buildAttributionSource(callingPid, callingUid);
54 attributionSource.deviceId = deviceId;
55 return attributionSource;
56 }
57
Austin Borger22c5c852024-03-08 13:31:36 -080058 // Utilities handling Binder calling identities (previously in CameraThreadState)
59 virtual int getCallingUid();
60 virtual int getCallingPid();
61 virtual int64_t clearCallingIdentity();
62 virtual void restoreCallingIdentity(int64_t token);
63
Austin Borgerb01a8702024-07-22 16:20:34 -070064 virtual bool resolveClientUid(/*inout*/ int& clientUid);
65 virtual bool resolveClientPid(/*inout*/ int& clientPid);
66
Austin Borger249e6592024-03-10 22:28:11 -070067 /**
68 * Pre-grants the permission if the attribution source uid is for an automotive
69 * privileged client. Otherwise uses system service permission checker to check
70 * for the appropriate permission. If this function is called for accessing a specific
71 * camera,then the cameraID must not be empty. CameraId is used only in case of automotive
72 * privileged client so that permission is pre-granted only to access system camera device
73 * which is located outside of the vehicle body frame because camera located inside the vehicle
74 * cabin would need user permission.
75 */
76 virtual bool checkPermissionForPreflight(const std::string &cameraId,
77 const std::string &permission, const AttributionSourceState& attributionSource,
78 const std::string& message, int32_t attributedOpCode);
79
80 // Can camera service trust the caller based on the calling UID?
81 virtual bool isTrustedCallingUid(uid_t uid);
82
83 virtual bool isAutomotiveDevice();
84 virtual bool isHeadlessSystemUserMode();
85
86 /**
87 * Returns true if the client has uid AID_AUTOMOTIVE_EVS and the device is an automotive device.
88 */
89 virtual bool isAutomotivePrivilegedClient(int32_t uid);
90
91 virtual status_t getUidForPackage(const std::string &packageName, int userId,
92 /*inout*/uid_t& uid, int err);
93 virtual bool isCallerCameraServerNotDelegating();
94
95 // Utils for checking specific permissions
96 virtual bool hasPermissionsForCamera(const std::string& cameraId,
97 const AttributionSourceState& attributionSource);
98 virtual bool hasPermissionsForSystemCamera(const std::string& cameraId,
99 const AttributionSourceState& attributionSource, bool checkCameraPermissions = true);
100 virtual bool hasPermissionsForCameraHeadlessSystemUser(const std::string& cameraId,
101 const AttributionSourceState& attributionSource);
102 virtual bool hasPermissionsForCameraPrivacyAllowlist(
103 const AttributionSourceState& attributionSource);
104 virtual bool hasPermissionsForOpenCloseListener(
105 const AttributionSourceState& attributionSource);
106
107 static const std::string sDumpPermission;
108 static const std::string sManageCameraPermission;
109 static const std::string sCameraPermission;
110 static const std::string sSystemCameraPermission;
111 static const std::string sCameraHeadlessSystemUserPermission;
112 static const std::string sCameraPrivacyAllowlistPermission;
113 static const std::string sCameraSendSystemEventsPermission;
114 static const std::string sCameraOpenCloseListenerPermission;
115 static const std::string sCameraInjectExternalCameraPermission;
116
Avichal Rakesh5788fec2024-03-15 14:39:20 -0700117 protected:
Austin Borger249e6592024-03-10 22:28:11 -0700118 wp<CameraService> mCameraService;
119
120 bool checkAutomotivePrivilegedClient(const std::string &cameraId,
121 const AttributionSourceState &attributionSource);
Avichal Rakesh5788fec2024-03-15 14:39:20 -0700122
123 private:
124 std::unique_ptr<permission::PermissionChecker> mPermissionChecker =
125 std::make_unique<permission::PermissionChecker>();
Austin Borger249e6592024-03-10 22:28:11 -0700126};
127
128/**
129 * Class to be inherited by classes encapsulating AttributionAndPermissionUtils. Provides an
130 * additional utility layer above AttributionAndPermissionUtils calls, and avoids verbosity
131 * in the encapsulating class's methods.
132 */
133class AttributionAndPermissionUtilsEncapsulator {
134protected:
135 std::shared_ptr<AttributionAndPermissionUtils> mAttributionAndPermissionUtils;
136
137public:
138 AttributionAndPermissionUtilsEncapsulator(
139 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils)
140 : mAttributionAndPermissionUtils(attributionAndPermissionUtils) { }
141
142 static AttributionSourceState buildAttributionSource(int callingPid, int callingUid) {
Austin Borger65e64642024-06-11 15:58:23 -0700143 return AttributionAndPermissionUtils::buildAttributionSource(callingPid, callingUid);
Austin Borger249e6592024-03-10 22:28:11 -0700144 }
145
146 static AttributionSourceState buildAttributionSource(int callingPid, int callingUid,
Biswarup Pale506e732024-03-27 13:46:20 +0000147 int32_t deviceId) {
Austin Borger65e64642024-06-11 15:58:23 -0700148 return AttributionAndPermissionUtils::buildAttributionSource(
149 callingPid, callingUid, deviceId);
Biswarup Pale506e732024-03-27 13:46:20 +0000150 }
151
152 static AttributionSourceState buildAttributionSource(int callingPid, int callingUid,
153 const std::string& packageName, int32_t deviceId) {
154 AttributionSourceState attributionSource = buildAttributionSource(callingPid, callingUid,
155 deviceId);
Austin Borger249e6592024-03-10 22:28:11 -0700156 attributionSource.packageName = packageName;
157 return attributionSource;
158 }
159
Austin Borger22c5c852024-03-08 13:31:36 -0800160 int getCallingUid() const {
161 return mAttributionAndPermissionUtils->getCallingUid();
162 }
163
164 int getCallingPid() const {
165 return mAttributionAndPermissionUtils->getCallingPid();
166 }
167
168 int64_t clearCallingIdentity() const {
169 return mAttributionAndPermissionUtils->clearCallingIdentity();
170 }
171
172 void restoreCallingIdentity(int64_t token) const {
173 mAttributionAndPermissionUtils->restoreCallingIdentity(token);
174 }
175
Austin Borgerb01a8702024-07-22 16:20:34 -0700176 bool resolveClientUid(/*inout*/ int& clientUid) const {
177 return mAttributionAndPermissionUtils->resolveClientUid(clientUid);
178 }
179
180 bool resolveClientPid(/*inout*/ int& clientPid) const {
181 return mAttributionAndPermissionUtils->resolveClientPid(clientPid);
182 }
183
Austin Borger22c5c852024-03-08 13:31:36 -0800184 // The word 'System' here does not refer to callers only on the system
185 // partition. They just need to have an android system uid.
186 bool callerHasSystemUid() const {
187 return (getCallingUid() < AID_APP_START);
188 }
189
Biswarup Pale506e732024-03-27 13:46:20 +0000190 bool hasPermissionsForCamera(int callingPid, int callingUid, int32_t deviceId) const {
191 return hasPermissionsForCamera(std::string(), callingPid, callingUid, deviceId);
Austin Borger249e6592024-03-10 22:28:11 -0700192 }
193
194 bool hasPermissionsForCamera(int callingPid, int callingUid,
Biswarup Pale506e732024-03-27 13:46:20 +0000195 const std::string& packageName, int32_t deviceId) const {
196 return hasPermissionsForCamera(std::string(), callingPid, callingUid, packageName,
197 deviceId);
Austin Borger249e6592024-03-10 22:28:11 -0700198 }
199
200 bool hasPermissionsForCamera(const std::string& cameraId, int callingPid,
Biswarup Pale506e732024-03-27 13:46:20 +0000201 int callingUid, int32_t deviceId) const {
202 auto attributionSource = buildAttributionSource(callingPid, callingUid,
203 deviceId);
Austin Borger249e6592024-03-10 22:28:11 -0700204 return mAttributionAndPermissionUtils->hasPermissionsForCamera(cameraId, attributionSource);
205 }
206
207 bool hasPermissionsForCamera(const std::string& cameraId, int callingPid, int callingUid,
Biswarup Pale506e732024-03-27 13:46:20 +0000208 const std::string& packageName, int32_t deviceId) const {
209 auto attributionSource = buildAttributionSource(callingPid, callingUid, packageName,
210 deviceId);
Austin Borger249e6592024-03-10 22:28:11 -0700211 return mAttributionAndPermissionUtils->hasPermissionsForCamera(cameraId, attributionSource);
212 }
213
214 bool hasPermissionsForSystemCamera(const std::string& cameraId, int callingPid, int callingUid,
215 bool checkCameraPermissions = true) const {
216 auto attributionSource = buildAttributionSource(callingPid, callingUid);
217 return mAttributionAndPermissionUtils->hasPermissionsForSystemCamera(
218 cameraId, attributionSource, checkCameraPermissions);
219 }
220
221 bool hasPermissionsForCameraHeadlessSystemUser(const std::string& cameraId, int callingPid,
222 int callingUid) const {
223 auto attributionSource = buildAttributionSource(callingPid, callingUid);
224 return mAttributionAndPermissionUtils->hasPermissionsForCameraHeadlessSystemUser(
225 cameraId, attributionSource);
226 }
227
228 bool hasPermissionsForCameraPrivacyAllowlist(int callingPid, int callingUid) const {
229 auto attributionSource = buildAttributionSource(callingPid, callingUid);
230 return mAttributionAndPermissionUtils->hasPermissionsForCameraPrivacyAllowlist(
231 attributionSource);
232 }
233
234 bool hasPermissionsForOpenCloseListener(int callingPid, int callingUid) const {
235 auto attributionSource = buildAttributionSource(callingPid, callingUid);
236 return mAttributionAndPermissionUtils->hasPermissionsForOpenCloseListener(
237 attributionSource);
238 }
239
240 bool isAutomotiveDevice() const {
241 return mAttributionAndPermissionUtils->isAutomotiveDevice();
242 }
243
244 bool isAutomotivePrivilegedClient(int32_t uid) const {
245 return mAttributionAndPermissionUtils->isAutomotivePrivilegedClient(uid);
246 }
247
248 bool isTrustedCallingUid(uid_t uid) const {
249 return mAttributionAndPermissionUtils->isTrustedCallingUid(uid);
250 }
251
252 bool isHeadlessSystemUserMode() const {
253 return mAttributionAndPermissionUtils->isHeadlessSystemUserMode();
254 }
255
256 status_t getUidForPackage(const std::string &packageName, int userId,
257 /*inout*/uid_t& uid, int err) const {
258 return mAttributionAndPermissionUtils->getUidForPackage(packageName, userId, uid, err);
259 }
260
261 bool isCallerCameraServerNotDelegating() const {
262 return mAttributionAndPermissionUtils->isCallerCameraServerNotDelegating();
263 }
264};
265
266} // namespace android
267
268#endif // ANDROID_SERVERS_CAMERA_ATTRIBUTION_AND_PERMISSION_UTILS_H