From 1eaa6c809d464fb5d1c1b9be03ffcc4adc29c2ed Mon Sep 17 00:00:00 2001 From: Rahul-cs50 Date: Sat, 24 Oct 2026 13:01:54 +0530 Subject: [PATCH] Refuse encapsulation option padding that runs past the buffer read_encapsulation() subtracts the option alignment from end_ when the representation options request trailing padding. On a buffer too short to actually contain that padding, end_ was moved before the read cursor; the remaining length then underflowed and later reads ran off the buffer, segfaulting. This is reachable from untrusted input, e.g. deserializing a truncated 4-byte CDR header through rmw_fastrtps (reported as ros2/rmw_fastrtps#940). Refuse such input with NotEnoughMemoryException before moving end_, the same way the other bounds checks in this file do. Buffers that are long enough to carry the padding are unaffected. Adds a regression test covering both the truncated header (now refused) and a buffer long enough to hold the padding (still accepted). Signed-off-by: Rahul-cs50 --- src/cpp/Cdr.cpp | 11 +++++++++++ test/cdr/SimpleTest.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/cpp/Cdr.cpp b/src/cpp/Cdr.cpp index 7b22c949..95563bec 100644 --- a/src/cpp/Cdr.cpp +++ b/src/cpp/Cdr.cpp @@ -297,6 +297,17 @@ Cdr& Cdr::read_encapsulation() if (0 == alignment) { + // The option alignment counts padding bytes at the end of + // the stream. A truncated buffer may not actually contain + // them; moving end_ back past the read cursor would make + // the remaining length underflow and later reads run off + // the buffer. Refuse such input instead. + if ((end_ - offset_) < option_align) + { + throw NotEnoughMemoryException( + NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT); + } + end_ -= option_align; } } diff --git a/test/cdr/SimpleTest.cpp b/test/cdr/SimpleTest.cpp index 5f75cab6..f8c6dfe8 100644 --- a/test/cdr/SimpleTest.cpp +++ b/test/cdr/SimpleTest.cpp @@ -540,6 +540,34 @@ TEST(CDRTests, DDSEncapsulation) } } +TEST(CDRTests, DDSEncapsulationTruncatedOptionAlignment) +{ + // Regression test: an encapsulation header whose options request trailing + // alignment padding on a buffer that is too short to contain it used to + // move end_ before the read cursor. The remaining length then underflowed + // and later reads ran off the buffer (segfault). It must be refused. + { + // 4-byte header only: PLAIN_CDR_BE, options {0x00, 0x01} -> align 1, + // but no payload byte for the claimed padding. + char encapsulation[4]{ 0, 0, 0, 1 }; + eprosima::fastcdr::FastBuffer buffer(encapsulation, sizeof(encapsulation)); + eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, + eprosima::fastcdr::CdrVersion::DDS_CDR); + EXPECT_THROW(cdr.read_encapsulation(), + eprosima::fastcdr::exception::NotEnoughMemoryException); + } + + { + // Same option alignment, but the buffer is long enough to hold the + // padding: read_encapsulation must still succeed. + char encapsulation[8]{ 0, 0, 0, 1, 0, 0, 0, 0 }; + eprosima::fastcdr::FastBuffer buffer(encapsulation, sizeof(encapsulation)); + eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, + eprosima::fastcdr::CdrVersion::DDS_CDR); + EXPECT_NO_THROW(cdr.read_encapsulation()); + } +} + TEST(CDRTests, CorbaEncapsulation) { char encapsulation[1]{ 0 };