git: bd596508db77 - releng/15.2 - unix: Fix a race between shutdown and listen

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

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

commit bd596508db779e166055a26fc2ff949e759f7f21
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-09 22:27:45 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 13:35:24 +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:    re (cperciva)
    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 6bafb33b5948..a5493a9ef6ef 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -2659,6 +2659,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) {
@@ -2666,6 +2667,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) {
@@ -2685,37 +2687,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);
@@ -4290,18 +4286,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: