Repository navigation
Validate lengths in the UDP, IPv4 and IPv6 receive paths - #23
Open
DicksonWu654 wants to merge 1 commit into
Open
DicksonWu654 wants to merge 1 commit into
DicksonWu654 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 NULLnbgets 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 whenudp_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, leavesnbNULL 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_processand_fnet_ip6_reassembly. The same check (plus saving the header fields before the trim) is added in_fnet_ip4_input_lowand_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 - 2uwraps, 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 whennb->length < 2.Routing header length (
_fnet_ip6_ext_header_handler_routing).hdr_ext_lengthisn'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 hasi ==its length, so it gets past thei > cur_frag_ptr->total_lengthcheck 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 9fnet_user_config.hfiles in the repo, and IPv6 fragmentation defaults on with it. They trigger with an on-linkff02::1destination 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:Happy to share the test frames or split this into one commit per fix if that's easier to review.