Skip to content

Fix IPv6 extension header bounds validation - #1379

Open
pamirmundt wants to merge 1 commit into
FreeRTOS:mainfrom
pamirmundt:fix/ipv6-extension-header-bounds
Open

pamirmundt wants to merge 1 commit into
FreeRTOS:mainfrom
pamirmundt:fix/ipv6-extension-header-bounds

Conversation

@pamirmundt

@pamirmundt pamirmundt commented Oct 25, 2026 •

Copy link
Copy Markdown

Description

xCheckIPv6SizeFields() could read beyond the received packet when processing a truncated IPv6 extension-header chain with ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 1.

The parser accessed the extension header's fields before checking whether enough bytes remained. For example, if a packet contained only the first byte of the next extension header, the parser could read past the buffer while accessing its length field.

This change validates the available bytes before reading each extension header and ensures that the declared header length fits within the IPv6 payload.

It also uses remaining-length checks for the IPv6 payload and upper-layer protocol headers, avoiding unnecessary arithmetic on absolute offsets.

Testing

Added regression tests covering:

  • Truncated first extension headers
  • Truncated extension-header chains
  • Extensions extending beyond the declared IPv6 payload
  • Extensions ending at the packet boundary
  • Valid single- and multiple-extension chains
  • Exact UDP header lengths following extensions

The truncation regression reproduces a one-byte out-of-bounds read with AddressSanitizer against the original implementation and passes with this fix.

Verification results:

  • All 84 relevant IPv6 tests pass on both 32-bit and 64-bit builds with ASan/UBSan enabled.
  • Repository formatting and whitespace checks pass.

The changes are limited to packet-length validation and preserve the existing handling of valid IPv6 packets.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@htibosch htibosch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pamirmundt , thanks for taking the afford to present these findings.

If I may ask, why did you start digging into the extension headers? Did you see a problem?

I tested the proposed changes in real silicon (STM32H7), connected with a switch to my laptop.

What we did is send extension headers of all sorts to trigger the various traps.

ext_headers.log

If you want I can send you the PY script.

I'm approving this PR.

@pamirmundt

Copy link
Copy Markdown
Author

@pamirmundt , thanks for taking the afford to present these findings.

If I may ask, why did you start digging into the extension headers? Did you see a problem?

I tested the proposed changes in real silicon (STM32H7), connected with a switch to my laptop.

What we did is send extension headers of all sorts to trigger the various traps.

ext_headers.log

If you want I can send you the PY script.

I'm approving this PR.

While working on IPv6 UDP support on our STM32H7, one of our coding agents flagged the extension header parsing during a code review. We hadn’t encountered any runtime issues, but decided to investigate and validate the finding.

Check extension header lengths before accessing their fields to prevent out-of-bounds reads on truncated packets.

Validate lengths against the declared IPv6 payload and add regression tests for malformed and valid extension chains.
@pamirmundt
pamirmundt force-pushed the fix/ipv6-extension-header-bounds branch 2 times, most recently from 826ae68 to 3b2c9fc Compare November 2, 2026 20:06

This branch has not been deployed

No deployments
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.

2 participants