🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Jiayuan Chen Wed, 18 Nov 2026 12:50
[PATCH 5.15.y v1 0/1] minimal mptcp UAF fix for 5.15
#18512
Author: Jiayuan Chen
Date: Wed, 18 Nov 2026 12:50
113 lines
12663 bytes
We encountered an MPTCP-associated use-after-free in kernel 5.15. The
affected applications do not enable MPTCP on purpose: Go turns MPTCP on
for listening sockets by default, so any Go service that calls Listen()
ends up with an MPTCP listener without the application opting in, and even
when the client speaks plain TCP.

In mptcp_close() the listener subflow is orphaned (marked SOCK_DEAD) before
__tcp_close() -> inet_csk_listen_stop() runs on it. A connection whose 3-way
handshake completes in that window is cloned from the already SOCK_DEAD
listener subflow and inherits SOCK_DEAD; a following RST then frees that child
via tcp_done() -> inet_csk_destroy_sock() while it is still linked in the
listener accept queue, and inet_csk_listen_stop() later walks onto the freed
socket.


time line:
close(listen_fd):                            3-way handshake & rst (plain TCP client)

mptcp_close
  // mark ssk as SOCK_DEAD
  sock_orphan(ssk);
  __mptcp_destroy_sock
    // ssk in conn_list is listen sk
    __mptcp_close_ssk
                                             // in 3-way handshake
                                             tcp_v4_syn_recv_sock
                                               // inherit SOCK_DEAD state from listen ssk
                                               inet_csk_clone_lock
                                             // process RST
                                             tcp_reset
                                               tcp_done
                                               // sk is SOCK_DEAD
                                               inet_csk_destroy_sock
                                                   sock_put(sk); // freed
      __tcp_close(ssk, 0)
        // free child
        inet_csk_listen_stop()
            process child        <==== process freed sk




