git: 6144315d1612 - main - unix: Fix a race between shutdown and listen
Date: Tue, 17 Nov 2026 17:01:49 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=6144315d1612bc0c3a7ffd22e75aa1fe06dee1ca
commit 6144315d1612bc0c3a7ffd22e75aa1fe06dee1ca
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-09 22:27:45 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 16:57:01 +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 f30119693a76..7d3edd7e32d2 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -2730,6 +2730,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) {
@@ -2737,6 +2738,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) {
@@ -2756,37 +2758,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);
@@ -4559,18 +4555,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: