Merged
fix(queue_schedule): 修复异常路径资源清理与参数校验 #6146
zhaowenruiAdd on 21 days ago
fix(queue_schedule): 修复异常路径资源清理与参数校验 #6146
Merged
Total: 6 files changed+108-15
| @@ -101,15 +101,6 @@ BqsStatus EzcomClient::SendBqsMsg(const BQSMsg& bqsReqMsg, BQSMsg& bqsRespMsg) c | |||
| 101 | 101 | ||
| 102 | EzcomRequest req = {.id = 0U, .data = PtrToPtr<char_t, uint8_t>(reqData.get()), .size = reqLength}; | 102 | EzcomRequest req = {.id = 0U, .data = PtrToPtr<char_t, uint8_t>(reqData.get()), .size = reqLength}; |
| 103 | struct EzcomResponse resp = {0U}; | 103 | struct EzcomResponse resp = {0U}; |
| 104 | - // Send msg and get response | ||
| 105 | - BQS_LOG_INFO("EzcomRPCSync begin, fd:%d, msg size:%u", clientFd_, reqLength); | ||
| 106 | - int32_t err = EzcomRPCSync(clientFd_, &req, &resp); | ||
| 107 | - if (err == -EAGAIN) { | ||
| 108 | - err = EzcomRPCSync(clientFd_, &req, &resp); | ||
| 109 | - BQS_LOG_INFO("Need to retry ezcom send, fd=%d", clientFd_); | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - // scope resp.data | ||
| 113 | const ScopeGuard respDataGuard([&resp]() { | 104 | const ScopeGuard respDataGuard([&resp]() { |
| 114 | if (resp.data != nullptr) { | 105 | if (resp.data != nullptr) { |
| 115 | delete[] resp.data; | 106 | delete[] resp.data; |
| @@ -117,6 +108,17 @@ BqsStatus EzcomClient::SendBqsMsg(const BQSMsg& bqsReqMsg, BQSMsg& bqsRespMsg) c | |||
| 117 | } | 108 | } |
| 118 | }); | 109 | }); |
| 119 | 110 | ||
| 111 | + // Send msg and get response. | ||
| 112 | + BQS_LOG_INFO("EzcomRPCSync begin, fd:%d, msg size:%u", clientFd_, reqLength); | ||
| 113 | + int32_t err = EzcomRPCSync(clientFd_, &req, &resp); | ||
| 114 | + if (err == -EAGAIN) { | ||
| 115 | + // Release any response from the failed attempt before the RPC overwrites it. | ||
| 116 | + delete[] resp.data; | ||
| 117 | + resp = {}; | ||
| 118 | + err = EzcomRPCSync(clientFd_, &req, &resp); | ||
| 119 | + BQS_LOG_INFO("Need to retry ezcom send, fd=%d", clientFd_); | ||
| 120 | + } | ||
| 121 | + | ||
| 120 | if ((err < 0) || (resp.data == nullptr)) { | 122 | if ((err < 0) || (resp.data == nullptr)) { |
| 121 | BQS_LOG_ERROR("EasyRPCSync failed: %d", err); | 123 | BQS_LOG_ERROR("EasyRPCSync failed: %d", err); |
| 122 | return BQS_STATUS_EASY_COMM_ERROR; | 124 | return BQS_STATUS_EASY_COMM_ERROR; |
| @@ -287,15 +287,21 @@ BqsStatus ConfigInfoOperator::CreateHcomHandle(const uintptr_t mbufData, const u | |||
| 287 | if (queryGroupRet != BQS_STATUS_OK) { | 287 | if (queryGroupRet != BQS_STATUS_OK) { |
| 288 | return queryGroupRet; | 288 | return queryGroupRet; |
| 289 | } | 289 | } |
| 290 | + if ((mbufData == 0U) || (dataLen < sizeof(HcomHandleInfo))) { | ||
| 291 | + BQS_LOG_ERROR("Invalid hcom handle buffer, dataLen[%lu].", dataLen); | ||
| 292 | + return BQS_STATUS_PARAM_INVALID; | ||
| 293 | + } | ||
| 290 | HcomHandleInfo* const info = PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData)); | 294 | HcomHandleInfo* const info = PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData)); |
| 291 | - if (info->rankTableLen == 0UL) { | 295 | + if ((info->rankTableLen == 0UL) || (info->rankTableLen > UINT32_MAX)) { |
| 292 | BQS_LOG_ERROR("Invalid rank table len[%lu].", info->rankTableLen); | 296 | BQS_LOG_ERROR("Invalid rank table len[%lu].", info->rankTableLen); |
| 293 | return BQS_STATUS_PARAM_INVALID; | 297 | return BQS_STATUS_PARAM_INVALID; |
| 294 | } | 298 | } |
| 295 | 299 | ||
| 296 | uint32_t tempAddHcomTab = 0U; | 300 | uint32_t tempAddHcomTab = 0U; |
| 297 | bool isOverflow = false; | 301 | bool isOverflow = false; |
| 298 | - BqsCheckAssign32UAdd(static_cast<uint32_t>(sizeof(HcomHandleInfo)), info->rankTableLen, tempAddHcomTab, isOverflow); | 302 | + BqsCheckAssign32UAdd( |
| 303 | + static_cast<uint32_t>(sizeof(HcomHandleInfo)), static_cast<uint32_t>(info->rankTableLen), tempAddHcomTab, | ||
| 304 | + isOverflow); | ||
| 299 | if (isOverflow) { | 305 | if (isOverflow) { |
| 300 | BQS_LOG_ERROR("tempAddHcomTab[%u] is invalid.", tempAddHcomTab); | 306 | BQS_LOG_ERROR("tempAddHcomTab[%u] is invalid.", tempAddHcomTab); |
| 301 | return BQS_STATUS_PARAM_INVALID; | 307 | return BQS_STATUS_PARAM_INVALID; |
| @@ -358,6 +364,10 @@ BqsStatus ConfigInfoOperator::CreateHcomHandle(const uintptr_t mbufData, const u | |||
| 358 | 364 | ||
| 359 | BqsStatus ConfigInfoOperator::DestroyHcomHandle(const uintptr_t mbufData, const uint64_t dataLen) const | 365 | BqsStatus ConfigInfoOperator::DestroyHcomHandle(const uintptr_t mbufData, const uint64_t dataLen) const |
| 360 | { | 366 | { |
| 367 | + if ((mbufData == 0U) || (dataLen < sizeof(HcomHandleInfo))) { | ||
| 368 | + BQS_LOG_ERROR("Invalid hcom handle buffer, dataLen[%lu].", dataLen); | ||
| 369 | + return BQS_STATUS_PARAM_INVALID; | ||
| 370 | + } | ||
| 361 | HcomHandleInfo* const info = PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData)); | 371 | HcomHandleInfo* const info = PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData)); |
| 362 | // check dataLen | 372 | // check dataLen |
| 363 | bool overFlow = false; | 373 | bool overFlow = false; |
| @@ -1407,8 +1417,8 @@ BqsStatus ConfigInfoOperator::ProcessInitDynamicSched() const | |||
| 1407 | BQS_LOG_ERROR( | 1417 | BQS_LOG_ERROR( |
| 1408 | "Fail to subscribe enque event of [qid:%u-deviceId:%u-isclientQ:%d] or " | 1418 | "Fail to subscribe enque event of [qid:%u-deviceId:%u-isclientQ:%d] or " |
| 1409 | "subscribe full-to-notfull event of [qid:%u-deviceId:%u-isclientQ:%d]", | 1419 | "subscribe full-to-notfull event of [qid:%u-deviceId:%u-isclientQ:%d]", |
| 1410 | - schedCfgInfo.responseQue.queueId, schedCfgInfo.responseQue.deviceId, schedCfgInfo.responseQue.isClientQ, | 1420 | + schedCfgInfo.requestQue.queueId, schedCfgInfo.requestQue.deviceId, schedCfgInfo.requestQue.isClientQ, |
| 1411 | - schedCfgInfo.requestQue.queueId, schedCfgInfo.requestQue.deviceId, schedCfgInfo.responseQue.isClientQ); | 1421 | + schedCfgInfo.responseQue.queueId, schedCfgInfo.responseQue.deviceId, schedCfgInfo.responseQue.isClientQ); |
| 1412 | resultVec[0UL]->retCode = static_cast<int32_t>(BQS_STATUS_INNER_ERROR); | 1422 | resultVec[0UL]->retCode = static_cast<int32_t>(BQS_STATUS_INNER_ERROR); |
| 1413 | return BQS_STATUS_INNER_ERROR; | 1423 | return BQS_STATUS_INNER_ERROR; |
| 1414 | } | 1424 | } |
| @@ -160,6 +160,10 @@ void SubModuleInterface::ReportErrMsgToTsd(const int32_t errCode) const | |||
| 160 | int32_t SubModuleInterface::StartQueueScheduleModule(const struct TsdSubEventInfo* const eventInfo) | 160 | int32_t SubModuleInterface::StartQueueScheduleModule(const struct TsdSubEventInfo* const eventInfo) |
| 161 | { | 161 | { |
| 162 | BQS_LOG_RUN_INFO("enter queue schedule sub module start process."); | 162 | BQS_LOG_RUN_INFO("enter queue schedule sub module start process."); |
| 163 | + if (eventInfo == nullptr) { | ||
| 164 | + BQS_LOG_ERROR("Queue schedule start eventInfo is nullptr."); | ||
| 165 | + return -1; | ||
| 166 | + } | ||
| 163 | // set event key | 167 | // set event key |
| 164 | SetTsdEventKey(eventInfo); | 168 | SetTsdEventKey(eventInfo); |
| 165 | 169 | ||
| @@ -322,4 +322,32 @@ TEST_F(EZCOM_CLIENT_UTest, ParseGetPagedBindRespMsgSuccess) | |||
| 322 | uint32_t ret = bqs::EzcomClient::GetInstance(0)->ParseGetPagedBindRespMsg(bqsRespMsg, bindQueueVec, total); | 322 | uint32_t ret = bqs::EzcomClient::GetInstance(0)->ParseGetPagedBindRespMsg(bqsRespMsg, bindQueueVec, total); |
| 323 | EXPECT_EQ(ret, 10); | 323 | EXPECT_EQ(ret, 10); |
| 324 | EXPECT_EQ(total, 10); | 324 | EXPECT_EQ(total, 10); |
| 325 | -} | 325 | +} |
| 326 | + | ||
| 327 | +namespace { | ||
| 328 | +uint32_t g_ezcomRetryCallCount = 0U; | ||
| 329 | + | ||
| 330 | +int EzcomRetryWithAllocatedResponse(int fd, struct EzcomRequest* req, struct EzcomResponse* resp) | ||
| 331 | +{ | ||
| 332 | + if (g_ezcomRetryCallCount++ == 0U) { | ||
| 333 | + resp->id = 123U; | ||
| 334 | + resp->size = 16U; | ||
| 335 | + resp->data = new uint8_t[resp->size]; | ||
| 336 | + return -EAGAIN; | ||
| 337 | + } | ||
| 338 | + EXPECT_EQ(resp->data, nullptr); | ||
| 339 | + EXPECT_EQ(resp->size, 0U); | ||
| 340 | + EXPECT_EQ(resp->id, 0U); | ||
| 341 | + return EzcomRPCSyncFake(fd, req, resp); | ||
| 342 | +} | ||
| 343 | +} // namespace | ||
| 344 | + | ||
| 345 | +TEST_F(EZCOM_CLIENT_UTest, RetryClearsResponseFromFailedAttempt) | ||
| 346 | +{ | ||
| 347 | + g_ezcomRetryCallCount = 0U; | ||
| 348 | + MOCKER(EzcomRPCSync).expects(exactly(2)).will(invoke(EzcomRetryWithAllocatedResponse)); | ||
| 349 | + bqs::BQSMsg request; | ||
| 350 | + bqs::BQSMsg response; | ||
| 351 | + EXPECT_EQ(bqs::EzcomClient::GetInstance(0)->SendBqsMsg(request, response), bqs::BQS_STATUS_OK); | ||
| 352 | + EXPECT_EQ(g_ezcomRetryCallCount, 2U); | ||
| 353 | +} | ||
| @@ -320,3 +320,9 @@ TEST_F(QsSubModuleInterfaceUtest, StubTsdEventClientFuncTest) | |||
| 320 | ret = SubModuleProcessResponse(0, TSD_QS, 123, 0, TSD_EVENT_START_QS_MODULE_RSP); | 320 | ret = SubModuleProcessResponse(0, TSD_QS, 123, 0, TSD_EVENT_START_QS_MODULE_RSP); |
| 321 | EXPECT_EQ(ret, 0); | 321 | EXPECT_EQ(ret, 0); |
| 322 | } | 322 | } |
| 323 | + | ||
| 324 | +TEST_F(QsSubModuleInterfaceUtest, StartRejectsNullEventBeforeReadingArguments) | ||
| 325 | +{ | ||
| 326 | + MOCKER_CPP(&SubModuleInterface::ParseArgsFromFile).expects(never()); | ||
| 327 | + EXPECT_EQ(StartQueueScheduleModule(nullptr), -1); | ||
| 328 | +} | ||
| @@ -11,6 +11,8 @@ | |||
| 11 | #include <iostream> | 11 | #include <iostream> |
| 12 | #include <cstdint> | 12 | #include <cstdint> |
| 13 | #include <climits> | 13 | #include <climits> |
| 14 | +#include <sys/mman.h> | ||
| 15 | +#include <unistd.h> | ||
| 14 | #include "securec.h" | 16 | #include "securec.h" |
| 15 | #include "gtest/gtest.h" | 17 | #include "gtest/gtest.h" |
| 16 | #include "mockcpp/mockcpp.hpp" | 18 | #include "mockcpp/mockcpp.hpp" |
| @@ -1504,4 +1506,45 @@ TEST_F(RouterServerUtest, ManageQsEventParmERR) | |||
| 1504 | curServer.ManageQsEvent(); | 1506 | curServer.ManageQsEvent(); |
| 1505 | EXPECT_EQ(curServer.deviceId_, 0U); | 1507 | EXPECT_EQ(curServer.deviceId_, 0U); |
| 1506 | curServer.Destroy(); | 1508 | curServer.Destroy(); |
| 1507 | -} | 1509 | +} |
| 1510 | + | ||
| 1511 | +TEST_F(RouterServerUtest, CreateHcomHandleRejectsTruncatedRankTableLength) | ||
| 1512 | +{ | ||
| 1513 | + ConfigInfoOperator op(1U); | ||
| 1514 | + MOCKER_CPP(&ConfigInfoOperator::QueryGroupAllocInfo).stubs().will(returnValue(BQS_STATUS_OK)); | ||
| 1515 | + HcomHandleInfo info = {}; | ||
| 1516 | + info.rankTableLen = static_cast<uint64_t>(UINT32_MAX) + 2UL; | ||
| 1517 | + EXPECT_EQ( | ||
| 1518 | + op.CreateHcomHandle(reinterpret_cast<uintptr_t>(&info), sizeof(info) + sizeof(CfgRetInfo)), | ||
| 1519 | + BQS_STATUS_PARAM_INVALID); | ||
| 1520 | +} | ||
| 1521 | + | ||
| 1522 | +TEST_F(RouterServerUtest, CreateHcomHandleRejectsMissingHeader) | ||
| 1523 | +{ | ||
| 1524 | + ConfigInfoOperator op(1U); | ||
| 1525 | + MOCKER_CPP(&ConfigInfoOperator::QueryGroupAllocInfo).stubs().will(returnValue(BQS_STATUS_OK)); | ||
| 1526 | + EXPECT_EQ(op.CreateHcomHandle(0U, sizeof(HcomHandleInfo)), BQS_STATUS_PARAM_INVALID); | ||
| 1527 | + HcomHandleInfo info = {}; | ||
| 1528 | + EXPECT_EQ(op.CreateHcomHandle(reinterpret_cast<uintptr_t>(&info), sizeof(info) - 1U), BQS_STATUS_PARAM_INVALID); | ||
| 1529 | +} | ||
| 1530 | + | ||
| 1531 | +TEST_F(RouterServerUtest, DestroyHcomHandleRejectsNullBuffer) | ||
| 1532 | +{ | ||
| 1533 | + ConfigInfoOperator op(1U); | ||
| 1534 | + EXPECT_EQ(op.DestroyHcomHandle(0U, sizeof(HcomHandleInfo)), BQS_STATUS_PARAM_INVALID); | ||
| 1535 | +} | ||
| 1536 | + | ||
| 1537 | +TEST_F(RouterServerUtest, DestroyHcomHandleRejectsShortHeaderBeforeReadingFields) | ||
| 1538 | +{ | ||
| 1539 | + ConfigInfoOperator op(1U); | ||
| 1540 | + const size_t pageSize = static_cast<size_t>(getpagesize()); | ||
| 1541 | + void* const buffer = mmap(nullptr, pageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| 1542 | + ASSERT_NE(buffer, MAP_FAILED); | ||
| 1543 | + const ScopeGuard unmapGuard([buffer, pageSize]() { (void)munmap(buffer, pageSize); }); | ||
| 1544 | + | ||
| 1545 | + // An incomplete header must be rejected without reading the protected memory. | ||
| 1546 | + EXPECT_EQ(op.DestroyHcomHandle(reinterpret_cast<uintptr_t>(buffer), 0U), BQS_STATUS_PARAM_INVALID); | ||
| 1547 | + EXPECT_EQ( | ||
| 1548 | + op.DestroyHcomHandle(reinterpret_cast<uintptr_t>(buffer), sizeof(HcomHandleInfo) - 1U), | ||
| 1549 | + BQS_STATUS_PARAM_INVALID); | ||
| 1550 | +} | ||
🟡 Medium Priority
变更行:config_info_operator.cpp:290-293 新增
(mbufData == 0U) || (dataLen < sizeof(HcomHandleInfo))校验后才允许把 mbuf 解释为HcomHandleInfo,并在 295 行读取info->rankTableLen;同一文件 302-304 行再用static_cast<uint32_t>消除 64→32 截断。这为“HcomHandleInfo 头部先于任何字段访问必须存在”树立了新的输入契约。受影响契约(未修改的同构兄弟函数):
ParseConfigEvent()在 91-97 行把同一个mbufDataAddr/dataLen(由halMbufGetBuffAddr+halMbufGetDataLen在 59-72 行得到)分别交给 CreateHcomHandle 与 DestroyHcomHandle。但DestroyHcomHandle()(365-375 行)仍是旧形态:先PtrToPtr<void, HcomHandleInfo>(ValueToPtr(mbufData)),随后在 371 行直接解引用info->rankTableLen参与BqsCheckAssign64UAdd,全程没有mbufData != 0/dataLen >= sizeof(HcomHandleInfo)的前置校验;mbufData == 0时 367 行也是空指针解引用。失败模式:客户端下发 DGW_DESTORY_HCOM_HANDLE 时若 mbuf 数据长度小于
sizeof(HcomHandleInfo)(例如截断/空消息),DestroyHcomHandle 会越界读取rankTableLen、hcomHandle等字段;若越界读出的rankTableLen较小使 372 行dataLen < cfgLen检查通过,378 行会把越界读出的垃圾值info->hcomHandle当作 HcclComm 传给HcclFinalizeComm(395 行),可能对任意地址做接续销毁,导致越界访问/进程崩溃。这与被修复的 Create 路径是同一类问题,本次只修了一侧,两条路径的校验语义现已不一致。