Skip to contentSkip to conversation

sctp: fix use-after-free after association migration

PATCH In review Yong Liu 3 participants 4 messages Nov 20 – Nov 24

Subsystems (3)
lore
Share & tools
lore

Yong Liu

author
#0 lore raw

PATCH v4 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. Skip socket error reporting after migration and check PMTU only under the owning socket's lock.

Use READ_ONCE() for socket pointer reads in these paths and receive/backlog processing, including transport lookup, 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().

This issue was discovered by an LLM.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
Changes in v4 (addressing Xin Long's review and the Sashiko report):
- Move the PMTU checks to sctp_transport_update_pmtu(), under the owning socket's lock, after validating the cached socket.
- Cache asoc->base.sk with READ_ONCE() in sctp_addrs_lookup_transport() and use that socket for both lookup checks.
- Skip IPv4/IPv6 soft error reporting if the association has migrated.
- Remove the unnecessary NULL transport checks in the ICMP helpers.
- State that this issue was discovered by an LLM, as requested by Jakub Kicinski.

v3: https://lore.kernel.org/all/[email protected]/
Review: https://lore.kernel.org/all/CADvbK_dO0B156LN_05QRqyo-7wCmCT64BYxbThzXYzJdL6kLBQ@mail.gmail.com/
Sashiko: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116033855.1792036-1-pkfxxxing%40gmail.com

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).

Compile-tested net/sctp/ with W=1 using x86_64_defconfig plus SCTP, IPv6, KASAN_GENERIC, KASAN_INLINE and WERROR. The original build configuration is no longer available. No runtime validation of v4.

 net/sctp/associola.c     |  4 ++--
 net/sctp/input.c         | 49 ++++++++++++++++++++--------------------
 net/sctp/ipv6.c          | 13 ++++++-----
 net/sctp/sm_sideeffect.c | 41 ++++++++++++++++-----------------
 net/sctp/transport.c     |  4 ++++
 5 files changed, 57 insertions(+), 54 deletions(-)
net/sctp/associola.c +2 -2
index 4521be3bd8..c5d520c8d7 100644
@@ -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 +24 -25

151 lines not on this page · raw message

net/sctp/ipv6.c +7 -6
index 97390569c4..4dbaa4f9d5 100644
@@ -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,10 +149,11 @@ 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 {
+	} else if (sk == READ_ONCE(asoc->base.sk)) {
 		WRITE_ONCE(sk->sk_err_soft, err);
 	}
 }
@@ -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

net/sctp/transport.c +4 -0
index 6ea55b9fbd..2124e73f7e 100644
@@ -404,6 +404,10 @@ bool sctp_transport_update_pmtu(struct sctp_transport *t, u32 pmtu)
 	}
 	pmtu = SCTP_TRUNC4(pmtu);
 
+	if (t->pathmtu <= pmtu &&
+	    t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu)
+		return false;
+
 	if (sctp_transport_pl_enabled(t))
 		return sctp_transport_pl_toobig(t, pmtu - sctp_transport_pl_hlen(t));
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch makes deferred PMTU handling lose valid MTU reductions.
- [Medium] sctp: unlocked state mutation on migrated sock in sctp_icmp_frag_needed()

Pre-existing issues:
- [Medium] The deferral branch of sctp_icmp_frag_needed() does a non- atomic read-modify-write of the 1-bit field t->pmtu_pending…

--

sctp: fix use-after-free after association migration

Timer and ICMP handlers that cache asoc->base.sk now check, after taking the BH lock, that the association still belongs to that socket. If it has migrated, they defer the work as they would for a busy socket. The patch also moves the PMTU size check from sctp_icmp_frag_needed() into sctp_transport_update_pmtu(), and adds READ_ONCE()/WRITE_ONCE() pairing for the socket pointer.

Quoted 4 lines
net/sctp/input.c +0 -0
index 9494cfa51106c..555695718abf9 100644

[ ... ]

net/sctp/input.c static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb) · line 376

