git: 3a847375eb72 - releng/15.1 - unix: Fix a race between shutdown and listen

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:08:18 UTC
The branch releng/15.1 has been updated by markj:

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

commit 3a847375eb723900ab33426028d5428b6abda55e
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-09 22:27:45 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-16 21:21:09 +0000

    unix: Fix a race between shutdown and listen
    
    In uipc_shutdown(), the SOLISTENING() check is stale as soon as the
    socket lock is dropped.  Acquire the unpcb lock earlier and use that to
    interlock with other work that touches the socket buffers which are
    invalid for a listening socket.  Fix unp_dispose() to handle errors when
    acquiring the I/O lock; contrary to the assertions, such races are
    possible.
    
    Approved by:    so
    Security:       FreeBSD-SA-26:83.unix
    Security:       CVE-2026-127751
    Reported by:    OpenAI Security Research Team
    Sponsored by:   The FreeBSD Foundation
---
 sys/kern/uipc_usrreq.c | 47 +++++++++++++++++++++++------------------------
 1 file changed, 23 insertions(+), 24 deletions(-)

diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index ccc02c98e3ac..35e055affd53 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -2677,6 +2677,7 @@ uipc_shutdown(struct socket *so, enum shutdown_how how)
 	struct unpcb *unp = sotounpcb(so);
 	int error;
 
+	UNP_PCB_LOCK(unp);
 	SOCK_LOCK(so);
 	if (SOLISTENING(so)) {
 		if (how != SHUT_WR) {
@@ -2684,6 +2685,7 @@ uipc_shutdown(struct socket *so, enum shutdown_how how)
 			solisten_wakeup(so);    /* unlocks so */
 		} else
 			SOCK_UNLOCK(so);
+		UNP_PCB_UNLOCK(unp);
 		return (ENOTCONN);
 	} else if ((so->so_state &
 	    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
@@ -2703,37 +2705,31 @@ uipc_shutdown(struct socket *so, enum shutdown_how how)
 		error = ENOTCONN;
 		if (so->so_type != SOCK_DGRAM) {
 			SOCK_UNLOCK(so);
+			UNP_PCB_UNLOCK(unp);
 			return (error);
 		}
 	} else
 		error = 0;
 	SOCK_UNLOCK(so);
 
-	switch (how) {
-	case SHUT_RD:
-		if (so->so_type == SOCK_DGRAM)
-			socantrcvmore(so);
-		else
-			uipc_cantrcvmore(so);
-		unp_dispose(so);
-		break;
-	case SHUT_RDWR:
-		if (so->so_type == SOCK_DGRAM)
-			socantrcvmore(so);
-		else
-			uipc_cantrcvmore(so);
-		unp_dispose(so);
-		/* FALLTHROUGH */
-	case SHUT_WR:
+	if (how == SHUT_WR || how == SHUT_RDWR) {
 		if (so->so_type == SOCK_DGRAM) {
 			socantsendmore(so);
 		} else {
-			UNP_PCB_LOCK(unp);
 			if (unp->unp_conn != NULL)
 				uipc_cantrcvmore(unp->unp_conn->unp_socket);
-			UNP_PCB_UNLOCK(unp);
 		}
 	}
+	if (how == SHUT_RD || how == SHUT_RDWR) {
+		if (so->so_type == SOCK_DGRAM)
+			socantrcvmore(so);
+		else
+			uipc_cantrcvmore(so);
+		UNP_PCB_UNLOCK(unp);
+		unp_dispose(so);
+	} else {
+		UNP_PCB_UNLOCK(unp);
+	}
 	wakeup(&so->so_timeo);
 
 	return (error);
@@ -4307,18 +4303,21 @@ unp_dispose(struct socket *so)
 	struct mbuf *m;
 	int error __diagused;
 
-	MPASS(!SOLISTENING(so));
+	/*
+	 * Grab our special mbufs before calling sbrelease().
+	 */
+	error = SOCK_IO_RECV_LOCK(so, SBL_WAIT | SBL_NOINTR);
+	if (error != 0) {
+		KASSERT(SOLISTENING(so),
+		    ("%s: socket %p is not listening", __func__, so));
+		return;
+	}
 
 	unp = sotounpcb(so);
 	UNP_LINK_WLOCK();
 	unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
 	UNP_LINK_WUNLOCK();
 
-	/*
-	 * Grab our special mbufs before calling sbrelease().
-	 */
-	error = SOCK_IO_RECV_LOCK(so, SBL_WAIT | SBL_NOINTR);
-	MPASS(!error);
 	SOCK_RECVBUF_LOCK(so);
 	switch (so->so_type) {
 	case SOCK_DGRAM: