Skip to content

external/dhcpc: Fix out-of-bounds reads in DHCP option parsing - #7671

Open
mangeshraut712 wants to merge 2 commits into
Samsung:masterfrom
mangeshraut712:fix/dhcpc-options-bounds
Open

mangeshraut712 wants to merge 2 commits into
Samsung:masterfrom
mangeshraut712:fix/dhcpc-options-bounds

Conversation

@mangeshraut712

Copy link
Copy Markdown

Fixes #7665

dhcpc_parsemsg skips the 4-byte magic cookie when it hands the options field to dhcpc_parseoptions, but it does not subtract those 4 bytes from the length. The parser's end pointer therefore sits 4 bytes past options[312], the trailing field of the heap-allocated struct dhcpc_state_s.

dhcpc_parseoptions then reads each option without checking it against the end:

  • A truncated SUBNET_MASK, ROUTER, SERVER_ID or LEASE_TIME option at the end of the field reads 4 bytes past the allocation; a DNS_SERVER option with a declared length of 8 reads up to 9 bytes past it.
  • A lone option code at the end reads a length byte that is not in the buffer.
  • DHCP_OPTION_PAD (0) falls to the default case and is skipped as a 2-byte option, so the option that follows a PAD is dropped.

The change. dhcpc_parsemsg subtracts the cookie from the length, rejects a non-positive length, and requires the cookie to be present before parsing. In dhcpc_parseoptions, PAD is 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 compiled dhcpc_parseoptions on 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.

  • Before: an ordinary OFFER parses correctly, and a truncated SUBNET_MASK at the end of the field triggers heap-buffer-overflow: READ of size 4 in dhcpc_parseoptions. A DNS_SERVER option whose length overruns the field overflows the same way. A PAD followed by a message type fails to parse.
  • After: 14 of 14 checks pass with no sanitizer report. The ordinary OFFER (message type, lease time, subnet, router, two DNS servers) parses to the same values as before; PAD, END and zero-length options are handled; every truncated option stops parsing without reading past the field.
  • The length arithmetic was checked on its own: a full 548-byte packet leaves 308 bytes of options (312 minus the cookie), and every shorter packet is rejected.

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.

mangeshraut712 and others added 2 commits December 1, 2026 17:09
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>
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.

Possible out-of-bounds read: dhcpc_parsemsg and dhcpc_parseoptions walk DHCPv4 options without magic-cookie subtraction or per-option bounds checks

1 participant