git: 22a632c366a9 - main - pf: Make pf_test6 handle m_len < sizeof(struct ip6_hdr) case

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Mon, 19 Aug 2024 16:02:46 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=22a632c366a98692d7114135241c10f154e52a76

commit 22a632c366a98692d7114135241c10f154e52a76
Author:     Igor Ostapenko <pm@igoro.pro>
AuthorDate: 2024-08-16 14:49:06 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2024-08-19 16:02:20 +0000

    pf: Make pf_test6 handle m_len < sizeof(struct ip6_hdr) case
    
    Reviewed by:    kp
    Differential Revision:  https://reviews.freebsd.org/D46312
---
 sys/netpfil/pf/pf.c          |  8 ++++++
 tests/sys/netpfil/pf/mbuf.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index 2bbd231b3ee9..9b1601ac0ee5 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -8946,6 +8946,14 @@ pf_test6(int dir, int pflags, struct ifnet *ifp, struct mbuf **m0, struct inpcb
 	pd.af = AF_INET6;
 	pd.act.rtableid = -1;
 
+	if (__predict_false(m->m_len < sizeof(struct ip6_hdr)) &&
+	    (m = *m0 = m_pullup(*m0, sizeof(struct ip6_hdr))) == NULL) {
+		DPFPRINTF(PF_DEBUG_URGENT,
+		    ("pf_test6: m_len < sizeof(struct ip6_hdr)"
+		     ", pullup failed\n"));
+		PF_RULES_RUNLOCK();
+		return (PF_DROP);
+	}
 	h = mtod(m, struct ip6_hdr *);
 	off = ((caddr_t)h - m->m_data) + sizeof(struct ip6_hdr);
 
diff --git a/tests/sys/netpfil/pf/mbuf.sh b/tests/sys/netpfil/pf/mbuf.sh
index 082de08b0838..2dffa48ed2f5 100644
--- a/tests/sys/netpfil/pf/mbuf.sh
+++ b/tests/sys/netpfil/pf/mbuf.sh
@@ -91,7 +91,68 @@ inet_in_mbuf_len_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "inet6_in_mbuf_len" "cleanup"
+inet6_in_mbuf_len_head()
+{
+	atf_set descr 'Test that pf can handle inbound with the first mbuf with m_len < sizeof(struct ip6_hdr)'
+	atf_set require.user root
+}
+inet6_in_mbuf_len_body()
+{
+	pft_init
+	dummymbuf_init
+
+	epair=$(vnet_mkepair)
+	ifconfig ${epair}a inet6 2001:db8::1/64 up no_dad
+
+	# Set up a simple jail with one interface
+	vnet_mkjail alcatraz ${epair}b
+	jexec alcatraz ifconfig ${epair}b inet6 2001:db8::2/64 up no_dad
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c1 2001:db8::2
+
+	# Should be denied
+	jexec alcatraz pfctl -e
+	pft_set_rules alcatraz \
+		"block" \
+		"pass quick inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }"
+	atf_check -s not-exit:0 -o ignore ping -c1 -t1 2001:db8::2
+
+	# Should be allowed by from/to addresses
+	pft_set_rules alcatraz \
+		"block" \
+		"pass quick inet6 proto icmp6 icmp6-type { neighbrsol, neighbradv }" \
+		"pass in inet6 from 2001:db8::1 to 2001:db8::2"
+	atf_check -s exit:0 -o ignore ping -c1 2001:db8::2
+
+	# Should still work for m_len=0
+	jexec alcatraz pfilctl link -i dummymbuf:inet6 inet6
+	jexec alcatraz sysctl net.dummymbuf.rules="inet6 in ${epair}b pull-head 0;"
+	atf_check_equal "0" "$(jexec alcatraz sysctl -n net.dummymbuf.hits)"
+	atf_check -s exit:0 -o ignore ping -c1 2001:db8::2
+	atf_check_equal "1" "$(jexec alcatraz sysctl -n net.dummymbuf.hits)"
+
+	# m_len=1
+	jexec alcatraz sysctl net.dummymbuf.rules="inet6 in ${epair}b pull-head 1;"
+	jexec alcatraz sysctl net.dummymbuf.hits=0
+	atf_check -s exit:0 -o ignore ping -c1 2001:db8::2
+	atf_check_equal "1" "$(jexec alcatraz sysctl -n net.dummymbuf.hits)"
+
+	# m_len=39
+	# provided IPv6 basic header is 40 bytes long, it should impact the dst addr
+	jexec alcatraz sysctl net.dummymbuf.rules="inet6 in ${epair}b pull-head 39;"
+	jexec alcatraz sysctl net.dummymbuf.hits=0
+	atf_check -s exit:0 -o ignore ping -c1 2001:db8::2
+	atf_check_equal "1" "$(jexec alcatraz sysctl -n net.dummymbuf.hits)"
+}
+inet6_in_mbuf_len_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "inet_in_mbuf_len"
+	atf_add_test_case "inet6_in_mbuf_len"
 }