git: fd59b508f07d - main - pf: fix removing kif when clearing flags

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 18:11:52 UTC
The branch main has been updated by kp:

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

commit fd59b508f07d5a0794210643c8efe20075baa5e9
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-11-20 12:17:49 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-23 17:17:12 +0000

    pf: fix removing kif when clearing flags
    
    When we clear flags on a struct pfi_kkif we checked if it was still
    used, and if not immediately removed it.
    This check did not take states into account. struct pf_kstate may also
    reference kifs, in `kif` or `orig_kif`. To safely remove a kif we have
    to sweep the state table and ensure nothing there uses the relevant kif.
    Fortunately pfi_kkif_remove_if_unref() already does this (along with the
    purge thread and pfi_kkif_purge()), so use that.
    
    Reported by:    Kevin Day <kevin@your.org>
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf_if.c           |  8 ++----
 tests/sys/netpfil/pf/set_skip.sh | 56 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/sys/netpfil/pf/pf_if.c b/sys/netpfil/pf/pf_if.c
index 57dfb0a8031d..1277fcc1374b 100644
--- a/sys/netpfil/pf/pf_if.c
+++ b/sys/netpfil/pf/pf_if.c
@@ -1005,12 +1005,8 @@ pfi_clear_flags(const char *name, int flags)
 			continue;
 		atomic_clear_int(&p->pfik_flags, flags);
 
-		if (p->pfik_ifp == NULL && p->pfik_group == NULL &&
-		    p->pfik_flags == 0 && p->pfik_rulerefs == 0) {
-			/* Delete this kif. */
-			RB_REMOVE(pfi_ifhead, &V_pfi_ifs, p);
-			pf_kkif_free(p);
-		}
+		/* Delete this kif. */
+		pfi_kkif_remove_if_unref(p);
 	}
 	NET_EPOCH_EXIT(et);
 	return (0);
diff --git a/tests/sys/netpfil/pf/set_skip.sh b/tests/sys/netpfil/pf/set_skip.sh
index e984377721b8..0f5c26ecf098 100644
--- a/tests/sys/netpfil/pf/set_skip.sh
+++ b/tests/sys/netpfil/pf/set_skip.sh
@@ -221,6 +221,61 @@ pr255852_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "unref_kif" "cleanup"
+unref_kif_head()
+{
+	atf_set descr 'Create a situation where we unref a kif and states still reference it'
+	atf_set require.user root
+}
+
+unref_kif_body()
+{
+	pft_init
+
+	epair=$(vnet_mkepair)
+
+	ifconfig ${epair}a 192.0.2.1/24 up
+
+	vnet_mkjail alcatraz ${epair}b
+	jexec alcatraz ifconfig lo0 127.0.0.1/8 up
+	jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
+
+	# Sanity check
+	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
+
+	jexec alcatraz pfctl -e
+	pft_set_rules alcatraz \
+		"pass"
+
+	# Create a state
+	atf_check -s exit:0 -o ignore ping -c 1 192.0.2.2
+
+	# Note: flushing avoid the issue
+	pft_set_rules noflush alcatraz \
+		"set skip on ${epair}b" \
+		"pass"
+
+	# The state should still exist here
+	atf_check -e ignore -o match:"all icmp 192.0.2.2:8.*" \
+	    jexec alcatraz pfctl -ss -vv
+
+	# Delete the epair interface
+	ifconfig ${epair}a destroy
+
+	# Now clear the skip flag.
+	pft_set_rules noflush alcatraz \
+		"pass"
+
+	# The state still exists, and references the kif
+	atf_check -e ignore -o match:"all icmp 192.0.2.2:8.*" \
+	    jexec alcatraz pfctl -ss -vv
+}
+
+unref_kif_cleanup()
+{
+	pft_cleanup
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case "unset"
@@ -228,4 +283,5 @@ atf_init_test_cases()
 	atf_add_test_case "set_skip_group_lo"
 	atf_add_test_case "set_skip_dynamic"
 	atf_add_test_case "pr255852"
+	atf_add_test_case "unref_kif"
 }