git: dde37e7d2014 - main - pf: fix NULL dereference on failure in DIOCADDETHRULE

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 17:16:53 UTC
The branch main has been updated by kp:

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

commit dde37e7d201413a3d8449dd87b50006dd1b793ea
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-11-25 17:08:27 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-26 17:16:40 +0000

    pf: fix NULL dereference on failure in DIOCADDETHRULE
    
    If the ticket or anchor check in DIOCADDETHRULE fails
    we pf_free_eth_rule() on a struct pf_keth_rule with NULL
    ipsrc.addr.p.tbl/ipdst.addr.p.tbl. That ends up dereferencing that NULL
    pointer.
    
    Teach pf_free_eth_rule() to check for NULL in those fields so it's safe
    to free a rule we're in the process of creating.
    
    Reported by:    llm-postcr
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf_ioctl.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c
index d4f8f1bd36d0..d1872ea12d69 100644
--- a/sys/netpfil/pf/pf_ioctl.c
+++ b/sys/netpfil/pf/pf_ioctl.c
@@ -559,10 +559,14 @@ pf_free_eth_rule(struct pf_keth_rule *rule)
 	if (rule->kif)
 		pfi_kkif_unref(rule->kif);
 
-	if (rule->ipsrc.addr.type == PF_ADDR_TABLE)
-		pfr_detach_table(rule->ipsrc.addr.p.tbl);
-	if (rule->ipdst.addr.type == PF_ADDR_TABLE)
-		pfr_detach_table(rule->ipdst.addr.p.tbl);
+	if (rule->ipsrc.addr.type == PF_ADDR_TABLE) {
+		if (rule->ipsrc.addr.p.tbl)
+			pfr_detach_table(rule->ipsrc.addr.p.tbl);
+	}
+	if (rule->ipdst.addr.type == PF_ADDR_TABLE) {
+		if (rule->ipdst.addr.p.tbl)
+			pfr_detach_table(rule->ipdst.addr.p.tbl);
+	}
 
 	counter_u64_free(rule->evaluations);
 	for (int i = 0; i < 2; i++) {