sctp: fix use-after-free after association migration
Subsystems (3)
PATCH v3 1/1 sctp: fix use-after-free after association migration
SCTP timers and ICMP error handling can cache asoc->base.sk before locking it. If accept or peeloff migrates the association in between, processing under the old socket lock can race with an abort on the new socket that frees the association's queues. Transport and association references do not keep those queues alive. With mdelay() instrumentation widening the race, KASAN reported a slab-use-after-free in sctp_chunk_abandoned() from the T3 retransmit path.
Recheck the association's socket after taking its BH lock and defer mismatched timer callbacks through the existing retry path. Pass the locked socket to the IPv4 and IPv6 ICMP handlers instead of rereading it, and treat a mismatch like a busy socket before processing ICMP errors.
Use READ_ONCE() for socket pointer reads in these paths and receive/backlog processing, paired with WRITE_ONCE() in sctp_assoc_migrate(). Take the new socket reference before publishing it. The cached socket is protected by BH context and the endpoint's RCU-deferred sock_put().
---
Changes in v3:
- Update the commit message and subject as requested by Jakub Kicinski.
v2: https://lore.kernel.org/all/[email protected]/
Review: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Use Yong Liu for the author and Signed-off-by, as requested by Jakub Kicinski.
- Address Xin Long's review and the Sashiko ICMP finding: pass the locked socket to the IPv4/IPv6 error handlers and their UDP encapsulation callers, and revalidate it before immediate ICMP processing.
- Use READ_ONCE() for socket pointer reads in sctp_err_lookup(), sctp_rcv() and sctp_backlog_rcv().
- Broaden the subject and commit message to cover the ICMP paths.
v1: https://lore.kernel.org/all/[email protected]/
Based on b3dfbde62cf1981c4638939c159dfd087b7a72e9 (Linux 7.4.0-rc2).
net/sctp/associola.c | 4 ++-- net/sctp/input.c | 32 ++++++++++++++++--------------- net/sctp/ipv6.c | 11 ++++++----- net/sctp/sm_sideeffect.c | 41 ++++++++++++++++++++-------------------- 4 files changed, 45 insertions(+), 43 deletions(-)
net/sctp/associola.c +2 -2
@@ -1088,8 +1088,8 @@ void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)sctp_endpoint_hold (assoc->ep);/* Get a reference to the new sock. */ - assoc->base.sk = newsk; -sock_hold (assoc->base.sk); +sock_hold (newsk); +WRITE_ONCE (assoc->base.sk, newsk);/* Add the association to the new endpoint's list of associations. */ sctp_endpoint_add_asoc (newsp->ep, assoc);
net/sctp/input.c +17 -15
@@ -169,7 +169,7 @@ int sctp_rcv(struct sk_buff *skb)/* Retrieve the common input handling substructure. */ rcvr = asoc ? &asoc->base : &ep->base; - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);/* * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
@@ -217,7 +217,7 @@ int sctp_rcv(struct sk_buff *skb)*/ bh_lock_sock (sk); -if (sk != rcvr->sk) { +if (sk !=READ_ONCE (rcvr->sk)) {/* Our cached sk is different from the rcvr->sk. This is * because migrate()/accept() may have moved the association * to a new socket and released all the sockets. So now we
@@ -226,7 +226,7 @@ int sctp_rcv(struct sk_buff *skb)* of the current sk. */ bh_unlock_sock (sk); - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);bh_lock_sock (sk); }
@@ -292,7 +292,7 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) goto done; } -if (unlikely (rcvr->sk != sk)) { +if (unlikely (READ_ONCE (rcvr->sk) != sk)) {/* In this case, the association moved from one socket to * another. We are currently sitting on the backlog of the * old socket, so we need to move.
@@ -304,7 +304,7 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)* Otherwise, we can safely push the chunk on the inqueue. */ - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);local_bh_disable ();bh_lock_sock (sk);
@@ -377,7 +377,7 @@ void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc, t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))return ; -if (sock_owned_by_user (sk)) { +if (sock_owned_by_user (sk) || sk !=READ_ONCE (asoc->base.sk)) {atomic_set (&t->mtu_info, pmtu); asoc->pmtu_pending =1 ; t->pmtu_pending =1 ;
@@ -410,7 +410,8 @@ void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t, {struct dst_entry *dst; -if (sock_owned_by_user (sk) || !t) +if (sock_owned_by_user (sk) || !t || + sk !=READ_ONCE (t->asoc->base.sk))return ; dst =sctp_transport_dst_check (t);if (dst)
@@ -432,7 +433,7 @@ void sctp_icmp_proto_unreachable(struct sock *sk, struct sctp_association *asoc, struct sctp_transport *t) { - if (sock_owned_by_user(sk)) { + if (sock_owned_by_user(sk) || sk != READ_ONCE(asoc->base.sk)) { if (timer_pending(&t->proto_unreach_timer)) return; else {
@@ -492,7 +493,7 @@ struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,if (!asoc)return NULL ; - sk = asoc->base.sk; + sk =READ_ONCE (asoc->base.sk);/* RFC 4960, Appendix C. ICMP Handling *
@@ -526,7 +527,7 @@ struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,/* If too many ICMPs get dropped on busy * servers this needs to be solved differently. */ -if (sock_owned_by_user (sk)) +if (sock_owned_by_user (sk) || sk !=READ_ONCE (asoc->base.sk))__NET_INC_STATS (net,LINUX_MIB_LOCKDROPPEDICMPS ); *app = asoc;
@@ -546,11 +547,11 @@ void sctp_err_finish(struct sock *sk, struct sctp_transport *t)sctp_transport_put (t); } -static void sctp_v4_err_handle (struct sctp_transport *t,struct sk_buff *skb, +static void sctp_v4_err_handle (struct sock *sk,struct sctp_transport *t, +struct sk_buff *skb,__u8 type,__u8 code,__u32 info) {struct sctp_association *asoc = t->asoc; -struct sock *sk = asoc->base.sk;int err =0 ;switch (type) {
@@ -582,7 +583,8 @@ static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,default :return ; } -if (!sock_owned_by_user (sk) &&inet_test_bit (RECVERR , sk)) { +if (!sock_owned_by_user (sk) && sk ==READ_ONCE (asoc->base.sk) && +inet_test_bit (RECVERR , sk)) { sk->sk_err = err;sk_error_report (sk); }else {/* Only an error on timeout */
@@ -630,7 +632,7 @@ int sctp_v4_err(struct sk_buff *skb, __u32 info)return -ENOENT ; } -sctp_v4_err_handle (transport, skb, type, code, info); +sctp_v4_err_handle (sk, transport, skb, type, code, info);sctp_err_finish (sk, transport);return 0 ;
@@ -660,7 +662,7 @@ int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb) }if (hdr->type ==ICMP_DEST_UNREACH && hdr->code ==ICMP_FRAG_NEEDED ) info =ntohs (hdr->un.frag.mtu); -sctp_v4_err_handle (t, skb, hdr->type, hdr->code, info); +sctp_v4_err_handle (sk, t, skb, hdr->type, hdr->code, info);sctp_err_finish (sk, t);return 1 ;
net/sctp/ipv6.c +6 -5
@@ -123,11 +123,11 @@ static struct notifier_block sctp_inet6addr_notifier = { .notifier_call = sctp_inet6addr_event, }; -static void sctp_v6_err_handle (struct sctp_transport *t,struct sk_buff *skb, +static void sctp_v6_err_handle (struct sock *sk,struct sctp_transport *t, +struct sk_buff *skb,__u8 type,__u8 code,__u32 info) {struct sctp_association *asoc = t->asoc; -struct sock *sk = asoc->base.sk;int err =0 ;switch (type) {
@@ -149,7 +149,8 @@ static void sctp_v6_err_handle(struct sctp_transport *t, struct sk_buff *skb, }icmpv6_err_convert (type, code, &err); -if (!sock_owned_by_user (sk) &&inet6_test_bit (RECVERR6 , sk)) { +if (!sock_owned_by_user (sk) && sk ==READ_ONCE (asoc->base.sk) && +inet6_test_bit (RECVERR6 , sk)) { sk->sk_err = err;sk_error_report (sk); }else {
@@ -181,7 +182,7 @@ static int sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,return -ENOENT ; } -sctp_v6_err_handle (transport, skb, type, code,ntohl (info)); +sctp_v6_err_handle (sk, transport, skb, type, code,ntohl (info));sctp_err_finish (sk, transport);return 0 ;
@@ -211,7 +212,7 @@ int sctp_udp_v6_err(struct sock *sk, struct sk_buff *skb) }if (hdr->icmp6_type ==ICMPV6_PKT_TOOBIG ) info =ntohl (hdr->icmp6_mtu); -sctp_v6_err_handle (t, skb, hdr->icmp6_type, hdr->icmp6_code, info); +sctp_v6_err_handle (sk, t, skb, hdr->icmp6_type, hdr->icmp6_code, info);sctp_err_finish (sk, t);return 1 ;
net/sctp/sm_sideeffect.c +20 -21
107 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Hi,
These issues were discovered by Qihoo 360's Tulongfeng multi-agent system.
Thanks,
Yong Liu
Quoted 19 lines · <[email protected]> 于2026年11月16日周一 11:43写道:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages. --
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Jakub Kicinski
maintainerQuoted 1 line · On Thu, 19 Nov 2026 18:16:28 +0800 Yong Liu wrote:
These issues were discovered by Qihoo 360's Tulongfeng multi-agent system.
Please say "Discovered by LLM" in the commit message when/if you post v4 The marketing name of your tool doesn't belong in the commit msg but the info that the LLM found it (vs you actually hit this as a user) matters.
Xin Long
maintainerQuoted 114 lines · Yong Liu
SCTP timers and ICMP error handling can cache
asoc->base.skbefore locking it. If accept or peeloff migrates the association in between, processing under the old socket lock can race with an abort on the new socket that frees the association's queues. Transport and association references do not keep those queues alive. Withmdelay()instrumentation widening the race, KASAN reported a slab-use-after-free insctp_chunk_abandoned()from the T3 retransmit path.Recheck the association's socket after taking its BH lock and defer mismatched timer callbacks through the existing retry path. Pass the locked socket to the IPv4 and IPv6 ICMP handlers instead of rereading it, and treat a mismatch like a busy socket before processing ICMP errors.
Use
READ_ONCE()for socket pointer reads in these paths and receive/backlog processing, paired withWRITE_ONCE()insctp_assoc_migrate(). Take the new socket reference before publishing it. The cached socket is protected by BH context and the endpoint's RCU-deferredsock_put().Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Yong Liu <[email protected]>---
Changes in v3:
- Update the commit message and subject as requested by Jakub Kicinski.v2: https://lore.kernel.org/all/[email protected]/
Review: https://lore.kernel.org/all/[email protected]/Changes in v2:
- Use Yong Liu for the author and Signed-off-by, as requested by Jakub Kicinski.
- Address Xin Long's review and the Sashiko ICMP finding: pass the locked socket to the IPv4/IPv6 error handlers and their UDP encapsulation callers, and revalidate it before immediate ICMP processing.
- UseREAD_ONCE()for socket pointer reads insctp_err_lookup(),sctp_rcv()andsctp_backlog_rcv().
- Broaden the subject and commit message to cover the ICMP paths.v1: https://lore.kernel.org/all/[email protected]/
Based on
b3dfbde62cf1981c4638939c159dfd087b7a72e9(Linux 7.4.0-rc2).net/sctp/associola.c | 4 ++-- net/sctp/input.c | 32 ++++++++++++++++--------------- net/sctp/ipv6.c | 11 ++++++----- net/sctp/sm_sideeffect.c | 41 ++++++++++++++++++++-------------------- 4 files changed, 45 insertions(+), 43 deletions(-)net/sctp/associola.c +2
-2@@ -1088,8 +1088,8 @@ void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)sctp_endpoint_hold (assoc->ep);/* Get a reference to the new sock. */ - assoc->base.sk = newsk; -sock_hold (assoc->base.sk); +sock_hold (newsk); +WRITE_ONCE (assoc->base.sk, newsk);/* Add the association to the new endpoint's list of associations. */ sctp_endpoint_add_asoc (newsp->ep, assoc);net/sctp/input.c +5
-5@@ -169,7 +169,7 @@ int sctp_rcv(struct sk_buff *skb)/* Retrieve the common input handling substructure. */ rcvr = asoc ? &asoc->base : &ep->base; - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);/* * RFC 2960, 8.4 - Handle "Out of the blue" Packets. @@ -217,7 +217,7 @@ int sctp_rcv(struct sk_buff *skb)*/ bh_lock_sock (sk); -if (sk != rcvr->sk) { +if (sk !=READ_ONCE (rcvr->sk)) {/* Our cached sk is different from the rcvr->sk. This is * because migrate()/accept() may have moved the association * to a new socket and released all the sockets. So now we @@ -226,7 +226,7 @@ int sctp_rcv(struct sk_buff *skb)* of the current sk. */ bh_unlock_sock (sk); - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);bh_lock_sock (sk); }@@ -292,7 +292,7 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) goto done; } -if (unlikely (rcvr->sk != sk)) { +if (unlikely (READ_ONCE (rcvr->sk) != sk)) {/* In this case, the association moved from one socket to * another. We are currently sitting on the backlog of the * old socket, so we need to move. @@ -304,7 +304,7 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)* Otherwise, we can safely push the chunk on the inqueue. */ - sk = rcvr->sk; + sk =READ_ONCE (rcvr->sk);local_bh_disable ();bh_lock_sock (sk);
net/sctp/input.c void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc, · line 378
Quoted 2 lines
net/sctp/input.c +0
-0@@ -377,7 +377,7 @@ void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc, t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))return ;
As the clashiko [1] reported, there's asoc->base.sk access in sctp_transport_pl_hlen(), You can delete the !t check (t can't be NULL), and move the rest check to sctp_transport_update_pmtu(), which is called under sock lock, like:
pmtu = SCTP_TRUNC4(pmtu);
+ if (t->pathmtu <= pmtu &&
+ t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu)
+ return false;
+
As for sctp_addrs_lookup_transport() in the report, I think you need to do something like:
- bound_dev_if = READ_ONCE(t->asoc->base.sk->sk_bound_dev_if);
- if (sctp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif) &&
+ sk = READ_ONCE(t->asoc->base.sk);
+ if (sctp_sk_bound_dev_eq(net,
READ_ONCE(sk->sk_bound_dev_if), dif, sdif) &&
sctp_bind_addr_match(&t->asoc->base.bind_addr,
- laddr, sctp_sk(t->asoc->base.sk)))
+ laddr, sctp_sk(sk)))
return t;net/sctp/input.c void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t, · line 414
Quoted 13 lines
net/sctp/input.c +3
-2-if (sock_owned_by_user (sk)) { +if (sock_owned_by_user (sk) || sk !=READ_ONCE (asoc->base.sk)) {atomic_set (&t->mtu_info, pmtu); asoc->pmtu_pending =1 ; t->pmtu_pending =1 ;@@ -410,7 +410,8 @@ void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t, {struct dst_entry *dst; -if (sock_owned_by_user (sk) || !t) +if (sock_owned_by_user (sk) || !t || + sk !=READ_ONCE (t->asoc->base.sk))
This '!t' check can be deleted, as the t passed to sctp_icmp_redirect() can never be NULL.
Quoted 45 lines
net/sctp/input.c +5
-5return ; dst =sctp_transport_dst_check (t);if (dst)@@ -432,7 +433,7 @@ void sctp_icmp_proto_unreachable(struct sock *sk, struct sctp_association *asoc, struct sctp_transport *t) { - if (sock_owned_by_user(sk)) { + if (sock_owned_by_user(sk) || sk != READ_ONCE(asoc->base.sk)) { if (timer_pending(&t->proto_unreach_timer)) return; else {@@ -492,7 +493,7 @@ struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,if (!asoc)return NULL ; - sk = asoc->base.sk; + sk =READ_ONCE (asoc->base.sk);/* RFC 4960, Appendix C. ICMP Handling * @@ -526,7 +527,7 @@ struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,/* If too many ICMPs get dropped on busy * servers this needs to be solved differently. */ -if (sock_owned_by_user (sk)) +if (sock_owned_by_user (sk) || sk !=READ_ONCE (asoc->base.sk))__NET_INC_STATS (net,LINUX_MIB_LOCKDROPPEDICMPS ); *app = asoc;@@ -546,11 +547,11 @@ void sctp_err_finish(struct sock *sk, struct sctp_transport *t)sctp_transport_put (t); } -static void sctp_v4_err_handle (struct sctp_transport *t,struct sk_buff *skb, +static void sctp_v4_err_handle (struct sock *sk,struct sctp_transport *t, +struct sk_buff *skb,__u8 type,__u8 code,__u32 info) {struct sctp_association *asoc = t->asoc; -struct sock *sk = asoc->base.sk;int err =0 ;switch (type) {
net/sctp/input.c static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb, · line 590
Quoted 9 lines
net/sctp/input.c +2
-1@@ -582,7 +583,8 @@ static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,default :return ; } -if (!sock_owned_by_user (sk) &&inet_test_bit (RECVERR , sk)) { +if (!sock_owned_by_user (sk) && sk ==READ_ONCE (asoc->base.sk) && +inet_test_bit (RECVERR , sk)) { sk->sk_err = err;sk_error_report (sk); }else {/* Only an error on timeout */
As the clashiko [1] reported, this 'else' need to become:
} else if (sk == READ_ONCE(asoc->base.sk)) {
Quoted 37 lines
net/sctp/input.c +2
-2@@ -630,7 +632,7 @@ int sctp_v4_err(struct sk_buff *skb, __u32 info)return -ENOENT ; } -sctp_v4_err_handle (transport, skb, type, code, info); +sctp_v4_err_handle (sk, transport, skb, type, code, info);sctp_err_finish (sk, transport);return 0 ;@@ -660,7 +662,7 @@ int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb) }if (hdr->type ==ICMP_DEST_UNREACH && hdr->code ==ICMP_FRAG_NEEDED ) info =ntohs (hdr->un.frag.mtu); -sctp_v4_err_handle (t, skb, hdr->type, hdr->code, info); +sctp_v4_err_handle (sk, t, skb, hdr->type, hdr->code, info);sctp_err_finish (sk, t);return 1 ;net/sctp/ipv6.c +2
-2@@ -123,11 +123,11 @@ static struct notifier_block sctp_inet6addr_notifier = { .notifier_call = sctp_inet6addr_event, }; -static void sctp_v6_err_handle (struct sctp_transport *t,struct sk_buff *skb, +static void sctp_v6_err_handle (struct sock *sk,struct sctp_transport *t, +struct sk_buff *skb,__u8 type,__u8 code,__u32 info) {struct sctp_association *asoc = t->asoc; -struct sock *sk = asoc->base.sk;int err =0 ;switch (type) {
net/sctp/ipv6.c static void sctp_v6_err_handle(struct sctp_transport *t, struct sk_buff *skb, · line 156
Quoted 9 lines
net/sctp/ipv6.c +2
-1@@ -149,7 +149,8 @@ static void sctp_v6_err_handle(struct sctp_transport *t, struct sk_buff *skb, }icmpv6_err_convert (type, code, &err); -if (!sock_owned_by_user (sk) &&inet6_test_bit (RECVERR6 , sk)) { +if (!sock_owned_by_user (sk) && sk ==READ_ONCE (asoc->base.sk) && +inet6_test_bit (RECVERR6 , sk)) { sk->sk_err = err;sk_error_report (sk); }else {
Same here, change to:
} else if (sk == READ_ONCE(asoc->base.sk)) {
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116033855.1792036-1-pkfxxxing%40gmail.com
Thanks.