git: 53e0d3fb3bcd - main - pf: reject table anchors with empty path components

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 16:28:41 UTC
The branch main has been updated by kp:

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

commit 53e0d3fb3bcd980ba919106abaaf55ac901ecc58
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-18 10:25:39 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-18 16:28:21 +0000

    pf: reject table anchors with empty path components
    
    pfr_fix_anchor() strips leading slashes from table anchors but not
    trailing or repeated ones.  pf_find_or_create_kruleset() ignores those,
    so a table added as "foo/bar/" lands in ruleset "foo/bar" but keeps
    "foo/bar/" as its anchor.  pfr_table_count() and pfr_skip_table() then
    disagree, and with INVARIANTS:
    
    ```
    panic: pfr_get_tables: corruption detected (1)
    ```
    
    "foo//bar" does the same once "foo" exists, and pfctl -vsT hits the
    same check in pfr_get_tstats().
    
    Loading rules into such an anchor already fails with EINVAL, since
    pf_find_kruleset() doesn't find it.  Reject it for tables as well.
    
    Reviewed by:    kp
    MFC after:      1 week
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D61409
---
 sys/netpfil/pf/pf_table.c     |  7 +++++++
 tests/sys/netpfil/pf/table.sh | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/sys/netpfil/pf/pf_table.c b/sys/netpfil/pf/pf_table.c
index 8a97428faafa..84df199ecb65 100644
--- a/sys/netpfil/pf/pf_table.c
+++ b/sys/netpfil/pf/pf_table.c
@@ -1895,6 +1895,13 @@ pfr_fix_anchor(char *anchor)
 	for (i = strlen(anchor); i < siz; i++)
 		if (anchor[i])
 			return (-1);
+	/*
+	 * Ruleset paths have no empty components.  A table anchor with one
+	 * would not match the path of the ruleset the table is attached to.
+	 */
+	if (strstr(anchor, "//") != NULL ||
+	    (anchor[0] != '\0' && anchor[strlen(anchor) - 1] == '/'))
+		return (-1);
 	return (0);
 }
 
diff --git a/tests/sys/netpfil/pf/table.sh b/tests/sys/netpfil/pf/table.sh
index 2b7c84032d41..aa1620627aee 100644
--- a/tests/sys/netpfil/pf/table.sh
+++ b/tests/sys/netpfil/pf/table.sh
@@ -619,6 +619,37 @@ anchor_cleanup()
 	pft_cleanup
 }
 
+atf_test_case "anchor_slash" "cleanup"
+anchor_slash_head()
+{
+	atf_set descr 'Test tables in anchors with empty path components'
+	atf_set require.user root
+}
+
+anchor_slash_body()
+{
+	pft_init
+
+	atf_check -s exit:0 -e ignore \
+	    pfctl -a foo/bar -t t1 -T add 192.0.2.1
+	atf_check -s exit:0 -e ignore -o match:"192.0.2.1" \
+	    pfctl -a /foo/bar -t t1 -T show
+
+	atf_check -s not-exit:0 -e ignore \
+	    pfctl -a foo/bar/ -t t2 -T add 192.0.2.2
+	atf_check -s not-exit:0 -e ignore \
+	    pfctl -a foo//bar -t t3 -T add 192.0.2.3
+
+	# Used to trip the corruption check in pfr_get_tables()
+	atf_check -s exit:0 -e ignore -o inline:"t1@foo/bar\n" \
+	    pfctl -a foo/bar -sT
+}
+
+anchor_slash_cleanup()
+{
+	pft_cleanup
+}
+
 atf_test_case "flush" "cleanup"
 flush_head()
 {
@@ -1156,6 +1187,7 @@ atf_init_test_cases()
 	atf_add_test_case "pr259689"
 	atf_add_test_case "precreate"
 	atf_add_test_case "anchor"
+	atf_add_test_case "anchor_slash"
 	atf_add_test_case "flush"
 	atf_add_test_case "large"
 	atf_add_test_case "show_recursive"