Skip to content

Refuse encapsulation option padding that runs past the buffer end - #349

Open
Rahul-cs50 wants to merge 1 commit into
eProsima:masterfrom
Rahul-cs50:fix/read-encapsulation-truncated-options
Open

Rahul-cs50 wants to merge 1 commit into
eProsima:masterfrom
Rahul-cs50:fix/read-encapsulation-truncated-options

Conversation

@Rahul-cs50

@Rahul-cs50 Rahul-cs50 commented Oct 24, 2026 •

Copy link
Copy Markdown

Description

Cdr::read_encapsulation() subtracts the option alignment from end_ when the representation options request trailing padding (end_ -= option_align). On a buffer that is too short to actually contain that padding, end_ was moved before the read cursor offset_; the remaining length end_ - offset_ then underflowed and later reads ran off the buffer, leading to a segmentation fault (observed in Cdr::read_string).

This is reachable from untrusted input. Deserializing a truncated 4-byte CDR header (a valid encapsulation header with no payload) through rmw_fastrtps crashes the process; it was reported publicly as ros2/rmw_fastrtps#940.

The fix refuses such input with NotEnoughMemoryException before moving end_, using the same (end_ - offset_) bounds check as the rest of the file. Buffers that are long enough to carry the padding are unaffected.

Contributor Checklist

  • Commit messages follow the project guidelines (DCO signed-off).
  • Regression test added and passing locally: CDRTests.DDSEncapsulationTruncatedOptionAlignment in test/cdr/SimpleTest.cpp covers both the truncated header (now refused) and a buffer long enough to hold the padding (still accepted). The full UnitTests (356) and xcdr_tests (1720) suites pass with the change.
  • Changes are backport compatible (no ABI/API change; a pure bounds check).
  • Changes are API compatible.

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 <rahulseervi.dev@gmail.com>
@MiguelCompany MiguelCompany added this to the v2.4.1 milestone Oct 26, 2026
@heathsanchez

Copy link
Copy Markdown

Independent verification of the fix in 1eaa6c809d464fb5d1c1b9be03ffcc4adc29c2ed, using native ROS 2 Jazzy deserialization in isolated child processes. Thanks for the fix.

The completed run #41280186916 covers 448 executions: 2 arms × 2 RMW backends × 7 test_msgs layouts × 16 input cases. For the ROS comparison, both arms build Fast-CDR v2.2.8 from the same source, with only this PR's Cdr.cpp fix applied to the patched arm; the loaded library paths are checked. The exact PR head (2.4.0) was also built separately, without substituting its ABI into Jazzy.

For rmw_fastrtps_cpp (112 cases per arm):

Outcome Baseline Patched
Process signal 24 0
Timeout (6 s) 1 0
Caught deserialization error 71 101
Accepted 16 11

All seven layouts reject the reported four-byte header-only input after the fix. This includes five signal→error cases and one accepted→error case; WStrings already returned an error. All 14 valid controls (seven layouts across both backends) pass in each arm. The Cyclone DDS comparator still has failures, so this result is limited to the tested Fast-CDR/Fast DDS boundary.

Evidence artifact: per-case outcomes, comparison, source/patch and library fingerprints, package versions, mapped-library checks and build logs. Harness at the run commit.

AI assistance was used for the verification harness, analysis and this summary; the figures above were checked against the completed run's output. This is additional regression evidence for this PR, rather than a general deserialization-safety claim.

@Rahul-cs50

Copy link
Copy Markdown
Author

Thanks for checking this independently across both backends, @heathsanchez. It's good to see the four-byte header-only input rejected in all seven layouts, with the valid controls still passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants