git: 41bdbe7dc06c - main - carp: Check if_carp under carp_sx in carp_nl_get()

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Wed, 11 Nov 2026 19:28:57 UTC
The branch main has been updated by rcm:

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

commit 41bdbe7dc06ca85baa774a97f585711e32544554
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-11 19:26:30 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-11 19:27:57 +0000

    carp: Check if_carp under carp_sx in carp_nl_get()
    
    carp_nl_get() tested ifp->if_carp for NULL before taking carp_sx and
    walked the softc list afterwards.  When the last vhid on the interface
    is removed in between, carp_free_if() clears if_carp and the walk
    dereferences a NULL pointer.  A reader that passes the check while the
    removal holds carp_sx always hits this.
    
    The check was done with the lock held until the ioctl(2) API was
    retired.  Do it under the lock again.
    
    Reported by:    Marcos Mendoza <mmendoza@netgate.com>
    Fixes:          72472e52e310 ("carp: retire ioctl(2) API")
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netinet/ip_carp.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/sys/netinet/ip_carp.c b/sys/netinet/ip_carp.c
index 38a8769ac48c..5022763c3a2e 100644
--- a/sys/netinet/ip_carp.c
+++ b/sys/netinet/ip_carp.c
@@ -2580,16 +2580,17 @@ carp_nl_get(struct nlmsghdr *hdr, struct nl_pstate *npt)
 	if ((error = carp_is_supported_if(ifp)) != 0)
 		goto out;
 
-	if (ifp->if_carp == NULL) {
-		error = ENOENT;
-		goto out;
-	}
-
 	hdr->nlmsg_flags |= NLM_F_MULTI;
 	privileged = (priv_check_cred(nlp_get_cred(npt->nlp),
 	    PRIV_NETINET_CARP) == 0);
 
 	sx_xlock(&carp_sx);
+	if (ifp->if_carp == NULL) {
+		sx_xunlock(&carp_sx);
+		error = ENOENT;
+		goto out;
+	}
+
 	IFNET_FOREACH_CARP(ifp, sc) {
 		struct genlmsghdr *ghdr_new;