1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "accesstoken_manager_stub.h"
17
18 #include <unistd.h>
19 #include "accesstoken_dfx_define.h"
20 #include "accesstoken_log.h"
21 #include "access_token_error.h"
22 #include "ipc_skeleton.h"
23 #include "memory_guard.h"
24 #include "string_ex.h"
25 #include "tokenid_kit.h"
26 #ifdef HICOLLIE_ENABLE
27 #include "xcollie/xcollie.h"
28 #endif // HICOLLIE_ENABLE
29
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 namespace {
34 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "ATMStub"};
35 constexpr int32_t FOUNDATION_UID = 5523;
36 static const int32_t DUMP_CAPACITY_SIZE = 2 * 1024 * 1000;
37 static const int MAX_PERMISSION_SIZE = 1000;
38 #ifdef TOKEN_SYNC_ENABLE
39 static const int MAX_NATIVE_TOKEN_INFO_SIZE = 20480;
40 #endif
41 const std::string GRANT_SENSITIVE_PERMISSIONS = "ohos.permission.GRANT_SENSITIVE_PERMISSIONS";
42 const std::string REVOKE_SENSITIVE_PERMISSIONS = "ohos.permission.REVOKE_SENSITIVE_PERMISSIONS";
43 const std::string GET_SENSITIVE_PERMISSIONS = "ohos.permission.GET_SENSITIVE_PERMISSIONS";
44 #ifdef HICOLLIE_ENABLE
45 constexpr uint32_t TIMEOUT = 10; // 10s
46 #endif // HICOLLIE_ENABLE
47 }
48
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int32_t AccessTokenManagerStub::OnRemoteRequest(
50 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
51 {
52 uint32_t callingTokenID = IPCSkeleton::GetCallingTokenID();
53 ACCESSTOKEN_LOG_DEBUG(LABEL, "code %{public}u token %{public}u", code, callingTokenID);
54 std::u16string descriptor = data.ReadInterfaceToken();
55 if (descriptor != IAccessTokenManager::GetDescriptor()) {
56 ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str());
57 return -1;
58 }
59
60 #ifdef HICOLLIE_ENABLE
61 std::string name = "AtmTimer";
62 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer(name, TIMEOUT, nullptr, nullptr,
63 HiviewDFX::XCOLLIE_FLAG_LOG);
64 #endif // HICOLLIE_ENABLE
65 auto itFunc = requestFuncMap_.find(code);
66 if (itFunc != requestFuncMap_.end()) {
67 auto requestFunc = itFunc->second;
68 if (requestFunc != nullptr) {
69 (this->*requestFunc)(data, reply);
70 } else {
71 // when valid code without any function to handle
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74 } else {
75 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); // when code invalid
76 }
77
78 #ifdef HICOLLIE_ENABLE
79 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
80 #endif // HICOLLIE_ENABLE
81
82 return NO_ERROR;
83 }
84
DeleteTokenInfoInner(MessageParcel & data,MessageParcel & reply)85 void AccessTokenManagerStub::DeleteTokenInfoInner(MessageParcel& data, MessageParcel& reply)
86 {
87 if (!IsFoundationCalling() && !IsPrivilegedCalling()) {
88 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
89 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
90 return;
91 }
92 AccessTokenID tokenID = data.ReadUint32();
93 int result = this->DeleteToken(tokenID);
94 reply.WriteInt32(result);
95 }
96
VerifyAccessTokenInner(MessageParcel & data,MessageParcel & reply)97 void AccessTokenManagerStub::VerifyAccessTokenInner(MessageParcel& data, MessageParcel& reply)
98 {
99 AccessTokenID tokenID = data.ReadUint32();
100 std::string permissionName = data.ReadString();
101 int result = this->VerifyAccessToken(tokenID, permissionName);
102 reply.WriteInt32(result);
103 }
104
GetDefPermissionInner(MessageParcel & data,MessageParcel & reply)105 void AccessTokenManagerStub::GetDefPermissionInner(MessageParcel& data, MessageParcel& reply)
106 {
107 std::string permissionName = data.ReadString();
108 PermissionDefParcel permissionDefParcel;
109 int result = this->GetDefPermission(permissionName, permissionDefParcel);
110 reply.WriteInt32(result);
111 if (result != RET_SUCCESS) {
112 return;
113 }
114 reply.WriteParcelable(&permissionDefParcel);
115 }
116
GetDefPermissionsInner(MessageParcel & data,MessageParcel & reply)117 void AccessTokenManagerStub::GetDefPermissionsInner(MessageParcel& data, MessageParcel& reply)
118 {
119 AccessTokenID tokenID = data.ReadUint32();
120 std::vector<PermissionDefParcel> permList;
121
122 int result = this->GetDefPermissions(tokenID, permList);
123 reply.WriteInt32(result);
124 if (result != RET_SUCCESS) {
125 return;
126 }
127 ACCESSTOKEN_LOG_DEBUG(LABEL, "%{public}s called, permList size: %{public}zu", __func__, permList.size());
128 reply.WriteUint32(permList.size());
129 for (const auto& permDef : permList) {
130 reply.WriteParcelable(&permDef);
131 }
132 }
133
GetReqPermissionsInner(MessageParcel & data,MessageParcel & reply)134 void AccessTokenManagerStub::GetReqPermissionsInner(MessageParcel& data, MessageParcel& reply)
135 {
136 AccessTokenID tokenID = data.ReadUint32();
137 int isSystemGrant = data.ReadInt32();
138 std::vector<PermissionStateFullParcel> permList;
139
140 int result = this->GetReqPermissions(tokenID, permList, isSystemGrant);
141 reply.WriteInt32(result);
142 if (result != RET_SUCCESS) {
143 return;
144 }
145 ACCESSTOKEN_LOG_DEBUG(LABEL, "permList size: %{public}zu", permList.size());
146 reply.WriteUint32(permList.size());
147 for (const auto& permDef : permList) {
148 reply.WriteParcelable(&permDef);
149 }
150 }
151
GetSelfPermissionsStateInner(MessageParcel & data,MessageParcel & reply)152 void AccessTokenManagerStub::GetSelfPermissionsStateInner(MessageParcel& data, MessageParcel& reply)
153 {
154 std::vector<PermissionListStateParcel> permList;
155 uint32_t size = 0;
156 if (!data.ReadUint32(size)) {
157 reply.WriteInt32(INVALID_OPER);
158 return;
159 }
160 ACCESSTOKEN_LOG_DEBUG(LABEL, "permList size read from client data is %{public}d.", size);
161 if (size > MAX_PERMISSION_SIZE) {
162 ACCESSTOKEN_LOG_ERROR(LABEL, "permList size %{public}d is invalid", size);
163 reply.WriteInt32(INVALID_OPER);
164 return;
165 }
166 for (uint32_t i = 0; i < size; i++) {
167 sptr<PermissionListStateParcel> permissionParcel = data.ReadParcelable<PermissionListStateParcel>();
168 if (permissionParcel != nullptr) {
169 permList.emplace_back(*permissionParcel);
170 }
171 }
172
173 PermissionOper result = this->GetSelfPermissionsState(permList);
174
175 reply.WriteInt32(result);
176
177 reply.WriteUint32(permList.size());
178 for (const auto& perm : permList) {
179 reply.WriteParcelable(&perm);
180 }
181 }
182
GetPermissionFlagInner(MessageParcel & data,MessageParcel & reply)183 void AccessTokenManagerStub::GetPermissionFlagInner(MessageParcel& data, MessageParcel& reply)
184 {
185 unsigned int callingTokenID = IPCSkeleton::GetCallingTokenID();
186 if ((this->GetTokenType(callingTokenID) == TOKEN_HAP) && (!IsSystemAppCalling())) {
187 reply.WriteInt32(AccessTokenError::ERR_NOT_SYSTEM_APP);
188 return;
189 }
190 AccessTokenID tokenID = data.ReadUint32();
191 std::string permissionName = data.ReadString();
192 if (!IsPrivilegedCalling() &&
193 VerifyAccessToken(callingTokenID, GRANT_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED &&
194 VerifyAccessToken(callingTokenID, REVOKE_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED &&
195 VerifyAccessToken(callingTokenID, GET_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
196 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
197 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
198 return;
199 }
200 int32_t flag;
201 int result = this->GetPermissionFlag(tokenID, permissionName, flag);
202 reply.WriteInt32(result);
203 if (result != RET_SUCCESS) {
204 return;
205 }
206 reply.WriteInt32(flag);
207 }
208
GrantPermissionInner(MessageParcel & data,MessageParcel & reply)209 void AccessTokenManagerStub::GrantPermissionInner(MessageParcel& data, MessageParcel& reply)
210 {
211 unsigned int callingTokenID = IPCSkeleton::GetCallingTokenID();
212 if ((this->GetTokenType(callingTokenID) == TOKEN_HAP) && (!IsSystemAppCalling())) {
213 reply.WriteInt32(AccessTokenError::ERR_NOT_SYSTEM_APP);
214 return;
215 }
216 AccessTokenID tokenID = data.ReadUint32();
217 std::string permissionName = data.ReadString();
218 int flag = data.ReadInt32();
219 if (!IsPrivilegedCalling() && !IsFoundationCalling() &&
220 VerifyAccessToken(callingTokenID, GRANT_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
221 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_VERIFY_REPORT",
222 HiviewDFX::HiSysEvent::EventType::SECURITY, "CODE", VERIFY_PERMISSION_ERROR,
223 "CALLER_TOKENID", callingTokenID, "PERMISSION_NAME", permissionName);
224 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
225 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
226 return;
227 }
228 int result = this->GrantPermission(tokenID, permissionName, flag);
229 reply.WriteInt32(result);
230 }
231
RevokePermissionInner(MessageParcel & data,MessageParcel & reply)232 void AccessTokenManagerStub::RevokePermissionInner(MessageParcel& data, MessageParcel& reply)
233 {
234 unsigned int callingTokenID = IPCSkeleton::GetCallingTokenID();
235 if ((this->GetTokenType(callingTokenID) == TOKEN_HAP) && (!IsSystemAppCalling())) {
236 reply.WriteInt32(AccessTokenError::ERR_NOT_SYSTEM_APP);
237 return;
238 }
239 AccessTokenID tokenID = data.ReadUint32();
240 std::string permissionName = data.ReadString();
241 int flag = data.ReadInt32();
242 if (!IsPrivilegedCalling() && !IsFoundationCalling() &&
243 VerifyAccessToken(callingTokenID, REVOKE_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
244 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_VERIFY_REPORT",
245 HiviewDFX::HiSysEvent::EventType::SECURITY, "CODE", VERIFY_PERMISSION_ERROR,
246 "CALLER_TOKENID", callingTokenID, "PERMISSION_NAME", permissionName);
247 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
248 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
249 return;
250 }
251 int result = this->RevokePermission(tokenID, permissionName, flag);
252 reply.WriteInt32(result);
253 }
254
ClearUserGrantedPermissionStateInner(MessageParcel & data,MessageParcel & reply)255 void AccessTokenManagerStub::ClearUserGrantedPermissionStateInner(MessageParcel& data, MessageParcel& reply)
256 {
257 uint32_t callingTokenID = IPCSkeleton::GetCallingTokenID();
258 if (!IsPrivilegedCalling() && !IsFoundationCalling() &&
259 VerifyAccessToken(callingTokenID, REVOKE_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
260 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_VERIFY_REPORT",
261 HiviewDFX::HiSysEvent::EventType::SECURITY, "CODE", VERIFY_PERMISSION_ERROR,
262 "CALLER_TOKENID", callingTokenID);
263 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
264 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
265 return;
266 }
267 AccessTokenID tokenID = data.ReadUint32();
268 int result = this->ClearUserGrantedPermissionState(tokenID);
269 reply.WriteInt32(result);
270 }
271
AllocHapTokenInner(MessageParcel & data,MessageParcel & reply)272 void AccessTokenManagerStub::AllocHapTokenInner(MessageParcel& data, MessageParcel& reply)
273 {
274 AccessTokenIDEx res = {0};
275 if (!IsFoundationCalling() && !IsPrivilegedCalling()) {
276 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
277 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
278 return;
279 }
280
281 sptr<HapInfoParcel> hapInfoParcel = data.ReadParcelable<HapInfoParcel>();
282 sptr<HapPolicyParcel> hapPolicyParcel = data.ReadParcelable<HapPolicyParcel>();
283 if (hapInfoParcel == nullptr || hapPolicyParcel == nullptr) {
284 ACCESSTOKEN_LOG_ERROR(LABEL, "read hapPolicyParcel or hapInfoParcel fail");
285 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
286 return;
287 }
288 res = this->AllocHapToken(*hapInfoParcel, *hapPolicyParcel);
289 reply.WriteUint64(res.tokenIDEx);
290 }
291
GetTokenTypeInner(MessageParcel & data,MessageParcel & reply)292 void AccessTokenManagerStub::GetTokenTypeInner(MessageParcel& data, MessageParcel& reply)
293 {
294 AccessTokenID tokenID = data.ReadUint32();
295 int result = this->GetTokenType(tokenID);
296 reply.WriteInt32(result);
297 }
298
CheckNativeDCapInner(MessageParcel & data,MessageParcel & reply)299 void AccessTokenManagerStub::CheckNativeDCapInner(MessageParcel& data, MessageParcel& reply)
300 {
301 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
302 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
303 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
304 return;
305 }
306 AccessTokenID tokenID = data.ReadUint32();
307 std::string dCap = data.ReadString();
308 int result = this->CheckNativeDCap(tokenID, dCap);
309 reply.WriteInt32(result);
310 }
311
GetHapTokenIDInner(MessageParcel & data,MessageParcel & reply)312 void AccessTokenManagerStub::GetHapTokenIDInner(MessageParcel& data, MessageParcel& reply)
313 {
314 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
315 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
316 reply.WriteInt32(INVALID_TOKENID);
317 return;
318 }
319 int userID = data.ReadInt32();
320 std::string bundleName = data.ReadString();
321 int instIndex = data.ReadInt32();
322 AccessTokenIDEx tokenIdEx = this->GetHapTokenID(userID, bundleName, instIndex);
323 reply.WriteUint64(tokenIdEx.tokenIDEx);
324 }
325
AllocLocalTokenIDInner(MessageParcel & data,MessageParcel & reply)326 void AccessTokenManagerStub::AllocLocalTokenIDInner(MessageParcel& data, MessageParcel& reply)
327 {
328 if ((!IsNativeProcessCalling()) && !IsPrivilegedCalling()) {
329 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
330 reply.WriteInt32(INVALID_TOKENID);
331 return;
332 }
333 std::string remoteDeviceID = data.ReadString();
334 AccessTokenID remoteTokenID = data.ReadUint32();
335 AccessTokenID result = this->AllocLocalTokenID(remoteDeviceID, remoteTokenID);
336 reply.WriteUint32(result);
337 }
338
UpdateHapTokenInner(MessageParcel & data,MessageParcel & reply)339 void AccessTokenManagerStub::UpdateHapTokenInner(MessageParcel& data, MessageParcel& reply)
340 {
341 if (!IsFoundationCalling() && !IsPrivilegedCalling()) {
342 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
343 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
344 return;
345 }
346 AccessTokenID tokenID = data.ReadUint32();
347 bool isSystemApp = data.ReadBool();
348 std::string appIDDesc = data.ReadString();
349 int32_t apiVersion = data.ReadInt32();
350 AccessTokenIDEx tokenIdEx;
351 tokenIdEx.tokenIdExStruct.tokenID = tokenID;
352 sptr<HapPolicyParcel> policyParcel = data.ReadParcelable<HapPolicyParcel>();
353 if (policyParcel == nullptr) {
354 ACCESSTOKEN_LOG_ERROR(LABEL, "policyParcel read faild");
355 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
356 return;
357 }
358 int32_t result = this->UpdateHapToken(tokenIdEx, isSystemApp, appIDDesc, apiVersion, *policyParcel);
359 reply.WriteInt32(result);
360 reply.WriteUint32(tokenIdEx.tokenIdExStruct.tokenAttr);
361 }
362
GetHapTokenInfoInner(MessageParcel & data,MessageParcel & reply)363 void AccessTokenManagerStub::GetHapTokenInfoInner(MessageParcel& data, MessageParcel& reply)
364 {
365 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
366 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
367 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
368 return;
369 }
370 HapTokenInfoParcel hapTokenInfoParcel;
371 AccessTokenID tokenID = data.ReadUint32();
372 int result = this->GetHapTokenInfo(tokenID, hapTokenInfoParcel);
373 reply.WriteInt32(result);
374 if (result != RET_SUCCESS) {
375 return;
376 }
377 reply.WriteParcelable(&hapTokenInfoParcel);
378 }
379
GetNativeTokenInfoInner(MessageParcel & data,MessageParcel & reply)380 void AccessTokenManagerStub::GetNativeTokenInfoInner(MessageParcel& data, MessageParcel& reply)
381 {
382 MemoryGuard guard;
383
384 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
385 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
386 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
387 return;
388 }
389 AccessTokenID tokenID = data.ReadUint32();
390 NativeTokenInfoParcel nativeTokenInfoParcel;
391 int result = this->GetNativeTokenInfo(tokenID, nativeTokenInfoParcel);
392 reply.WriteInt32(result);
393 if (result != RET_SUCCESS) {
394 return;
395 }
396 reply.WriteParcelable(&nativeTokenInfoParcel);
397 }
398
RegisterPermStateChangeCallbackInner(MessageParcel & data,MessageParcel & reply)399 void AccessTokenManagerStub::RegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply)
400 {
401 uint32_t callingTokenID = IPCSkeleton::GetCallingTokenID();
402 if ((this->GetTokenType(callingTokenID) == TOKEN_HAP) && (!IsSystemAppCalling())) {
403 reply.WriteInt32(AccessTokenError::ERR_NOT_SYSTEM_APP);
404 return;
405 }
406 if (VerifyAccessToken(callingTokenID, GET_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
407 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
408 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
409 return;
410 }
411 sptr<PermStateChangeScopeParcel> scopeParcel = data.ReadParcelable<PermStateChangeScopeParcel>();
412 if (scopeParcel == nullptr) {
413 ACCESSTOKEN_LOG_ERROR(LABEL, "read scopeParcel fail");
414 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
415 return;
416 }
417 sptr<IRemoteObject> callback = data.ReadRemoteObject();
418 if (callback == nullptr) {
419 ACCESSTOKEN_LOG_ERROR(LABEL, "read callback fail");
420 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
421 return;
422 }
423 int32_t result = this->RegisterPermStateChangeCallback(*scopeParcel, callback);
424 reply.WriteInt32(result);
425 }
426
UnRegisterPermStateChangeCallbackInner(MessageParcel & data,MessageParcel & reply)427 void AccessTokenManagerStub::UnRegisterPermStateChangeCallbackInner(MessageParcel& data, MessageParcel& reply)
428 {
429 uint32_t callingTokenID = IPCSkeleton::GetCallingTokenID();
430 if ((this->GetTokenType(callingTokenID) == TOKEN_HAP) && (!IsSystemAppCalling())) {
431 reply.WriteInt32(AccessTokenError::ERR_NOT_SYSTEM_APP);
432 return;
433 }
434 if (VerifyAccessToken(callingTokenID, GET_SENSITIVE_PERMISSIONS) == PERMISSION_DENIED) {
435 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", callingTokenID);
436 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
437 return;
438 }
439 sptr<IRemoteObject> callback = data.ReadRemoteObject();
440 if (callback == nullptr) {
441 ACCESSTOKEN_LOG_ERROR(LABEL, "read callback fail");
442 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
443 return;
444 }
445 int32_t result = this->UnRegisterPermStateChangeCallback(callback);
446 reply.WriteInt32(result);
447 }
448
449 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
ReloadNativeTokenInfoInner(MessageParcel & data,MessageParcel & reply)450 void AccessTokenManagerStub::ReloadNativeTokenInfoInner(MessageParcel& data, MessageParcel& reply)
451 {
452 if (!IsPrivilegedCalling()) {
453 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
454 reply.WriteUint32(AccessTokenError::ERR_PERMISSION_DENIED);
455 return;
456 }
457 int32_t result = this->ReloadNativeTokenInfo();
458 reply.WriteInt32(result);
459 }
460 #endif
461
GetNativeTokenIdInner(MessageParcel & data,MessageParcel & reply)462 void AccessTokenManagerStub::GetNativeTokenIdInner(MessageParcel& data, MessageParcel& reply)
463 {
464 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
465 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
466 reply.WriteUint32(INVALID_TOKENID);
467 return;
468 }
469 std::string processName;
470 if (!data.ReadString(processName)) {
471 ACCESSTOKEN_LOG_ERROR(LABEL, "readString fail, processName=%{public}s", processName.c_str());
472 return;
473 }
474 AccessTokenID result = this->GetNativeTokenId(processName);
475 reply.WriteUint32(result);
476 }
477
478 #ifdef TOKEN_SYNC_ENABLE
GetHapTokenInfoFromRemoteInner(MessageParcel & data,MessageParcel & reply)479 void AccessTokenManagerStub::GetHapTokenInfoFromRemoteInner(MessageParcel& data, MessageParcel& reply)
480 {
481 if (!IsAccessTokenCalling()) {
482 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
483 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
484 return;
485 }
486 AccessTokenID tokenID = data.ReadUint32();
487 HapTokenInfoForSyncParcel hapTokenParcel;
488
489 int result = this->GetHapTokenInfoFromRemote(tokenID, hapTokenParcel);
490 reply.WriteInt32(result);
491 if (result != RET_SUCCESS) {
492 return;
493 }
494 reply.WriteParcelable(&hapTokenParcel);
495 }
496
GetAllNativeTokenInfoInner(MessageParcel & data,MessageParcel & reply)497 void AccessTokenManagerStub::GetAllNativeTokenInfoInner(MessageParcel& data, MessageParcel& reply)
498 {
499 if (!IsAccessTokenCalling()) {
500 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
501 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
502 return;
503 }
504 std::vector<NativeTokenInfoForSyncParcel> nativeTokenInfosRes;
505 int result = this->GetAllNativeTokenInfo(nativeTokenInfosRes);
506 reply.WriteInt32(result);
507 if (result != RET_SUCCESS) {
508 return;
509 }
510 reply.WriteUint32(nativeTokenInfosRes.size());
511 for (const auto& native : nativeTokenInfosRes) {
512 reply.WriteParcelable(&native);
513 }
514 }
515
SetRemoteHapTokenInfoInner(MessageParcel & data,MessageParcel & reply)516 void AccessTokenManagerStub::SetRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply)
517 {
518 if (!IsAccessTokenCalling()) {
519 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
520 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
521 return;
522 }
523 std::string deviceID = data.ReadString();
524 sptr<HapTokenInfoForSyncParcel> hapTokenParcel = data.ReadParcelable<HapTokenInfoForSyncParcel>();
525 if (hapTokenParcel == nullptr) {
526 ACCESSTOKEN_LOG_ERROR(LABEL, "hapTokenParcel read faild");
527 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
528 return;
529 }
530 int result = this->SetRemoteHapTokenInfo(deviceID, *hapTokenParcel);
531 reply.WriteInt32(result);
532 }
533
SetRemoteNativeTokenInfoInner(MessageParcel & data,MessageParcel & reply)534 void AccessTokenManagerStub::SetRemoteNativeTokenInfoInner(MessageParcel& data, MessageParcel& reply)
535 {
536 if (!IsAccessTokenCalling()) {
537 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
538 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
539 return;
540 }
541 std::string deviceID = data.ReadString();
542
543 std::vector<NativeTokenInfoForSyncParcel> nativeParcelList;
544 uint32_t size = data.ReadUint32();
545 if (size > MAX_NATIVE_TOKEN_INFO_SIZE) {
546 ACCESSTOKEN_LOG_ERROR(LABEL, "size %{public}u is invalid", size);
547 reply.WriteInt32(AccessTokenError::ERR_OVERSIZE);
548 return;
549 }
550 for (uint32_t i = 0; i < size; i++) {
551 sptr<NativeTokenInfoForSyncParcel> nativeParcel = data.ReadParcelable<NativeTokenInfoForSyncParcel>();
552 if (nativeParcel == nullptr) {
553 ACCESSTOKEN_LOG_ERROR(LABEL, "nativeParcel read faild");
554 reply.WriteInt32(AccessTokenError::ERR_READ_PARCEL_FAILED);
555 return;
556 }
557 nativeParcelList.emplace_back(*nativeParcel);
558 }
559
560 int result = this->SetRemoteNativeTokenInfo(deviceID, nativeParcelList);
561 reply.WriteInt32(result);
562 }
563
DeleteRemoteTokenInner(MessageParcel & data,MessageParcel & reply)564 void AccessTokenManagerStub::DeleteRemoteTokenInner(MessageParcel& data, MessageParcel& reply)
565 {
566 if (!IsAccessTokenCalling()) {
567 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
568 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
569 return;
570 }
571 std::string deviceID = data.ReadString();
572 AccessTokenID tokenID = data.ReadUint32();
573
574 int result = this->DeleteRemoteToken(deviceID, tokenID);
575 reply.WriteInt32(result);
576 }
577
GetRemoteNativeTokenIDInner(MessageParcel & data,MessageParcel & reply)578 void AccessTokenManagerStub::GetRemoteNativeTokenIDInner(MessageParcel& data, MessageParcel& reply)
579 {
580 if (!IsAccessTokenCalling()) {
581 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
582 reply.WriteInt32(INVALID_TOKENID);
583 return;
584 }
585 std::string deviceID = data.ReadString();
586 AccessTokenID tokenID = data.ReadUint32();
587
588 AccessTokenID result = this->GetRemoteNativeTokenID(deviceID, tokenID);
589 reply.WriteUint32(result);
590 }
591
DeleteRemoteDeviceTokensInner(MessageParcel & data,MessageParcel & reply)592 void AccessTokenManagerStub::DeleteRemoteDeviceTokensInner(MessageParcel& data, MessageParcel& reply)
593 {
594 if (!IsAccessTokenCalling()) {
595 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
596 reply.WriteInt32(AccessTokenError::ERR_PERMISSION_DENIED);
597 return;
598 }
599 std::string deviceID = data.ReadString();
600
601 int result = this->DeleteRemoteDeviceTokens(deviceID);
602 reply.WriteInt32(result);
603 }
604 #endif
605
DumpTokenInfoInner(MessageParcel & data,MessageParcel & reply)606 void AccessTokenManagerStub::DumpTokenInfoInner(MessageParcel& data, MessageParcel& reply)
607 {
608 if (!IsNativeProcessCalling() && !IsPrivilegedCalling()) {
609 ACCESSTOKEN_LOG_ERROR(LABEL, "permission denied(tokenID=%{public}d)", IPCSkeleton::GetCallingTokenID());
610 reply.WriteString("");
611 return;
612 }
613 AccessTokenID tokenID = data.ReadUint32();
614 std::string dumpInfo = "";
615 this->DumpTokenInfo(tokenID, dumpInfo);
616 if (!reply.SetDataCapacity(DUMP_CAPACITY_SIZE)) {
617 ACCESSTOKEN_LOG_WARN(LABEL, "SetDataCapacity failed");
618 }
619 if (!reply.WriteString(dumpInfo)) {
620 ACCESSTOKEN_LOG_ERROR(LABEL, "WriteString failed");
621 }
622 }
623
IsPrivilegedCalling() const624 bool AccessTokenManagerStub::IsPrivilegedCalling() const
625 {
626 // shell process is root in debug mode.
627 int32_t callingUid = IPCSkeleton::GetCallingUid();
628 return callingUid == ROOT_UID;
629 }
630
IsFoundationCalling() const631 bool AccessTokenManagerStub::IsFoundationCalling() const
632 {
633 int32_t callingUid = IPCSkeleton::GetCallingUid();
634 return callingUid == FOUNDATION_UID;
635 }
636
IsAccessTokenCalling()637 bool AccessTokenManagerStub::IsAccessTokenCalling()
638 {
639 uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID();
640 if (tokenSyncId_ == 0) {
641 tokenSyncId_ = this->GetNativeTokenId("token_sync_service");
642 }
643 return tokenCaller == tokenSyncId_;
644 }
645
IsNativeProcessCalling()646 bool AccessTokenManagerStub::IsNativeProcessCalling()
647 {
648 AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
649 return this->GetTokenType(tokenCaller) == TOKEN_NATIVE;
650 }
651
IsSystemAppCalling() const652 bool AccessTokenManagerStub::IsSystemAppCalling() const
653 {
654 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
655 return TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
656 }
657
658 #ifdef TOKEN_SYNC_ENABLE
SetTokenSyncFuncInMap()659 void AccessTokenManagerStub::SetTokenSyncFuncInMap()
660 {
661 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_HAP_TOKEN_FROM_REMOTE)] =
662 &AccessTokenManagerStub::GetHapTokenInfoFromRemoteInner;
663 requestFuncMap_[
664 static_cast<uint32_t>(AccessTokenInterfaceCode::GET_ALL_NATIVE_TOKEN_FROM_REMOTE)] =
665 &AccessTokenManagerStub::GetAllNativeTokenInfoInner;
666 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::SET_REMOTE_HAP_TOKEN_INFO)] =
667 &AccessTokenManagerStub::SetRemoteHapTokenInfoInner;
668 requestFuncMap_[
669 static_cast<uint32_t>(AccessTokenInterfaceCode::SET_REMOTE_NATIVE_TOKEN_INFO)] =
670 &AccessTokenManagerStub::SetRemoteNativeTokenInfoInner;
671 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::DELETE_REMOTE_TOKEN_INFO)] =
672 &AccessTokenManagerStub::DeleteRemoteTokenInner;
673 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::DELETE_REMOTE_DEVICE_TOKEN)] =
674 &AccessTokenManagerStub::DeleteRemoteDeviceTokensInner;
675 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_NATIVE_REMOTE_TOKEN)] =
676 &AccessTokenManagerStub::GetRemoteNativeTokenIDInner;
677 }
678 #endif
679
SetLocalTokenOpFuncInMap()680 void AccessTokenManagerStub::SetLocalTokenOpFuncInMap()
681 {
682 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::ALLOC_TOKEN_HAP)] =
683 &AccessTokenManagerStub::AllocHapTokenInner;
684 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::TOKEN_DELETE)] =
685 &AccessTokenManagerStub::DeleteTokenInfoInner;
686 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_TOKEN_TYPE)] =
687 &AccessTokenManagerStub::GetTokenTypeInner;
688 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::CHECK_NATIVE_DCAP)] =
689 &AccessTokenManagerStub::CheckNativeDCapInner;
690 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_HAP_TOKEN_ID)] =
691 &AccessTokenManagerStub::GetHapTokenIDInner;
692 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::ALLOC_LOCAL_TOKEN_ID)] =
693 &AccessTokenManagerStub::AllocLocalTokenIDInner;
694 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_NATIVE_TOKENINFO)] =
695 &AccessTokenManagerStub::GetNativeTokenInfoInner;
696 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_HAP_TOKENINFO)] =
697 &AccessTokenManagerStub::GetHapTokenInfoInner;
698 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::UPDATE_HAP_TOKEN)] =
699 &AccessTokenManagerStub::UpdateHapTokenInner;
700 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
701 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::RELOAD_NATIVE_TOKEN_INFO)] =
702 &AccessTokenManagerStub::ReloadNativeTokenInfoInner;
703 #endif
704 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_NATIVE_TOKEN_ID)] =
705 &AccessTokenManagerStub::GetNativeTokenIdInner;
706 }
707
SetPermissionOpFuncInMap()708 void AccessTokenManagerStub::SetPermissionOpFuncInMap()
709 {
710 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::VERIFY_ACCESSTOKEN)] =
711 &AccessTokenManagerStub::VerifyAccessTokenInner;
712 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_DEF_PERMISSION)] =
713 &AccessTokenManagerStub::GetDefPermissionInner;
714 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_DEF_PERMISSIONS)] =
715 &AccessTokenManagerStub::GetDefPermissionsInner;
716 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_REQ_PERMISSIONS)] =
717 &AccessTokenManagerStub::GetReqPermissionsInner;
718 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_PERMISSION_FLAG)] =
719 &AccessTokenManagerStub::GetPermissionFlagInner;
720 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GRANT_PERMISSION)] =
721 &AccessTokenManagerStub::GrantPermissionInner;
722 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::REVOKE_PERMISSION)] =
723 &AccessTokenManagerStub::RevokePermissionInner;
724 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::CLEAR_USER_GRANT_PERMISSION)] =
725 &AccessTokenManagerStub::ClearUserGrantedPermissionStateInner;
726 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::GET_PERMISSION_OPER_STATE)] =
727 &AccessTokenManagerStub::GetSelfPermissionsStateInner;
728 requestFuncMap_[
729 static_cast<uint32_t>(AccessTokenInterfaceCode::REGISTER_PERM_STATE_CHANGE_CALLBACK)] =
730 &AccessTokenManagerStub::RegisterPermStateChangeCallbackInner;
731 requestFuncMap_[
732 static_cast<uint32_t>(AccessTokenInterfaceCode::UNREGISTER_PERM_STATE_CHANGE_CALLBACK)] =
733 &AccessTokenManagerStub::UnRegisterPermStateChangeCallbackInner;
734 requestFuncMap_[static_cast<uint32_t>(AccessTokenInterfaceCode::DUMP_TOKENINFO)] =
735 &AccessTokenManagerStub::DumpTokenInfoInner;
736 }
737
AccessTokenManagerStub()738 AccessTokenManagerStub::AccessTokenManagerStub()
739 {
740 SetPermissionOpFuncInMap();
741 SetLocalTokenOpFuncInMap();
742 #ifdef TOKEN_SYNC_ENABLE
743 SetTokenSyncFuncInMap();
744 #endif
745 }
746
~AccessTokenManagerStub()747 AccessTokenManagerStub::~AccessTokenManagerStub()
748 {
749 requestFuncMap_.clear();
750 }
751 } // namespace AccessToken
752 } // namespace Security
753 } // namespace OHOS
754