git: cededaef8357 - releng/14.4 - so_splice: Serialize teardowns of splices

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

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

commit cededaef835706784f07f6ced1d8736f00455323
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-12 19:23:32 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-16 21:16:22 +0000

    so_splice: Serialize teardowns of splices
    
    A socket splice can be configured with a timeout; once the timeout
    expires, the splice is automatically undone.  The implementation of an
    unsplice operation detaches each socket from the splice structure, and
    then drains the timeout task.  The timeout task takes the splice
    structure as an argument, and passes the source socket to so_unsplice().
    
    It is possible for two threads to race with the task thread: one removes
    the source socket from the splice associated with currently running
    task, and the other thread splices that socket again, creating a new
    splice structure.  The timeout thread may then unsplice the source
    socket even though it doesn't own that socket's current splice.  The
    resulting use-after-free can be exploited by a local user to escalate
    privileges.
    
    Fix the problem by serializing teardowns of splices: make so_unsplice()
    callers specify the cause of the teardown, and ensure that threads can't
    race to destroy a splice after an error occurs.
    
    Approved by:    so
    Security:       FreeBSD-SA-26:87.socket
    Security:       CVE-2026-127740
    Reviewed by:    gallatin
    Reported by:    OpenAI Security Research Team
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D61443
---
 sys/kern/uipc_socket.c | 84 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 55 insertions(+), 29 deletions(-)

diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index 14b2eeccc043..bc89843ceb8c 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -345,7 +345,13 @@ static uint32_t splice_index = 0;
 
 static void so_splice_timeout(void *arg, int pending);
 static void so_splice_xfer(struct so_splice *s);
-static int so_unsplice(struct socket *so, bool timeout);
+enum so_unsplice_reason {
+	SO_UNSPLICE_SETSOCKOPT,
+	SO_UNSPLICE_TIMEOUT,
+	SO_UNSPLICE_EXCEPTION,
+};
+static int so_unsplice(struct socket *so, struct so_splice *sp,
+    enum so_unsplice_reason why);
 
 static void
 splice_work_thread(void *ctx)
@@ -653,7 +659,7 @@ closing:
 			sp->state = SPLICE_EXCEPTION;
 			soref(so_src);
 			mtx_unlock(&sp->mtx);
-			(void)so_unsplice(so_src, false);
+			(void)so_unsplice(so_src, sp, SO_UNSPLICE_EXCEPTION);
 			sorele(so_src);
 		} else {
 			/*
@@ -1632,7 +1638,7 @@ so_splice_timeout(void *arg, int pending __unused)
 	struct so_splice *sp;
 
 	sp = arg;
-	(void)so_unsplice(sp->src, true);
+	(void)so_unsplice(sp->src, sp, SO_UNSPLICE_TIMEOUT);
 }
 
 /*
@@ -1705,7 +1711,7 @@ so_splice(struct socket *so, struct socket *so2, struct splice *splice)
 		sp->dst = NULL;
 		sp->state = SPLICE_EXCEPTION;
 		mtx_unlock(&sp->mtx);
-		so_unsplice(so, false);
+		(void)so_unsplice(so, sp, SO_UNSPLICE_EXCEPTION);
 		return (error);
 	}
 	SOCK_SENDBUF_LOCK(so2);
@@ -1716,7 +1722,7 @@ so_splice(struct socket *so, struct socket *so2, struct splice *splice)
 		sp->dst = NULL;
 		sp->state = SPLICE_EXCEPTION;
 		mtx_unlock(&sp->mtx);
-		so_unsplice(so, false);
+		(void)so_unsplice(so, sp, SO_UNSPLICE_EXCEPTION);
 		return (EINVAL);
 	}
 	so2->so_snd.sb_flags |= SB_SPLICED;
@@ -1742,10 +1748,11 @@ so_splice(struct socket *so, struct socket *so2, struct splice *splice)
 }
 
 static int
-so_unsplice(struct socket *so, bool timeout)
+so_unsplice(struct socket *so, struct so_splice *sp,
+    enum so_unsplice_reason why)
 {
 	struct socket *so2;
-	struct so_splice *sp;
+	int error;
 	bool drain;
 
 	/*
@@ -1764,18 +1771,47 @@ so_unsplice(struct socket *so, bool timeout)
 		SOCK_UNLOCK(so);
 		return (ENOTCONN);
 	}
-	sp = so->so_splice;
+	if (sp == NULL) {
+		sp = so->so_splice;
+	} else if (sp != so->so_splice) {
+		SOCK_RECVBUF_UNLOCK(so);
+		SOCK_UNLOCK(so);
+		return (EINVAL);
+	}
+	error = 0;
 	mtx_lock(&sp->mtx);
-	if (sp->state == SPLICE_INIT) {
+	switch (sp->state) {
+	case SPLICE_INIT:
 		/*
 		 * A splice is in the middle of being set up.
 		 */
-		mtx_unlock(&sp->mtx);
+		error = ENOTCONN;
+		break;
+	case SPLICE_EXCEPTION:
+		/*
+		 * Make sure that unsplicing is serialized.
+		 */
+		if (why != SO_UNSPLICE_EXCEPTION) {
+			error = ENOTCONN;
+			break;
+		}
+		/* FALLTHROUGH */
+	case SPLICE_IDLE:
+		sp->state = SPLICE_CLOSED;
+		break;
+	case SPLICE_QUEUED:
+	case SPLICE_RUNNING:
+		sp->state = SPLICE_CLOSING;
+		break;
+	default:
+		__assert_unreachable();
+	}
+	mtx_unlock(&sp->mtx);
+	if (error != 0) {
 		SOCK_RECVBUF_UNLOCK(so);
 		SOCK_UNLOCK(so);
-		return (ENOTCONN);
+		return (error);
 	}
-	mtx_unlock(&sp->mtx);
 	so->so_rcv.sb_flags &= ~SB_SPLICED;
 	so->so_splice = NULL;
 	SOCK_RECVBUF_UNLOCK(so);
@@ -1802,22 +1838,11 @@ so_unsplice(struct socket *so, bool timeout)
 	 * finish before proceeding.
 	 */
 	mtx_lock(&sp->mtx);
-	switch (sp->state) {
-	case SPLICE_QUEUED:
-	case SPLICE_RUNNING:
-		sp->state = SPLICE_CLOSING;
-		while (sp->state == SPLICE_CLOSING)
-			msleep(sp, &sp->mtx, PSOCK, "unsplice", 0);
-		break;
-	case SPLICE_INIT:
-	case SPLICE_IDLE:
-	case SPLICE_EXCEPTION:
-		sp->state = SPLICE_CLOSED;
-		break;
-	default:
-		__assert_unreachable();
-	}
-	if (!timeout) {
+	while (sp->state == SPLICE_CLOSING)
+		msleep(sp, &sp->mtx, PSOCK, "unsplice", 0);
+	KASSERT(sp->state == SPLICE_CLOSED,
+	    ("%s: splice %p state %d", __func__, sp, sp->state));
+	if (why != SO_UNSPLICE_TIMEOUT) {
 		drain = taskqueue_cancel_timeout(taskqueue_thread, &sp->timeout,
 		    NULL) != 0;
 	} else {
@@ -4020,7 +4045,8 @@ sosetopt(struct socket *so, struct sockopt *sopt)
 				error = so_splice(so, so2, &splice);
 				fdrop(fp, sopt->sopt_td);
 			} else {
-				error = so_unsplice(so, false);
+				error = so_unsplice(so, NULL,
+				    SO_UNSPLICE_SETSOCKOPT);
 			}
 			break;
 		}