1 /*
2 * Copyright (C) 2021-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
16 #include "ipc_dev_auth_stub.h"
17
18 #include "common_defs.h"
19 #include "hc_log.h"
20 #include "ipc_adapt.h"
21 #include "ipc_callback_stub.h"
22 #include "ipc_sdk.h"
23 #include "permission_adapter.h"
24 #include "securec.h"
25 #include "system_ability_definition.h"
26 #include "hc_string_vector.h"
27 #include "hidump_adapter.h"
28 #include "string_ex.h"
29
30 using namespace std;
31 namespace OHOS {
32 static std::mutex g_cBMutex;
33
34 struct CbStubInfo {
35 sptr<IRemoteObject> cbStub;
36 bool inUse;
37 };
38 static struct CbStubInfo g_cbStub[MAX_CBSTUB_SIZE];
39 static bool g_cbStubInited = false;
40
ServiceDevAuth()41 ServiceDevAuth::ServiceDevAuth()
42 {}
43
~ServiceDevAuth()44 ServiceDevAuth::~ServiceDevAuth()
45 {
46 maxCallMapSz = MAX_CALLMAP_SIZE;
47 if (callMapTable != nullptr) {
48 delete[] callMapTable;
49 callMapTable = nullptr;
50 }
51 callMapElemNum = 0;
52 }
53
Dump(int32_t fd,const std::vector<std::u16string> & args)54 int32_t ServiceDevAuth::Dump(int32_t fd, const std::vector<std::u16string>& args)
55 {
56 std::vector<std::string> strArgs;
57 for (auto arg : args) {
58 strArgs.emplace_back(Str16ToStr8(arg));
59 }
60 int argc = strArgs.size();
61 StringVector strArgVec = CreateStrVector();
62 for (int i = 0; i < argc; i++) {
63 HcString strArg = CreateString();
64 if (!StringSetPointer(&strArg, strArgs[i].c_str())) {
65 LOGE("Failed to set strArg!");
66 DeleteString(&strArg);
67 continue;
68 }
69 if (strArgVec.pushBackT(&strArgVec, strArg) == NULL) {
70 LOGE("Failed to push strArg to strArgVec!");
71 DeleteString(&strArg);
72 }
73 }
74 DEV_AUTH_DUMP(fd, &strArgVec);
75 DestroyStrVector(&strArgVec);
76 return 0;
77 }
78
GetCallMethodByMethodId(int32_t methodId)79 IpcServiceCall ServiceDevAuth::GetCallMethodByMethodId(int32_t methodId)
80 {
81 int32_t i;
82
83 if (callMapTable == nullptr) {
84 return nullptr;
85 }
86
87 for (i = 0; i < maxCallMapSz; i++) {
88 if ((callMapTable[i].methodId == methodId) && (callMapTable[i].method != nullptr)) {
89 return callMapTable[i].method;
90 }
91 }
92 return nullptr;
93 }
94
DecodeCallRequest(MessageParcel & data,IpcDataInfo * paramsCache,int32_t cacheNum,int32_t & inParamNum)95 static int32_t DecodeCallRequest(MessageParcel &data, IpcDataInfo *paramsCache, int32_t cacheNum, int32_t &inParamNum)
96 {
97 int32_t dataLen = 0;
98 int32_t i;
99 int32_t ret;
100
101 if (data.GetReadableBytes() == 0) {
102 return HC_SUCCESS;
103 }
104
105 if (data.GetReadableBytes() < sizeof(int32_t)) {
106 LOGE("Insufficient data available in IPC container. [Data]: dataLen");
107 return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
108 }
109 data.ReadInt32(dataLen);
110 if (dataLen > static_cast<int32_t>(data.GetReadableBytes())) {
111 LOGE("Insufficient data available in IPC container. [Data]: data");
112 return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
113 }
114
115 if (data.GetReadableBytes() < sizeof(int32_t)) {
116 LOGE("Insufficient data available in IPC container. [Data]: inParamNum");
117 return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
118 }
119 data.ReadInt32(inParamNum);
120 if ((inParamNum < 0) || (inParamNum > cacheNum)) {
121 LOGE("param number invalid, inParamNum - %d", inParamNum);
122 return HC_ERR_IPC_BAD_PARAM_NUM;
123 }
124
125 for (i = 0; i < inParamNum; i++) {
126 ret = DecodeIpcData(reinterpret_cast<uintptr_t>(&data), &(paramsCache[i].type),
127 &(paramsCache[i].val), &(paramsCache[i].valSz));
128 if (ret != HC_SUCCESS) {
129 LOGE("decode failed, ret %d", ret);
130 return ret;
131 }
132 }
133 return HC_SUCCESS;
134 }
135
GetMethodId(MessageParcel & data,int32_t & methodId)136 static int32_t GetMethodId(MessageParcel &data, int32_t &methodId)
137 {
138 if (data.GetDataSize() < sizeof(int32_t)) {
139 LOGE("Insufficient data available in IPC container. [Data]: methodId");
140 return HC_ERR_IPC_CALL_DATA_LENGTH;
141 }
142 methodId = data.ReadInt32();
143 return HC_SUCCESS;
144 }
145
WithObject(int32_t methodId,MessageParcel & data,IpcDataInfo & ipcData,int32_t & cnt)146 static void WithObject(int32_t methodId, MessageParcel &data, IpcDataInfo &ipcData, int32_t &cnt)
147 {
148 if (!IsCallbackMethod(methodId)) {
149 return;
150 }
151 if (data.GetReadableBytes() < sizeof(int32_t)) {
152 LOGE("Insufficient data available in IPC container. [Data]: type");
153 return;
154 }
155 ipcData.type = data.ReadInt32();
156 ipcData.valSz = sizeof(StubDevAuthCb);
157 sptr<IRemoteObject> tmp = data.ReadRemoteObject();
158 if (!tmp) {
159 LOGE("should with remote object, but read failed");
160 return;
161 }
162 ipcData.idx = ServiceDevAuth::SetRemoteObject(tmp);
163 if (ipcData.idx >= 0) {
164 ipcData.val = reinterpret_cast<uint8_t *>(&(ipcData.idx));
165 LOGI("object trans success, set id %d", ipcData.idx);
166 cnt++;
167 }
168 }
169
InitCbStubTable()170 static void InitCbStubTable()
171 {
172 int32_t i;
173 if (g_cbStubInited) {
174 return;
175 }
176 std::lock_guard<std::mutex> autoLock(g_cBMutex);
177 if (g_cbStubInited) { /* for first init at the same time */
178 return;
179 }
180 for (i = 0; i < MAX_CBSTUB_SIZE; i++) {
181 g_cbStub[i].inUse = false;
182 }
183 g_cbStubInited = true;
184 return;
185 }
186
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)187 int32_t ServiceDevAuth::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
188 {
189 if (data.ReadInterfaceToken() != GetDescriptor()) {
190 LOGE("[IPC][C->S]: The proxy interface token is invalid!");
191 return -1;
192 }
193 SET_LOG_MODE(NORMAL_MODE);
194 int32_t ret = HC_ERR_IPC_UNKNOW_OPCODE;
195 int32_t dataLen;
196 int32_t methodId = 0;
197 int32_t reqParamNum = 0;
198 MessageParcel replyCache;
199 IpcDataInfo reqParams[MAX_REQUEST_PARAMS_NUM] = { { 0 } };
200 IpcServiceCall serviceCall = nullptr;
201
202 switch (code) {
203 case static_cast<uint32_t>(DevAuthInterfaceCode::DEV_AUTH_CALL_REQUEST):
204 ret = GetMethodId(data, methodId);
205 if (ret != HC_SUCCESS) {
206 break;
207 }
208 if (CheckPermission(methodId) != HC_SUCCESS) {
209 return -1;
210 }
211 serviceCall = GetCallMethodByMethodId(methodId);
212 if (serviceCall == nullptr) {
213 ret = HC_ERR_IPC_METHOD_ID_INVALID;
214 break;
215 }
216 ret = DecodeCallRequest(data, reqParams, MAX_REQUEST_PARAMS_NUM, reqParamNum);
217 if (ret != HC_SUCCESS) {
218 break;
219 }
220 if (reqParamNum < (MAX_REQUEST_PARAMS_NUM - 1)) {
221 InitCbStubTable();
222 WithObject(methodId, data, reqParams[reqParamNum], reqParamNum);
223 }
224 ret = serviceCall(reqParams, reqParamNum, reinterpret_cast<uintptr_t>(&replyCache));
225 break;
226 default:
227 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
228 }
229 reply.WriteInt32(ret);
230 dataLen = replyCache.GetDataSize();
231 if (dataLen > 0) {
232 reply.WriteInt32(dataLen);
233 reply.WriteBuffer(reinterpret_cast<const void *>(replyCache.GetData()), dataLen);
234 }
235 return 0;
236 }
237
SetCallMap(IpcServiceCall method,int32_t methodId)238 int32_t ServiceDevAuth::SetCallMap(IpcServiceCall method, int32_t methodId)
239 {
240 int32_t len;
241 errno_t eno;
242 IpcServiceCallMap *callMapTmp = nullptr;
243
244 if ((1 + callMapElemNum) > maxCallMapSz) {
245 maxCallMapSz += MAX_CALLMAP_SIZE;
246 if (callMapTable != nullptr) {
247 callMapTmp = callMapTable;
248 callMapTable = nullptr;
249 }
250 }
251 if (callMapTable == nullptr) {
252 callMapTable = new(std::nothrow) IpcServiceCallMap[maxCallMapSz];
253 if (callMapTable == nullptr) {
254 return HC_ERR_ALLOC_MEMORY;
255 }
256 len = sizeof(IpcServiceCallMap) * maxCallMapSz;
257 (void)memset_s(callMapTable, len, 0, len);
258 if (callMapTmp != nullptr) {
259 eno = memcpy_s(callMapTable, len, callMapTmp, (sizeof(IpcServiceCallMap) * callMapElemNum));
260 if (eno != EOK) {
261 delete[] callMapTable;
262 callMapTable = callMapTmp;
263 maxCallMapSz -= MAX_CALLMAP_SIZE;
264 return HC_ERR_MEMORY_COPY;
265 }
266 delete[] callMapTmp;
267 callMapTmp = nullptr;
268 }
269 }
270 callMapTable[callMapElemNum].method = method;
271 callMapTable[callMapElemNum].methodId = methodId;
272 callMapElemNum++;
273 return HC_SUCCESS;
274 }
275
SetRemoteObject(sptr<IRemoteObject> & object)276 int32_t ServiceDevAuth::SetRemoteObject(sptr<IRemoteObject> &object)
277 {
278 int32_t idx = -1;
279 int32_t i;
280
281 std::lock_guard<std::mutex> autoLock(g_cBMutex);
282 for (i = 0; i < MAX_CBSTUB_SIZE; i++) {
283 if (!g_cbStub[i].inUse) {
284 idx = i;
285 break;
286 }
287 }
288 LOGI("remote object cache index %d", idx);
289 if (idx == -1) {
290 return -1;
291 }
292 g_cbStub[idx].cbStub = object;
293 g_cbStub[idx].inUse = true;
294 return idx;
295 }
296
AddCbDeathRecipient(int32_t cbStubIdx,int32_t cbDataIdx)297 void ServiceDevAuth::AddCbDeathRecipient(int32_t cbStubIdx, int32_t cbDataIdx)
298 {
299 bool bRet = false;
300 if ((cbStubIdx < 0) || (cbStubIdx >= MAX_CBSTUB_SIZE) || (!g_cbStub[cbStubIdx].inUse)) {
301 return;
302 }
303
304 std::lock_guard<std::mutex> autoLock(g_cBMutex);
305 bRet = g_cbStub[cbStubIdx].cbStub->AddDeathRecipient(new(std::nothrow) DevAuthDeathRecipient(cbDataIdx));
306 LOGI("AddDeathRecipient %s, callback stub idx %d", bRet ? "success" : "failed", cbStubIdx);
307 return;
308 }
309
ResetRemoteObject(int32_t idx)310 void ServiceDevAuth::ResetRemoteObject(int32_t idx)
311 {
312 if ((idx >= 0) && (idx < MAX_CBSTUB_SIZE)) {
313 LOGI("remote object used done, idx %d", idx);
314 std::lock_guard<std::mutex> autoLock(g_cBMutex);
315 g_cbStub[idx].inUse = false;
316 }
317 return;
318 }
319
ActCallback(int32_t objIdx,int32_t callbackId,bool sync,uintptr_t cbHook,MessageParcel & dataParcel,MessageParcel & reply)320 void ServiceDevAuth::ActCallback(int32_t objIdx, int32_t callbackId, bool sync,
321 uintptr_t cbHook, MessageParcel &dataParcel, MessageParcel &reply)
322 {
323 if ((objIdx < 0) || (objIdx >= MAX_CBSTUB_SIZE) || (!g_cbStub[objIdx].inUse)) {
324 LOGW("nothing to do, callback id %d, remote object id %d", callbackId, objIdx);
325 return;
326 }
327 MessageOption option(MessageOption::TF_SYNC);
328 option.SetWaitTime(DEV_AUTH_CALL_WAIT_TIME);
329 if (!sync) {
330 option.SetFlags(MessageOption::TF_ASYNC);
331 option.SetWaitTime(0);
332 }
333 std::lock_guard<std::mutex> autoLock(g_cBMutex);
334 sptr<ICommIpcCallback> proxy = iface_cast<ICommIpcCallback>(g_cbStub[objIdx].cbStub);
335 proxy->DoCallBack(callbackId, cbHook, dataParcel, reply, option);
336 return;
337 }
338
DevAuthDeathRecipient(int32_t cbIdx)339 DevAuthDeathRecipient::DevAuthDeathRecipient(int32_t cbIdx)
340 {
341 callbackIdx = cbIdx;
342 }
343
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)344 void DevAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
345 {
346 LOGI("remote is not actively, to reset local resource");
347 ResetIpcCallBackNodeByNodeId(callbackIdx);
348 }
349 }
350