git: 66f2f1c83247 - main - pf: handle divert packets

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Sat, 15 Nov 2025 21:39:07 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=66f2f1c83247f05a3a599d7e88c7e7efbedd16b5

commit 66f2f1c83247f05a3a599d7e88c7e7efbedd16b5
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2025-11-15 13:44:54 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2025-11-15 21:38:21 +0000

    pf: handle divert packets
    
    In a divert setup pf_test_state() may return PF_PASS, but not set the state
    pointer. We didn't handle that, and as a result crashed immediately afterwards
    trying to dereference that NULL state pointer.
    
    Add a test case to provoke the problem.
    
    PR:             260867
    MFC after:      2 weeks
    Submitted by:   Phil Budne <phil.budne@gmail.com>
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf.c               | 20 ++++++++++--------
 tests/sys/netpfil/pf/divert-to.sh | 43 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index a39f5fe58cd6..5e919e35b07b 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -11075,10 +11075,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0
 			break;
 		action = pf_test_state(&s, &pd, &reason);
 		if (action == PF_PASS || action == PF_AFRT) {
-			if (V_pfsync_update_state_ptr != NULL)
-				V_pfsync_update_state_ptr(s);
-			r = s->rule;
-			a = s->anchor;
+			if (s != NULL) {
+				if (V_pfsync_update_state_ptr != NULL)
+					V_pfsync_update_state_ptr(s);
+				r = s->rule;
+				a = s->anchor;
+			}
 		} else if (s == NULL) {
 			/* Validate remote SYN|ACK, re-create original SYN if
 			 * valid. */
@@ -11127,10 +11129,12 @@ pf_test(sa_family_t af, int dir, int pflags, struct ifnet *ifp, struct mbuf **m0
 	default:
 		action = pf_test_state(&s, &pd, &reason);
 		if (action == PF_PASS || action == PF_AFRT) {
-			if (V_pfsync_update_state_ptr != NULL)
-				V_pfsync_update_state_ptr(s);
-			r = s->rule;
-			a = s->anchor;
+			if (s != NULL) {
+				if (V_pfsync_update_state_ptr != NULL)
+					V_pfsync_update_state_ptr(s);
+				r = s->rule;
+				a = s->anchor;
+			}
 		} else if (s == NULL) {
 			action = pf_test_rule(&r, &s,
 			    &pd, &a, &ruleset, &reason, inp, &match_rules);
diff --git a/tests/sys/netpfil/pf/divert-to.sh b/tests/sys/netpfil/pf/divert-to.sh
index ae44cd5d51af..153136199311 100644
--- a/tests/sys/netpfil/pf/divert-to.sh
+++ b/tests/sys/netpfil/pf/divert-to.sh
@@ -372,6 +372,47 @@ in_dn_in_div_in_out_div_out_dn_out_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "pr260867" "cleanup"
+pr260867_head()
+{
+	atf_set descr 'Test for the loop reported in PR260867'
+	atf_set require.user root
+}
+
+pr260867_body()
+{
+	pft_init
+	divert_init
+
+	epair=$(vnet_mkepair)
+
+	ifconfig ${epair}a 192.0.2.1/24 up
+
+	vnet_mkjail alcatraz ${epair}b
+	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c3 192.0.2.2
+
+	jexec alcatraz /usr/sbin/inetd -p ${PWD}/inetd-echo.pid $(atf_get_srcdir)/echo_inetd.conf
+	jexec alcatraz $(atf_get_srcdir)/../common/divapp 1001 divert-back &
+
+	jexec alcatraz pfctl -e
+	pft_set_rules alcatraz \
+	    "pass in on ${epair}b proto tcp from any to port 7 divert-to 0.0.0.0 port 1001"
+
+	reply=$(echo "foo" | nc -N 192.0.2.2 7)
+	if ["${reply}" != "foo" ];
+	then
+		atf_fail "Did not receive echo reply"
+	fi
+}
+
+pr260867_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "in_div"
@@ -383,4 +424,6 @@ atf_init_test_cases()
 	atf_add_test_case "in_div_in_fwd_out_div_out"
 
 	atf_add_test_case "in_dn_in_div_in_out_div_out_dn_out"
+
+	atf_add_test_case "pr260867"
 }