Quoted 12 lines
net/sctp/input.c +1 -6
@@ -372,12 +372,7 @@ static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
 			   struct sctp_transport *t, __u32 pmtu)
 {
-	if (!t ||
-	    (t->pathmtu <= pmtu &&
-	     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);

[Severity: Medium]
The association can migrate between the READ_ONCE() of asoc->base.sk and bh_lock_sock(sk). When that happens, sctp_err_lookup() still returns the old socket with only the old socket's slock held. It counts the ICMP in LINUX_MIB_LOCKDROPPEDICMPS, but it still hands asoc and transport back to the callers.

sctp_rcv() handles the same window differently. It drops the old lock, rereads rcvr->sk and takes the new socket's lock. Is there a reason sctp_err_lookup() can't do the same?

As written, the migrated case falls into the deferral branches of sctp_icmp_frag_needed() and sctp_icmp_proto_unreachable(). Those branches still modify shared association and transport state.

sctp_icmp_frag_needed() writes t->mtu_info, asoc->pmtu_pending and t->pmtu_pending. t->pmtu_pending is a bitfield that shares a word with t->dead, rto_pending, hb_sent and the other transport flags. BH and process context on the new socket update those flags under the new socket's lock, not the one held here.

sctp_icmp_proto_unreachable() checks timer_pending() on t->proto_unreach_timer, then takes a transport reference and calls mod_timer(). If sctp_transport_free() runs on the new socket at the same time, its timer_delete() of proto_unreach_timer can land before this mod_timer(). The timer is then re-armed after teardown and keeps the transport alive until it fires. sctp_generate_proto_unreach_event() should then see asoc->base.dead set and bail out.

None of this is serialized against the new socket's owner. The only lock held belongs to a socket that no longer owns the association.

The busy-socket case has a similar shape and predates this patch. Before the patch, the migrated case ran full ICMP processing under the wrong lock, so this is an improvement. It still isn't fully closed.

Would it be simpler to switch to the new socket's lock in sctp_err_lookup()? Alternatively, the ICMP could be dropped entirely once the association has moved, instead of recording deferred PMTU and proto-unreach state.

[Severity: Medium]
Can a pending PMTU reduction get lost here?

The size filter used to run before this deferral branch. Now that it has moved, the branch stores every Frag-Needed/PTB value in t->mtu_info, including values at or above t->pathmtu. t->mtu_info holds only one value, so a later report that cannot lower the MTU overwrites an earlier pending reduction.

For example, with pathmtu=1500 and the socket owned by the user:

ICMP#1: mtu_info = 1200, pmtu_pending = 1
ICMP#2 (stale, or from another ECMP path, matching vtag): mtu_info = 1500

On the next transmit, sctp_packet_config() or sctp_sendmsg_to_asoc() calls sctp_assoc_sync_pmtu():

net/sctp/associola.c:sctp_assoc_sync_pmtu() {
    ...
		if (t->pmtu_pending && t->dst) {
			sctp_transport_update_pmtu(t,
						   atomic_read(&t->mtu_info));
			t->pmtu_pending = 0;
		}
    ...
}

sctp_transport_update_pmtu() is called with 1500. The relocated check returns false:

	if (t->pathmtu <= pmtu &&
	    t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu)
		return false;

t->pmtu_pending is then cleared anyway, so the 1200 reduction is never applied or retried.

Wouldn't the association then keep sending packets larger than the real path MTU until another ICMP is processed? The new migrated-socket condition also sends more ICMPs into this unfiltered branch.

net/sctp/input.c static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb) · line 378

Quoted 2 lines
net/sctp/input.c +0 -0
 		asoc->pmtu_pending = 1;
 		t->pmtu_pending = 1;

[Severity: Medium]
This is a pre-existing issue, and the patch did not introduce it. t->pmtu_pending is a 1-bit field that shares one __u32 word with other transport flags in struct sctp_transport:

	__u32	dead:1,
		rto_pending:1,
		hb_sent:1,
		pmtu_pending:1,
		dst_pending_confirm:1,
		sack_generation:1;

Here only the cached socket's slock is held. In the busy-socket case, the process context that owns the socket can update those bits at the same time.

In the migrated case, code running under the new socket's lock can do the same. Two examples are sctp_transport_free() setting transport->dead = 1 and sctp_assoc_sync_pmtu() clearing pmtu_pending.

Could this read-modify-write of the bitfield word lose a concurrent update to one of the other bits, such as transport->dead?

The busy-socket form of this race goes back to the original SCTP code. This patch adds the migrated case as a second way into the same branch. Before the patch, that case did full PMTU and retransmit processing under the wrong lock, so the patch reduces this exposure rather than creating it.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120035521.2098757-1-pkfxxxing%40gmail.com
maintainer
#2 lore raw
Quoted 84 lines · On Mon, Nov 23, 2026 at 11:13 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch makes deferred PMTU handling lose valid MTU reductions.
- [Medium] sctp: unlocked state mutation on migrated sock in sctp_icmp_frag_needed()

Pre-existing issues:
- [Medium] The deferral branch of sctp_icmp_frag_needed() does a non- atomic read-modify-write of the 1-bit field t->pmtu_pending…

--

sctp: fix use-after-free after association migration

Timer and ICMP handlers that cache asoc->base.sk now check, after taking the BH lock, that the association still belongs to that socket. If it has migrated, they defer the work as they would for a busy socket. The patch also moves the PMTU size check from sctp_icmp_frag_needed() into sctp_transport_update_pmtu(), and adds READ_ONCE()/WRITE_ONCE() pairing for the socket pointer.

Quoted 4 lines
net/sctp/input.c +0 -0
index 9494cfa51106c..555695718abf9 100644

[ ... ]

net/sctp/input.c static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb) · line 376

