sctp: fix race between sctp_sendmsg free path and peeloff
Subsystems (4)
PATCH 1/1 sctp: fix race between sctp_sendmsg free path and peeloff
From: Haolin Xue <[email protected]>
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).
--- net/sctp/socket.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)
net/sctp/socket.c +22 -3
@@ -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);
Signature · 1 line
2.43.0
Xin Long
maintainerQuoted 84 lines · Xue Haolin
From: Haolin Xue <[email protected]>
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 viagetsockopt(SCTP_SOCKOPT_PEELOFF), which migrates the association by settingasoc->base.skto 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 callssctp_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 betweensctp_wait_for_connectand peeloff") by adding the same sk !=asoc->base.skcheck thatsctp_wait_for_sndbufalready has. This patch applies the analogous fix on the caller side: guard thesctp_association_free()call withasoc->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 unguardedsctp_association_free()in the sendmsg error path existed before thesctp_sendmsg_to_asoc()refactor, and thesctp_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 callingsendmsg_to_asocwhich 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")Signed-off-by Haolin Xue <[email protected]>--- net/sctp/socket.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-)net/sctp/socket.c +22
-4@@ -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); --
Quoted 2 lines
2.43.0
I think this is a dup of
https://lore.kernel.org/netdev/[email protected]/
Thanks.