• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "hc_mutex.h"
22 #include "hc_types.h"
23 #include "ipc_skeleton.h"
24 #include "ipc_adapt.h"
25 #include "ipc_callback_proxy.h"
26 #include "ipc_sdk_defines.h"
27 #include "securec.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #define MAX_DATA_LEN 102400
34 static HcMutex g_cBMutex;
35 
36 struct CbStubInfo {
37     SvcIdentity cbStub;
38     uint32_t cbDieId;
39     bool inUse;
40 };
41 static struct CbStubInfo g_cbStub[MAX_CBSTUB_SIZE];
42 
43 IpcServiceCallMap *g_callMapTable = NULL;
44 int32_t g_maxCallMapSz = MAX_CALLMAP_SIZE;
45 int32_t g_callMapElemNum = 0;
46 
47 #define BINDER_TYPE_ACQUIRE 1
48 #define BINDER_TYPE_ACQUIRE_AND_FREE 2
49 #define BINDER_TYPE_RELEASE 3
50 
51 #define IPC_IO_BUFF_SZ 1024
52 
ResetCallMap(void)53 void ResetCallMap(void)
54 {
55     g_maxCallMapSz = MAX_CALLMAP_SIZE;
56     if (g_callMapTable != NULL) {
57         HcFree(g_callMapTable);
58         g_callMapTable = NULL;
59     }
60     g_callMapElemNum = 0;
61 }
62 
GetCallMethodByMethodId(int32_t methodId)63 static IpcServiceCall GetCallMethodByMethodId(int32_t methodId)
64 {
65     int32_t i;
66 
67     if (g_callMapTable == NULL) {
68         return NULL;
69     }
70 
71     for (i = 0; i < g_maxCallMapSz; i++) {
72         if ((g_callMapTable[i].methodId == methodId) && (g_callMapTable[i].method != NULL)) {
73             return g_callMapTable[i].method;
74         }
75     }
76     return NULL;
77 }
78 
DecodeCallRequest(IpcIo * data,IpcDataInfo * paramsCache,int32_t cacheNum,int32_t * inParamNum)79 static int32_t DecodeCallRequest(IpcIo *data, IpcDataInfo *paramsCache, int32_t cacheNum, int32_t *inParamNum)
80 {
81     int32_t dataLen;
82     int32_t i;
83     int32_t ret;
84 
85     if (GetIpcIoDataLength(data) > MAX_DATA_LEN) {
86         LOGE("Data len over MAX_DATA_LEN");
87         return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
88     }
89 
90     ReadInt32(data, &dataLen);
91     if (dataLen <= 0) {
92         return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
93     }
94 
95     ReadInt32(data, inParamNum);
96     if ((*inParamNum < 0) || (*inParamNum > cacheNum)) {
97         LOGE("param number invalid, inParamNum(%" LOG_PUB "d)", *inParamNum);
98         return HC_ERR_IPC_BAD_PARAM_NUM;
99     }
100 
101     uint32_t len = 0;
102     ReadUint32(data, &len); /* skip flat object length information */
103     for (i = 0; i < *inParamNum; i++) {
104         ret = DecodeIpcData((uintptr_t)data, &(paramsCache[i].type), &(paramsCache[i].val), &(paramsCache[i].valSz));
105         if (ret != HC_SUCCESS) {
106             LOGE("decode failed, ret %" LOG_PUB "d", ret);
107             return ret;
108         }
109     }
110     return HC_SUCCESS;
111 }
112 
GetMethodId(IpcIo * data,int32_t * methodId)113 static int32_t GetMethodId(IpcIo *data, int32_t *methodId)
114 {
115     ReadInt32(data, methodId);
116     return HC_SUCCESS;
117 }
118 
WithObject(int32_t methodId,IpcIo * data,IpcDataInfo * ipcData,int32_t * cnt)119 static void WithObject(int32_t methodId, IpcIo *data, IpcDataInfo *ipcData, int32_t *cnt)
120 {
121     if (!IsCallbackMethod(methodId)) {
122         return;
123     }
124     ReadInt32(data, &(ipcData->type));
125     ipcData->valSz = 0;
126     SvcIdentity tmp;
127     bool ret = ReadRemoteObject(data, &tmp);
128     if (!ret || (ipcData->type != PARAM_TYPE_CB_OBJECT)) {
129         LOGE("should with remote object, but failed, param type %" LOG_PUB "d", ipcData->type);
130         return;
131     }
132     ShowIpcSvcInfo(&tmp);
133     ipcData->idx = SetRemoteObject(&tmp);
134     if (ipcData->idx >= 0) {
135         ipcData->val = (uint8_t *)(&(ipcData->idx));
136         LOGI("object trans success, set id %" LOG_PUB "d", ipcData->idx);
137         (*cnt)++;
138     }
139 }
140 
InitCbStubTable(void)141 void InitCbStubTable(void)
142 {
143     if (InitHcMutex(&g_cBMutex, false) != HC_SUCCESS) {
144         LOGE("[DB]: Init mutex failed");
145         return;
146     }
147     (void)memset_s(g_cbStub, sizeof(g_cbStub), 0, sizeof(g_cbStub));
148     return;
149 }
150 
LockCbStubTable(void)151 static void LockCbStubTable(void)
152 {
153     (void)LockHcMutex(&g_cBMutex);
154     return;
155 }
156 
UnLockCbStubTable(void)157 static void UnLockCbStubTable(void)
158 {
159     UnlockHcMutex(&g_cBMutex);
160     return;
161 }
162 
DevAuthRequestCall(void * origin,IpcIo * req,IpcIo * reply)163 static int32_t DevAuthRequestCall(void *origin, IpcIo *req, IpcIo *reply)
164 {
165     int32_t ret;
166     int32_t methodId = -1;
167     int32_t reqParamNum = 0;
168     IpcDataInfo reqParams[MAX_REQUEST_PARAMS_NUM] = { { 0 } };
169     IpcServiceCall serviceCall = NULL;
170 
171     (void)origin;
172     ret = GetMethodId(req, &methodId);
173     if (ret != HC_SUCCESS || methodId <= 0) {
174         LOGE("GetMethodId failed, ret = %" LOG_PUB "d", ret);
175         return HC_ERR_IPC_METHOD_ID_INVALID;
176     }
177     serviceCall = GetCallMethodByMethodId(methodId);
178     if (serviceCall == NULL) {
179         return HC_ERR_IPC_METHOD_ID_INVALID;
180     }
181     ret = DecodeCallRequest(req, reqParams, MAX_REQUEST_PARAMS_NUM, &reqParamNum);
182     if (ret != HC_SUCCESS) {
183         return ret;
184     }
185     if (reqParamNum < (MAX_REQUEST_PARAMS_NUM - 1)) {
186         WithObject(methodId, req, &reqParams[reqParamNum], &reqParamNum);
187     }
188     return serviceCall(reqParams, reqParamNum, (uintptr_t)(reply));
189 }
190 
191 static struct {
192     int32_t (*callCtx)(void *origin, IpcIo *req, IpcIo *reply);
193     int32_t reqId;
194 } g_reqCallMaps[] = {
195     {DevAuthRequestCall, DEV_AUTH_CALL_REQUEST},
196 };
197 
OnRemoteInvoke(IServerProxy * iProxy,int32_t reqId,void * origin,IpcIo * req,IpcIo * reply)198 int32_t OnRemoteInvoke(IServerProxy *iProxy, int32_t reqId, void *origin, IpcIo *req, IpcIo *reply)
199 {
200     int32_t i;
201     int32_t n;
202     int32_t (*callCtx)(void *origin, IpcIo *req, IpcIo *reply) = NULL;
203     IpcIo replyTmp;
204     uint8_t dataBuff[IPC_IO_BUFF_SZ] = { 0 };
205     int32_t ret = HC_ERR_IPC_UNKNOW_OPCODE;
206 
207     (void)origin;
208     (void)iProxy;
209     LOGI("request code %" LOG_PUB "u", reqId);
210     n = sizeof(g_reqCallMaps) / sizeof(g_reqCallMaps[0]);
211     for (i = 0; i < n; i++) {
212         if ((int32_t)reqId == g_reqCallMaps[i].reqId) {
213             callCtx = g_reqCallMaps[i].callCtx;
214             break;
215         }
216     }
217     (void)memset_s(&replyTmp, sizeof(IpcIo), 0, sizeof(IpcIo));
218     IpcIoInit(&replyTmp, dataBuff, sizeof(dataBuff), 0);
219     if (callCtx) {
220         ret = callCtx(origin, req, &replyTmp);
221     }
222     WriteInt32(reply, ret);
223     if (reply != NULL) {
224         n = GetIpcIoDataLength(&replyTmp);
225         if (n > 0) {
226             WriteUint32(reply, n);
227             if (!WriteBuffer(reply, (const void *)(replyTmp.bufferBase + IpcIoBufferOffset()), n)) {
228                 LOGI("WriteBuffer faild");
229                 return HC_ERROR;
230             }
231             LOGI("form service result done, result length(%" LOG_PUB "d)", n);
232         }
233     }
234     LOGI("done, request code %" LOG_PUB "d, call result %" LOG_PUB "d", reqId, ret);
235     return 0;
236 }
237 
SetCallMap(IpcServiceCall method,int32_t methodId)238 int32_t SetCallMap(IpcServiceCall method, int32_t methodId)
239 {
240     int32_t len;
241     IpcServiceCallMap *callMapTmp = NULL;
242 
243     if ((1 + g_callMapElemNum) > g_maxCallMapSz) {
244         g_maxCallMapSz += MAX_CALLMAP_SIZE;
245         if (g_callMapTable != NULL) {
246             callMapTmp = g_callMapTable;
247             g_callMapTable = NULL;
248         }
249     }
250     if (g_callMapTable == NULL) {
251         len = sizeof(IpcServiceCallMap) * g_maxCallMapSz;
252         g_callMapTable = (IpcServiceCallMap *)HcMalloc(len, 0);
253         if (g_callMapTable == NULL) {
254             return HC_ERR_ALLOC_MEMORY;
255         }
256         (void)memset_s(g_callMapTable, len, 0, len);
257         if (callMapTmp != NULL) {
258             errno_t eno = memcpy_s(g_callMapTable, len, callMapTmp, (sizeof(IpcServiceCallMap) * g_callMapElemNum));
259             if (eno != EOK) {
260                 HcFree((void *)g_callMapTable);
261                 g_callMapTable = callMapTmp;
262                 g_maxCallMapSz -= MAX_CALLMAP_SIZE;
263                 return HC_ERR_MEMORY_COPY;
264             }
265             HcFree((void *)callMapTmp);
266             callMapTmp = NULL;
267         }
268     }
269     g_callMapTable[g_callMapElemNum].method = method;
270     g_callMapTable[g_callMapElemNum].methodId = methodId;
271     g_callMapElemNum++;
272     return HC_SUCCESS;
273 }
274 
SetRemoteObject(const SvcIdentity * object)275 int32_t SetRemoteObject(const SvcIdentity *object)
276 {
277     int32_t idx = -1;
278     int32_t i;
279 
280     LockCbStubTable();
281     for (i = 0; i < MAX_CBSTUB_SIZE; i++) {
282         if (!g_cbStub[i].inUse) {
283             idx = i;
284             break;
285         }
286     }
287     LOGI("remote object cache index %" LOG_PUB "d", idx);
288     if (idx == -1) {
289         UnLockCbStubTable();
290         return -1;
291     }
292     g_cbStub[idx].cbStub = *object;
293     g_cbStub[idx].cbDieId = 0;
294     g_cbStub[idx].inUse = true;
295     UnLockCbStubTable();
296     ShowIpcSvcInfo(&(g_cbStub[idx].cbStub));
297     return idx;
298 }
299 
ClientDeathCallback(void * arg)300 static void ClientDeathCallback(void *arg)
301 {
302     int32_t callbackIdx = (int32_t)(size_t)arg;
303 
304     LOGI("remote is not actively, to reset local resource");
305     ResetIpcCallBackNodeByNodeId(callbackIdx);
306 }
307 
AddCbDeathRecipient(int32_t cbStubIdx,int32_t cbDataIdx)308 void AddCbDeathRecipient(int32_t cbStubIdx, int32_t cbDataIdx)
309 {
310     int32_t ret;
311     uint32_t cbId = 0;
312     if ((cbStubIdx < 0) || (cbStubIdx >= MAX_CBSTUB_SIZE)) {
313         return;
314     }
315 
316     LockCbStubTable();
317     if (!g_cbStub[cbStubIdx].inUse) {
318         UnLockCbStubTable();
319         return;
320     }
321     ret = AddDeathRecipient(g_cbStub[cbStubIdx].cbStub, ClientDeathCallback, (void *)(size_t)cbDataIdx, &cbId);
322     if (ret == 0) {
323         g_cbStub[cbStubIdx].cbDieId = cbId;
324     }
325     UnLockCbStubTable();
326     LOGI("done, ret %" LOG_PUB "d, callback stub idx %" LOG_PUB "d", ret, cbStubIdx);
327     return;
328 }
329 
ResetRemoteObject(int32_t idx)330 void ResetRemoteObject(int32_t idx)
331 {
332     if ((idx >= 0) && (idx < MAX_CBSTUB_SIZE)) {
333         LOGI("object idx %" LOG_PUB "d", idx);
334         LockCbStubTable();
335         if (!g_cbStub[idx].inUse) {
336             UnLockCbStubTable();
337             return;
338         }
339         RemoveDeathRecipient(g_cbStub[idx].cbStub, g_cbStub[idx].cbDieId);
340         (void)memset_s(&(g_cbStub[idx].cbStub), sizeof(g_cbStub[idx].cbStub), 0, sizeof(g_cbStub[idx].cbStub));
341         g_cbStub[idx].inUse = false;
342         UnLockCbStubTable();
343         LOGI("remote object used done, idx %" LOG_PUB "d", idx);
344     }
345     return;
346 }
347 
ActCallback(int32_t objIdx,int32_t callbackId,uintptr_t cbHook,IpcIo * dataParcel,IpcIo * reply)348 void ActCallback(int32_t objIdx, int32_t callbackId, uintptr_t cbHook, IpcIo *dataParcel, IpcIo *reply)
349 {
350     if ((objIdx < 0) || (objIdx >= MAX_CBSTUB_SIZE) || (!g_cbStub[objIdx].inUse)) {
351         LOGW("nothing to do, callback id %" LOG_PUB "d, remote object id %" LOG_PUB "d", callbackId, objIdx);
352         return;
353     }
354 
355     ShowIpcSvcInfo(&g_cbStub[objIdx].cbStub);
356     LockCbStubTable();
357     CbProxySendRequest(g_cbStub[objIdx].cbStub, callbackId, cbHook, dataParcel, reply);
358     UnLockCbStubTable();
359     return;
360 }
361 
362 #ifdef __cplusplus
363 }
364 #endif
365