1 /*
2 * Copyright (c) 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 "token_sync_manager_stub.h"
17
18 #include "accesstoken_log.h"
19 #include "hap_token_info_for_sync_parcel.h"
20 #include "ipc_skeleton.h"
21 #include "native_token_info_for_sync_parcel.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace Security {
26 namespace AccessToken {
27 namespace {
28 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncManagerStub"};
29 static const int32_t ROOT_UID = 0;
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t TokenSyncManagerStub::OnRemoteRequest(
33 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
34 {
35 ACCESSTOKEN_LOG_INFO(LABEL, "%{public}s called, code: %{public}d", __func__, code);
36 std::u16string descriptor = data.ReadInterfaceToken();
37 if (descriptor != ITokenSyncManager::GetDescriptor()) {
38 ACCESSTOKEN_LOG_ERROR(LABEL, "get unexpect descriptor: %{public}s", Str16ToStr8(descriptor).c_str());
39 return -1;
40 }
41 switch (code) {
42 case static_cast<uint32_t>(ITokenSyncManager::InterfaceCode::GET_REMOTE_HAP_TOKEN_INFO):
43 GetRemoteHapTokenInfoInner(data, reply);
44 break;
45 case static_cast<uint32_t>(ITokenSyncManager::InterfaceCode::DELETE_REMOTE_HAP_TOKEN_INFO):
46 DeleteRemoteHapTokenInfoInner(data, reply);
47 break;
48 case static_cast<uint32_t>(ITokenSyncManager::InterfaceCode::UPDATE_REMOTE_HAP_TOKEN_INFO):
49 UpdateRemoteHapTokenInfoInner(data, reply);
50 break;
51 default:
52 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54 return NO_ERROR;
55 }
56
IsNativeProcessCalling() const57 bool TokenSyncManagerStub::IsNativeProcessCalling() const
58 {
59 AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
60 uint32_t type = (reinterpret_cast<AccessTokenIDInner *>(&tokenCaller))->type;
61 ACCESSTOKEN_LOG_DEBUG(LABEL, "Calling type: %{public}d", type);
62 return type == TOKEN_NATIVE;
63 }
64
IsRootCalling() const65 bool TokenSyncManagerStub::IsRootCalling() const
66 {
67 int callingUid = IPCSkeleton::GetCallingUid();
68 ACCESSTOKEN_LOG_DEBUG(LABEL, "Calling uid: %{public}d", callingUid);
69 return callingUid == ROOT_UID;
70 }
71
GetRemoteHapTokenInfoInner(MessageParcel & data,MessageParcel & reply)72 void TokenSyncManagerStub::GetRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply)
73 {
74 if (!IsRootCalling() && !IsNativeProcessCalling()) {
75 ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s called, permission denied", __func__);
76 reply.WriteInt32(RET_FAILED);
77 return;
78 }
79
80 std::string deviceID = data.ReadString();
81 AccessTokenID tokenID = data.ReadUint32();
82
83 HapTokenInfoForSync tokenInfo;
84 int result = this->GetRemoteHapTokenInfo(deviceID, tokenID);
85 reply.WriteInt32(result);
86 }
87
DeleteRemoteHapTokenInfoInner(MessageParcel & data,MessageParcel & reply)88 void TokenSyncManagerStub::DeleteRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply)
89 {
90 if (!IsRootCalling() && !IsNativeProcessCalling()) {
91 ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s called, permission denied", __func__);
92 reply.WriteInt32(RET_FAILED);
93 return;
94 }
95 AccessTokenID tokenID = data.ReadUint32();
96 int result = this->DeleteRemoteHapTokenInfo(tokenID);
97 reply.WriteInt32(result);
98 }
99
UpdateRemoteHapTokenInfoInner(MessageParcel & data,MessageParcel & reply)100 void TokenSyncManagerStub::UpdateRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply)
101 {
102 if (!IsRootCalling() && !IsNativeProcessCalling()) {
103 ACCESSTOKEN_LOG_ERROR(LABEL, "%{public}s called, permission denied", __func__);
104 reply.WriteInt32(RET_FAILED);
105 return;
106 }
107
108 sptr<HapTokenInfoForSyncParcel> tokenInfoParcelPtr = data.ReadParcelable<HapTokenInfoForSyncParcel>();
109 int result = RET_FAILED;
110 if (tokenInfoParcelPtr != nullptr) {
111 result = this->UpdateRemoteHapTokenInfo(tokenInfoParcelPtr->hapTokenInfoForSyncParams);
112 }
113 reply.WriteInt32(result);
114 }
115 } // namespace AccessToken
116 } // namespace Security
117 } // namespace OHOS
118