Upstream closed this window with commit 57fc0f1ceaa40 ("mptcp: ensure listener
is unhashed before updating the sk status"), so that would be the one to
backport. But it does not fit 5.15 well:

1. It calls mptcp_subflow_queue_clean() and mptcp_event_pm_listener(), which
   are not in 5.15.
2. Its mptcp_disconnect() hunk relies on commit b29fcfb54cd7 ("mptcp: full
   disconnect implementation"), which is not in 5.15 either. Without it,
   shutdown(SHUT_RD) on a listener leaves the msk in TCP_LISTEN with the
   subflow already in TCP_CLOSE, and the WARN_ON_ONCE() in
   mptcp_check_listen_stop() fires on close().
3. It needs the follow-up commit 3fffa15bfef4 ("mptcp: ensure subflow is
   unhashed before cleaning the backlog") to get the unhash order right.

The simplest fix for 5.15 is this patch: in mptcp_close(), unhash the listener
subflow and drain its accept queue before it is orphaned. A better backport is
welcome.

This may become much easier to hit once services move to a newer Go.

A reproducer (written by an LLM) is at:
https://gist.github.com/mrpre/053f1849b5accec13c0fe9c569739fd7


------------[ cut here ]------------
refcount_t: addition on 0; use-after-free.
WARNING: CPU: 59 PID: 404175 at lib/refcount.c:25 refcount_warn_saturate+0x12e/0x150
RIP: 0010:refcount_warn_saturate+0x12e/0x150
RSP: 0018:ffffad3c8a2d7b68 EFLAGS: 00010246
RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
RBP: ffffad3c8a2d7b70 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000000 R12: ffff88f3421c2300
R13: ffff88d43e13b508 R14: ffff88f3421c26f0 R15: ffff88d3df5d4900
FS:  0000000000000000(0000) GS:ffff89127fd40000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000c04e5f8000 CR3: 00000005dfaba004 CR4: 00000000007706e0
PKRU: 55555554
Call Trace:
 <TASK>
 inet_csk_listen_stop+0x3e1/0x530
 __tcp_close+0x3bc/0x440
 __mptcp_close_ssk+0xb9/0x210
 __mptcp_destroy_sock+0x11c/0x280
 mptcp_close+0x25d/0x3b0
 inet_release+0x44/0x90
 __sock_release+0x42/0xb0
 __fput+0x98/0x270
 ____fput+0xe/0x20
 task_work_run+0x6d/0xb0
 do_exit+0x1e7/0x3c0
 do_group_exit+0x3b/0xb0
 get_signal+0x17a/0x940
 arch_do_signal_or_restart+0xba/0x130
 exit_to_user_mode_loop+0xae/0x160
 exit_to_user_mode_prepare+0xad/0xc0
 irqentry_exit_to_user_mode+0x9/0x20
 irqentry_exit+0x21/0x40
 exc_page_fault+0x93/0x190
 asm_exc_page_fault+0x27/0x30


Jiayuan Chen (1):
  mptcp: fix UAF of child socket on listener close

 net/mptcp/protocol.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

-- 
2.43.0
[PATCH 5.15.y v1 1/1] mptcp: fix UAF of child socket on listener close
#18513
Author: Jiayuan Chen
Date: Wed, 18 Nov 2026 12:50
90 lines
11530 bytes
mptcp_close() orphans the listener subflow before inet_csk_listen_stop()
runs on it. A child created in that window inherits SOCK_DEAD, so a
following RST frees it while it is still in the accept queue:

close(listen_fd):                            3-way handshake & rst (plain TCP client)

mptcp_close
  // mark ssk as SOCK_DEAD
  sock_orphan(ssk);
  __mptcp_destroy_sock
    // ssk in conn_list is listen sk
    __mptcp_close_ssk
                                             // in 3-way handshake
                                             tcp_v4_syn_recv_sock
                                               // inherit SOCK_DEAD state from listen ssk
                                               inet_csk_clone_lock
                                             // process RST
                                             tcp_reset
                                               tcp_done
                                               // sk is SOCK_DEAD
                                               inet_csk_destroy_sock
                                                   sock_put(sk); // freed
      __tcp_close(ssk, 0)
        // free child
        inet_csk_listen_stop()
            process child        <==== process freed sk

refcount_t: addition on 0; use-after-free.
WARNING: CPU: 59 PID: 404175 at lib/refcount.c:25 refcount_warn_saturate+0x12e/0x150
RIP: 0010:refcount_warn_saturate+0x12e/0x150
Call Trace:
 <TASK>
 inet_csk_listen_stop+0x3e1/0x530
 __tcp_close+0x3bc/0x440
 __mptcp_close_ssk+0xb9/0x210
 __mptcp_destroy_sock+0x11c/0x280
 mptcp_close+0x25d/0x3b0
 inet_release+0x44/0x90
 __sock_release+0x42/0xb0
 __fput+0x98/0x270
 ____fput+0xe/0x20
 task_work_run+0x6d/0xb0
 do_exit+0x1e7/0x3c0
 do_group_exit+0x3b/0xb0
 get_signal+0x17a/0x940
 arch_do_signal_or_restart+0xba/0x130
 exit_to_user_mode_loop+0xae/0x160
 exit_to_user_mode_prepare+0xad/0xc0
 irqentry_exit_to_user_mode+0x9/0x20
 irqentry_exit+0x21/0x40
 exc_page_fault+0x93/0x190
 asm_exc_page_fault+0x27/0x30

Unhash the listener subflow and drain its accept queue before it is
orphaned. No new child can be queued after the unhash, and the ones
already queued are not SOCK_DEAD.

Upstream fixed this in commit 57fc0f1ceaa4 ("mptcp: ensure listener is
unhashed before updating the sk status"), which does not fit 5.15.

Fixes: e16163b6e2b7 ("mptcp: refactor shutdown and close")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 net/mptcp/protocol.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 2f18dcb8d..f1122038d 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -2834,6 +2834,16 @@ static void mptcp_close(struct sock *sk, long timeout)
 	sk->sk_shutdown = SHUTDOWN_MASK;
 
 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
+		struct sock *ssk = mptcp_sk(sk)->first;
+
+		if (ssk) {
+			lock_sock_nested(ssk, SINGLE_DEPTH_NESTING);
+			if (ssk->sk_state == TCP_LISTEN) {
+				tcp_set_state(ssk, TCP_CLOSE);
+				inet_csk_listen_stop(ssk);
+			}
+			release_sock(ssk);
+		}
 		inet_sk_state_store(sk, TCP_CLOSE);
 		goto cleanup;
 	}
-- 
2.43.0
Re: [PATCH 5.15.y v1 1/1] mptcp: fix UAF of child socket on listener close
#19044
Author: Matthieu Baerts
Date: Thu, 19 Nov 2026 08:38
77 lines
13928 bytes
Hi Jiayuan,

On 18/11/2026 13:50, Jiayuan Chen wrote:
> mptcp_close() orphans the listener subflow before inet_csk_listen_stop()
> runs on it. A child created in that window inherits SOCK_DEAD, so a
> following RST frees it while it is still in the accept queue:
> 
> close(listen_fd):                            3-way handshake & rst (plain TCP client)
> 
> mptcp_close
>   // mark ssk as SOCK_DEAD
>   sock_orphan(ssk);
>   __mptcp_destroy_sock
>     // ssk in conn_list is listen sk
>     __mptcp_close_ssk
>                                              // in 3-way handshake
>                                              tcp_v4_syn_recv_sock
>                                                // inherit SOCK_DEAD state from listen ssk
>                                                inet_csk_clone_lock
>                                              // process RST
>                                              tcp_reset
>                                                tcp_done
>                                                // sk is SOCK_DEAD
>                                                inet_csk_destroy_sock
>                                                    sock_put(sk); // freed
>       __tcp_close(ssk, 0)
>         // free child
>         inet_csk_listen_stop()
>             process child        <==== process freed sk
> 
> refcount_t: addition on 0; use-after-free.
> WARNING: CPU: 59 PID: 404175 at lib/refcount.c:25 refcount_warn_saturate+0x12e/0x150
> RIP: 0010:refcount_warn_saturate+0x12e/0x150
> Call Trace:
>  <TASK>
>  inet_csk_listen_stop+0x3e1/0x530
>  __tcp_close+0x3bc/0x440
>  __mptcp_close_ssk+0xb9/0x210
>  __mptcp_destroy_sock+0x11c/0x280
>  mptcp_close+0x25d/0x3b0
>  inet_release+0x44/0x90
>  __sock_release+0x42/0xb0
>  __fput+0x98/0x270
>  ____fput+0xe/0x20
>  task_work_run+0x6d/0xb0
>  do_exit+0x1e7/0x3c0
>  do_group_exit+0x3b/0xb0
>  get_signal+0x17a/0x940
>  arch_do_signal_or_restart+0xba/0x130
>  exit_to_user_mode_loop+0xae/0x160
>  exit_to_user_mode_prepare+0xad/0xc0
>  irqentry_exit_to_user_mode+0x9/0x20
>  irqentry_exit+0x21/0x40
>  exc_page_fault+0x93/0x190
>  asm_exc_page_fault+0x27/0x30
> 
> Unhash the listener subflow and drain its accept queue before it is
> orphaned. No new child can be queued after the unhash, and the ones
> already queued are not SOCK_DEAD.
> 
> Upstream fixed this in commit 57fc0f1ceaa4 ("mptcp: ensure listener is
> unhashed before updating the sk status"), which does not fit 5.15.

This patch looks good to me for v5.15:

Reviewed-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>

> This may become much easier to hit once services move to a newer Go.

I hope they will switch to a newer kernel first, v5.15 is supposed to be
EOL next month :)

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.
Re: [PATCH 5.15.y v1 1/1] mptcp: fix UAF of child socket on listener close
#19242
Author: Sasha Levin
Date: Thu, 19 Nov 2026 16:08
10 lines
7935 bytes
> mptcp_close() orphans the listener subflow before inet_csk_listen_stop()
> runs on it. A child created in that window inherits SOCK_DEAD, so a
> following RST frees it while it is still in the accept queue

Queued for 5.15, thanks.

-- 
Thanks,
Sasha
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads