fix: rectify the large function of SendEntryToRemote

Signed-off-by: jason <jialinbo@huawei.com>
This commit is contained in:
jason 2024-05-15 12:06:33 +00:00
parent 5e03bf7ca6
commit 8a0afd30c3
2 changed files with 63 additions and 24 deletions

View File

@ -319,6 +319,11 @@ private:
bool DetachSessionObject(binder_uintptr_t stub);
bool AttachSessionObject(std::shared_ptr<struct SessionInfo> object, binder_uintptr_t stub);
sptr<IRemoteObject> FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId);
bool CheckDeviceIDsInvalid(const std::string &deviceID, const std::string &localDevID);
bool CopyDeviceIDsToMessage(std::shared_ptr<struct DHandleEntryTxRx> message,
const std::string &localDevID, const std::string &deviceID);
std::shared_ptr<struct DHandleEntryTxRx> CreateMessage(const sptr<DBinderServiceStub> &stub, uint32_t seqNumber,
uint32_t pid, uint32_t uid);
bool SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid, uint32_t uid);
uint16_t AllocFreeSocketPort();
std::string GetLocalDeviceID();

View File

@ -364,19 +364,39 @@ sptr<DBinderServiceStub> DBinderService::MakeRemoteBinder(const std::u16string &
return dBinderServiceStub;
}
bool DBinderService::SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid,
uint32_t uid)
bool DBinderService::CheckDeviceIDsInvalid(const std::string &deviceID, const std::string &localDevID)
{
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());
DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__);
return true;
}
return false;
}
bool DBinderService::CopyDeviceIDsToMessage(std::shared_ptr<struct DHandleEntryTxRx> 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<struct DHandleEntryTxRx> DBinderService::CreateMessage(const sptr<DBinderServiceStub> &stub,
uint32_t seqNumber, uint32_t pid, uint32_t uid)
{
auto message = std::make_shared<struct DHandleEntryTxRx>();
if (message == nullptr) {
DBINDER_LOGE(LOG_LABEL, "new DHandleEntryTxRx fail");
return nullptr;
}
std::shared_ptr<struct DHandleEntryTxRx> message = std::make_shared<struct DHandleEntryTxRx>();
message->head.len = sizeof(DHandleEntryTxRx);
message->head.version = RPC_TOKENID_SUPPORT_VERSION;
message->dBinderCode = MESSAGE_AS_INVOKER;
@ -390,19 +410,33 @@ bool DBinderService::SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint
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);
return message;
}
bool DBinderService::SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid,
uint32_t uid)
{
const std::string deviceID = stub->GetDeviceID();
const std::string localDevID = GetLocalDeviceID();
if (CheckDeviceIDsInvalid(deviceID, localDevID)) {
DfxReportFailEvent(DbinderErrorCode::RPC_DRIVER, RADAR_ERR_INVALID_DATA, __FUNCTION__);
return false;
}
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<DBinderRemoteListener> remoteListener = GetRemoteListener();
if (remoteListener == nullptr) {
DBINDER_LOGE(LOG_LABEL, "remoteListener is null, service:%{public}" PRIu64 " seq:%{public}u",