git: b5039a81b410 - main - dhclient: Fix validation of IP and UDP lengths
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 27 Nov 2026 14:53:29 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=b5039a81b410b6f19de170f3779dd95ee42be787
commit b5039a81b410b6f19de170f3779dd95ee42be787
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-27 14:51:49 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-27 14:51:49 +0000
dhclient: Fix validation of IP and UDP lengths
decode_udp_ip_header() assumed that the sizes recorded in the IP and UDP
headers are valid, but if not, OOB memory accesses are possible.
Add checks, and use unsigned types for buffer sizes and offsets.
Add regression tests.
Reported by: Brendan Davidson <binary141@gmail.com>
Reviewed by: emaste
MFC after: 1 week
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62120
---
sbin/dhclient/bpf.c | 3 +-
sbin/dhclient/dhcpd.h | 12 +++---
sbin/dhclient/packet.c | 30 +++++++++-----
sbin/dhclient/tests/functional.py | 82 ++++++++++++++++++++++++++++++++++++++-
4 files changed, 110 insertions(+), 17 deletions(-)
diff --git a/sbin/dhclient/bpf.c b/sbin/dhclient/bpf.c
index d4bbe56332c8..4a716ea5bcfc 100644
--- a/sbin/dhclient/bpf.c
+++ b/sbin/dhclient/bpf.c
@@ -344,7 +344,8 @@ send_packet_priv(struct interface_info *interface, struct imsg_hdr *hdr, int fd)
struct dhcp_packet raw;
size_t len;
struct in_addr from, to;
- int result, bufp = 0;
+ size_t bufp = 0;
+ int result;
if (hdr->len < sizeof(*hdr) + sizeof(size_t))
error("corrupted message received");
diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h
index bd89ed1be4a9..c2801dcc5f2b 100644
--- a/sbin/dhclient/dhcpd.h
+++ b/sbin/dhclient/dhcpd.h
@@ -430,12 +430,12 @@ void bootp(struct packet *);
void dhcp(struct packet *);
/* packet.c */
-void assemble_hw_header(struct interface_info *, unsigned char *, int *);
-void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
- unsigned int, unsigned char *, int);
-ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
-ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
- unsigned char *, int);
+void assemble_hw_header(struct interface_info *, unsigned char *, size_t *);
+void assemble_udp_ip_header(unsigned char *, size_t *, u_int32_t, u_int32_t,
+ unsigned int, unsigned char *, size_t);
+ssize_t decode_hw_header(unsigned char *, size_t, struct hardware *);
+ssize_t decode_udp_ip_header(unsigned char *, size_t, struct sockaddr_in *,
+ unsigned char *, size_t);
/* clparse.c */
int read_client_conf(void);
diff --git a/sbin/dhclient/packet.c b/sbin/dhclient/packet.c
index fc0305a8cb0c..d1e9cbb082da 100644
--- a/sbin/dhclient/packet.c
+++ b/sbin/dhclient/packet.c
@@ -90,7 +90,7 @@ wrapsum(u_int32_t sum)
void
assemble_hw_header(struct interface_info *interface, unsigned char *buf,
- int *bufix)
+ size_t *bufix)
{
struct ether_header eh;
@@ -108,8 +108,8 @@ assemble_hw_header(struct interface_info *interface, unsigned char *buf,
}
void
-assemble_udp_ip_header(unsigned char *buf, int *bufix, u_int32_t from,
- u_int32_t to, unsigned int port, unsigned char *data, int len)
+assemble_udp_ip_header(unsigned char *buf, size_t *bufix, u_int32_t from,
+ u_int32_t to, unsigned int port, unsigned char *data, size_t len)
{
struct ip ip;
struct udphdr udp;
@@ -148,7 +148,7 @@ assemble_udp_ip_header(unsigned char *buf, int *bufix, u_int32_t from,
}
ssize_t
-decode_hw_header(unsigned char *buf, int bufix, struct hardware *from)
+decode_hw_header(unsigned char *buf, size_t bufix, struct hardware *from)
{
struct ether_header eh;
@@ -163,8 +163,8 @@ decode_hw_header(unsigned char *buf, int bufix, struct hardware *from)
}
ssize_t
-decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
- unsigned char *data, int buflen)
+decode_udp_ip_header(unsigned char *buf, size_t bufix, struct sockaddr_in *from,
+ unsigned char *data, size_t buflen)
{
struct ip *ip;
struct udphdr *udp;
@@ -176,7 +176,12 @@ decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
static int udp_packets_bad_checksum;
static int udp_packets_length_checked;
static int udp_packets_length_overflow;
- int len = 0;
+ unsigned int len = 0;
+
+ if (ip_len < sizeof(*ip) || buflen < ip_len + sizeof(*udp)) {
+ debug("invalid ip length %u", ip_len);
+ return (-1);
+ }
ip = (struct ip *)(buf + bufix);
udp = (struct udphdr *)(buf + bufix + ip_len);
@@ -195,7 +200,7 @@ decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
}
if (ntohs(ip->ip_len) != buflen)
- debug("ip length %d disagrees with bytes received %d.",
+ debug("ip length %d disagrees with bytes received %zu.",
ntohs(ip->ip_len), buflen);
memcpy(&from->sin_addr, &ip->ip_src, 4);
@@ -206,8 +211,15 @@ decode_udp_ip_header(unsigned char *buf, int bufix, struct sockaddr_in *from,
* we're not supposed to do a checksum.
*/
if (!data) {
+ unsigned int ulen;
+
+ ulen = ntohs(udp->uh_ulen);
+ if ((size_t)ulen < sizeof(*udp)) {
+ debug("invalid UDP length %d.", ulen);
+ return (-1);
+ }
+ len = ulen - sizeof(*udp);
data = buf + bufix + ip_len + sizeof(*udp);
- len = ntohs(udp->uh_ulen) - sizeof(*udp);
udp_packets_length_checked++;
if (len + data > buf + bufix + buflen) {
udp_packets_length_overflow++;
diff --git a/sbin/dhclient/tests/functional.py b/sbin/dhclient/tests/functional.py
index 518d1050aba7..6da88e43315f 100644
--- a/sbin/dhclient/tests/functional.py
+++ b/sbin/dhclient/tests/functional.py
@@ -7,6 +7,8 @@
# SPDX-License-Identifier: BSD-2-Clause
#
+import socket
+import struct
import subprocess
import pytest
@@ -20,6 +22,17 @@ def mac_to_chaddr(mac_str):
return mac_bytes + b"\x00" * (16 - len(mac_bytes))
+def inet_cksum(data):
+ """Compute the Internet checksum of a byte string."""
+ data = bytes(data)
+ if len(data) % 2 != 0:
+ data += b"\x00"
+ s = sum(struct.unpack("!{}H".format(len(data) // 2), data))
+ while s > 0xffff:
+ s = (s & 0xffff) + (s >> 16)
+ return ~s & 0xffff
+
+
class TestDhclient(VnetTestTemplate):
_SERVER_IP = "192.0.2.1"
_CLIENT_IP = "192.0.2.10"
@@ -71,13 +84,16 @@ class TestDhclient(VnetTestTemplate):
return ifname
def _do_dora(self, ifname, first_extra_opts=None, extra_opts=None,
- sname=b"", file=b""):
+ sname=b"", file=b"", first_offer_hook=None):
"""
Run a discover-offer-request-acknowledge exchange and
return (discover_count, result).
If first_extra_opts is set, it is used in the OFFER for the first
DHCPDISCOVER only; subsequent DISCOVERs use extra_opts.
+
+ If first_offer_hook is set, it is called with the OFFER for the
+ first DHCPDISCOVER, and the packet it returns is sent instead.
"""
discover_count = 0
done = False
@@ -101,6 +117,8 @@ class TestDhclient(VnetTestTemplate):
ifname, self._SERVER_IP, self._CLIENT_IP, client_mac,
xid, "offer", extra_opts=opts, sname=sname, file=file,
)
+ if discover_count == 1 and first_offer_hook is not None:
+ reply = first_offer_hook(reply)
self.sp.sendp(reply, iface=ifname, verbose=0)
elif msg_type == "request":
@@ -245,3 +263,65 @@ class TestDhclient(VnetTestTemplate):
"Server name with quote should not appear in the lease file"
assert "server-name" not in result["lease"], \
"Unsafe server name should be discarded entirely"
+
+ def _offer_ip_hl_short(self, offer):
+ """Encode an OFFER with an 8-byte IP header."""
+ payload = bytes(offer[self.sp.BOOTP])
+ ulen = 8 + len(payload)
+ ip = bytearray(struct.pack("!BBHHH", 0x42, 0, 8 + ulen, 0, 0))
+ struct.pack_into("!H", ip, 4, inet_cksum(ip))
+ udp = struct.pack("!HHHH", (64 << 8) | socket.IPPROTO_UDP, 68,
+ ulen, 0)
+ return self.sp.Raw(load=bytes(offer)[:14] + ip + udp + payload)
+
+ def _offer_udp_hdr_truncated(self, offer):
+ """
+ Build a frame which ends immediately after the UDP destination
+ port, the last field examined by dhclient's BPF filter.
+ """
+ ip = bytearray(struct.pack(
+ "!BBHHHBBH4s4s", 0x4f, 0, 60 + 4, 0, 0, 64, socket.IPPROTO_UDP,
+ 0, socket.inet_aton(self._SERVER_IP),
+ socket.inet_aton("255.255.255.255"),
+ ))
+ ip += b"\x01" * 40
+ struct.pack_into("!H", ip, 10, inet_cksum(ip))
+ udp = struct.pack("!HH", 67, 68)
+ return self.sp.Raw(load=bytes(offer)[:14] + ip + udp)
+
+ def _offer_udp_len_short(self, offer, ulen, chksum):
+ """Set the UDP length and checksum fields of an OFFER."""
+ bad = offer.copy()
+ bad[self.sp.UDP].len = ulen
+ bad[self.sp.UDP].chksum = chksum
+ return bad
+
+ @pytest.mark.parametrize("kind", [
+ "ip_hl_short",
+ "udp_hdr_truncated",
+ "udp_len_short_cksum",
+ "udp_len_short_nocksum",
+ ])
+ @pytest.mark.require_user("root")
+ @pytest.mark.require_progs(["scapy"])
+ def test_malformed_udp_ip_hdr(self, kind):
+ """
+ Verify that dhclient discards packets with invalid IP header or
+ UDP lengths. A bogus packet is sent in place of the first
+ DHCPOFFER.
+ """
+ ifname = self._setup_server()
+ hooks = {
+ "ip_hl_short": self._offer_ip_hl_short,
+ "udp_hdr_truncated": self._offer_udp_hdr_truncated,
+ "udp_len_short_cksum":
+ lambda offer: self._offer_udp_len_short(offer, 7, 0xffff),
+ "udp_len_short_nocksum":
+ lambda offer: self._offer_udp_len_short(offer, 0, 0),
+ }
+ discover_count, result = self._do_dora(
+ ifname, first_offer_hook=hooks[kind],
+ )
+ assert discover_count >= 2, \
+ "Expected dhclient to discard the bogus offer and retry"
+ assert "inet {}".format(self._CLIENT_IP) in result["ifconfig"]