fix(crsf_rc): reject truncated variable-length status frames (#28463)

Require each known variable-length frame to contain the fixed prefix
consumed by its processor while continuing to allow trailing extension fields.
Bound ELRS status text copying by the received payload and add parser contract regression coverage.
This commit is contained in:
Andreas authored and GitHub committed 2026-08-30 13:55:43 -06:00
1 parent bd0b15d434
commit 5b333f04a1
4 files changed
+176 -17

No files matched your search

+1
View File
@@ -1,2 +1,3 @@
CONFIG_BOARD_NOLOCKSTEP=y
CONFIG_DRIVERS_DISTANCE_SENSOR_LIGHTWARE_LASER_SERIAL=y
CONFIG_DRIVERS_RC_CRSF_RC=y
+8
View File
@@ -47,3 +47,11 @@ px4_add_module(
MODULE_CONFIG
module.yaml
)
px4_add_unit_gtest(
SRC CrsfParserContractTest.cpp
EXTRA_SRCS
CrsfParser.cpp
QueueBuffer.cpp
Crc8.cpp
)
+27 -17
View File
@@ -60,11 +60,11 @@ enum CRSF_PAYLOAD_SIZE {
CRSF_PAYLOAD_SIZE_GPS = 15,
CRSF_PAYLOAD_SIZE_BATTERY = 8,
CRSF_PAYLOAD_SIZE_LINK_STATISTICS = 10,
CRSF_PAYLOAD_SIZE_LINK_STATISTICS_TX = -1,
CRSF_PAYLOAD_MIN_SIZE_LINK_STATISTICS_TX = 6,
CRSF_PAYLOAD_SIZE_RC_CHANNELS = 22,
CRSF_PAYLOAD_SIZE_ATTITUDE = 6,
CRSF_PAYLOAD_SIZE_MSP_WRITE = -1, // -1 means variable length
CRSF_PAYLOAD_SIZE_ELRS_STATUS = -1, // unclear how large this message is
CRSF_PAYLOAD_MIN_SIZE_MSP_WRITE = 5,
CRSF_PAYLOAD_MIN_SIZE_ELRS_STATUS = 6,
};
enum CRSF_PACKET_TYPE {
@@ -121,10 +121,16 @@ enum PARSER_STATE {
PARSER_STATE_CRC,
};
enum class CrsfSizePolicy : uint8_t {
Exact,
Minimum,
};
typedef struct {
uint8_t packet_type;
int32_t packet_size;
bool (*processor)(const uint8_t *data, const uint32_t size, CrsfPacket_t *const new_packet);
uint8_t packet_type; ///< CRSF frame type byte.
uint32_t payload_size; ///< Payload bytes, excluding the type byte and CRC.
CrsfSizePolicy payload_size_policy; ///< Exact size or minimum accepted size.
bool (*processor)(const uint8_t *data, const uint32_t size, CrsfPacket_t *const new_packet); ///< Called with the validated payload.
} CrsfPacketDescriptor_t;
static bool ProcessChannelData(const uint8_t *data, const uint32_t size, CrsfPacket_t *const new_packet);
@@ -136,12 +142,12 @@ static bool ProcessMspWrite(const uint8_t *data, const uint32_t size, CrsfPacket
#endif
static const CrsfPacketDescriptor_t crsf_packet_descriptors[] = {
{CRSF_PACKET_TYPE_RC_CHANNELS_PACKED, CRSF_PAYLOAD_SIZE_RC_CHANNELS, ProcessChannelData},
{CRSF_PACKET_TYPE_LINK_STATISTICS, CRSF_PAYLOAD_SIZE_LINK_STATISTICS, ProcessLinkStatistics},
{CRSF_PACKET_TYPE_LINK_STATISTICS_TX, CRSF_PAYLOAD_SIZE_LINK_STATISTICS_TX, ProcessLinkStatisticsTx},
{CRSF_PACKET_TYPE_ELRS_STATUS, CRSF_PAYLOAD_SIZE_ELRS_STATUS, ProcessElrsStatus},
{CRSF_PACKET_TYPE_RC_CHANNELS_PACKED, CRSF_PAYLOAD_SIZE_RC_CHANNELS, CrsfSizePolicy::Exact, ProcessChannelData},
{CRSF_PACKET_TYPE_LINK_STATISTICS, CRSF_PAYLOAD_SIZE_LINK_STATISTICS, CrsfSizePolicy::Exact, ProcessLinkStatistics},
{CRSF_PACKET_TYPE_LINK_STATISTICS_TX, CRSF_PAYLOAD_MIN_SIZE_LINK_STATISTICS_TX, CrsfSizePolicy::Minimum, ProcessLinkStatisticsTx},
{CRSF_PACKET_TYPE_ELRS_STATUS, CRSF_PAYLOAD_MIN_SIZE_ELRS_STATUS, CrsfSizePolicy::Minimum, ProcessElrsStatus},
#ifdef CONFIG_VTX_CRSF_MSP_SUPPORT
{CRSF_PACKET_TYPE_MSP_WRITE, CRSF_PAYLOAD_SIZE_MSP_WRITE, ProcessMspWrite},
{CRSF_PACKET_TYPE_MSP_WRITE, CRSF_PAYLOAD_MIN_SIZE_MSP_WRITE, CrsfSizePolicy::Minimum, ProcessMspWrite},
#endif
};
#define CRSF_PACKET_DESCRIPTOR_COUNT (sizeof(crsf_packet_descriptors) / sizeof(CrsfPacketDescriptor_t))
@@ -264,8 +270,11 @@ static bool ProcessElrsStatus(const uint8_t *data, const uint32_t size, CrsfPack
new_packet->elrs_status.packets_bad = data[2];
new_packet->elrs_status.packets_good = (data[3] << 8) | data[4];
new_packet->elrs_status.flags = data[5];
strncpy(new_packet->elrs_status.message, (const char *)&data[6], sizeof(new_packet->elrs_status.message) - 1);
new_packet->elrs_status.message[sizeof(new_packet->elrs_status.message) - 1] = '\0';
const uint32_t message_size = size - CRSF_PAYLOAD_MIN_SIZE_ELRS_STATUS;
const uint32_t copy_size = message_size < sizeof(new_packet->elrs_status.message) - 1 ?
message_size : sizeof(new_packet->elrs_status.message) - 1;
memcpy(new_packet->elrs_status.message, &data[CRSF_PAYLOAD_MIN_SIZE_ELRS_STATUS], copy_size);
new_packet->elrs_status.message[copy_size] = '\0';
return true;
}
@@ -410,10 +419,11 @@ bool CrsfParser_TryParseCrsfPacket(CrsfPacket_t *const new_packet, CrsfParserSta
// If we know what this packet is...
if (working_descriptor != NULL) {
// Validate length
if (working_descriptor->packet_size == -1) {
if (working_descriptor->payload_size_policy == CrsfSizePolicy::Minimum) {
working_segment_size = packet_size - PACKET_SIZE_TYPE_SIZE;
if (working_index + working_segment_size + CRC_SIZE > CRSF_MAX_PACKET_LEN) {
if (working_segment_size < working_descriptor->payload_size
|| working_index + working_segment_size + CRC_SIZE > CRSF_MAX_PACKET_LEN) {
parser_statistics->invalid_known_packet_sizes++;
parser_state = PARSER_STATE_HEADER;
working_segment_size = HEADER_SIZE;
@@ -423,7 +433,7 @@ bool CrsfParser_TryParseCrsfPacket(CrsfPacket_t *const new_packet, CrsfParserSta
}
} else {
if (packet_size != working_descriptor->packet_size + PACKET_SIZE_TYPE_SIZE) {
if (packet_size != working_descriptor->payload_size + PACKET_SIZE_TYPE_SIZE) {
parser_statistics->invalid_known_packet_sizes++;
parser_state = PARSER_STATE_HEADER;
working_segment_size = HEADER_SIZE;
@@ -432,7 +442,7 @@ bool CrsfParser_TryParseCrsfPacket(CrsfPacket_t *const new_packet, CrsfParserSta
continue;
}
working_segment_size = working_descriptor->packet_size;
working_segment_size = working_descriptor->payload_size;
}
} else {
@@ -0,0 +1,140 @@
/****************************************************************************
*
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
****************************************************************************/
#include <gtest/gtest.h>
#include "Crc8.hpp"
#include "CrsfParser.hpp"
#include <cstdint>
#include <vector>
namespace
{
constexpr uint8_t CRSF_HEADER_VALUE = 0xC8;
constexpr uint8_t CRSF_CHANNEL_TYPE = 0x16;
constexpr uint8_t CRSF_LINK_STATISTICS_TX_TYPE = 0x1D;
constexpr uint8_t CRSF_ELRS_STATUS_TYPE = 0x2E;
constexpr size_t CRSF_CHANNEL_PAYLOAD_SIZE = 22;
std::vector<uint8_t> makeFrame(uint8_t type, const std::vector<uint8_t> &payload)
{
const uint8_t length = static_cast<uint8_t>(payload.size() + 2);
std::vector<uint8_t> frame{CRSF_HEADER_VALUE, length, type};
frame.insert(frame.end(), payload.begin(), payload.end());
frame.push_back(Crc8Calc(frame.data() + 2, length - 1));
return frame;
}
class CrsfParserContractTest : public ::testing::Test
{
protected:
void SetUp() override
{
Crc8Init(0xD5);
CrsfParser_Init();
}
};
TEST_F(CrsfParserContractTest, TruncatedVariableLengthFramesAreRejected)
{
for (const uint8_t type : {CRSF_LINK_STATISTICS_TX_TYPE, CRSF_ELRS_STATUS_TYPE}) {
for (size_t payload_size = 0; payload_size < 6; ++payload_size) {
const std::vector<uint8_t> frame = makeFrame(type, std::vector<uint8_t>(payload_size));
ASSERT_TRUE(CrsfParser_LoadBuffer(frame.data(), frame.size()));
CrsfPacket_t packet{};
CrsfParserStatistics_t statistics{};
EXPECT_FALSE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(statistics.invalid_known_packet_sizes, 1u);
}
}
}
TEST_F(CrsfParserContractTest, MinimumVariableLengthFramesAreAccepted)
{
CrsfPacket_t packet{};
CrsfParserStatistics_t statistics{};
const std::vector<uint8_t> link_statistics = makeFrame(CRSF_LINK_STATISTICS_TX_TYPE,
std::vector<uint8_t> {1, 2, 3, 4, 5, 6});
ASSERT_TRUE(CrsfParser_LoadBuffer(link_statistics.data(), link_statistics.size()));
ASSERT_TRUE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(packet.message_type, CRSF_MESSAGE_TYPE_LINK_STATISTICS_TX);
EXPECT_EQ(packet.link_statistics_tx.uplink_fps, 6);
const std::vector<uint8_t> elrs_status = makeFrame(CRSF_ELRS_STATUS_TYPE,
std::vector<uint8_t> {0xEA, 0xEE, 3, 0x01, 0x23, 0x80});
ASSERT_TRUE(CrsfParser_LoadBuffer(elrs_status.data(), elrs_status.size()));
ASSERT_TRUE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(packet.message_type, CRSF_MESSAGE_TYPE_ELRS_STATUS);
EXPECT_STREQ(packet.elrs_status.message, "");
}
TEST_F(CrsfParserContractTest, VariableLengthFramesAcceptTrailingFields)
{
CrsfPacket_t packet{};
CrsfParserStatistics_t statistics{};
const std::vector<uint8_t> link_statistics = makeFrame(CRSF_LINK_STATISTICS_TX_TYPE,
std::vector<uint8_t> {1, 2, 3, 4, 5, 6, 0xAA});
ASSERT_TRUE(CrsfParser_LoadBuffer(link_statistics.data(), link_statistics.size()));
ASSERT_TRUE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(packet.message_type, CRSF_MESSAGE_TYPE_LINK_STATISTICS_TX);
EXPECT_EQ(packet.link_statistics_tx.uplink_fps, 6);
const std::vector<uint8_t> elrs_status = makeFrame(CRSF_ELRS_STATUS_TYPE,
std::vector<uint8_t> {0xEA, 0xEE, 3, 0x01, 0x23, 0x80, 'O', 'K'});
ASSERT_TRUE(CrsfParser_LoadBuffer(elrs_status.data(), elrs_status.size()));
ASSERT_TRUE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(packet.message_type, CRSF_MESSAGE_TYPE_ELRS_STATUS);
EXPECT_STREQ(packet.elrs_status.message, "OK");
}
TEST_F(CrsfParserContractTest, ParserRecoversAfterTruncatedKnownFrame)
{
const std::vector<uint8_t> truncated_frame = makeFrame(CRSF_ELRS_STATUS_TYPE, std::vector<uint8_t>(5));
ASSERT_TRUE(CrsfParser_LoadBuffer(truncated_frame.data(), truncated_frame.size()));
CrsfPacket_t packet{};
CrsfParserStatistics_t statistics{};
EXPECT_FALSE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(statistics.invalid_known_packet_sizes, 1u);
const std::vector<uint8_t> channel_frame = makeFrame(CRSF_CHANNEL_TYPE,
std::vector<uint8_t>(CRSF_CHANNEL_PAYLOAD_SIZE));
ASSERT_TRUE(CrsfParser_LoadBuffer(channel_frame.data(), channel_frame.size()));
ASSERT_TRUE(CrsfParser_TryParseCrsfPacket(&packet, &statistics));
EXPECT_EQ(packet.message_type, CRSF_MESSAGE_TYPE_RC_CHANNELS);
}
} // namespace