Skip to content

Validate lengths in the UDP, IPv4 and IPv6 receive paths - #23

Open
DicksonWu654 wants to merge 1 commit into
butok:masterfrom
DicksonWu654:fix-rx-length-validation
Open

DicksonWu654 wants to merge 1 commit into
butok:masterfrom
DicksonWu654:fix-rx-length-validation

Conversation

@DicksonWu654

Copy link
Copy Markdown

Hi Andrej,

Going by your note in #19, here are fixes for a few crashes and hangs in the receive path. Each one is triggered by a single unauthenticated frame. I tested on master 1a4d001 and on v4.7.1, which have the same code here.

Crashes

UDP length below 8 (fnet_udp.c, _fnet_udp_input). With a UDP length of 0, _fnet_netbuf_trim(&nb, udp_length - nb->total_length) trims the whole chain and frees it, and the NULL nb gets dereferenced. A 33-byte IPv4 UDP datagram does it: to any port, listener or not, when the checksum field is nonzero, and to a listening port even with a zero checksum. That works on an IPv4-only build with UDP on, which is the default. Fix: drop the datagram when udp_length < sizeof(fnet_udp_header_t).

Empty IPv6 chain (fnet_ip6.c). An IPv6 packet with Next Header 0 and a payload length of 0, or one whose whole payload is an 8-byte Fragment header, leaves nb NULL after the trim, and the next step dereferences it. Fix: check for an empty chain after the trims in _fnet_ip6_input_low, _fnet_ip6_ext_header_process and _fnet_ip6_reassembly. The same check (plus saving the header fields before the trim) is added in _fnet_ip4_input_low and _fnet_ip4_reassembly. I didn't get a crash from the IPv4 side, so treat those as hardening.

Hangs

Hop-by-Hop / Destination Options length (_fnet_ip6_ext_header_handler_options). When fewer than 2 bytes are left, nb->length - 2u wraps, the length check passes, the trim does nothing, and Destination Options gets processed again forever. Going from Hop-by-Hop to Destination Options gets past the Hop-by-Hop repeat check. Fix: reject when nb->length < 2.

Routing header length (_fnet_ip6_ext_header_handler_routing). hdr_ext_length isn't checked against the remaining data, so a Routing header that claims more than is there makes the trim a no-op and the loop never moves on. Fix: drop it when the header doesn't fit.

Fully overlapping fragment (_fnet_ip6_reassembly). A fragment completely covered by an earlier one has i == its length, so it gets past the i > cur_frag_ptr->total_length check instead of being dropped, and the later concatenation ends up with a cycle. Fix: >=.

Fragment header inside a fragment (_fnet_ip6_reassembly). The reassembled datagram gets the inner Next Header copied into the fixed header. If that's 44, the same payload goes back into reassembly forever. Fix: drop fragments whose Next Header is Fragment.

The IPv6 ones need FNET_CFG_IP6, which is off in the stack default but on in 8 of the 9 fnet_user_config.h files in the repo, and IPv6 fragmentation defaults on with it. They trigger with an on-link ff02::1 destination and also with a routed global unicast one.

Testing

I built the stack with -m32 -O2, using the normal netbuf heap and a fake Ethernet driver that calls _fnet_eth_input, and ran each input 20 times in fresh processes:

  • Without the patch: the UDP and empty-chain inputs crash 20/20, and the four hang inputs never return (1 s deadline, 20/20). That held on both master and v4.7.1, and the UDP case also crashes on an IPv4-only build.
  • With the patch: every input is dropped cleanly.
  • Valid traffic still gets delivered on all builds: IPv4 UDP, IPv4 fragments, IPv6 UDP, IPv6 fragments, and IPv6 UDP behind Hop-by-Hop, Routing and Destination Options headers. In each case 8 bytes reach a bound socket.

Happy to share the test frames or split this into one commit per fix if that's easier to review.

Single malformed frames could crash the stack or spin the receive loop:

- UDP: a length field below 8 made the trim free the whole chain, and
  the NULL netbuf was then dereferenced. Drop such datagrams.
- IPv4/IPv6: trimming the network or extension header could leave an
  empty chain that the next step dereferenced. Check for it, and save
  the IPv4 header fields before the trim.
- IPv6 Hop-by-Hop/Destination Options: "nb->length - 2u" wrapped when
  under two bytes were left, so the header was reprocessed forever.
- IPv6 Routing: Hdr Ext Len wasn't checked against the data, so the trim
  was a no-op and the header loop never advanced.
- IPv6 reassembly: a fragment fully covered by an earlier one created a
  cycle in the chain, and a Fragment header inside the fragmentable part
  re-entered reassembly forever. Drop both.
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.

1 participant