From 8a0afd30c3ba4a7db6d646edfcd93f4d20453aff Mon Sep 17 00:00:00 2001 From: jason Date: Wed, 15 May 2024 12:06:33 +0000 Subject: [PATCH] fix: rectify the large function of SendEntryToRemote Signed-off-by: jason --- .../libdbinder/include/dbinder_service.h | 5 ++ .../dbinder_service/src/dbinder_service.cpp | 82 +++++++++++++------ 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/interfaces/innerkits/libdbinder/include/dbinder_service.h b/interfaces/innerkits/libdbinder/include/dbinder_service.h index 3b909b7f..c8fce9b2 100644 --- a/interfaces/innerkits/libdbinder/include/dbinder_service.h +++ b/interfaces/innerkits/libdbinder/include/dbinder_service.h @@ -319,6 +319,11 @@ private: bool DetachSessionObject(binder_uintptr_t stub); bool AttachSessionObject(std::shared_ptr object, binder_uintptr_t stub); sptr FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId); + bool CheckDeviceIDsInvalid(const std::string &deviceID, const std::string &localDevID); + bool CopyDeviceIDsToMessage(std::shared_ptr message, + const std::string &localDevID, const std::string &deviceID); + std::shared_ptr CreateMessage(const sptr &stub, uint32_t seqNumber, + uint32_t pid, uint32_t uid); bool SendEntryToRemote(const sptr stub, uint32_t seqNumber, uint32_t pid, uint32_t uid); uint16_t AllocFreeSocketPort(); std::string GetLocalDeviceID(); diff --git a/services/dbinder/dbinder_service/src/dbinder_service.cpp b/services/dbinder/dbinder_service/src/dbinder_service.cpp index 0c0c23c3..030fb479 100644 --- a/services/dbinder/dbinder_service/src/dbinder_service.cpp +++ b/services/dbinder/dbinder_service/src/dbinder_service.cpp @@ -364,45 +364,79 @@ sptr DBinderService::MakeRemoteBinder(const std::u16string & return dBinderServiceStub; } +bool DBinderService::CheckDeviceIDsInvalid(const std::string &deviceID, const std::string &localDevID) +{ + if (IsDeviceIdIllegal(deviceID) || IsDeviceIdIllegal(localDevID)) { + DBINDER_LOGE(LOG_LABEL, "wrong device id length, remote:%{public}zu local:%{public}zu", + deviceID.length(), localDevID.length()); + return true; + } + return false; +} + +bool DBinderService::CopyDeviceIDsToMessage(std::shared_ptr message, + const std::string &localDevID, const std::string &deviceID) +{ + if (memcpy_s(message->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, localDevID.data(), localDevID.length()) != 0 || + memcpy_s(message->deviceIdInfo.toDeviceId, DEVICEID_LENGTH, deviceID.data(), deviceID.length()) != 0) { + DBINDER_LOGE(LOG_LABEL, "fail to copy memory, service:%{public}" PRIu64" seq:%{public}u", + message->stubIndex, message->seqNumber); + return false; + } + message->deviceIdInfo.fromDeviceId[localDevID.length()] = '\0'; + message->deviceIdInfo.toDeviceId[deviceID.length()] = '\0'; + return true; +} + +std::shared_ptr DBinderService::CreateMessage(const sptr &stub, + uint32_t seqNumber, uint32_t pid, uint32_t uid) +{ + auto message = std::make_shared(); + if (message == nullptr) { + DBINDER_LOGE(LOG_LABEL, "new DHandleEntryTxRx fail"); + return nullptr; + } + + message->head.len = sizeof(DHandleEntryTxRx); + message->head.version = RPC_TOKENID_SUPPORT_VERSION; + message->dBinderCode = MESSAGE_AS_INVOKER; + message->transType = GetRemoteTransType(); + message->fromPort = 0; + message->toPort = 0; + message->stubIndex = static_cast(std::atoi(stub->GetServiceName().c_str())); + message->seqNumber = seqNumber; + message->binderObject = stub->GetBinderObject(); + message->stub = AddStubByTag(reinterpret_cast(stub.GetRefPtr())); + message->deviceIdInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + message->pid = pid; + message->uid = uid; + + return message; +} + bool DBinderService::SendEntryToRemote(const sptr stub, uint32_t seqNumber, uint32_t pid, uint32_t uid) { const std::string deviceID = stub->GetDeviceID(); const std::string localDevID = GetLocalDeviceID(); - if (IsDeviceIdIllegal(deviceID) || IsDeviceIdIllegal(localDevID)) { - DBINDER_LOGE(LOG_LABEL, "wrong device id length, remote:%{public}zu local:%{public}zu", - deviceID.length(), localDevID.length()); + if (CheckDeviceIDsInvalid(deviceID, localDevID)) { DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__); return false; } - std::shared_ptr message = std::make_shared(); - message->head.len = sizeof(DHandleEntryTxRx); - message->head.version = RPC_TOKENID_SUPPORT_VERSION; - message->dBinderCode = MESSAGE_AS_INVOKER; - message->transType = GetRemoteTransType(); - message->fromPort = 0; - message->toPort = 0; - message->stubIndex = static_cast(std::atoi(stub->GetServiceName().c_str())); - message->seqNumber = seqNumber; - message->binderObject = stub->GetBinderObject(); - message->stub = AddStubByTag(reinterpret_cast(stub.GetRefPtr())); - message->deviceIdInfo.tokenId = IPCSkeleton::GetCallingTokenID(); - message->pid = pid; - message->uid = uid; - if (memcpy_s(message->deviceIdInfo.fromDeviceId, DEVICEID_LENGTH, localDevID.data(), localDevID.length()) != 0 || - memcpy_s(message->deviceIdInfo.toDeviceId, DEVICEID_LENGTH, deviceID.data(), deviceID.length()) != 0) { - DBINDER_LOGE(LOG_LABEL, "fail to copy memory, service:%{public}" PRIu64" seq:%{public}u", - message->stubIndex, message->seqNumber); + auto message = CreateMessage(stub, seqNumber, pid, uid); + if (message == nullptr) { + return false; + } + + if (!CopyDeviceIDsToMessage(message, localDevID, deviceID)) { DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_MEMCPY_DATA, __FUNCTION__); return false; } - message->deviceIdInfo.fromDeviceId[localDevID.length()] = '\0'; - message->deviceIdInfo.toDeviceId[deviceID.length()] = '\0'; + DBINDER_LOGI(LOG_LABEL, "pid:%{public}u uid:%{public}u seq:%{public}u stub:%{public}llu" " tokenId:%{public}u", message->pid, message->uid, message->seqNumber, (message->stub & BINDER_MASK), message->deviceIdInfo.tokenId); - std::shared_ptr remoteListener = GetRemoteListener(); if (remoteListener == nullptr) { DBINDER_LOGE(LOG_LABEL, "remoteListener is null, service:%{public}" PRIu64 " seq:%{public}u",