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 "dbinder_databus_invoker.h"
17
18 #include <cinttypes>
19 #include "string_ex.h"
20 #include "securec.h"
21 #include "sys_binder.h"
22
23 #include "databus_socket_listener.h"
24 #include "dbinder_error_code.h"
25 #include "ipc_object_stub.h"
26 #include "ipc_object_proxy.h"
27 #include "ipc_process_skeleton.h"
28 #include "ipc_thread_skeleton.h"
29 #include "ipc_debug.h"
30 #include "log_tags.h"
31
32 namespace OHOS {
33 using namespace OHOS::HiviewDFX;
DBinderDatabusInvoker()34 DBinderDatabusInvoker::DBinderDatabusInvoker()
35 : stopWorkThread_(false), callerPid_(getpid()), callerUid_(getuid()), callerDeviceID_(""),
36 callerTokenID_(0), firstTokenID_(0), status_(0)
37 {
38 }
39
~DBinderDatabusInvoker()40 DBinderDatabusInvoker::~DBinderDatabusInvoker()
41 {
42 }
43
AcquireHandle(int32_t handle)44 bool DBinderDatabusInvoker::AcquireHandle(int32_t handle)
45 {
46 ZLOGI(LOG_LABEL, "handle:%{public}d", handle);
47 return true;
48 }
49
ReleaseHandle(int32_t handle)50 bool DBinderDatabusInvoker::ReleaseHandle(int32_t handle)
51 {
52 ZLOGI(LOG_LABEL, "handle:%{public}d", handle);
53 return true;
54 }
55
NewSessionOfBinderProxy(uint32_t handle,std::shared_ptr<DBinderSessionObject> session)56 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::NewSessionOfBinderProxy(uint32_t handle,
57 std::shared_ptr<DBinderSessionObject> session)
58 {
59 if (session == nullptr) {
60 ZLOGE(LOG_LABEL, "remote session is nullptr");
61 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_ERR_INVALID_DATA, __FUNCTION__);
62 return nullptr;
63 }
64
65 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
66 if (current == nullptr) {
67 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
68 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_IPC_PROCESS_SKELETON_NULL, __FUNCTION__);
69 return nullptr;
70 }
71 sptr<IPCObjectProxy> ipcProxy = reinterpret_cast<IPCObjectProxy *>(current->FindOrNewObject(handle).GetRefPtr());
72 if (ipcProxy == nullptr) {
73 ZLOGE(LOG_LABEL, "attempt to send a invalid handle:%{public}u", handle);
74 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_SEND_INVALID_HANDLE, __FUNCTION__);
75 return nullptr;
76 }
77
78 if (ipcProxy->GetProto() != IRemoteObject::IF_PROT_BINDER) {
79 ZLOGE(LOG_LABEL, "attempt to send a distributed proxy, handle:%{public}u", handle);
80 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_SEND_DISTRIBUTED_PROXY, __FUNCTION__);
81 return nullptr;
82 }
83
84 std::string localDeviceID = current->GetLocalDeviceID();
85 if (localDeviceID.empty()) {
86 ZLOGE(LOG_LABEL, "get localDeviceID error, handle:%{public}u", handle);
87 }
88
89 return GetSessionForProxy(ipcProxy, session, localDeviceID);
90 }
91
GetSessionForProxy(sptr<IPCObjectProxy> ipcProxy,std::shared_ptr<DBinderSessionObject> session,const std::string & localDeviceID)92 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::GetSessionForProxy(sptr<IPCObjectProxy> ipcProxy,
93 std::shared_ptr<DBinderSessionObject> session, const std::string &localDeviceID)
94 {
95 uint32_t handle = ipcProxy->GetHandle();
96 std::string sessionName = ipcProxy->GetSessionName();
97 if (sessionName.empty()) {
98 ZLOGE(LOG_LABEL, "get bus name error, handle:%{public}u", handle);
99 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_GET_SESSION_NAME_FAIL, __FUNCTION__);
100 return nullptr;
101 }
102 MessageParcel data;
103 MessageParcel reply;
104 if (!data.WriteUint32(IRemoteObject::DATABUS_TYPE) || !data.WriteString(localDeviceID) ||
105 !data.WriteUint32(session->GetPeerPid()) || !data.WriteUint32(session->GetPeerUid()) ||
106 !data.WriteString(session->GetDeviceId()) || !data.WriteString(sessionName) ||
107 !data.WriteUint32(session->GetTokenId())) {
108 ZLOGE(LOG_LABEL, "write to parcel fail, handle:%{public}u", handle);
109 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_WRITE_TO_PARCEL_FAIL, __FUNCTION__);
110 return nullptr;
111 }
112 int err = ipcProxy->InvokeListenThread(data, reply);
113 if (err != ERR_NONE) {
114 ZLOGE(LOG_LABEL, "start service listen error:%{public}d handle:%{public}u", err, handle);
115 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_START_LISTEN_FAIL, __FUNCTION__);
116 return nullptr;
117 }
118
119 uint64_t stubIndex = reply.ReadUint64();
120 if (stubIndex == 0) {
121 ZLOGE(LOG_LABEL, "stubindex error:%{public}" PRIu64 " handle:%{public}u", stubIndex, handle);
122 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_STUB_INVALID, __FUNCTION__);
123 return nullptr;
124 }
125 std::string serverName = reply.ReadString();
126 std::string deviceId = reply.ReadString();
127 uint32_t peerTokenId = reply.ReadUint32();
128
129 ZLOGI(LOG_LABEL, "serverName:%{public}s stubIndex:%{public}" PRIu64 " peerTokenId:%{public}u deviceId:%{public}s",
130 serverName.c_str(), stubIndex, peerTokenId, IPCProcessSkeleton::ConvertToSecureString(deviceId).c_str());
131 std::shared_ptr<DBinderSessionObject> connectSession =
132 std::make_shared<DBinderSessionObject>(serverName, deviceId, stubIndex, nullptr, peerTokenId);
133 if (connectSession == nullptr) {
134 ZLOGE(LOG_LABEL, "new server session fail, handle:%{public}u", handle);
135 DfxReportFailHandleEvent(DbinderErrorCode::RPC_DRIVER, handle, RADAR_NEW_SERVER_SESSION_FAIL, __FUNCTION__);
136 return nullptr;
137 }
138
139 return connectSession;
140 }
141
AuthSession2Proxy(uint32_t handle,const std::shared_ptr<DBinderSessionObject> session)142 bool DBinderDatabusInvoker::AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> session)
143 {
144 if (session == nullptr) {
145 ZLOGE(LOG_LABEL, "remote session is nullptr, handle:%{public}u", handle);
146 return false;
147 }
148
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option;
152
153 if (!data.WriteUint32(session->GetPeerPid()) || !data.WriteUint32(session->GetPeerUid()) ||
154 !data.WriteString(session->GetDeviceId()) || !data.WriteUint64(session->GetStubIndex()) ||
155 !data.WriteUint32(session->GetTokenId())) {
156 ZLOGE(LOG_LABEL, "write to MessageParcel fail, handle:%{public}u", handle);
157 return false;
158 }
159
160 int err = SendRequest(handle, DBINDER_ADD_COMMAUTH, data, reply, option);
161 if (err != ERR_NONE) {
162 ZLOGE(LOG_LABEL, "send auth info to remote fail, handle:%{public}u", handle);
163 return false;
164 }
165 return true;
166 }
167
QuerySessionOfBinderProxy(uint32_t handle,std::shared_ptr<DBinderSessionObject> session)168 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::QuerySessionOfBinderProxy(uint32_t handle,
169 std::shared_ptr<DBinderSessionObject> session)
170 {
171 if (AuthSession2Proxy(handle, session) != true) {
172 ZLOGE(LOG_LABEL, "auth handle:%{public}u to socketId failed, socketId:%{public}d", handle,
173 session->GetSocketId());
174 return nullptr;
175 }
176 return QueryServerSessionObject(handle);
177 }
178
QueryClientSessionObject(uint32_t databusHandle)179 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::QueryClientSessionObject(uint32_t databusHandle)
180 {
181 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
182 if (current == nullptr) {
183 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
184 return nullptr;
185 }
186 std::shared_ptr<DBinderSessionObject> sessionOfPeer = current->StubQueryDBinderSession(databusHandle);
187 if (sessionOfPeer == nullptr) {
188 ZLOGE(LOG_LABEL, "no session attach to this proxy:%{public}u", databusHandle);
189 return nullptr;
190 }
191 ZLOGI(LOG_LABEL, "socketId:%{public}d", sessionOfPeer->GetSocketId());
192 return sessionOfPeer;
193 }
194
QueryServerSessionObject(uint32_t handle)195 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::QueryServerSessionObject(uint32_t handle)
196 {
197 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
198 if (current == nullptr) {
199 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
200 return nullptr;
201 }
202
203 std::shared_ptr<DBinderSessionObject> sessionOfPeer = current->ProxyQueryDBinderSession(handle);
204 if (sessionOfPeer == nullptr) {
205 ZLOGI(LOG_LABEL, "no session attach to this handle:%{public}u", handle);
206 return nullptr;
207 }
208
209 return sessionOfPeer;
210 }
211
OnReceiveNewConnection(int32_t socketId,int peerPid,int peerUid,std::string peerName,std::string networkId)212 bool DBinderDatabusInvoker::OnReceiveNewConnection(int32_t socketId, int peerPid, int peerUid,
213 std::string peerName, std::string networkId)
214 {
215 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
216 if (current == nullptr) {
217 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
218 return false;
219 }
220
221 AppAuthInfo appAuthInfo = { peerPid, peerUid, 0, socketId, 0, nullptr, networkId };
222 if (!current->QueryCommAuthInfo(appAuthInfo)) {
223 ZLOGE(LOG_LABEL, "remote device is not auth, socket:%{public}d, peerName:%{public}s",
224 socketId, peerName.c_str());
225 return false;
226 }
227 uint32_t peerTokenId = appAuthInfo.tokenId;
228 uint32_t oldTokenId = 0;
229 if (current->StubDetachDBinderSession(socketId, oldTokenId)) {
230 ZLOGI(LOG_LABEL, "delete left socketId:%{public}d device:%{public}s oldTokenId:%{public}u", socketId,
231 IPCProcessSkeleton::ConvertToSecureString(networkId).c_str(), oldTokenId);
232 }
233 std::shared_ptr<DBinderSessionObject> sessionObject = std::make_shared<DBinderSessionObject>(
234 peerName, networkId, 0, nullptr, peerTokenId);
235 sessionObject->SetSocketId(socketId);
236 sessionObject->SetPeerPid(peerPid);
237 sessionObject->SetPeerUid(peerUid);
238 if (!current->StubAttachDBinderSession(socketId, sessionObject)) {
239 ZLOGE(LOG_LABEL, "attach session to process skeleton failed, socketId:%{public}d", socketId);
240 return false;
241 }
242 ZLOGI(LOG_LABEL, "pid:%{public}u uid:%{public}u deviceId:%{public}s tokenId:%{public}u "
243 "oldTokenId:%{public}u socketId:%{public}d", peerPid, peerUid,
244 IPCProcessSkeleton::ConvertToSecureString(networkId).c_str(),
245 peerTokenId, oldTokenId, socketId);
246 // update socketId
247 current->AttachOrUpdateAppAuthInfo(appAuthInfo);
248 return true;
249 }
250
CreateProcessThread()251 bool DBinderDatabusInvoker::CreateProcessThread()
252 {
253 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
254 if (current == nullptr) {
255 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
256 return false;
257 }
258 /* epoll thread obtained one thread, so idle thread must more than 1 */
259 if (current->GetSocketIdleThreadNum() > 0) {
260 current->SpawnThread(IPCWorkThread::PROCESS_PASSIVE, IRemoteObject::IF_PROT_DATABUS);
261 ZLOGI(LOG_LABEL, "success");
262 return true;
263 }
264
265 ZLOGE(LOG_LABEL, "failed, no idle socket thread left");
266 return false;
267 }
268
OnRawDataAvailable(int32_t socketId,const char * data,uint32_t dataSize)269 void DBinderDatabusInvoker::OnRawDataAvailable(int32_t socketId, const char *data, uint32_t dataSize)
270 {
271 ZLOGI(LOG_LABEL, "socketId:%{public}d, size:%{public}u", socketId, dataSize);
272 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
273 if (current == nullptr) {
274 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
275 return;
276 }
277
278 uint32_t rawDataSize = dataSize - sizeof(dbinder_transaction_data);
279 if (rawDataSize > 0 && rawDataSize <= MAX_RAWDATA_SIZE - sizeof(dbinder_transaction_data)) {
280 std::shared_ptr<InvokerRawData> invokerRawData = std::make_shared<InvokerRawData>(rawDataSize);
281 if (memcpy_s(invokerRawData->GetData().get(), rawDataSize, data + sizeof(dbinder_transaction_data),
282 rawDataSize) != EOK) {
283 ZLOGE(LOG_LABEL, "memcpy_s failed , size:%{public}u", rawDataSize);
284 return;
285 }
286 if (!current->AttachRawData(socketId, invokerRawData)) {
287 return;
288 }
289 }
290 return;
291 }
292
293 /*
294 * Write 64K=SOCKET_BUFF_SIZE
295 * +------------------+-------------------------+----------------|
296 * |0<---processed--->|Read <----need process-->|<--idle buffer-->
297 * -cursor
298 *
299 * when idle buffer less 1k, move need process to buffer head, then update R/W cursor
300 * when idle buffer can not put a full package, also move need process package to buffer head
301 */
OnMessageAvailable(int32_t socketId,const char * data,ssize_t len)302 void DBinderDatabusInvoker::OnMessageAvailable(int32_t socketId, const char *data, ssize_t len)
303 {
304 if (socketId <= 0 || data == nullptr || len > static_cast<ssize_t>(MAX_RAWDATA_SIZE) ||
305 len < static_cast<ssize_t>(sizeof(dbinder_transaction_data))) {
306 ZLOGE(LOG_LABEL, "wrong inputs, data length:%{public}zd(expected size:%{public}zu) "
307 " socketId:%{public}d", len, sizeof(dbinder_transaction_data), socketId);
308 return;
309 }
310
311 uint32_t packageSize = HasRawDataPackage(data, len);
312 if (packageSize > 0) {
313 // Only one set of big data can be transferred at a time.
314 return OnRawDataAvailable(socketId, data, packageSize);
315 }
316
317 uint32_t readSize = 0;
318 do {
319 packageSize = HasCompletePackage(data, readSize, len);
320 if (packageSize > 0) {
321 StartProcessLoop(socketId, data + readSize, packageSize);
322 readSize += packageSize;
323 } else {
324 // If the current is abnormal, the subsequent is no longer processed.
325 ZLOGE(LOG_LABEL, "not complete message, socketId:%{public}d", socketId);
326 break;
327 }
328 } while (readSize + sizeof(dbinder_transaction_data) < static_cast<uint32_t>(len));
329 }
330
OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer)331 int DBinderDatabusInvoker::OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer)
332 {
333 if (sessionOfPeer == nullptr) {
334 ZLOGE(LOG_LABEL, "sessionOfPeer is null");
335 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
336 }
337
338 int32_t socketId = sessionOfPeer->GetSocketId();
339 if (socketId <= 0) {
340 ZLOGE(LOG_LABEL, "socket id is invalid");
341 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
342 }
343
344 std::shared_ptr<BufferObject> sessionBuff = sessionOfPeer->GetSessionBuff();
345 if (sessionBuff == nullptr) {
346 ZLOGE(LOG_LABEL, "databus session buff is null");
347 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
348 }
349
350 return SendData(sessionBuff, socketId);
351 }
352
SendData(std::shared_ptr<BufferObject> sessionBuff,int32_t socketId)353 int DBinderDatabusInvoker::SendData(std::shared_ptr<BufferObject> sessionBuff, int32_t socketId)
354 {
355 char *sendBuffer = sessionBuff->GetSendBufferAndLock(SOCKET_DEFAULT_BUFF_SIZE);
356 /* session buffer contain mutex, need release mutex */
357 if (sendBuffer == nullptr) {
358 ZLOGE(LOG_LABEL, "buffer alloc failed in session");
359 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
360 }
361 sessionBuff->UpdateSendBuffer(0); // 0 means do not expand buffer when send it
362 ssize_t writeCursor = sessionBuff->GetSendBufferWriteCursor();
363 ssize_t readCursor = sessionBuff->GetSendBufferReadCursor();
364 if (writeCursor < readCursor) {
365 ZLOGE(LOG_LABEL, "no data to send, write cursor:%{public}zu, read cursor:%{public}zu",
366 writeCursor, readCursor);
367 sessionBuff->ReleaseSendBufferLock();
368 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
369 }
370 if (writeCursor == readCursor) {
371 ZLOGE(LOG_LABEL, "no data to send, write cursor:%{public}zu, read cursor:%{public}zu",
372 writeCursor, readCursor);
373 sessionBuff->ReleaseSendBufferLock();
374 return ERR_NONE;
375 }
376 ssize_t size = writeCursor - readCursor;
377
378 int32_t ret = DBinderSoftbusClient::GetInstance().SendBytes(
379 socketId, static_cast<const void *>(sendBuffer + readCursor), size);
380 if (ret != 0) {
381 ZLOGE(LOG_LABEL, "SendBytes fail, ret:%{public}d seq:%{public}" PRIu64
382 " size:%{public}zd, socketId:%{public}d", ret, seqNumber_, size, socketId);
383 DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_SEND_BYTES_FAIL, __FUNCTION__);
384 sessionBuff->ReleaseSendBufferLock();
385 return ret;
386 }
387
388 readCursor += size;
389 sessionBuff->SetSendBufferReadCursor(readCursor);
390 sessionBuff->SetSendBufferWriteCursor(writeCursor);
391 ZLOGI(LOG_LABEL, "succ, seq:%{public}" PRIu64 " size:%{public}zd, socketId:%{public}d",
392 seqNumber_, size, socketId);
393
394 sessionBuff->ReleaseSendBufferLock();
395 return ret;
396 }
397
OnSendRawData(std::shared_ptr<DBinderSessionObject> session,const void * data,size_t size)398 int DBinderDatabusInvoker::OnSendRawData(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size)
399 {
400 if (session == nullptr) {
401 ZLOGE(LOG_LABEL, "sessionOfPeer is null");
402 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
403 }
404
405 int32_t socketId = session->GetSocketId();
406 if (socketId <= 0) {
407 ZLOGE(LOG_LABEL, "socketId is invalid");
408 return -RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
409 }
410
411 int ret = DBinderSoftbusClient::GetInstance().SendBytes(socketId, data, size);
412 if (ret != 0) {
413 ZLOGE(LOG_LABEL, "fail, ret:%{public}d seq:%{public}" PRIu64 " size:%{public}zu socketId:%{public}d",
414 ret, seqNumber_, size, socketId);
415 return ret;
416 }
417
418 ZLOGI(LOG_LABEL, "succ, seq:%{public}" PRIu64 " size:%{public}zu, socketId:%{public}d",
419 seqNumber_, size, socketId);
420
421 return ret;
422 }
423
JoinThread(bool initiative)424 void DBinderDatabusInvoker::JoinThread(bool initiative) {}
425
JoinProcessThread(bool initiative)426 void DBinderDatabusInvoker::JoinProcessThread(bool initiative)
427 {
428 std::thread::id threadId = std::this_thread::get_id();
429 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
430 if (current == nullptr) {
431 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
432 return;
433 }
434
435 std::shared_ptr<ThreadProcessInfo> processInfo = nullptr;
436 do {
437 current->AddDataThreadInWait(threadId);
438 while ((processInfo = current->PopDataInfoFromThread(threadId)) != nullptr) {
439 OnTransaction(processInfo);
440 processInfo = nullptr;
441 }
442 } while (!stopWorkThread_);
443 }
444
StopWorkThread()445 void DBinderDatabusInvoker::StopWorkThread()
446 {
447 stopWorkThread_ = true;
448 }
449
FlattenSession(char * sessionOffset,const std::shared_ptr<DBinderSessionObject> connectSession,uint32_t binderVersion)450 uint32_t DBinderDatabusInvoker::FlattenSession(char *sessionOffset,
451 const std::shared_ptr<DBinderSessionObject> connectSession, uint32_t binderVersion)
452 {
453 FlatDBinderSession *flatSession = reinterpret_cast<FlatDBinderSession *>(sessionOffset);
454 (void)memset_s(flatSession, sizeof(struct FlatDBinderSession), 0, sizeof(struct FlatDBinderSession));
455
456 flatSession->stubIndex = connectSession->GetStubIndex();
457 flatSession->version = binderVersion;
458 flatSession->magic = TOKENID_MAGIC;
459 flatSession->tokenId = connectSession->GetTokenId();
460 flatSession->deviceIdLength = connectSession->GetDeviceId().length();
461 if (flatSession->deviceIdLength == 0 || flatSession->deviceIdLength > DEVICEID_LENGTH) {
462 ZLOGE(LOG_LABEL, "wrong devices id length:%{public}u", flatSession->deviceIdLength);
463 return 0;
464 }
465 int memcpyResult = memcpy_s(flatSession->deviceId, DEVICEID_LENGTH, connectSession->GetDeviceId().data(),
466 flatSession->deviceIdLength);
467 if (memcpyResult != 0) {
468 ZLOGE(LOG_LABEL, "memcpy_s failed , devices id length:%{public}hu", flatSession->deviceIdLength);
469 return 0;
470 }
471 flatSession->deviceId[flatSession->deviceIdLength] = '\0';
472
473 flatSession->serviceNameLength = connectSession->GetServiceName().length();
474 if (flatSession->serviceNameLength == 0 || flatSession->serviceNameLength > SUPPORT_TOKENID_SERVICENAME_LENGTH) {
475 ZLOGE(LOG_LABEL, "wrong service name length:%{public}u", flatSession->serviceNameLength);
476 return 0;
477 }
478 memcpyResult = memcpy_s(flatSession->serviceName, SUPPORT_TOKENID_SERVICENAME_LENGTH,
479 connectSession->GetServiceName().data(), flatSession->serviceNameLength);
480 if (memcpyResult != 0) {
481 ZLOGE(LOG_LABEL, "memcpy_s failed , service name length:%{public}u", flatSession->serviceNameLength);
482 return 0;
483 }
484 flatSession->serviceName[flatSession->serviceNameLength] = '\0';
485
486 ZLOGI(LOG_LABEL, "serviceName:%{public}s stubIndex:%{public}" PRIu64 " tokenId:%{public}u",
487 flatSession->serviceName, flatSession->stubIndex, flatSession->tokenId);
488
489 return sizeof(struct FlatDBinderSession);
490 }
491
UnFlattenSession(char * sessionOffset,uint32_t binderVersion)492 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::UnFlattenSession(char *sessionOffset,
493 uint32_t binderVersion)
494 {
495 FlatDBinderSession *flatSession = reinterpret_cast<FlatDBinderSession *>(sessionOffset);
496 if (flatSession->stubIndex == 0) {
497 ZLOGE(LOG_LABEL, "stubIndex err");
498 return nullptr;
499 }
500
501 uint32_t tokenId = 0;
502 if (binderVersion >= SUPPORT_TOKENID_VERSION_NUM && flatSession->version >= SUPPORT_TOKENID_VERSION_NUM &&
503 flatSession->magic == TOKENID_MAGIC) {
504 tokenId = flatSession->tokenId;
505 }
506 /* makes sure end with a null terminator */
507 flatSession->deviceId[DEVICEID_LENGTH] = '\0';
508 flatSession->serviceName[SUPPORT_TOKENID_SERVICENAME_LENGTH] = '\0';
509 ZLOGI(LOG_LABEL, "serviceName:%{public}s stubIndex:%{public}" PRIu64 " tokenId:%{public}u",
510 flatSession->serviceName, flatSession->stubIndex, tokenId);
511
512 return std::make_shared<DBinderSessionObject>(
513 flatSession->serviceName, flatSession->deviceId, flatSession->stubIndex, nullptr, tokenId);
514 }
515
FlattenObject(Parcel & parcel,const IRemoteObject * object) const516 bool DBinderDatabusInvoker::FlattenObject(Parcel &parcel, const IRemoteObject *object) const
517 {
518 return true;
519 }
520
UnflattenObject(Parcel & parcel)521 sptr<IRemoteObject> DBinderDatabusInvoker::UnflattenObject(Parcel &parcel)
522 {
523 return nullptr;
524 }
525
ReadFileDescriptor(Parcel & parcel)526 int DBinderDatabusInvoker::ReadFileDescriptor(Parcel &parcel)
527 {
528 return -1;
529 }
530
WriteFileDescriptor(Parcel & parcel,int fd,bool takeOwnership)531 bool DBinderDatabusInvoker::WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership)
532 {
533 return true;
534 }
535
UpdateClientSession(std::shared_ptr<DBinderSessionObject> sessionObject)536 bool DBinderDatabusInvoker::UpdateClientSession(std::shared_ptr<DBinderSessionObject> sessionObject)
537 {
538 ZLOGI(LOG_LABEL, "enter");
539 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
540 if (current == nullptr) {
541 ZLOGE(LOG_LABEL, "current process skeleton is nullptr");
542 return false;
543 }
544
545 std::string ownName = current->GetDatabusName();
546 if (ownName.empty()) {
547 ZLOGE(LOG_LABEL, "fail to get session name");
548 return false;
549 }
550
551 std::shared_ptr<DatabusSocketListener> listener =
552 DelayedSingleton<DatabusSocketListener>::GetInstance();
553 if (listener == nullptr) {
554 ZLOGE(LOG_LABEL, "listener is nullptr");
555 return false;
556 }
557
558 int32_t socketId = listener->CreateClientSocket(ownName,
559 sessionObject->GetServiceName(), sessionObject->GetDeviceId());
560 if (socketId <= 0) {
561 ZLOGE(LOG_LABEL, "fail to creat client Socket");
562 return false;
563 }
564 std::string serviceName = sessionObject->GetServiceName();
565 std::string str = serviceName.substr(DBINDER_SOCKET_NAME_PREFIX.length());
566 std::string::size_type pos = str.find("_");
567 std::string peerUid = str.substr(0, pos);
568 std::string peerPid = str.substr(pos + 1);
569 sessionObject->SetSocketId(socketId);
570 sessionObject->SetPeerPid(std::stoi(peerPid));
571 sessionObject->SetPeerUid(std::stoi(peerUid));
572
573 ZLOGI(LOG_LABEL, "create socket succ, ownName:%{public}s peerName:%{public}s deviceId:%{public}s "
574 "socketId:%{public}d", ownName.c_str(), serviceName.c_str(),
575 IPCProcessSkeleton::ConvertToSecureString(sessionObject->GetDeviceId()).c_str(),
576 socketId);
577 return true;
578 }
579
OnDatabusSessionClientSideClosed(int32_t socketId)580 void DBinderDatabusInvoker::OnDatabusSessionClientSideClosed(int32_t socketId)
581 {
582 std::vector<uint32_t> proxyHandle;
583 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
584 if (current == nullptr) {
585 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
586 return;
587 }
588 if (!current->QueryProxyBySocketId(socketId, proxyHandle)) {
589 ZLOGE(LOG_LABEL, "session id:%{public}d is invalid", socketId);
590 return;
591 }
592 if (proxyHandle.empty()) {
593 ZLOGE(LOG_LABEL, "proxy handle is empty");
594 return;
595 }
596 for (auto it = proxyHandle.begin(); it != proxyHandle.end(); ++it) {
597 std::u16string descriptor = current->MakeHandleDescriptor(*it);
598 const std::string descStr8 = Str16ToStr8(descriptor);
599 sptr<IRemoteObject> remoteObject = current->QueryObject(descriptor);
600 if (remoteObject != nullptr) {
601 IPCObjectProxy *remoteProxy = reinterpret_cast<IPCObjectProxy *>(remoteObject.GetRefPtr());
602 // No need to close session again here. First erase session and then notify user session has been closed.
603 current->ProxyDetachDBinderSession(*it, remoteProxy);
604 if (remoteProxy->IsSubscribeDeathNotice()) {
605 ZLOGD(LOG_LABEL, "SendObituary begin, desc:%{public}s", descStr8.c_str());
606 remoteProxy->SendObituary();
607 ZLOGD(LOG_LABEL, "SendObituary end, desc:%{public}s", descStr8.c_str());
608 } else {
609 ZLOGW(LOG_LABEL, "desc:%{public}s does not subscribe death notice",
610 descStr8.c_str());
611 }
612 } else {
613 ZLOGE(LOG_LABEL, "cannot find proxy with desc:%{public}s", descStr8.c_str());
614 }
615 }
616 ZLOGI(LOG_LABEL, "close:%{public}d sussess", socketId);
617 return;
618 }
619
OnDatabusSessionServerSideClosed(int32_t socketId)620 void DBinderDatabusInvoker::OnDatabusSessionServerSideClosed(int32_t socketId)
621 {
622 uint32_t tokenId = 0;
623 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
624 if (current == nullptr) {
625 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
626 return;
627 }
628 bool ret = current->StubDetachDBinderSession(socketId, tokenId);
629 // detach info whose socketId equals the given one
630 std::list<uint64_t> stubIndexs = current->DetachAppAuthInfoBySocketId(socketId);
631 std::lock_guard<std::mutex> lockGuard(GetObjectMutex());
632 for (auto it = stubIndexs.begin(); it != stubIndexs.end(); it++) {
633 // note that we canont remove mapping from stub to index here because other session may still be used
634 IRemoteObject *stub = current->QueryStubByIndex(*it);
635 if (stub == nullptr) {
636 continue;
637 }
638 // a proxy doesn't refers this stub, we need to dec ref
639 stub->DecStrongRef(this);
640 }
641 ZLOGI(LOG_LABEL, "socketId:%{public}d ret:%{public}d", socketId, ret);
642 }
643
QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session)644 uint32_t DBinderDatabusInvoker::QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session)
645 {
646 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
647 if (current == nullptr) {
648 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
649 return 0;
650 }
651 return current->QueryHandleByDatabusSession(session->GetServiceName(), session->GetDeviceId(),
652 session->GetStubIndex());
653 }
654
GetSeqNum() const655 uint64_t DBinderDatabusInvoker::GetSeqNum() const
656 {
657 return seqNumber_;
658 }
659
SetSeqNum(uint64_t seq)660 void DBinderDatabusInvoker::SetSeqNum(uint64_t seq)
661 {
662 seqNumber_ = seq;
663 }
664
GetClientFd() const665 int32_t DBinderDatabusInvoker::GetClientFd() const
666 {
667 return clientFd_;
668 }
669
SetClientFd(int32_t fd)670 void DBinderDatabusInvoker::SetClientFd(int32_t fd)
671 {
672 clientFd_ = fd;
673 }
674
GetCallerSid() const675 std::string DBinderDatabusInvoker::GetCallerSid() const
676 {
677 return "";
678 }
679
GetCallerPid() const680 pid_t DBinderDatabusInvoker::GetCallerPid() const
681 {
682 return callerPid_;
683 }
684
GetCallerRealPid() const685 pid_t DBinderDatabusInvoker::GetCallerRealPid() const
686 {
687 return callerPid_;
688 }
689
SetStatus(uint32_t status)690 void DBinderDatabusInvoker::SetStatus(uint32_t status)
691 {
692 status_ = status;
693 }
694
GetStatus()695 uint32_t DBinderDatabusInvoker::GetStatus()
696 {
697 return status_;
698 }
699
SetCallerPid(pid_t pid)700 void DBinderDatabusInvoker::SetCallerPid(pid_t pid)
701 {
702 callerPid_ = pid;
703 }
704
GetCallerUid() const705 uid_t DBinderDatabusInvoker::GetCallerUid() const
706 {
707 return callerUid_;
708 }
709
SetCallerUid(pid_t uid)710 void DBinderDatabusInvoker::SetCallerUid(pid_t uid)
711 {
712 callerUid_ = uid;
713 }
714
GetCallerTokenID() const715 uint64_t DBinderDatabusInvoker::GetCallerTokenID() const
716 {
717 return callerTokenID_;
718 }
719
GetFirstCallerTokenID() const720 uint64_t DBinderDatabusInvoker::GetFirstCallerTokenID() const
721 {
722 return firstTokenID_;
723 }
724
GetSelfTokenID() const725 uint64_t DBinderDatabusInvoker::GetSelfTokenID() const
726 {
727 IRemoteInvoker *invoker = IPCThreadSkeleton::GetDefaultInvoker();
728 if (invoker != nullptr) {
729 return invoker->GetSelfTokenID();
730 }
731 return 0;
732 }
733
GetSelfFirstCallerTokenID() const734 uint64_t DBinderDatabusInvoker::GetSelfFirstCallerTokenID() const
735 {
736 IRemoteInvoker *invoker = IPCThreadSkeleton::GetDefaultInvoker();
737 if (invoker != nullptr) {
738 return invoker->GetSelfFirstCallerTokenID();
739 }
740 return 0;
741 }
742
SetCallerDeviceID(const std::string & deviceId)743 void DBinderDatabusInvoker::SetCallerDeviceID(const std::string &deviceId)
744 {
745 callerDeviceID_ = deviceId;
746 }
747
SetCallerTokenID(const uint32_t tokenId)748 void DBinderDatabusInvoker::SetCallerTokenID(const uint32_t tokenId)
749 {
750 callerTokenID_ = tokenId;
751 firstTokenID_ = tokenId;
752 }
753
IsLocalCalling()754 bool DBinderDatabusInvoker::IsLocalCalling()
755 {
756 return false;
757 }
758
CheckAndSetCallerInfo(int32_t socketId,uint64_t stubIndex)759 int DBinderDatabusInvoker::CheckAndSetCallerInfo(int32_t socketId, uint64_t stubIndex)
760 {
761 std::shared_ptr<DBinderSessionObject> session = QueryClientSessionObject(socketId);
762 if (session == nullptr) {
763 ZLOGE(LOG_LABEL, "get databus session fail");
764 return RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
765 }
766
767 int pid = static_cast<int>(session->GetPeerPid());
768 int uid = static_cast<int>(session->GetPeerUid());
769 std::string deviceId = session->GetDeviceId();
770 if (uid < 0 || deviceId.length() > DEVICEID_LENGTH) {
771 ZLOGE(LOG_LABEL, "user id and device id error");
772 return RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
773 }
774
775 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
776 if (current == nullptr) {
777 ZLOGE(LOG_LABEL, "current process skeleton is nullptr");
778 return IPC_SKELETON_ERR;
779 }
780 uint32_t callerTokenId = session->GetTokenId();
781 AppAuthInfo appAuthInfo = { pid, uid, callerTokenId, socketId, stubIndex, nullptr, deviceId };
782 if (current->QueryAppInfoToStubIndex(appAuthInfo) == false) {
783 ZLOGE(LOG_LABEL, "stubIndex:%{public}" PRIu64 " is NOT belong to caller, pid:%{public}d uid:%{public}d"
784 " deviceId:%{public}s socketId:%{public}d callerTokenId:%{public}u", stubIndex, pid, uid,
785 IPCProcessSkeleton::ConvertToSecureString(deviceId).c_str(), socketId, callerTokenId);
786 return RPC_DATABUS_INVOKER_INVALID_STUB_INDEX;
787 }
788 callerPid_ = pid;
789 callerUid_ = uid;
790 callerDeviceID_ = deviceId;
791 clientFd_ = socketId;
792 callerTokenID_ = callerTokenId;
793 firstTokenID_ = callerTokenId;
794 return ERR_NONE;
795 }
796
GetLocalDeviceID()797 std::string DBinderDatabusInvoker::GetLocalDeviceID()
798 {
799 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
800 if (current == nullptr) {
801 ZLOGE(LOG_LABEL, "current process skeleton is nullptr");
802 return "";
803 }
804
805 return current->GetLocalDeviceID();
806 }
807
GetCallerDeviceID() const808 std::string DBinderDatabusInvoker::GetCallerDeviceID() const
809 {
810 return callerDeviceID_;
811 }
812
MakeStubIndexByRemoteObject(IRemoteObject * stubObject)813 uint64_t DBinderDatabusInvoker::MakeStubIndexByRemoteObject(IRemoteObject *stubObject)
814 {
815 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
816 if (current == nullptr) {
817 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
818 return 0;
819 }
820
821 if (!current->IsContainsObject(stubObject)) {
822 ZLOGE(LOG_LABEL, "fail to find stub");
823 return 0;
824 }
825
826 uint64_t stubIndex = current->AddStubByIndex(stubObject);
827 if (!stubIndex) {
828 ZLOGE(LOG_LABEL, "fail to add stub");
829 return 0;
830 }
831 return stubIndex;
832 }
833
MakeDefaultServerSessionObject(uint64_t stubIndex,const std::shared_ptr<DBinderSessionObject> sessionObject)834 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::MakeDefaultServerSessionObject(uint64_t stubIndex,
835 const std::shared_ptr<DBinderSessionObject> sessionObject)
836 {
837 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
838 if (current == nullptr) {
839 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
840 return nullptr;
841 }
842 std::string serviceName = current->GetDatabusName();
843 std::string deviceId = current->GetLocalDeviceID();
844 if (serviceName.empty() || deviceId.empty()) {
845 ZLOGE(LOG_LABEL, "fail to get databus name:%{public}s or deviceId length:%{public}zu",
846 serviceName.c_str(), deviceId.length());
847 return nullptr;
848 }
849
850 auto session = std::make_shared<DBinderSessionObject>(serviceName, deviceId, stubIndex, nullptr,
851 sessionObject->GetTokenId());
852 if (session == nullptr) {
853 ZLOGE(LOG_LABEL, "new server session fail, service:%{public}s deviceId:%{public}s stubIndex:%{public}" PRIu64,
854 serviceName.c_str(), IPCProcessSkeleton::ConvertToSecureString(deviceId).c_str(), stubIndex);
855 return nullptr;
856 }
857 return session;
858 }
859
ConnectRemoteObject2Session(IRemoteObject * stubObject,uint64_t stubIndex,const std::shared_ptr<DBinderSessionObject> sessionObject)860 bool DBinderDatabusInvoker::ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex,
861 const std::shared_ptr<DBinderSessionObject> sessionObject)
862 {
863 if (sessionObject == nullptr) {
864 ZLOGE(LOG_LABEL, "session object is nullptr");
865 return false;
866 }
867 IPCProcessSkeleton *current = IPCProcessSkeleton::GetCurrent();
868 if (current == nullptr) {
869 ZLOGE(LOG_LABEL, "IPCProcessSkeleton is nullptr");
870 return false;
871 }
872
873 int peerPid = sessionObject->GetPeerPid();
874 int peerUid = sessionObject->GetPeerUid();
875 uint32_t tokenId = sessionObject->GetTokenId();
876 std::string deviceId = sessionObject->GetDeviceId();
877 ZLOGI(LOG_LABEL, "pid:%{public}d uid:%{public}d deviceId:%{public}s tokenId:%{public}u "
878 "stubIndex:%{public}" PRIu64, peerPid, peerUid, IPCProcessSkeleton::ConvertToSecureString(deviceId).c_str(),
879 tokenId, stubIndex);
880 // mark socketId as 0
881 AppAuthInfo appAuthInfo = { peerPid, peerUid, tokenId, 0, stubIndex, stubObject, deviceId };
882 if (current->AttachOrUpdateAppAuthInfo(appAuthInfo)) {
883 // first time send this stub to proxy indicating by deviceId, pid, uid
884 stubObject->IncStrongRef(this);
885 }
886 return true;
887 }
888
CreateServerSessionObject(binder_uintptr_t binder,std::shared_ptr<DBinderSessionObject> sessionObject)889 std::shared_ptr<DBinderSessionObject> DBinderDatabusInvoker::CreateServerSessionObject(binder_uintptr_t binder,
890 std::shared_ptr<DBinderSessionObject> sessionObject)
891 {
892 IRemoteObject *stubObject = reinterpret_cast<IPCObjectStub *>(binder);
893 if (stubObject == nullptr) {
894 ZLOGE(LOG_LABEL, "binder is nullptr");
895 return nullptr;
896 }
897
898 uint64_t stubIndex = MakeStubIndexByRemoteObject(stubObject);
899 if (stubIndex == 0) {
900 ZLOGE(LOG_LABEL, "fail to add stub");
901 return nullptr;
902 }
903 if (ConnectRemoteObject2Session(stubObject, stubIndex, sessionObject) != true) {
904 ZLOGE(LOG_LABEL, "fail to connect stub to session, stubIndex:%{public}" PRIu64, stubIndex);
905 return nullptr;
906 }
907 return MakeDefaultServerSessionObject(stubIndex, sessionObject);
908 }
909
FlushCommands(IRemoteObject * object)910 int DBinderDatabusInvoker::FlushCommands(IRemoteObject *object)
911 {
912 if (object == nullptr || !object->IsProxyObject()) {
913 ZLOGE(LOG_LABEL, "proxy is invalid");
914 return RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
915 }
916
917 IPCObjectProxy *proxy = reinterpret_cast<IPCObjectProxy *>(object);
918
919 std::shared_ptr<DBinderSessionObject> session = QueryServerSessionObject(proxy->GetHandle());
920 if (session == nullptr) {
921 ZLOGE(LOG_LABEL, "session is nullptr");
922 return RPC_DATABUS_INVOKER_INVALID_DATA_ERR;
923 }
924
925 (void)OnSendMessage(session);
926 return ERR_NONE;
927 }
928
ResetCallingIdentity()929 std::string DBinderDatabusInvoker::ResetCallingIdentity()
930 {
931 std::string token = std::to_string(((static_cast<uint64_t>(callerUid_) << PID_LEN)
932 | static_cast<uint64_t>(callerPid_)));
933 std::string identity = callerDeviceID_ + token;
934 char buf[ACCESS_TOKEN_MAX_LEN + 1] = {0};
935 int ret = sprintf_s(buf, ACCESS_TOKEN_MAX_LEN + 1, "%010" PRIu64, callerTokenID_);
936 if (ret < 0) {
937 ZLOGE(LOG_LABEL, "sprintf callerTokenID:%{public}" PRIu64 " failed", callerTokenID_);
938 return "";
939 }
940 std::string accessToken(buf);
941 callerUid_ = (pid_t)getuid();
942 callerPid_ = getpid();
943 callerDeviceID_ = GetLocalDeviceID();
944 callerTokenID_ = GetSelfTokenID();
945 return accessToken + identity;
946 }
947
SetCallingIdentity(std::string & identity,bool flag)948 bool DBinderDatabusInvoker::SetCallingIdentity(std::string &identity, bool flag)
949 {
950 (void)flag;
951 if (identity.empty() || identity.length() <= DEVICEID_LENGTH) {
952 return false;
953 }
954 std::string tokenIdStr;
955 if (!ProcessSkeleton::GetSubStr(identity, tokenIdStr, 0, ACCESS_TOKEN_MAX_LEN) ||
956 !ProcessSkeleton::IsNumStr(tokenIdStr)) {
957 ZLOGE(LOG_LABEL, "Identity param tokenId is invalid");
958 return false;
959 }
960 std::string deviceId;
961 if (!ProcessSkeleton::GetSubStr(identity, deviceId, ACCESS_TOKEN_MAX_LEN, DEVICEID_LENGTH)) {
962 ZLOGE(LOG_LABEL, "Identity param deviceId is invalid");
963 return false;
964 }
965 std::string tokenStr;
966 size_t offset = ACCESS_TOKEN_MAX_LEN + DEVICEID_LENGTH;
967 if (identity.length() <= offset) {
968 ZLOGE(LOG_LABEL, "Identity param no token, len:%{public}zu, offset:%{public}zu", identity.length(), offset);
969 return false;
970 }
971 size_t subLen = identity.length() - offset;
972 if (!ProcessSkeleton::GetSubStr(identity, tokenStr, offset, subLen) || !ProcessSkeleton::IsNumStr(tokenStr)) {
973 ZLOGE(LOG_LABEL, "Identity param token is invalid");
974 return false;
975 }
976 uint64_t tokenId = std::stoull(tokenIdStr.c_str());
977 uint64_t token = std::stoull(tokenStr.c_str());
978 callerUid_ = static_cast<int>(token >> PID_LEN);
979 callerPid_ = static_cast<int>(token);
980 callerDeviceID_ = deviceId;
981 callerTokenID_ = tokenId;
982
983 return true;
984 }
985
TranslateIRemoteObject(int32_t cmd,const sptr<IRemoteObject> & obj)986 int DBinderDatabusInvoker::TranslateIRemoteObject(int32_t cmd, const sptr<IRemoteObject> &obj)
987 {
988 return -IPC_INVOKER_TRANSLATE_ERR;
989 }
990
HasRawDataPackage(const char * data,ssize_t len)991 uint32_t DBinderDatabusInvoker::HasRawDataPackage(const char *data, ssize_t len)
992 {
993 const dbinder_transaction_data *tr = reinterpret_cast<const dbinder_transaction_data *>(data);
994 if ((tr->magic == DBINDER_MAGICWORD) && (tr->cmd == BC_SEND_RAWDATA) &&
995 (tr->sizeOfSelf == static_cast<uint32_t>(len))) {
996 if (tr->sizeOfSelf > MAX_RAWDATA_SIZE) {
997 return MAX_RAWDATA_SIZE;
998 }
999 return tr->sizeOfSelf;
1000 }
1001 return 0;
1002 }
1003
HasCompletePackage(const char * data,uint32_t readCursor,ssize_t len)1004 uint32_t DBinderDatabusInvoker::HasCompletePackage(const char *data, uint32_t readCursor, ssize_t len)
1005 {
1006 const dbinder_transaction_data *tr = reinterpret_cast<const dbinder_transaction_data *>(data + readCursor);
1007 if ((tr->magic == DBINDER_MAGICWORD) &&
1008 (tr->sizeOfSelf <= SOCKET_MAX_BUFF_SIZE + sizeof(dbinder_transaction_data)) &&
1009 (readCursor + tr->sizeOfSelf <= static_cast<uint32_t>(len)) && CheckTransactionData(tr)) {
1010 return tr->sizeOfSelf;
1011 }
1012 return 0;
1013 }
1014 } // namespace OHOS
1015