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