• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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     DevAuthDump(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     if (CheckPermission() != HC_SUCCESS) {
177         return -1;
178     }
179     int32_t ret = HC_ERR_IPC_UNKNOW_OPCODE;
180     int32_t dataLen;
181     int32_t methodId = 0;
182     int32_t reqParamNum = 0;
183     MessageParcel replyCache;
184     IpcDataInfo reqParams[MAX_REQUEST_PARAMS_NUM] = { { 0 } };
185     IpcServiceCall serviceCall = nullptr;
186 
187     LOGI("request code %u", code);
188     switch (code) {
189         case DEV_AUTH_CALL_REQUEST:
190             ret = GetMethodId(data, methodId);
191             if (ret != HC_SUCCESS) {
192                 break;
193             }
194             serviceCall = GetCallMethodByMethodId(methodId);
195             if (serviceCall == nullptr) {
196                 ret = HC_ERR_IPC_METHOD_ID_INVALID;
197                 break;
198             }
199             ret = DecodeCallRequest(data, reqParams, MAX_REQUEST_PARAMS_NUM, reqParamNum);
200             if (ret != HC_SUCCESS) {
201                 break;
202             }
203             if (reqParamNum < (MAX_REQUEST_PARAMS_NUM - 1)) {
204                 InitCbStubTable();
205                 WithObject(methodId, data, reqParams[reqParamNum], reqParamNum);
206             }
207             ret = serviceCall(reqParams, reqParamNum, reinterpret_cast<uintptr_t>(&replyCache));
208             break;
209         default:
210             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
211     }
212     reply.WriteInt32(ret);
213     dataLen = replyCache.GetDataSize();
214     if (dataLen > 0) {
215         reply.WriteInt32(dataLen);
216         reply.WriteBuffer(reinterpret_cast<const void *>(replyCache.GetData()), dataLen);
217     }
218     LOGI("done, request code %u, method id %d, call result %d", code, methodId, ret);
219     return 0;
220 }
221 
SetCallMap(IpcServiceCall method,int32_t methodId)222 int32_t ServiceDevAuth::SetCallMap(IpcServiceCall method, int32_t methodId)
223 {
224     int32_t len;
225     errno_t eno;
226     IpcServiceCallMap *callMapTmp = nullptr;
227 
228     if ((1 + callMapElemNum) > maxCallMapSz) {
229         maxCallMapSz += MAX_CALLMAP_SIZE;
230         if (callMapTable != nullptr) {
231             callMapTmp = callMapTable;
232             callMapTable = nullptr;
233         }
234     }
235     if (callMapTable == nullptr) {
236         callMapTable = new(std::nothrow) IpcServiceCallMap[maxCallMapSz];
237         if (callMapTable == nullptr) {
238             return HC_ERR_ALLOC_MEMORY;
239         }
240         len = sizeof(IpcServiceCallMap) * maxCallMapSz;
241         (void)memset_s(callMapTable, len, 0, len);
242         if (callMapTmp != nullptr) {
243             eno = memcpy_s(callMapTable, len, callMapTmp, (sizeof(IpcServiceCallMap) * callMapElemNum));
244             if (eno != EOK) {
245                 delete[] callMapTable;
246                 callMapTable = callMapTmp;
247                 maxCallMapSz -= MAX_CALLMAP_SIZE;
248                 return HC_ERR_MEMORY_COPY;
249             }
250             delete[] callMapTmp;
251             callMapTmp = nullptr;
252         }
253     }
254     callMapTable[callMapElemNum].method = method;
255     callMapTable[callMapElemNum].methodId = methodId;
256     callMapElemNum++;
257     return HC_SUCCESS;
258 }
259 
SetRemoteObject(sptr<IRemoteObject> & object)260 int32_t ServiceDevAuth::SetRemoteObject(sptr<IRemoteObject> &object)
261 {
262     int32_t idx = -1;
263     int32_t i;
264 
265     std::lock_guard<std::mutex> autoLock(g_cBMutex);
266     for (i = 0; i < MAX_CBSTUB_SIZE; i++) {
267         if (!g_cbStub[i].inUse) {
268             idx = i;
269             break;
270         }
271     }
272     LOGI("remote object cache index %d", idx);
273     if (idx == -1) {
274         return -1;
275     }
276     g_cbStub[idx].cbStub = object;
277     g_cbStub[idx].inUse = true;
278     return idx;
279 }
280 
AddCbDeathRecipient(int32_t cbStubIdx,int32_t cbDataIdx)281 void ServiceDevAuth::AddCbDeathRecipient(int32_t cbStubIdx, int32_t cbDataIdx)
282 {
283     bool bRet = false;
284     if ((cbStubIdx < 0) || (cbStubIdx >= MAX_CBSTUB_SIZE) || (!g_cbStub[cbStubIdx].inUse)) {
285         return;
286     }
287 
288     std::lock_guard<std::mutex> autoLock(g_cBMutex);
289     bRet = g_cbStub[cbStubIdx].cbStub->AddDeathRecipient(new(std::nothrow) DevAuthDeathRecipient(cbDataIdx));
290     LOGI("AddDeathRecipient %s, callback stub idx %d", bRet ? "success" : "failed", cbStubIdx);
291     return;
292 }
293 
ResetRemoteObject(int32_t idx)294 void ServiceDevAuth::ResetRemoteObject(int32_t idx)
295 {
296     if ((idx >= 0) && (idx < MAX_CBSTUB_SIZE)) {
297         LOGI("remote object used done, idx %d", idx);
298         std::lock_guard<std::mutex> autoLock(g_cBMutex);
299         g_cbStub[idx].inUse = false;
300     }
301     return;
302 }
303 
ActCallback(int32_t objIdx,int32_t callbackId,bool sync,uintptr_t cbHook,MessageParcel & dataParcel,MessageParcel & reply)304 void ServiceDevAuth::ActCallback(int32_t objIdx, int32_t callbackId, bool sync,
305     uintptr_t cbHook, MessageParcel &dataParcel, MessageParcel &reply)
306 {
307     if ((objIdx < 0) || (objIdx >= MAX_CBSTUB_SIZE) || (!g_cbStub[objIdx].inUse)) {
308         LOGW("nothing to do, callback id %d, remote object id %d", callbackId, objIdx);
309         return;
310     }
311     MessageOption option(MessageOption::TF_SYNC);
312     option.SetWaitTime(DEV_AUTH_CALL_WAIT_TIME);
313     if (!sync) {
314         option.SetFlags(MessageOption::TF_ASYNC);
315         option.SetWaitTime(0);
316     }
317     std::lock_guard<std::mutex> autoLock(g_cBMutex);
318     sptr<ICommIpcCallback> proxy = iface_cast<ICommIpcCallback>(g_cbStub[objIdx].cbStub);
319     proxy->DoCallBack(callbackId, cbHook, dataParcel, reply, option);
320     return;
321 }
322 
DevAuthDeathRecipient(int32_t cbIdx)323 DevAuthDeathRecipient::DevAuthDeathRecipient(int32_t cbIdx)
324 {
325     callbackIdx = cbIdx;
326 }
327 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)328 void DevAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
329 {
330     LOGI("remote is not actively, to reset local resource");
331     ResetIpcCallBackNodeByNodeId(callbackIdx);
332 }
333 }
334