Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178047077
D62120.1795844351.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
D62120.1795844351.diff
View Options
diff --git a/sbin/dhclient/bpf.c b/sbin/dhclient/bpf.c
--- a/sbin/dhclient/bpf.c
+++ b/sbin/dhclient/bpf.c
@@ -344,7 +344,8 @@
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
--- a/sbin/dhclient/dhcpd.h
+++ b/sbin/dhclient/dhcpd.h
@@ -430,12 +430,12 @@
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
--- a/sbin/dhclient/packet.c
+++ b/sbin/dhclient/packet.c
@@ -90,7 +90,7 @@
void
assemble_hw_header(struct interface_info *interface, unsigned char *buf,
- int *bufix)
+ size_t *bufix)
{
struct ether_header eh;
@@ -108,8 +108,8 @@
}
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 @@
}
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 @@
}
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 @@
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 @@
}
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 @@
* 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
--- 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 @@
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 @@
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 @@
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 @@
"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"]
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 28, 5:39 AM (13 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42052281
Default Alt Text
D62120.1795844351.diff (8 KB)
Attached To
Mode
D62120: dhclient: Fix validation of IP and UDP lengths
Attached
Detach File
Event Timeline