Repository navigation
external/dhcpc: Fix out-of-bounds reads in DHCP option parsing - #7671
Open
mangeshraut712 wants to merge 2 commits into
Open
mangeshraut712 wants to merge 2 commits into
mangeshraut712 wants to merge 2 commits into
Conversation
dhcpc_parsemsg skipped the 4-byte magic cookie when it passed the options field to dhcpc_parseoptions, but did not subtract those 4 bytes from the length, so the parser's end pointer sat 4 bytes past options[312] in the heap-allocated struct dhcpc_state_s. dhcpc_parseoptions then read every option without checking it against the end: a truncated SUBNET, ROUTER, SERVER_ID or LEASE_TIME option at the end of the field, or a DNS option whose declared length overruns it, read up to 9 bytes past the allocation. A lone option code at the end read a length byte that was not there. DHCP_OPTION_PAD (0) fell to the default case and was skipped as a 2-byte option, dropping the option after it. dhcpc_parsemsg now subtracts the cookie from the length, rejects a non-positive length, and requires the cookie to be present before parsing. dhcpc_parseoptions skips PAD as one byte and checks that each option's code, length and payload lie inside the field before reading them. Reported-by: OvOhao Refs: Samsung#7665 Signed-off-by: Mangesh Raut <95149253+mangeshraut712@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
dhcpc.c keeps its own list of option constants and does not include lwip's dhcp.h, so DHCP_OPTION_PAD was undeclared and the pad-option skip added for the bounds fix failed to compile on every configuration that builds the DHCP client (artik053/tc and artik055s/audio in CI). Define it as 0, its value in RFC 2132 section 3.1, next to the other constants. Refs Samsung#7665 Signed-off-by: Mangesh Raut <95149253+mangeshraut712@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Fixes #7665
dhcpc_parsemsgskips the 4-byte magic cookie when it hands the options field todhcpc_parseoptions, but it does not subtract those 4 bytes from the length. The parser's end pointer therefore sits 4 bytes pastoptions[312], the trailing field of the heap-allocatedstruct dhcpc_state_s.dhcpc_parseoptionsthen reads each option without checking it against the end:SUBNET_MASK,ROUTER,SERVER_IDorLEASE_TIMEoption at the end of the field reads 4 bytes past the allocation; aDNS_SERVERoption with a declared length of 8 reads up to 9 bytes past it.DHCP_OPTION_PAD(0) falls to thedefaultcase and is skipped as a 2-byte option, so the option that follows a PAD is dropped.The change.
dhcpc_parsemsgsubtracts the cookie from the length, rejects a non-positive length, and requires the cookie to be present before parsing. Indhcpc_parseoptions,PADis skipped as one byte, and each option's code, length and payload are checked against the end of the field before anything is read. Parsing stops at the first option that does not fit.Verification. There is no host test target for
dhcpc, so I compileddhcpc_parseoptionson the host (extracted from the source file, not retyped) with the options field placed at the end of a heap allocation, and ran it under AddressSanitizer against the code before and after this change.SUBNET_MASKat the end of the field triggersheap-buffer-overflow: READ of size 4indhcpc_parseoptions. ADNS_SERVERoption whose length overruns the field overflows the same way. A PAD followed by a message type fails to parse.ENDand zero-length options are handled; every truncated option stops parsing without reading past the field.Credit for finding and analysing this goes to @OvOhao, who reported it in #7665 with the suggested fix; this PR implements it. I can add the harness as a regression test if you would like one kept with the code.