From nobody Fri Nov 13 22:07:52 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jRBG11mrPz6yCF2 for ; Fri, 13 Nov 2026 22:07:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jRBG10TSGz3KND for ; Fri, 13 Nov 2026 22:07:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794607673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8F0aoGB1g68revffHbpx6Qb8FmH5X9lCuyyASAYxI14=; b=sjOnvnHLbkTnyT0n1TTdy2rihVNra/jH1kyfeNCte21KwCZDG8WF5pzWUtrQJbwojKIUus A7JkdMmPUd01Gy/Ct5Tw5E5pTkdEaZoLEXyHTQeE7lqOlXMFq9Y4buiZpiYvY2w/Q9ksUZ Yn/LuI8gLJ9DmG2XWAeG0BICsCQl53UTlAmdjHE6fMdFy197YwXOZRpNCUuMfNW3Yi3OoC 285kQInQ3J1eYC+1BYyTOEmobLFaZE3j1GtkrJLhxhbfGJM+yEbqu3cg/PZTUX1dqnlb49 hdX1qwmfgeU9aUnt7A0AcXSnE2P/ZGnppmwvDXcf5Q84P3B3dVsxtTTW288Ycw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794607673; b=MGobqL83FLLxHgchRJM8o4q4MnO/snXrTHAyKF3d27CY5tB0hoA0zyK5/5yxm9Kto8PdSV Wt5OLdffPDj699Xic9nBWlpNbZir17rd4MxwNfJmldrtKO68wADMZPn9TzWIIdiRHTLSqB f6j45DM/Q8r+Qej2DP52ZRyuHWjRi2D3F7Bnba3SS/c9nehGehF8yb5JzCKoDhFljiAVyy Jk7vlP0pCtOn/fjNOzGgw38PDru6k8t3jGg4O4Uq8xTQoZO7i2ovFh79YdMrIYBSVA6nFY gSX8Gr5iNAxMTBOsCKai9O7qDJJnBoB9pbT1F7Evy5/hPS+Rv3IEPfJluPonOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794607673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8F0aoGB1g68revffHbpx6Qb8FmH5X9lCuyyASAYxI14=; b=vu8xdXMveGGJpYH3N48tlyUAP69QM7j37ERVsQY3ZYlaYfBAcKGyk0nhsZrrrcIcmlFp8Y vQox07V0E1YlVLeP1nTxHEBQCgC3SsCfjcmPRIF5szvj4MS8LhE2tB4KBspmtf2RbkMX6Q jJpb5h44Ddc1cZlqqVNQFIpcQWvOt9Pwaac+slyX8J+nAEJqrHRAO9AxSiGWwywUqPYujO hFdOJthqcQz9ONXrRF5XhHoefV430ooOFTSB1uiXtP0rHf1EakCCa6Hhrt8mrcXBiFRWoE v65k/PNnte3n2P+Cl+3rzuZUQhrrSxH8+hAiceBo1nArynCVMFJpYJbP/rctaQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jRBG103C7zv3T for ; Fri, 13 Nov 2026 22:07:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id df10 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 13 Nov 2026 22:07:52 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: d926285ea366 - stable/14 - dhclient: Add some regression tests List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: d926285ea366da13930c9cebef9b04ebd9fb36ba Auto-Submitted: auto-generated Date: Fri, 13 Nov 2026 22:07:52 +0000 Message-Id: <6af78a38.df10.1a96039@gitrepo.freebsd.org> The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=d926285ea366da13930c9cebef9b04ebd9fb36ba commit d926285ea366da13930c9cebef9b04ebd9fb36ba Author: Mark Johnston AuthorDate: 2026-10-14 21:12:51 +0000 Commit: Mark Johnston CommitDate: 2026-11-13 22:07:31 +0000 dhclient: Add some regression tests Add tests for some recently fixed bugs in dhclient. All of these tests set up two VNET jails connected by an epair. dhclient runs in one jail, and the test code runs on the other end. It uses scapy to inject "interesting" packets in response to DHCPDISCOVER messages from dhclient, then checks the configured interface and lease file. MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D60485 (cherry picked from commit a143197801c680b958ab662c88cf086668a1958b) --- sbin/dhclient/tests/Makefile | 2 + sbin/dhclient/tests/functional.py | 242 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 244 insertions(+) diff --git a/sbin/dhclient/tests/Makefile b/sbin/dhclient/tests/Makefile index 7d4359d4b762..bba05108193a 100644 --- a/sbin/dhclient/tests/Makefile +++ b/sbin/dhclient/tests/Makefile @@ -3,6 +3,8 @@ ATF_TESTS_SH= pcp +ATF_TESTS_PYTEST+= functional.py + # Tests assign a common IP address. TEST_METADATA.pcp+= is_exclusive=true diff --git a/sbin/dhclient/tests/functional.py b/sbin/dhclient/tests/functional.py new file mode 100644 index 000000000000..3a984307c691 --- /dev/null +++ b/sbin/dhclient/tests/functional.py @@ -0,0 +1,242 @@ +# +# Copyright (c) 2026 The FreeBSD Foundation +# +# This software was developed by Mark Johnston under sponsorship from +# the FreeBSD Foundation. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +import subprocess + +import pytest +import scapy.all as sp +from atf_python.sys.net.tools import ToolsHelper +from atf_python.sys.net.vnet import VnetTestTemplate + + +def mac_to_chaddr(mac_str): + """Convert a MAC address string to a 16-byte BOOTP chaddr.""" + mac_bytes = bytes.fromhex(mac_str.replace(":", "")) + return mac_bytes + b"\x00" * (16 - len(mac_bytes)) + + +def get_dhcp_type(pkt): + """Extract the DHCP message type from a packet.""" + if not pkt.haslayer(sp.DHCP): + return None + for opt in pkt[sp.DHCP].options: + if isinstance(opt, tuple) and opt[0] == "message-type": + return sp.DHCPTypes[opt[1]] + return None + + +def build_reply(iface, server_ip, client_ip, client_mac, xid, msg_type, + extra_opts=None, sname=b"", file=b""): + """Build a DHCP OFFER or ACK packet.""" + options = [ + ("message-type", msg_type), + ("server_id", server_ip), + ("lease_time", 3600), + ("subnet_mask", "255.255.255.0"), + ("router", server_ip), + ] + if extra_opts: + options.extend(extra_opts) + options.append("end") + + return ( + sp.Ether(dst=client_mac, src=sp.get_if_hwaddr(iface)) + / sp.IP(src=server_ip, dst="255.255.255.255") + / sp.UDP(sport=67, dport=68) + / sp.BOOTP(op=2, + xid=xid, + yiaddr=client_ip, + siaddr=server_ip, + chaddr=mac_to_chaddr(client_mac), + sname=sname, + file=file, + ) + / sp.DHCP(options=options) + ) + + +class TestDhclient(VnetTestTemplate): + _SERVER_IP = "192.0.2.1" + _CLIENT_IP = "192.0.2.10" + + TOPOLOGY = { + "vnet1": {"ifaces": ["if1"]}, + "vnet2": {"ifaces": ["if1"]}, + "if1": {}, + } + + def vnet1_handler(self, vnet): + """ + Run dhclient in vnet1 (the client side of the epair). Send + back the output of "ifconfig " once dhclient has finished + its exchange. + """ + ifname = vnet.iface_alias_map["if1"].name + + # Wait for the test body to be ready before starting dhclient. + vnet.pipe.recv() + + try: + result = subprocess.run( + ["/sbin/dhclient", "-c", "/dev/null", + "-l", "dhclient.lease", ifname], + capture_output=True, text=True, timeout=30, + ) + stderr = result.stderr + except subprocess.TimeoutExpired: + stderr = "dhclient timed out" + out = ToolsHelper.get_output("/sbin/ifconfig {}".format(ifname)) + lease = "" + try: + with open("dhclient.lease", "r") as f: + lease = f.read() + except FileNotFoundError: + pass + vnet.pipe.send({"ifconfig": out, "stderr": stderr, "lease": lease}) + + def _setup_server(self): + """Configure the server side of the epair and return its name.""" + ifname = self.vnet.iface_alias_map["if1"].name + ToolsHelper.print_output( + "/sbin/ifconfig {} inet {}/24 up".format(ifname, self._SERVER_IP) + ) + return ifname + + def _do_dora(self, ifname, first_extra_opts=None, extra_opts=None, + sname=b"", file=b""): + """ + 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. + """ + discover_count = 0 + done = False + + def handle_dhcp(pkt): + nonlocal discover_count, done + + msg_type = get_dhcp_type(pkt) + if msg_type is None: + return + + client_mac = pkt[sp.Ether].src + xid = pkt[sp.BOOTP].xid + + if msg_type == "discover": + discover_count += 1 + opts = extra_opts + if discover_count == 1 and first_extra_opts is not None: + opts = first_extra_opts + reply = build_reply( + ifname, self._SERVER_IP, self._CLIENT_IP, client_mac, + xid, "offer", extra_opts=opts, sname=sname, file=file, + ) + sp.sendp(reply, iface=ifname, verbose=0) + + elif msg_type == "request": + reply = build_reply( + ifname, self._SERVER_IP, self._CLIENT_IP, client_mac, + xid, "ack", sname=sname, file=file, + ) + sp.sendp(reply, iface=ifname, verbose=0) + done = True + + sniffer = sp.AsyncSniffer( + iface=ifname, + filter="udp and dst port 67", + prn=handle_dhcp, + stop_filter=lambda _: done, + started_callback=lambda: self.send_object( + self.vnet_map["vnet1"].pipe, "go", + ), + ) + sniffer.start() + sniffer.join(timeout=30) + + assert done, "DHCP exchange did not complete" + result = self.wait_object(self.vnet_map["vnet1"].pipe, timeout=15) + return discover_count, result + + @pytest.mark.require_user("root") + def test_classless_routes_truncated(self): + """ + Verify that dhclient rejects a DHCPOFFER containing a truncated + classless static routes option. + + A malformed option 121 payload is constructed with a valid first + entry (default route, width=0) followed by a second entry with + width=24 that has only 1 byte of subnet data instead of the + required 3 bytes. + + The test sends the bad offer first, then a valid offer, and + verifies that dhclient rejects the bad offer and successfully + acquires a lease from the good one. + """ + ifname = self._setup_server() + bad_routes = ( + b"\x00" # width=0 (default route) + b"\xc0\xa8\x01\x01" # gateway: 192.168.1.1 + b"\x18" # width=24 (/24 subnet) + b"\x0a" # only 1 of 3 required subnet octets + ) + discover_count, result = self._do_dora( + ifname, first_extra_opts=[(121, bad_routes)], + ) + assert discover_count >= 2, \ + "Expected dhclient to reject the first (bad) offer and retry" + assert "inet {}".format(self._CLIENT_IP) in result["ifconfig"] + + @pytest.mark.require_user("root") + def test_filename_backslash(self): + """ + Verify that backslashes in the BOOTP filename don't cause the + lease to be rejected. These can appear in Windows boot paths. + """ + ifname = self._setup_server() + discover_count, result = self._do_dora( + ifname, file=b"\\windows\\boot\\pxeboot.com", + ) + assert discover_count == 1, \ + "Lease with backslash in filename should be accepted" + assert "inet {}".format(self._CLIENT_IP) in result["ifconfig"] + + @pytest.mark.require_user("root") + def test_sname_valid(self): + """ + Verify that a valid server name is preserved in the lease file. + """ + ifname = self._setup_server() + discover_count, result = self._do_dora( + ifname, sname=b"dhcp.example.com", + ) + assert discover_count == 1 + assert "inet {}".format(self._CLIENT_IP) in result["ifconfig"] + assert "dhcp.example.com" in result["lease"] + + @pytest.mark.require_user("root") + def test_sname_quote(self): + """ + Verify that a double quote in the BOOTP server name causes the + server name to be discarded but does not cause the lease to be + rejected. Values from the lease file may be handled by + dhclient-script and thus are susceptible to shell injection. + """ + ifname = self._setup_server() + discover_count, result = self._do_dora( + ifname, sname=b'evil"server', + ) + assert discover_count == 1, \ + "Lease with quote in sname should be accepted" + assert "inet {}".format(self._CLIENT_IP) in result["ifconfig"] + assert 'evil"server' not in result["lease"], \ + "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"