Bug 290478 - pf(1): anchors are not listed correctly when nested anchors exist
Summary: pf(1): anchors are not listed correctly when nested anchors exist
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-pf (Nobody)
URL: https://reviews.freebsd.org/D53358
Keywords: regression
Depends on:
Blocks:
 
Reported: 2025-10-24 12:30 UTC by Marek Zarychta
Modified: 2025-10-30 05:13 UTC (History)
2 users (show)

See Also:


Attachments
Add tests (1.25 KB, patch)
2025-10-26 00:40 UTC, Jose Luis Duran
no flags Details | Diff
Add tests (1.23 KB, patch)
2025-10-26 01:55 UTC, Jose Luis Duran
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Zarychta 2025-10-24 12:30:49 UTC
While testing migration from blacklistd to blocklistd, I found that anchors are not listed correctly by the command "pf -sA". In the past, they were not listed recursively; however, at least the parent anchors were displayed. Now, only the first anchor with its first subanchor shows up:

# pfctl -a blocklistd -sA
  25
pfctl: Anchor does not exist.

But, for example, such anchors do exist:

# pfctl -a blocklistd/25 -sr
block drop in quick proto tcp from <port25> to any port = smtp
# pfctl -a blocklistd/465 -sr
block drop in quick proto tcp from <port465> to any port = smtps
Comment 1 Marek Zarychta 2025-10-24 12:37:04 UTC
I should have begun by explaining that the affected system is the most recent 15.0-STABLE, and when no specific anchor is requested, still only two are displayed:

# pfctl -sA
  blacklistd
  22

