Re: [PATCH net] sctp: fix race between sctp_sendmsg free path and peeloff
From: Xin Long
Date: Fri Nov 20 2026 - 11:38:02 EST
On Fri, Nov 20, 2026 at 12:01 AM Xue Haolin <xuehaolin@xxxxxxxxxxxxx> wrote:
>
> From: Haolin Xue <xuehaolin@xxxxxxxxxxxxx>
>
> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Haolin Xue <xuehaolin@xxxxxxxxxxxxx>
> ---
> 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
>
I think this is a dup of
https://lore.kernel.org/netdev/20261112172149.27355-1-yves.bieri@xxxxxxxxx/
Thanks.