fix(rc): bound ST24 write index against shared rc_decode_buf union (#29693)

RCInput's protocol auto-scan rotates between RC decoders without
resetting their static state, and all decoders alias the same global
rc_decode_buf union. If a sibling decoder overwrites the union's
length byte while ST24 is mid-frame, st24_decode()'s GOT_TYPE write
index can never resynchronize and walks off the end of st24_data.

Bound the GOT_TYPE write and GOT_DATA CRC read against the buffer's
real size, resyncing to UNSYNCED instead of writing out of bounds.
Add a regression test reproducing the cross-protocol clobber.
This commit is contained in:
jeroldcamacho authored and GitHub committed 2026-11-17 12:36:29 +13:00
1 parent f6562b1075
commit 32c68d75c8
2 files changed
+49 -1

No files matched your search

+38
View File
@@ -15,6 +15,7 @@
#include <lib/rc/sumd.h>
#include <lib/rc/crsf.h>
#include <lib/rc/ghst.hpp>
#include <lib/rc/common_rc.h>
#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)
+11 -1
View File
@@ -42,6 +42,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <cmath>
#include <cstddef>
#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;