mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] sctp: fix race between sctp_sendmsg free path and peeloff
@ 2026-11-20  5:01 Xue Haolin
  0 siblings, 0 replies; only message in thread
From: Xue Haolin @ 2026-11-20  5:01 UTC (permalink / raw)
  To: Marcelo Ricardo Leitner, Xin Long
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, linux-sctp, netdev, linux-kernel, stable

From: Haolin Xue <xuehaolin@uniontech.com>

sctp_sendmsg_to_asoc() may release the socket lock while waiting for
sndbuf space (sctp_wait_for_sndbuf) or connection establishment
(sctp_wait_for_connect).  During this window, another thread can peel
off the association to a new socket via getsockopt(SCTP_SOCKOPT_PEELOFF),
which migrates the association by setting asoc->base.sk to the new
socket and moving it to the new endpoint's association list.

When the waiting thread resumes, sctp_wait_for_sndbuf() detects the
migration (sk != asoc->base.sk) and returns -EPIPE.  However,
sctp_sendmsg() still calls sctp_association_free() on the migrated
association when err < 0 && err != -ESRCH && new.  This frees an
association that is now owned by the new socket, leading to a
use-after-free or double-free.

The connect-wait variant was fixed in commit f14fe6395a8b ("sctp: fix
race between sctp_wait_for_connect and peeloff") by adding the same
sk != asoc->base.sk check that sctp_wait_for_sndbuf already has.  This
patch applies the analogous fix on the caller side: guard the
sctp_association_free() call with asoc->base.sk == sk, and hold an
extra reference across the guard check so the association memory
remains valid even if the new socket is closed concurrently.

Note that the sndbuf variant of this window predates the connect
variant: the unguarded sctp_association_free() in the sendmsg error
path existed before the sctp_sendmsg_to_asoc() refactor, and the
sctp_wait_for_sndbuf() sk revalidation check was already present in
v4.14.  The Fixes tag targets the refactor commit because that is
where the current code structure (sendmsg calling sendmsg_to_asoc
which sleeps) was introduced; earlier kernels have the same race in
a slightly different code layout.

Discovered by manual code inspection, following the public report of
the SCTP local privilege escalation (Xint "NGINX2Root", 2026-11-18).
The issue was not triggered here and the fix has not been tested
against a reproducer; compile-tested only (make M=net/sctp).

Fixes: f84af33138a8 ("sctp: factor out sctp_sendmsg_to_asoc from sctp_sendmsg")
Cc: stable@vger.kernel.org
Signed-off-by: Haolin Xue <xuehaolin@uniontech.com>
---
 net/sctp/socket.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index b5a57f61ee6d..0039c54a55d6 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -2082,10 +2082,29 @@ static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
 	/* Update snd_info with the asoc */
 	sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
 
-	/* Send msg to the asoc */
+	/* Send msg to the asoc.
+	 * Hold an extra reference for newly created associations so the
+	 * memory stays alive across the migration check below; see comment.
+	 */
+	if (new)
+		sctp_association_hold(asoc);
 	err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
-	if (err < 0 && err != -ESRCH && new)
-		sctp_association_free(asoc);
+	if (err < 0 && err != -ESRCH && new) {
+		/* sctp_sendmsg_to_asoc() may have released the socket lock
+		 * while waiting for sndbuf or connect, during which the newly
+		 * created association could have been peeled off to another
+		 * socket.  The hold above keeps the association memory alive
+		 * across this check.  If the association was migrated,
+		 * asoc->base.sk no longer matches @sk and the association is
+		 * now owned by the new socket; freeing it here would leave
+		 * the new socket referencing freed memory (use-after-free /
+		 * double-free).
+		 */
+		if (asoc->base.sk == sk)
+			sctp_association_free(asoc);
+	}
+	if (new)
+		sctp_association_put(asoc);
 
 out_unlock:
 	release_sock(sk);
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20  5:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20  5:01 [PATCH net] sctp: fix race between sctp_sendmsg free path and peeloff Xue Haolin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®