From nobody Fri Jun 6 11:17:13 2025 X-Original-To: dev-commits-src-main@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 4bDJgZ1B8vz5yCDY; Fri, 06 Jun 2025 11:17:14 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bDJgY47Chz3Jf9; Fri, 06 Jun 2025 11:17:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749208633; 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=6R/Djv1+NBaootyoAy7/8RH40Um20omo3SRH4gJVyik=; b=khSkvx8bIxEe8zI74TRyA46MeAzl2VU+3CYy2lycOfoj84EsohkfpsKBZg/i/0jQHDkP/q 9B6Lolaxb+iqDBAQdA54bWsPsEwXEEKqDZx9MykZarzswgkqoBoeaxQyOpq0TkPSYDHGX+ NjGvSLcB9a3lAhXPFqe6fCxXCsuyD80NUQkgobIIZgxXXLwjYKjhh9onIiwVdN/b4kKsIH JWt6NNJUOdzdEJXcD1pu3ICfFLacN5993u6vkLoOaZymXPRSw6+wKbSBerbZX2cCOQx1ts wJKV2ocZzUmXKV7FdrBKdZhSiqApAciWEu1RmHnXwZsmmJuat3OGU0ArybbtWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749208633; 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=6R/Djv1+NBaootyoAy7/8RH40Um20omo3SRH4gJVyik=; b=DEyhk18h7ew8M86AcKQOmwnbw6dpDb9nSccHXi+Fj3oslvqeSobBIRh/EedS30M+KnMwmL c4kXrNtM5YePn8tp0xPmQKidrrXkeolX+U8LwQlK9X2ZSW9kzKEh3CjcXBiA8j/VWYJwoD lkv5kIkCa55XhpNL0wNe9rYkDOBWo49X3exY+6pizVfWYmMGQex1PKSNWcxqCD2bUY8vHz xj9d2lNAHPsrlHq2gLYXnCqsH7KersRllSJ3Eukjmr3/lA/PmCAjhD05/Mp8z8vjV4Cb0V HZIkI9XCa5Wze4RCAxeQx0O0fWC9VpcXFm2upLwrZwcKZiawfyl9LP5fYvkQSw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1749208633; a=rsa-sha256; cv=none; b=W3XCQZRs7PXq6ko13eiuWKTA2PaaNhmRBrS9G2InKNy+fwk+H+l4JWzzetHihPUQ2je9uF 4HbqpF1wUxtqj/8GPJNs61P7o91gisui5l+nUn9erWHqMF04EwZIh9vsoMkjYDp5eaZkxO fYGCrErOtrZXfHx6SJlwGhaZs0B1pXQNfa3rwG6rdocU+bghQIPC7UdtP7GRLMMn43ev4j bCnSodqmEhe/OzdaWbijk4pZbvjiQ/Fs1KnrO1zGN9xUdDk8uRlb/Kf7u2Xd2iE5yQ8j2O Tpw0HIUdThJ+8G+OW7JxnplVVBiMBwh+WGRQ+TJNOvw4i6ouRvA1/wWha49c9g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bDJgY3V2jzrv2; Fri, 06 Jun 2025 11:17:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 556BHDJC083677; Fri, 6 Jun 2025 11:17:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 556BHDvo083674; Fri, 6 Jun 2025 11:17:13 GMT (envelope-from git) Date: Fri, 6 Jun 2025 11:17:13 GMT Message-Id: <202506061117.556BHDvo083674@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 32546d57f3bb - main - pf tests: verify header processing limit List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 32546d57f3bb371ca17943509a8315b73de684c9 Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=32546d57f3bb371ca17943509a8315b73de684c9 commit 32546d57f3bb371ca17943509a8315b73de684c9 Author: Kristof Provost AuthorDate: 2025-05-26 16:03:22 +0000 Commit: Kristof Provost CommitDate: 2025-06-06 11:16:00 +0000 pf tests: verify header processing limit Verify that we drop packets with 20 or more AH headers (IPv4) or 20 or more extension headers (IPv6). Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D50660 --- tests/sys/netpfil/pf/Makefile | 1 + tests/sys/netpfil/pf/header.py | 216 +++++++++++++++++++++++++++++++++++++++++ tests/sys/netpfil/pf/utils.py | 9 +- 3 files changed, 224 insertions(+), 2 deletions(-) diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile index d70631a9de53..e926c9dd50c2 100644 --- a/tests/sys/netpfil/pf/Makefile +++ b/tests/sys/netpfil/pf/Makefile @@ -54,6 +54,7 @@ ATF_TESTS_SH+= altq \ tos ATF_TESTS_PYTEST+= frag6.py +ATF_TESTS_PYTEST+= header.py ATF_TESTS_PYTEST+= icmp.py ATF_TESTS_PYTEST+= nat64.py ATF_TESTS_PYTEST+= nat66.py diff --git a/tests/sys/netpfil/pf/header.py b/tests/sys/netpfil/pf/header.py new file mode 100644 index 000000000000..71abc8e9a049 --- /dev/null +++ b/tests/sys/netpfil/pf/header.py @@ -0,0 +1,216 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2025 Rubicon Communications, LLC (Netgate) +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +import pytest +import re +from utils import DelayedSend +from atf_python.sys.net.tools import ToolsHelper +from atf_python.sys.net.vnet import VnetTestTemplate + +class TestHeader(VnetTestTemplate): + REQUIRED_MODULES = [ "pf" ] + TOPOLOGY = { + "vnet1": {"ifaces": ["if1", "if2"]}, + "vnet2": {"ifaces": ["if1", "if2"]}, + "if1": {"prefixes4": [("192.0.2.2/24", "192.0.2.1/24")]}, + "if2": {"prefixes4": [("198.51.100.1/24", "198.51.100.2/24")]}, + } + + def vnet2_handler(self, vnet): + ToolsHelper.print_output("/sbin/sysctl net.inet.ip.forwarding=1") + ToolsHelper.print_output("/usr/sbin/arp -s 198.51.100.3 00:01:02:03:04:05") + ToolsHelper.print_output("/sbin/pfctl -e") + ToolsHelper.print_output("/sbin/pfctl -x loud") + ToolsHelper.pf_rules([ + "pass", + ]) + + @pytest.mark.require_user("root") + @pytest.mark.require_progs(["scapy"]) + def test_too_many(self): + "Verify that we drop packets with silly numbers of headers." + + sendif = self.vnet.iface_alias_map["if1"].name + recvif = self.vnet.iface_alias_map["if2"].name + gw_mac = ToolsHelper.get_output("/sbin/ifconfig %s ether | awk '/ether/ { print $2; }'" % sendif) + gw_mac = re.sub("0a$", "0b", gw_mac) + + ToolsHelper.print_output("/sbin/route add default 192.0.2.1") + + # Import in the correct vnet, so at to not confuse Scapy + import scapy.all as sp + + # Sanity check, ensure we get replies to normal ping + pkt = sp.Ether(dst=gw_mac) \ + / sp.IP(dst="198.51.100.3") \ + / sp.ICMP(type='echo-request') + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + + found = False + for r in reply: + r.show() + + icmp = r.getlayer(sp.ICMP) + if not icmp: + continue + assert icmp.type == 8 # 'echo-request' + found = True + assert found + + # Up to 19 AH headers will pass + pkt = sp.Ether(dst=gw_mac) \ + / sp.IP(dst="198.51.100.3") + for i in range(0, 18): + pkt = pkt / sp.AH(nh=51, payloadlen=1) + pkt = pkt / sp.AH(nh=1, payloadlen=1) / sp.ICMP(type='echo-request') + + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + found = False + for r in reply: + r.show() + + ah = r.getlayer(sp.AH) + if not ah: + continue + found = True + assert found + + # But more will get dropped + pkt = sp.Ether(dst=gw_mac) \ + / sp.IP(dst="198.51.100.3") + for i in range(0, 19): + pkt = pkt / sp.AH(nh=51, payloadlen=1) + pkt = pkt / sp.AH(nh=1, payloadlen=1) / sp.ICMP(type='echo-request') + + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + + found = False + for r in reply: + r.show() + + ah = r.getlayer(sp.AH) + if not ah: + continue + found = True + assert not found + +class TestHeader6(VnetTestTemplate): + REQUIRED_MODULES = [ "pf" ] + TOPOLOGY = { + "vnet1": {"ifaces": ["if1", "if2"]}, + "vnet2": {"ifaces": ["if1", "if2"]}, + "if1": {"prefixes6": [("2001:db8::2/64", "2001:db8::1/64")]}, + "if2": {"prefixes6": [("2001:db8:1::2/64", "2001:db8:1::1/64")]}, + } + + def vnet2_handler(self, vnet): + ToolsHelper.print_output("/sbin/sysctl net.inet6.ip6.forwarding=1") + ToolsHelper.print_output("/usr/sbin/ndp -s 2001:db8:1::3 00:01:02:03:04:05") + ToolsHelper.print_output("/sbin/pfctl -e") + ToolsHelper.print_output("/sbin/pfctl -x loud") + ToolsHelper.pf_rules([ + "pass", + ]) + + @pytest.mark.require_user("root") + @pytest.mark.require_progs(["scapy"]) + def test_too_many(self): + "Verify that we drop packets with silly numbers of headers." + ToolsHelper.print_output("/sbin/ifconfig") + + sendif = self.vnet.iface_alias_map["if1"].name + recvif = self.vnet.iface_alias_map["if2"].name + our_mac = ToolsHelper.get_output("/sbin/ifconfig %s ether | awk '/ether/ { print $2; }'" % sendif) + gw_mac = re.sub("0a$", "0b", our_mac) + + ToolsHelper.print_output("/sbin/route -6 add default 2001:db8::1") + + # Import in the correct vnet, so at to not confuse Scapy + import scapy.all as sp + + # Sanity check, ensure we get replies to normal ping + pkt = sp.Ether(src=our_mac, dst=gw_mac) \ + / sp.IPv6(src="2001:db8::2", dst="2001:db8:1::3") \ + / sp.ICMPv6EchoRequest() + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + + found = False + for r in reply: + r.show() + + icmp = r.getlayer(sp.ICMPv6EchoRequest) + if not icmp: + continue + found = True + assert found + + # Up to 19 AH headers will pass + pkt = sp.Ether(src=our_mac, dst=gw_mac) \ + / sp.IPv6(src="2001:db8::2", dst="2001:db8:1::3") + for i in range(0, 18): + pkt = pkt / sp.AH(nh=51, payloadlen=1) + pkt = pkt / sp.AH(nh=58, payloadlen=1) / sp.ICMPv6EchoRequest() + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + + found = False + for r in reply: + r.show() + + ah = r.getlayer(sp.AH) + if not ah: + continue + found = True + assert found + + # But more will get dropped + pkt = sp.Ether(src=our_mac, dst=gw_mac) \ + / sp.IPv6(src="2001:db8::2", dst="2001:db8:1::3") + for i in range(0, 19): + pkt = pkt / sp.AH(nh=51, payloadlen=1) + pkt = pkt / sp.AH(nh=58, payloadlen=1) / sp.ICMPv6EchoRequest() + s = DelayedSend(pkt, sendif) + reply = sp.sniff(iface=recvif, timeout=3) + print(reply) + + found = False + for r in reply: + r.show() + + ah = r.getlayer(sp.AH) + if not ah: + continue + found = True + assert not found diff --git a/tests/sys/netpfil/pf/utils.py b/tests/sys/netpfil/pf/utils.py index 3cd8278f7cf7..3d1c1de86aad 100644 --- a/tests/sys/netpfil/pf/utils.py +++ b/tests/sys/netpfil/pf/utils.py @@ -28,14 +28,19 @@ import threading import time class DelayedSend(threading.Thread): - def __init__(self, packet): + def __init__(self, packet, sendif=None): threading.Thread.__init__(self) self._packet = packet + self._sendif = sendif self.start() def run(self): import scapy.all as sp time.sleep(1) - sp.send(self._packet) + + if self._sendif: + sp.sendp(self._packet, iface=self._sendif) + else: + sp.send(self._packet)