diff --git a/src/lib/rc/rc_tests/RCTest.cpp b/src/lib/rc/rc_tests/RCTest.cpp index 761e6809b0c..d617edaf71c 100644 --- a/src/lib/rc/rc_tests/RCTest.cpp +++ b/src/lib/rc/rc_tests/RCTest.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #if defined(CONFIG_ARCH_BOARD_PX4_SITL) #define TEST_DATA_PATH "./test_data/" @@ -41,6 +42,7 @@ private: bool st24MalformedLengthTest(); bool st24Test(); bool sumdTest(); + bool crossProtocolSharedUnionOobTest(); }; bool RCTest::run_tests() @@ -55,6 +57,7 @@ bool RCTest::run_tests() ut_run_test(st24MalformedLengthTest); ut_run_test(st24Test); ut_run_test(sumdTest); + ut_run_test(crossProtocolSharedUnionOobTest); return (_tests_failed == 0); } @@ -554,4 +557,39 @@ bool RCTest::sumdTest() return true; } +bool RCTest::crossProtocolSharedUnionOobTest() +{ + uint8_t rssi{}, lost{}; + uint16_t channel_count{}; + uint16_t channels[18] {}; // input_rc_s::RC_INPUT_MAX_CHANNELS + + PX4_INFO("rc_decode_buf address=%p size=%zu", (void *)&rc_decode_buf, sizeof(rc_decode_buf)); + + st24_decode(ST24_STX1, &rssi, &lost, &channel_count, channels, 18); + st24_decode(ST24_STX2, &rssi, &lost, &channel_count, channels, 18); + st24_decode(66, &rssi, &lost, &channel_count, channels, 18); // length=66 + st24_decode(0x00, &rssi, &lost, &channel_count, channels, 18); // type + + for (int i = 0; i < 60; i++) { + st24_decode(0xAA, &rssi, &lost, &channel_count, channels, 18); + } + + uint16_t dsm_values[18]; uint16_t dsm_num_values = 0; bool dsm_11bit = false; + unsigned dsm_frame_drops = 0; int8_t dsm_rssi = 0; + const uint8_t dsm_bytes[8] = {0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}; + dsm_parse(hrt_absolute_time(), dsm_bytes, sizeof(dsm_bytes), dsm_values, &dsm_num_values, + &dsm_11bit, &dsm_frame_drops, &dsm_rssi, 18); + + PX4_INFO("resuming ST24 after cross-protocol union clobber (expect OOB write under ASan/UBSan)"); + + for (int i = 0; i < 20; i++) { + st24_decode(0xBB, &rssi, &lost, &channel_count, channels, 18); + } + + PX4_INFO("crossProtocolSharedUnionOobTest: completed without crashing " + "(no sanitizer active, or already reset upstream)"); + + return true; +} + ut_declare_test_c(rc_tests_main, RCTest) diff --git a/src/lib/rc/st24.cpp b/src/lib/rc/st24.cpp index 239b62155a8..73d85c03d47 100644 --- a/src/lib/rc/st24.cpp +++ b/src/lib/rc/st24.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include "st24.h" #include "common_rc.h" @@ -69,6 +70,8 @@ static uint8_t _rxlen; static ReceiverFcPacket &_rxpacket = rc_decode_buf._strxpacket; +static constexpr uint8_t ST24_CRC_SPAN_MAX = sizeof(ReceiverFcPacket) - offsetof(ReceiverFcPacket, length); + uint8_t st24_common_crc8(uint8_t *ptr, uint8_t len) { uint8_t i, crc ; @@ -144,6 +147,12 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan break; case ST24_DECODE_STATE_GOT_TYPE: + + if (_rxlen == 0 || (unsigned)(_rxlen - 1) >= sizeof(_rxpacket.st24_data)) { + _decode_state = ST24_DECODE_STATE_UNSYNCED; + break; + } + _rxpacket.st24_data[_rxlen - 1] = byte; _rxlen++; @@ -157,7 +166,8 @@ int st24_decode(uint8_t byte, uint8_t *rssi, uint8_t *lost_count, uint16_t *chan _rxpacket.crc8 = byte; _rxlen++; - if (st24_common_crc8((uint8_t *) & (_rxpacket.length), _rxlen) == _rxpacket.crc8) { + if (_rxlen <= ST24_CRC_SPAN_MAX + && st24_common_crc8((uint8_t *) & (_rxpacket.length), _rxlen) == _rxpacket.crc8) { ret = 0;