This system has at least "blacklistd" anchor with several subanchors, and "blocklist" anchor, also with several subanchors.
Comment 2 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-24 15:39:34 UTC
(In reply to Marek Zarychta from comment #0)

With regards to the message:

pfctl: Anchor does not exist.

It is referring to a nonexistent ethernet anchor named blacklist (bug #280516).
Comment 3 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-24 15:42:22 UTC
(In reply to Marek Zarychta from comment #1)

What we can do in the meantime, is "hide" those error messages?
Comment 4 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-24 16:30:42 UTC
(In reply to Jose Luis Duran from comment #2)

I apologize, please discard comment #2. It does not apply.
Comment 5 Marek Zarychta 2025-10-24 16:35:05 UTC
(In reply to Jose Luis Duran from comment #3)
It's not the same bug, but a problem with recursive anchors. 

After entering the first subanchor, the loop gets exited, and no further anchors are displayed. IIRC, someone requested displaying nested anchors with the default listing; the request was fulfilled, but probably also introduced this regression.

On another machine:

# pfctl -sA
  blocklistd
  ftp-proxy
  student-vm
  virt-pc
  pc30
pfctl: Anchor does not exist.

The "pc30" is the first subanchor nested in virt-pc, but there are more of them:

# pfctl -a virt-pc/pc30 -sn
rdr on vlan0 inet proto tcp from any to 89.x.x.x port = xxx30 -> x.y.z.30 port 3389
# pfctl -a virt-pc/pc31 -sn
rdr on vlan0 inet proto tcp from any to 89.x.x.x port = xxx31 -> x.y.z.31 port 3389
# pfctl -a virt-pc/pc32 -sn
rdr on vlan0 inet proto tcp from any to 89.x.x.x port = xxx32 -> x.y.z.32 port 3389
...


The reported issue is only mild, since the above rdr anchors are working fine.
Comment 6 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-24 17:03:04 UTC
(In reply to Marek Zarychta from comment #5)

Yes, sorry for the confusion, and thank you for improving the tile.

MWE:

# pfctl -a parent/child1 -t table1 -T add 192.0.2.0/24
1 table created.
1/1 addresses added.

# pfctl -a parent/child2 -t table2 -T add 198.51.100.0/24
1 table created.
1/1 addresses added.

# pfctl -sA
  parent
  child1
pfctl: Anchor does not exist.
Comment 7 Marek Zarychta 2025-10-24 17:09:42 UTC
(In reply to Jose Luis Duran from comment #6)
Yes, likely in bug 280516, you were also falling into this.

Bug 262622 probably opened this Pandora's box with the nested anchors listing. I'm not sure if they were ever displayed correctly. In the past, I used syntax:

#pfctl -a parrentanchor -sA 

to list the children, and it worked. But it was in the unspecified past.
Comment 8 Marek Zarychta 2025-10-24 17:41:06 UTC
(In reply to Marek Zarychta from comment #7)

Please forgive blaming the patch committed to close bug 262622. Listing rules recursively is actually helpful, since commands: 

#pfctl -a "*" -sn

and 

#pfctl -a "*" -sr

are actually the only method to list all anchors correctly.
Comment 9 Marek Zarychta 2025-10-24 22:02:30 UTC
This patch fixes the issue for me:


diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c
index 190ee46baf21..83332433fd11 100644
--- a/lib/libpfctl/libpfctl.c
+++ b/lib/libpfctl/libpfctl.c
@@ -3158,6 +3158,7 @@ pfctl_get_rulesets(struct pfctl_handle *h, const char *path, uint32_t *nr)
 	}
 
 	*nr = rs.nr;
+	e.error = (e.error == ENOENT) ? 0 : e.error;
 
 	return (e.error);
 }
Comment 10 Marek Zarychta 2025-10-25 07:09:17 UTC
It appears that for pfctl(8), the transition to netlink(4) still has some rough edges. The patch from comment #9 is only a temporary workaround, not a real fix. Since the issue is minor and a workaround is known, I’ll leave the bug for future attention.
Comment 11 commit-hook freebsd_committer freebsd_triage 2025-10-25 23:25:49 UTC
A commit in branch main references this bug:

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

commit 67ade69eb6079887215db1fde86eba2fb8e2acf7
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2025-10-25 23:14:48 +0000
Commit:     Jose Luis Duran <jlduran@FreeBSD.org>
CommitDate: 2025-10-25 23:21:07 +0000

    blocklistd-helper: Silence another bogus pf warning

    It has been reported as PR 290478.  In the meantime, just sweep under
    the carpet.

    It is worth noting that neither commit:
    2347ca21d657 ("blocklist-helper: Silence a bogus pf warning")
    nor this one will be upstreamed, as this is a FreeBSD-specific issue.

    PR:             290478
    MFC after:      2 days

 contrib/blocklist/libexec/blocklistd-helper | 2 +-
 libexec/blocklistd-helper/blacklistd-helper | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Comment 12 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-26 00:40:46 UTC
Created attachment 264888 [details]
Add tests

Add a test that illustrates the regression.
Comment 13 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-26 01:47:19 UTC
I believe the regression was introduced in:

041ce1d690f1 ("pfctl: recursively flush rules and tables")
Comment 14 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-26 01:55:44 UTC
Created attachment 264889 [details]
Add tests
Comment 15 Marek Zarychta 2025-10-26 06:47:15 UTC
The patch proposed in review D53358 solves the issue. Thanks for chasing it. 
I have tested it with double-nested anchors, and they are also fine. TBH, I'm not sure if such anchors are very popular, but extending the test case would be appreciated.
Comment 16 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-26 12:06:55 UTC
(In reply to Marek Zarychta from comment #15)
Thank you Marek for reporting and testing.
Just to clarify, when you say:

> I have tested it with double-nested anchors, and they are also fine. TBH, I'm not
> sure if such anchors are very popular, but extending the test case would be
> appreciated.

By double-nested you mean deeply-nested anchors like:

 foo/bar/baz

If so, I can absolutely extend the test case.
Comment 17 Marek Zarychta 2025-10-26 14:10:49 UTC
(In reply to Jose Luis Duran from comment #16)
>By double-nested, you mean deeply-nested anchors like:
>
> foo/bar/baz
Yes, extending the test case with the following entries would be nice:

foo/bar/foobar
foo/bar/quux 
foo/baz
foo/qux

This syntax, though rarely used, is definitely supported by pf(1).
Comment 18 commit-hook freebsd_committer freebsd_triage 2025-10-28 11:49:43 UTC
A commit in branch main references this bug:

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

commit a943a96a50ba7e9d1e1935bdd18df0e11d158acb
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2025-10-25 09:19:18 +0000
Commit:     Jose Luis Duran <jlduran@FreeBSD.org>
CommitDate: 2025-10-28 11:29:19 +0000

    libpfctl: Fix displaying deeply nested anchors

    Set the number of rulesets (i.e., anchors) directly attached to the
    anchor and its path in pfctl_get_ruleset().

    While here, add a test to document this behavior.

    PR:             290478
    Reviewed by:    kp
    Fixes:          041ce1d690f1 ("pfctl: recursively flush rules and tables")
    MFC after:      2 days
    Differential Revision:  https://reviews.freebsd.org/D53358

 lib/libpfctl/libpfctl.c        |  3 +++
 tests/sys/netpfil/pf/anchor.sh | 46 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
Comment 19 commit-hook freebsd_committer freebsd_triage 2025-10-30 01:52:37 UTC
A commit in branch stable/15 references this bug:

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

commit 1c8a554f757de06f64e6fd0d86fc674a215ee314
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2025-10-25 09:19:18 +0000
Commit:     Jose Luis Duran <jlduran@FreeBSD.org>
CommitDate: 2025-10-30 01:48:03 +0000

    libpfctl: Fix displaying deeply nested anchors

    Set the number of rulesets (i.e., anchors) directly attached to the
    anchor and its path in pfctl_get_ruleset().

    While here, add a test to document this behavior.

    PR:             290478
    Reviewed by:    kp
    Fixes:          041ce1d690f1 ("pfctl: recursively flush rules and tables")
    MFC after:      2 days
    Differential Revision:  https://reviews.freebsd.org/D53358

    (cherry picked from commit a943a96a50ba7e9d1e1935bdd18df0e11d158acb)

 lib/libpfctl/libpfctl.c        |  3 +++
 tests/sys/netpfil/pf/anchor.sh | 46 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
Comment 20 Jose Luis Duran freebsd_committer freebsd_triage 2025-10-30 01:53:23 UTC
Thank you for the bug report!
Comment 21 commit-hook freebsd_committer freebsd_triage 2025-10-30 04:37:08 UTC
A commit in branch releng/15.0 references this bug:

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

commit b39a387442db422ff3e962a440cf7dc7ec277df3
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2025-10-25 09:19:18 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-30 04:36:20 +0000

    libpfctl: Fix displaying deeply nested anchors

    Set the number of rulesets (i.e., anchors) directly attached to the
    anchor and its path in pfctl_get_ruleset().

    While here, add a test to document this behavior.

    Approved by:    re (cperciva)
    PR:             290478
    Reviewed by:    kp
    Fixes:          041ce1d690f1 ("pfctl: recursively flush rules and tables")
    MFC after:      2 days
    Differential Revision:  https://reviews.freebsd.org/D53358

    (cherry picked from commit a943a96a50ba7e9d1e1935bdd18df0e11d158acb)
    (cherry picked from commit 1c8a554f757de06f64e6fd0d86fc674a215ee314)

 lib/libpfctl/libpfctl.c        |  3 +++
 tests/sys/netpfil/pf/anchor.sh | 46 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
Comment 22 Marek Zarychta 2025-10-30 05:13:24 UTC
(In reply to Jose Luis Duran from comment #20)
Thank you for solving the problem and taking my comments into account.