git: 952ea69cf245 - stable/15 - netlink_snl: make snl_free() safe to call twice

From: Kevin Bowling <kbowling_at_FreeBSD.org>
Date: Sat, 31 Oct 2026 18:58:54 UTC
The branch stable/15 has been updated by kbowling:

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

commit 952ea69cf245cd4843071026cda1732ac968d415
Author:     Avinash Duduskar <avinash.duduskar@gmail.com>
AuthorDate: 2026-10-28 10:22:14 +0000
Commit:     Kevin Bowling <kbowling@FreeBSD.org>
CommitDate: 2026-10-31 18:57:05 +0000

    netlink_snl: make snl_free() safe to call twice
    
    snl_init() calls snl_free() on its own failure paths but leaves the
    state marked initialised and the buffer pointers set, so a caller that
    also frees the state on failure, as libpfctl's pfctl_open() does, closes
    the descriptor twice and, on the lb_init() failure path, frees the
    buffer twice. Reset what snl_free() releases so a second call is a
    no-op.
    
    Signed-off-by:  Avinash Duduskar <avinash.duduskar@gmail.com>
    Reviewed by:    kp
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2508
    
    (cherry picked from commit 489ded6d5231f44e02988e8c47969a9e6a1e07b7)
---
 sys/netlink/netlink_snl.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h
index 2d9798f1e95d..7e7d9fa19db5 100644
--- a/sys/netlink/netlink_snl.h
+++ b/sys/netlink/netlink_snl.h
@@ -240,13 +240,19 @@ snl_clear_lb(struct snl_state *ss)
 static void
 snl_free(struct snl_state *ss)
 {
-	if (ss->init_done)
+	if (ss->init_done) {
 		close(ss->fd);
-	if (ss->buf != NULL)
+		ss->fd = -1;
+		ss->init_done = false;
+	}
+	if (ss->buf != NULL) {
 		free(ss->buf);
+		ss->buf = NULL;
+	}
 	if (ss->lb != NULL) {
 		snl_clear_lb(ss);
 		lb_free(ss->lb);
+		ss->lb = NULL;
 	}
 }