git: a38bc03561f8 - main - pf: protect against the ethernet rule going away during DIOCGETETHRULE

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

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

commit a38bc03561f8b6428a6c4cb0d22904653aafe73a
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-11-24 13:01:22 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-25 16:05:18 +0000

    pf: protect against the ethernet rule going away during DIOCGETETHRULE
    
    We release the rules lock during DIOCGETETHRULE, but wait to clear the
    counters until after we've unlocked. This risks the rule being freed at
    the point where we reset the counters. Move the counter clearing to be
    covered by the lock.
    
    Reported by:    llm-postcr
    Fixes:          30087aa2e00a ("pf: Support clearing ether counters")
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf_ioctl.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c
index 297be51a58b5..aaf6659d7047 100644
--- a/sys/netpfil/pf/pf_ioctl.c
+++ b/sys/netpfil/pf/pf_ioctl.c
@@ -4335,6 +4335,13 @@ DIOCGETETHRULES_error:
 			PF_RULES_RUNLOCK();
 			ERROUT(EBUSY);
 		}
+		if (clear) {
+			counter_u64_zero(rule->evaluations);
+			for (int i = 0; i < 2; i++) {
+				counter_u64_zero(rule->packets[i]);
+				counter_u64_zero(rule->bytes[i]);
+			}
+		}
 		PF_RULES_RUNLOCK();
 		if (nvl == NULL)
 			ERROUT(ENOMEM);
@@ -4349,13 +4356,6 @@ DIOCGETETHRULES_error:
 			ERROUT(ENOSPC);
 
 		error = copyout(nvlpacked, nv->data, nv->len);
-		if (error == 0 && clear) {
-			counter_u64_zero(rule->evaluations);
-			for (int i = 0; i < 2; i++) {
-				counter_u64_zero(rule->packets[i]);
-				counter_u64_zero(rule->bytes[i]);
-			}
-		}
 
 #undef ERROUT
 DIOCGETETHRULE_error: