sctp: check RCV_SHUTDOWN after the sendmsg connect wait
Subsystems (4)
PATCH v3 1/1 sctp: check RCV_SHUTDOWN after the sendmsg connect wait
From: Jun Yang <[email protected]>
sctp_wait_for_connect() drops the socket lock while it sleeps. An out-of-the-blue ABORT can then be processed from the socket backlog and unlink the association. If a concurrent shutdown(fd, SHUT_RD) sets RCV_SHUTDOWN, the waiter breaks with err == 0 before checking asoc->base.dead. Its final sctp_association_put() can then free the association, leaving sctp_sendmsg_to_asoc() to continue with a dangling pointer.
Check RCV_SHUTDOWN along with the wait error in sctp_sendmsg_to_asoc() before using the association again. The check only accesses the socket, so it needs no additional association reference. Return the existing -ESRCH so that sctp_sendmsg() skips freeing a new association that may already have been destroyed.
Keep sctp_wait_for_connect() unchanged to preserve its behavior for the connect() caller.
---
v3:
- Check RCV_SHUTDOWN after the sendmsg connect wait, as suggested by
Xin Long, instead of taking an extra association reference and checking asoc->base.dead.
- Keep the shared wait helper unchanged and retain the -ESRCH error path.
- Link to v2: https://lore.kernel.org/all/[email protected]/
- Review: https://lore.kernel.org/all/CADvbK_f=S-2waid_92PbOCh-LWjU5oiZT+__xWfttVeTHEPQTg@mail.gmail.com/
v2:
- Leave sctp_wait_for_connect() unchanged to preserve connect() semantics.
- Hold and validate the association in the sendmsg-only caller.
net/sctp/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/sctp/socket.c +1 -1
@@ -1844,7 +1844,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,if (asoc->ep->intl_enable) { timeo =sock_sndtimeo (sk,0 ); err =sctp_wait_for_connect (asoc, &timeo); -if (err) { +if (err || sk->sk_shutdown &RCV_SHUTDOWN ) { err = -ESRCH ; goto err; }
Signature · 1 line
2.54.0 (Apple Git-157)
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 (1) · High: 0 · Medium: 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] In sctp_sendmsg_to_asoc() (net/sctp/socket.c:1847), the new condition if (err || sk->sk_shutdown & RCV_SHUTDOWN) returns -ESRCH…
Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but it sits on the same post-wait path the patch hardens.
--
sctp: check RCV_SHUTDOWN after the sendmsg connect wait
On the intl_enable path of sctp_sendmsg_to_asoc(), this patch checks sk->sk_shutdown & RCV_SHUTDOWN after sctp_wait_for_connect() and returns -ESRCH if the bit is set. The aim is to stop the send from continuing with an association that was freed while the socket lock was dropped. That can happen when a concurrent SHUT_RD makes the wait break with err == 0.
net/sctp/socket.c static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, · line 1850
Quoted 13 lines
net/sctp/socket.c +1
-1@@ -1844,7 +1844,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,if (asoc->ep->intl_enable) { timeo =sock_sndtimeo (sk,0 ); err =sctp_wait_for_connect (asoc, &timeo); -if (err) { +if (err || sk->sk_shutdown &RCV_SHUTDOWN ) { err = -ESRCH ; goto err; }
[Severity: Medium]
Can this reject sends where the association is still alive and no race happened?
The new condition only looks at the socket-wide RCV_SHUTDOWN bit. It does not check whether the asoc actually died.
One case needs no race: shutdown(fd, SHUT_RD) on an unconnected socket. inet_shutdown() falls through from TCP_CLOSE and sets the bit permanently, even though it returns -ENOTCONN:
net/ipv4/af_inet.c:inet_shutdown() {
...
case TCP_CLOSE:
err = -ENOTCONN;
fallthrough;
default:
WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | how);
...
}sctp_shutdown() does nothing for SHUT_RD alone. A later sendmsg() to a new peer then goes through sctp_primitive_ASSOCIATE(). On its first iteration, sctp_wait_for_connect() breaks with err == 0 without sleeping:
if (sk->sk_shutdown & RCV_SHUTDOWN) break;
A concurrent SHUT_RD that arrives after the handshake, on a healthy ESTABLISHED asoc, would also hit this check.
Before this patch, both cases went on to sctp_primitive_SEND() and returned msg_len. Now the data is dropped and userspace gets ESRCH. sctp_sendmsg() reads -ESRCH as "the asoc may be gone":
if (err < 0 && err != -ESRCH && new) sctp_association_free(asoc);
So the new asoc stays on ep->asocs with its INIT already sent. A retry then succeeds because the asoc is no longer CLOSED.
This also looks inconsistent with how other code handles the same wait result:
- __sctp_connect() returns it as connect() success.
- The non-intl wait_connect branch in this function sends and returns msg_len.
- The comment above sctp_shutdown() says SHUT_RD "Disables further receive operations. No SCTP protocol action is taken". Disabling sends is left to SHUT_WR and SHUT_RDWR.
The commit message only describes closing the concurrent SHUT_RD + ABORT race. It doesn't mention the new errno from sendmsg() or the association left behind.
Would a narrower check work here? One option is to hold the asoc across the wait and test asoc->base.dead. Another is to treat RCV_SHUTDOWN as an error only when the asoc is actually dead.
[Severity: Critical]
This is a pre-existing issue, but it sits on the same post-wait path this patch hardens. Can the transport pointer also be stale after this wait?
sctp_wait_for_connect() drops the socket lock but only pins the asoc. It takes no reference on the caller's transport and never checks transport->dead afterwards. sctp_wait_for_sndbuf() does both.
When the wait returns 0 because the asoc reached ESTABLISHED, sctp_sendmsg_to_asoc() goes on to:
list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
sctp_chunk_hold(chunk);
sctp_set_owner_w(chunk);
chunk->transport = transport;
}
err = sctp_primitive_SEND(net, asoc, datamsg);This sequence appears to free the transport while the lock is dropped:
1. sctp_sendmsg_new_asoc() adds the primary daddr as SCTP_UNKNOWN through sctp_connect_new_asoc(). Extra SCTP_DSTADDRV4/V6 cmsg addresses are added the same way through sctp_connect_add_peer().
2. sctp_sendmsg() keeps the transport for TCP-style sockets, or when SCTP_ADDR_OVER is set.
3. An INIT-ACK arrives from one of the extra addresses with the correct vtag. Its address parameters do not list the primary.
4. sctp_process_init() removes every transport still in SCTP_UNKNOWN:
list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
transport = list_entry(pos, struct sctp_transport, transports);
if (transport->state == SCTP_UNKNOWN) {
sctp_assoc_rm_peer(asoc, transport);
}
} In COOKIE_WAIT nothing else pins the primary transport. So sctp_transport_free() drops the last reference, and the transport is freed after an RCU grace period.
5. COOKIE-ACK moves the asoc to ESTABLISHED. The waiter returns 0 with RCV_SHUTDOWN clear, so the new check passes.
6. The flush that sctp_primitive_SEND() triggers reaches sctp_outq_select_transport(). That function reads new_transport->state, then does list_add_tail() on ctx->transport->send_ready and calls sctp_packet_config() on the freed transport.
A local socket needs net.sctp.intl_enable, SCTP_FRAGMENT_INTERLEAVE and SCTP_INTERLEAVING_SUPPORTED for this path. The INIT-ACK contents and source address are controlled by the remote peer. The problem seems to date back to when the intl path started waiting for the connection before sending.
Does this path need a sctp_transport_hold() and a transport->dead check across the wait, as in sctp_wait_for_sndbuf()? Re-deriving the transport after the wait would be another option.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260926095606.68601-1-juny24602%40gmail.com
Xin Long
maintainerQuoted 94 lines · On Tue, Sep 29, 2026 at 8:58 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 (1) · High: 0 · Medium: 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] In
sctp_sendmsg_to_asoc()(net/sctp/socket.c:1847), the new conditionif (err || sk->sk_shutdown & RCV_SHUTDOWN)returns -ESRCH…Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but it sits on the same post-wait path the patch hardens.--
sctp: check
RCV_SHUTDOWNafter the sendmsg connect waitOn the
intl_enablepath ofsctp_sendmsg_to_asoc(), this patch checkssk->sk_shutdown&RCV_SHUTDOWNaftersctp_wait_for_connect()and returns-ESRCHif the bit is set. The aim is to stop the send from continuing with an association that was freed while the socket lock was dropped. That can happen when a concurrentSHUT_RDmakes the wait break with err == 0.net/sctp/socket.c static int sctp_sendmsg_to_asoc(struct sctp_association *asoc, · line 1850
Quoted 13 lines
net/sctp/socket.c +1
-1@@ -1844,7 +1844,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,if (asoc->ep->intl_enable) { timeo =sock_sndtimeo (sk,0 ); err =sctp_wait_for_connect (asoc, &timeo); -if (err) { +if (err || sk->sk_shutdown &RCV_SHUTDOWN ) { err = -ESRCH ; goto err; }[Severity: Medium]
Can this reject sends where the association is still alive and no race happened?The new condition only looks at the socket-wide
RCV_SHUTDOWNbit. It does not check whether the asoc actually died.One case needs no race:
shutdown(fd, SHUT_RD)on an unconnected socket.inet_shutdown()falls through fromTCP_CLOSEand sets the bit permanently, even though it returns-ENOTCONN:net/ipv4/af_inet.c:inet_shutdown() { ... case TCP_CLOSE: err = -ENOTCONN; fallthrough; default: WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | how); ... }
sctp_shutdown()does nothing forSHUT_RDalone. A latersendmsg()to a new peer then goes throughsctp_primitive_ASSOCIATE(). On its first iteration,sctp_wait_for_connect()breaks with err == 0 without sleeping:if (sk->sk_shutdown & RCV_SHUTDOWN) break;A concurrent
SHUT_RDthat arrives after the handshake, on a healthy ESTABLISHED asoc, would also hit this check.Before this patch, both cases went on to
sctp_primitive_SEND()and returnedmsg_len. Now the data is dropped and userspace gets ESRCH.sctp_sendmsg()reads-ESRCHas "the asoc may be gone":if (err < 0 && err != -ESRCH && new) sctp_association_free(asoc);So the new asoc stays on
ep->asocswith its INIT already sent. A retry then succeeds because the asoc is no longer CLOSED.This also looks inconsistent with how other code handles the same wait result:
-
__sctp_connect()returns it asconnect()success.
- The non-intlwait_connectbranch in this function sends and returnsmsg_len.
- The comment abovesctp_shutdown()saysSHUT_RD"Disables further receive operations. No SCTP protocol action is taken". Disabling sends is left toSHUT_WRandSHUT_RDWR.
Quoted 8 lines
The commit message only describes closing the concurrent
SHUT_RD+ ABORT race. It doesn't mention the new errno fromsendmsg()or the association left behind.Would a narrower check work here? One option is to hold the asoc across the wait and test
asoc->base.dead. Another is to treatRCV_SHUTDOWNas an error only when the asoc is actually dead.
intl_enable case differs from the common case: chunks must not be enqueued until the connection is established in sctp_sendmsg(). Therefore, we must ensure the connection is established before continuing the send.
If shutdown(SHUT_RD) is called during the handshake in such a case. sctp_wait_for_connect() may return 0 after seeing RCV_SHUTDOWN, without confirming that the association reached ESTABLISHED. In this rare case, it's safer to fail the send rather than enqueueing chunks based only on !asoc->base.dead check.
So I would prefer the fix in this patch.
Quoted 52 lines
[Severity: Critical]
This is a pre-existing issue, but it sits on the same post-wait path this patch hardens. Can the transport pointer also be stale after this wait?
sctp_wait_for_connect()drops the socket lock but only pins the asoc. It takes no reference on the caller's transport and never checkstransport->deadafterwards.sctp_wait_for_sndbuf()does both.When the wait returns 0 because the asoc reached ESTABLISHED,
sctp_sendmsg_to_asoc()goes on to:list_for_each_entry(chunk, &datamsg->chunks, frag_list) { sctp_chunk_hold(chunk); sctp_set_owner_w(chunk); chunk->transport = transport; } err = sctp_primitive_SEND(net, asoc, datamsg);This sequence appears to free the transport while the lock is dropped:
1.
sctp_sendmsg_new_asoc()adds the primary daddr asSCTP_UNKNOWNthroughsctp_connect_new_asoc(). Extra SCTP_DSTADDRV4/V6 cmsg addresses are added the same way throughsctp_connect_add_peer().2.
sctp_sendmsg()keeps the transport for TCP-style sockets, or whenSCTP_ADDR_OVERis set.3. An INIT-ACK arrives from one of the extra addresses with the correct vtag. Its address parameters do not list the primary.
4.
sctp_process_init()removes every transport still inSCTP_UNKNOWN:list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { transport = list_entry(pos, struct sctp_transport, transports); if (transport->state == SCTP_UNKNOWN) { sctp_assoc_rm_peer(asoc, transport); } }In
COOKIE_WAITnothing else pins the primary transport. Sosctp_transport_free()drops the last reference, and the transport is freed after an RCU grace period.5. COOKIE-ACK moves the asoc to ESTABLISHED. The waiter returns 0 with
RCV_SHUTDOWNclear, so the new check passes.6. The flush that
sctp_primitive_SEND()triggers reachessctp_outq_select_transport(). That function readsnew_transport->state, then doeslist_add_tail()onctx->transport->send_readyand callssctp_packet_config()on the freed transport.
Quoted 9 lines
A local socket needs net.sctp.intl_enable,
SCTP_FRAGMENT_INTERLEAVEandSCTP_INTERLEAVING_SUPPORTEDfor this path. The INIT-ACK contents and source address are controlled by the remote peer. The problem seems to date back to when the intl path started waiting for the connection before sending.Does this path need a
sctp_transport_hold()and atransport->deadcheck across the wait, as insctp_wait_for_sndbuf()? Re-deriving the transport after the wait would be another option.
Seung already posted a patch for this pre-existing issue:
https://lore.kernel.org/netdev/[email protected]/
Thanks.
Quoted 55 lines · Jun Yang
From: Jun Yang <[email protected]>
sctp_wait_for_connect()drops the socket lock while it sleeps. An out-of-the-blue ABORT can then be processed from the socket backlog and unlink the association. If a concurrentshutdown(fd, SHUT_RD)setsRCV_SHUTDOWN, the waiter breaks with err == 0 before checkingasoc->base.dead. Its finalsctp_association_put()can then free the association, leavingsctp_sendmsg_to_asoc()to continue with a dangling pointer.Check
RCV_SHUTDOWNalong with the wait error insctp_sendmsg_to_asoc()before using the association again. The check only accesses the socket, so it needs no additional association reference. Return the existing-ESRCHso thatsctp_sendmsg()skips freeing a new association that may already have been destroyed.Keep
sctp_wait_for_connect()unchanged to preserve its behavior for theconnect()caller.Fixes 668c9beb9020 ("sctp: implement assign_number for sctp_stream_interleave")Reported-by TencentOS Corvus AI <[email protected]>Assisted-by tencentos-corvus-ai:hy4-previewSigned-off-by Jun Yang <[email protected]>---
v3:
- CheckRCV_SHUTDOWNafter the sendmsg connect wait, as suggested by
Xin Long, instead of taking an extra association reference and checkingasoc->base.dead.
- Keep the shared wait helper unchanged and retain the-ESRCHerror path.
- Link to v2: https://lore.kernel.org/all/[email protected]/
- Review: https://lore.kernel.org/all/CADvbK_f=S-2waid_92PbOCh-LWjU5oiZT+__xWfttVeTHEPQTg@mail.gmail.com/v2:
- Leavesctp_wait_for_connect()unchanged to preserveconnect()semantics.
- Hold and validate the association in the sendmsg-only caller.net/sctp/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/sctp/socket.c +1
-2@@ -1844,7 +1844,7 @@ static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,if (asoc->ep->intl_enable) { timeo =sock_sndtimeo (sk,0 ); err =sctp_wait_for_connect (asoc, &timeo); -if (err) { +if (err || sk->sk_shutdown &RCV_SHUTDOWN ) { err = -ESRCH ; goto err; } --
Quoted 1 line
2.54.0 (Apple Git-157)
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Sat, 26 Sep 2026 17:55:15 +0800 you wrote:
From: Jun Yang <[email protected]>
sctp_wait_for_connect()drops the socket lock while it sleeps. An out-of-the-blue ABORT can then be processed from the socket backlog and unlink the association. If a concurrentshutdown(fd, SHUT_RD)setsRCV_SHUTDOWN, the waiter breaks with err == 0 before checkingasoc->base.dead. Its finalsctp_association_put()can then free the association, leavingsctp_sendmsg_to_asoc()to continue with a dangling pointer.[...]
Here is the summary with links:
- [net,v3] sctp: check RCV_SHUTDOWN after the sendmsg connect wait
https://git.kernel.org/netdev/net/c/4f1da630d13d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html