Netdev List
 help / color / mirror / Atom feed
From: Xue Haolin <xuehaolin@uniontech.com>
To: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Xin Long <lucien.xin@gmail.com>
Cc: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	linux-sctp@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH net] sctp: fix race between sctp_sendmsg free path and peeloff
Date: Fri, 20 Nov 2026 13:01:08 +0800	[thread overview]
Message-ID: <20261120050108.426545-1-xuehaolin@uniontech.com> (raw)

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


                 reply	other threads:[~2026-11-20  5:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120050108.426545-1-xuehaolin@uniontech.com \
    --to=xuehaolin@uniontech.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sctp@vger.kernel.org \
    --cc=lucien.xin@gmail.com \
    --cc=marcelo.leitner@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox