1 /*
2 * Copyright (c) 2023 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 #include "oaid_service_stub.h"
16 #include <singleton.h>
17 #include "bundle_mgr_helper.h"
18 #include "bundle_mgr_client.h"
19 #include "accesstoken_kit.h"
20 #include "privacy_kit.h"
21 #include "tokenid_kit.h"
22 #include "oaid_common.h"
23 #include "oaid_service_define.h"
24 #include "oaid_service.h"
25 #include "oaid_service_ipc_interface_code.h"
26
27 using namespace OHOS::Security::AccessToken;
28
29 namespace OHOS {
30 namespace Cloud {
31 using namespace OHOS::HiviewDFX;
32
OAIDServiceStub()33 OAIDServiceStub::OAIDServiceStub()
34 {
35 memberFuncMap_[static_cast<uint32_t>(OAIDInterfaceCode::GET_OAID)] = &OAIDServiceStub::OnGetOAID;
36 memberFuncMap_[static_cast<uint32_t>(OAIDInterfaceCode::RESET_OAID)] = &OAIDServiceStub::OnResetOAID;
37 }
38
~OAIDServiceStub()39 OAIDServiceStub::~OAIDServiceStub()
40 {
41 memberFuncMap_.clear();
42 }
43
CheckPermission(const std::string & permissionName)44 bool OAIDServiceStub::CheckPermission(const std::string &permissionName)
45 {
46 // Verify the invoker's permission.
47 AccessTokenID firstCallToken = IPCSkeleton::GetFirstTokenID();
48 AccessTokenID callingToken = IPCSkeleton::GetCallingTokenID();
49 ATokenTypeEnum callingType = AccessTokenKit::GetTokenTypeFlag(callingToken);
50
51 ErrCode result = TypePermissionState::PERMISSION_DENIED;
52
53 if (firstCallToken == 0) {
54 if (callingType == TOKEN_INVALID) {
55 OAID_HILOGE(OAID_MODULE_SERVICE, "callingToken is invalid");
56 return false;
57 } else {
58 result = AccessTokenKit::VerifyAccessToken(callingToken, permissionName);
59 }
60 } else {
61 result = AccessTokenKit::VerifyAccessToken(callingToken, firstCallToken, permissionName);
62 }
63
64 if (callingType == TOKEN_HAP) {
65 int32_t successCnt = (int32_t)(result == TypePermissionState::PERMISSION_GRANTED);
66 int32_t failCnt = 1 - successCnt; // 1 means that there is only one visit in total
67 // AddPermissionUsedRecord needs to transfer both the number of successful and failed permission access requests
68 int32_t ret = PrivacyKit::AddPermissionUsedRecord(callingToken, permissionName, successCnt, failCnt);
69 OAID_HILOGI(OAID_MODULE_SERVICE, "AddPermissionUsedRecord ret=%{public}d", ret);
70 }
71
72 if (result == TypePermissionState::PERMISSION_DENIED) {
73 OAID_HILOGI(OAID_MODULE_SERVICE, "the caller not granted the app tracking permission");
74 return false;
75 }
76 return true;
77 }
78
CheckSystemApp()79 bool OAIDServiceStub::CheckSystemApp()
80 {
81 FullTokenID callingFullToken = IPCSkeleton::GetCallingFullTokenID();
82
83 auto tokenType = AccessTokenKit::GetTokenTypeFlag(IPCSkeleton::GetCallingTokenID());
84 if (!TokenIdKit::IsSystemAppByFullTokenID(callingFullToken) &&
85 tokenType != TOKEN_NATIVE &&
86 tokenType != TOKEN_SHELL) {
87 OAID_HILOGI(OAID_MODULE_SERVICE, "the caller App is not system app");
88 return false;
89 }
90
91 return true;
92 }
93
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t OAIDServiceStub::OnRemoteRequest(
95 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
96 {
97 OAID_HILOGI(OAID_MODULE_SERVICE, "Start, code is %{public}u.", code);
98 std::string bundleName;
99 pid_t uid = IPCSkeleton::GetCallingUid();
100 DelayedSingleton<BundleMgrHelper>::GetInstance()->GetBundleNameByUid(static_cast<int>(uid), bundleName);
101 if (code != static_cast<uint32_t>(OAIDInterfaceCode::RESET_OAID) &&
102 !CheckPermission(OAID_TRACKING_CONSENT_PERMISSION)) {
103 OAID_HILOGW(
104 OAID_MODULE_SERVICE, "bundleName %{public}s not granted the app tracking permission", bundleName.c_str());
105 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
106 }
107
108 if (code == static_cast<uint32_t>(OAIDInterfaceCode::RESET_OAID) && !CheckSystemApp()) {
109 OAID_HILOGW(OAID_MODULE_SERVICE, "CheckSystemApp fail");
110 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
111 }
112
113 std::u16string myDescripter = OAIDServiceStub::GetDescriptor();
114 std::u16string remoteDescripter = data.ReadInterfaceToken();
115 if (myDescripter != remoteDescripter) {
116 OAID_HILOGE(OAID_MODULE_SERVICE, "Descriptor checked fail.");
117 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
118 }
119
120 OAID_HILOGI(OAID_MODULE_SERVICE, "Remote bundleName is %{public}s.", bundleName.c_str());
121 auto itFunc = memberFuncMap_.find(code);
122 if (itFunc != memberFuncMap_.end()) {
123 auto memberFunc = itFunc->second;
124 if (memberFunc != nullptr) {
125 return (this->*memberFunc)(data, reply);
126 }
127 }
128
129 int32_t ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
130 OAID_HILOGE(OAID_MODULE_SERVICE, "No find process to handle, ret is %{public}d.", ret);
131 return ret;
132 }
133
OnGetOAID(MessageParcel & data,MessageParcel & reply)134 int32_t OAIDServiceStub::OnGetOAID(MessageParcel& data, MessageParcel& reply)
135 {
136 OAID_HILOGI(OAID_MODULE_SERVICE, "Start.");
137
138 auto oaid = GetOAID();
139 if (oaid == "") {
140 OAID_HILOGE(OAID_MODULE_SERVICE, "Get OAID failed.");
141 return ERR_SYSYTEM_ERROR;
142 }
143
144 if (!reply.WriteString(oaid)) {
145 OAID_HILOGE(OAID_MODULE_SERVICE, "Failed to write parcelable.");
146 return ERR_SYSYTEM_ERROR;
147 }
148 OAID_HILOGI(OAID_MODULE_SERVICE, "End.");
149 return ERR_OK;
150 }
151
OnResetOAID(MessageParcel & data,MessageParcel & reply)152 int32_t OAIDServiceStub::OnResetOAID(MessageParcel& data, MessageParcel& reply)
153 {
154 OAID_HILOGI(OAID_MODULE_SERVICE, "Reset OAID Start.");
155
156 ResetOAID();
157
158 OAID_HILOGI(OAID_MODULE_SERVICE, "Reset OAID End.");
159 return ERR_OK;
160 }
161 } // namespace Cloud
162 } // namespace OHOS