Quoted 12 lines
net/sctp/input.c +1 -6
@@ -372,12 +372,7 @@ static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
                         struct sctp_transport *t, __u32 pmtu)
 {
-     if (!t ||
-         (t->pathmtu <= pmtu &&
-          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);

[Severity: Medium]
The association can migrate between the READ_ONCE() of asoc->base.sk and bh_lock_sock(sk). When that happens, sctp_err_lookup() still returns the old socket with only the old socket's slock held. It counts the ICMP in LINUX_MIB_LOCKDROPPEDICMPS, but it still hands asoc and transport back to the callers.

sctp_rcv() handles the same window differently. It drops the old lock, rereads rcvr->sk and takes the new socket's lock. Is there a reason sctp_err_lookup() can't do the same?

As written, the migrated case falls into the deferral branches of sctp_icmp_frag_needed() and sctp_icmp_proto_unreachable(). Those branches still modify shared association and transport state.

sctp_icmp_frag_needed() writes t->mtu_info, asoc->pmtu_pending and t->pmtu_pending. t->pmtu_pending is a bitfield that shares a word with t->dead, rto_pending, hb_sent and the other transport flags. BH and process context on the new socket update those flags under the new socket's lock, not the one held here.

sctp_icmp_proto_unreachable() checks timer_pending() on t->proto_unreach_timer, then takes a transport reference and calls mod_timer(). If sctp_transport_free() runs on the new socket at the same time, its timer_delete() of proto_unreach_timer can land before this mod_timer(). The timer is then re-armed after teardown and keeps the transport alive until it fires. sctp_generate_proto_unreach_event() should then see asoc->base.dead set and bail out.

None of this is serialized against the new socket's owner. The only lock held belongs to a socket that no longer owns the association.

Quoted 9 lines

The busy-socket case has a similar shape and predates this patch. Before the patch, the migrated case ran full ICMP processing under the wrong lock, so this is an improvement. It still isn't fully closed.

Would it be simpler to switch to the new socket's lock in sctp_err_lookup()? Alternatively, the ICMP could be dropped entirely once the association has moved, instead of recording deferred PMTU and proto-unreach state.

This is a pre-existing issue.

Quoted 34 lines

[Severity: Medium]
Can a pending PMTU reduction get lost here?

The size filter used to run before this deferral branch. Now that it has moved, the branch stores every Frag-Needed/PTB value in t->mtu_info, including values at or above t->pathmtu. t->mtu_info holds only one value, so a later report that cannot lower the MTU overwrites an earlier pending reduction.

For example, with pathmtu=1500 and the socket owned by the user:

ICMP#1: mtu_info = 1200, pmtu_pending = 1
ICMP#2 (stale, or from another ECMP path, matching vtag): mtu_info = 1500

On the next transmit, sctp_packet_config() or sctp_sendmsg_to_asoc() calls sctp_assoc_sync_pmtu():

net/sctp/associola.c:sctp_assoc_sync_pmtu() {
    ...
                if (t->pmtu_pending && t->dst) {
                        sctp_transport_update_pmtu(t,
                                                   atomic_read(&t->mtu_info));
                        t->pmtu_pending = 0;
                }
    ...
}

sctp_transport_update_pmtu() is called with 1500. The relocated check returns false:

        if (t->pathmtu <= pmtu &&
            t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu)
                return false;
Quoted 10 lines

t->pmtu_pending is then cleared anyway, so the 1200 reduction is never applied or retried.

Wouldn't the association then keep sending packets larger than the real path MTU until another ICMP is processed? The new migrated-socket condition also sends more ICMPs into this unfiltered branch.

Quoted 2 lines
net/sctp/input.c +0 -0
              asoc->pmtu_pending = 1;
              t->pmtu_pending = 1;

The scenario requires two ICMPs for the same transport to hit the deferral path in quick succession, with the second carrying a stale, higher MTU. This is already a narrow race.

Even if it occurs, PMTU discovery self-corrects on the next oversized packet, when a non-deferred update can take effect.

The original sock_owned_by_user path already had the same single-slot overwrite limitation. The patch only extends it to migration; it does not introduce a new bug.

Thanks.

Quoted 33 lines

[Severity: Medium]
This is a pre-existing issue, and the patch did not introduce it. t->pmtu_pending is a 1-bit field that shares one __u32 word with other transport flags in struct sctp_transport:

        __u32   dead:1,
                rto_pending:1,
                hb_sent:1,
                pmtu_pending:1,
                dst_pending_confirm:1,
                sack_generation:1;

Here only the cached socket's slock is held. In the busy-socket case, the process context that owns the socket can update those bits at the same time.

In the migrated case, code running under the new socket's lock can do the same. Two examples are sctp_transport_free() setting transport->dead = 1 and sctp_assoc_sync_pmtu() clearing pmtu_pending.

Could this read-modify-write of the bitfield word lose a concurrent update to one of the other bits, such as transport->dead?

The busy-socket form of this race goes back to the original SCTP code. This patch adds the migrated case as a second way into the same branch. Before the patch, that case did full PMTU and retransmit processing under the wrong lock, so the patch reduces this exposure rather than creating it.

[ ... ]

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120035521.2098757-1-pkfxxxing%40gmail.com

maintainer Acked-by on v4 1/1
#3 lore raw
In reply to Yong Liu
Quoted 22 lines · Yong Liu

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. Skip socket error reporting after migration and check PMTU only under the owning socket's lock.

Use READ_ONCE() for socket pointer reads in these paths and receive/backlog processing, including transport lookup, 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().

Quoted 5 lines

This issue was discovered by an LLM.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")