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