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