Skip to contentSkip to conversation

mptcp: misc fixes for v7.4-rc5

PATCH Applied Matthieu Baerts (NGI0) 7 participants 23 messages Nov 23 – Dec 1

Subsystems (7)
lore
Share & tools
lore

Matthieu Baerts (NGI0)

author maintainer
#0 lore raw

cover mptcp: misc fixes for v7.4-rc5

Here are some unrelated fixes:

- Patches 1-2: drain the queues of a TCP or MPTCP socket when listen() fails, not to leak sockets and their netns. Fixes for v4.4 and v5.11.

- Patch 3: clear inet_opt in the child of an IPv6 listener, to avoid a double free with SELinux and NetLabel. A fix for v5.6.

- Patch 4: fix UaF in corner cases from delegated actions. A fix for v5.12.

- Patches 5-6: do not leak an orphan moved to the CLOSE state outside the worker. Fixes for v6.15, and for another fix introduced in v6.6 and targeting v5.11.

- Patches 7-8: do not let PM listeners pin their netns, leaked otherwise. A fix for v5.12.

- Patch 9: initialize drop_req, not to leak sockets with SO_REUSEPORT migration. A fix for v5.14.

- Patch 10: do not pass a BPF request sock to an MPTCP listener, to avoid a slab-out-of-bounds write. A fix for v6.9.

---
Chris Mason (6):
tcp: drain the queues of a listen() that fails
mptcp: drain the queues of a listen() that fails
mptcp: clear inet_opt in the child of an IPv6 listener
mptcp: leave an orphan's DATA_FIN to the worker
mptcp: initialize drop_req in subflow_init_req()
tcp: do not pass a BPF reqsk to an MPTCP listener

Hyunwoo Kim (1):
mptcp: fix use-after-free of the subflow context on the delegated list

Josef Bacik (1):
mptcp: pm: don't let listeners pin their netns

Matthieu Baerts (NGI0) (2):
mptcp: don't leak an orphan when the PM closes its last subflow
net: add __sk_net_refcnt_upgrade() with a GFP mask

 include/net/sock.h              |  1 +
 net/core/sock.c                 |  9 +++++++--
 net/ipv4/inet_connection_sock.c | 12 ++++++++----
 net/ipv4/syncookies.c           |  3 ++-
 net/mptcp/protocol.c            | 42 +++++++++++++++++++++++++++++++++--------
 net/mptcp/protocol.h            | 18 ++++++++++++++++--
 net/mptcp/subflow.c             | 32 ++++++++++++++++++++++++++++---
 7 files changed, 97 insertions(+), 20 deletions(-)

---
base-commit: 3896ae4adf5ffc11ffd0f7f42c3923e8dc195e75
change-id: 20261123-net-mptcp-misc-fixes-7-4-rc5-965d6b4fb777

Best regards,

Signature · 1 line
Matthieu Baerts (NGI0) <[email protected]>
author maintainer
#1 lore raw

PATCH 1/10 tcp: drain the queues of a listen() that fails

From: Chris Mason <[email protected]>

If listen() fails on a TCP socket that has listened before, the sockets of connections from the earlier listen() can leak, together with their network namespace.

inet_csk_listen_start() sets TCP_LISTEN before it calls get_port() and hash(), and its error path sets TCP_CLOSE again, then purges the Fast Open RST queue since commit 342e62fd34c2 ("tcp: purge Fast Open RST queue when listen() fails"), but not the accept queue. Pending request socks outlive an earlier listen() and still point to the socket, so in TCP_LISTEN, inet_csk_reqsk_queue_add() adds them to the accept queue, with their new child. These entries, and the sockets that they hold, are never freed, since close() drains this queue only in TCP_LISTEN and the next listen() stores NULL in rskq_accept_head.

The fix used here is to call inet_csk_listen_stop() after the error path sets TCP_CLOSE, which also purges the RST queue, except for an MPTCP subflow, which needs mptcp_subflow_queue_clean() first: only its RST queue is purged here, and mptcp_listen() has to drain its accept queue, which is done in the next patch, "mptcp: drain the queues of a listen() that fails".

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, run as an unprivileged user: listen() failing with EADDRINUSE in a loop, while the connections of an earlier listen() complete. Without this patch, sockets were leaked, and network namespaces too when each round was in its own one, without any message from the kernel:

result: request socks: 10000 made, 2535 left. Sockets: 33373 made, 5070 left [...]
result: namespaces: 1000 made, 694 taken down, 306 left

Fixes 079096f103fa ("tcp/dccp: install syn_recv requests into ehash table")

[ Shorter comments. Commit message: how the issue was found, and its
symptoms. On top of commit 342e62fd34c2, with a Fixes tag for the accept queue. ]

---
Cc: Neal Cardwell <[email protected]>
Cc: Kuniyuki Iwashima <[email protected]>

---
 net/ipv4/inet_connection_sock.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
net/ipv4/inet_connection_sock.c +8 -4
index 9a3b05edb62b..2e27b3e592a8 100644
@@ -1382,8 +1382,8 @@ int inet_csk_listen_start(struct sock *sk)
 
 	/* There is race window here: we announce ourselves listening,
 	 * but this transition is still not validated by get_port().
-	 * It is OK, because this socket enters to hash table only
-	 * after validation is complete.
+	 * This socket enters to hash table only after validation is
+	 * complete, but reqs of an earlier listen() may queue children.
 	 */
 	inet_sk_state_store(sk, TCP_LISTEN);
 	err = sk->sk_prot->get_port(sk, inet->inet_num);
@@ -1401,9 +1401,13 @@ int inet_csk_listen_start(struct sock *sk)
 
 	/* A Fast Open child accepted before a previous shutdown() of this
 	 * listener might have queued its request in rskq_rst_head while
-	 * we were transiently in TCP_LISTEN state.
+	 * we were transiently in TCP_LISTEN state. The accept queue of an
+	 * MPTCP subflow is drained by mptcp_listen().
 	 */
-	reqsk_fastopen_purge_rst(&icsk->icsk_accept_queue);
+	if (sk_is_mptcp(sk))
+		reqsk_fastopen_purge_rst(&icsk->icsk_accept_queue);
+	else
+		inet_csk_listen_stop(sk);
 	return err;
 }
 
Signature · 1 line
2.55.0
maintainer
#2 lore raw

Re: [PATCH net 01/10] tcp: drain the queues of a listen() that fails

Quoted 34 lines · On 11/23/26 14:24, Matthieu Baerts (NGI0) wrote:

From: Chris Mason <[email protected]>

If listen() fails on a TCP socket that has listened before, the sockets of connections from the earlier listen() can leak, together with their network namespace.

inet_csk_listen_start() sets TCP_LISTEN before it calls get_port() and hash(), and its error path sets TCP_CLOSE again, then purges the Fast Open RST queue since commit 342e62fd34c2 ("tcp: purge Fast Open RST queue when listen() fails"), but not the accept queue. Pending request socks outlive an earlier listen() and still point to the socket, so in TCP_LISTEN, inet_csk_reqsk_queue_add() adds them to the accept queue, with their new child. These entries, and the sockets that they hold, are never freed, since close() drains this queue only in TCP_LISTEN and the next listen() stores NULL in rskq_accept_head.

The fix used here is to call inet_csk_listen_stop() after the error path sets TCP_CLOSE, which also purges the RST queue, except for an MPTCP subflow, which needs mptcp_subflow_queue_clean() first: only its RST queue is purged here, and mptcp_listen() has to drain its accept queue, which is done in the next patch, "mptcp: drain the queues of a listen() that fails".

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, run as an unprivileged user: listen() failing with EADDRINUSE in a loop, while the connections of an earlier listen() complete. Without this patch, sockets were leaked, and network namespaces too when each round was in its own one, without any message from the kernel:

result: request socks: 10000 made, 2535 left. Sockets: 33373 made, 5070 left [...]
result: namespaces: 1000 made, 694 taken down, 306 left

Quoted 7 lines
Fixes 079096f103fa ("tcp/dccp: install syn_recv requests into ehash table")

[ Shorter comments. Commit message: how the issue was found, and its
symptoms. On top of commit 342e62fd34c2, with a Fixes tag for the accept queue. ]

@Kuniyuki, @Neal: an explicit ack would be appreciated here.

/P

maintainer Reviewed-by
#3 lore raw

Re: [PATCH net 01/10] tcp: drain the queues of a listen() that fails

Quoted 35 lines · Paolo Abeni
Quoted 34 lines · On 11/23/26 14:24, Matthieu Baerts (NGI0) wrote:

From: Chris Mason <[email protected]>

If listen() fails on a TCP socket that has listened before, the sockets of connections from the earlier listen() can leak, together with their network namespace.

inet_csk_listen_start() sets TCP_LISTEN before it calls get_port() and hash(), and its error path sets TCP_CLOSE again, then purges the Fast Open RST queue since commit 342e62fd34c2 ("tcp: purge Fast Open RST queue when listen() fails"), but not the accept queue. Pending request socks outlive an earlier listen() and still point to the socket, so in TCP_LISTEN, inet_csk_reqsk_queue_add() adds them to the accept queue, with their new child. These entries, and the sockets that they hold, are never freed, since close() drains this queue only in TCP_LISTEN and the next listen() stores NULL in rskq_accept_head.

The fix used here is to call inet_csk_listen_stop() after the error path sets TCP_CLOSE, which also purges the RST queue, except for an MPTCP subflow, which needs mptcp_subflow_queue_clean() first: only its RST queue is purged here, and mptcp_listen() has to drain its accept queue, which is done in the next patch, "mptcp: drain the queues of a listen() that fails".

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, run as an unprivileged user: listen() failing with EADDRINUSE in a loop, while the connections of an earlier listen() complete. Without this patch, sockets were leaked, and network namespaces too when each round was in its own one, without any message from the kernel:

result: request socks: 10000 made, 2535 left. Sockets: 33373 made, 5070 left [...]
result: namespaces: 1000 made, 694 taken down, 306 left

Quoted 8 lines
Quoted 7 lines
Fixes 079096f103fa ("tcp/dccp: install syn_recv requests into ehash table")

[ Shorter comments. Commit message: how the issue was found, and its
symptoms. On top of commit 342e62fd34c2, with a Fixes tag for the accept queue. ]

@Kuniyuki, @Neal: an explicit ack would be appreciated here.

Sorry I missed this.

Looks good, thanks !

author maintainer
#4 lore raw

PATCH 2/10 mptcp: drain the queues of a listen() that fails

In reply to Matthieu Baerts (NGI0)

From: Chris Mason <[email protected]>

Sockets and their network namespace can leak, if a handshake completes while listen() fails on an MPTCP socket that has listened before.

mptcp_listen() passes the first subflow through __inet_listen_sk() to inet_csk_listen_start(), which sets TCP_LISTEN, then calls get_port() and hash(), and sets TCP_CLOSE again if either fails. Until then the subflow is in TCP_LISTEN, so a request sock of a previous listen() that still points to it can get a new child and be added to the subflow's accept queue. But neither close() nor the next listen() frees what is queued, and the earlier commit "tcp: drain the queues of a listen() that fails" only purges the RST queue of an MPTCP subflow, which needs mptcp_subflow_queue_clean() before a drain.

The fix used here is to call mptcp_subflow_queue_clean() and then inet_csk_listen_stop() on the subflow in mptcp_listen(), if __inet_listen_sk() returns an error with the subflow in TCP_CLOSE.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer of the previous commit, modified to use MPTCP sockets, on both sides. Without this patch, sockets were leaked, and network namespaces too when each round was in its own one, without any message from the kernel:

result: request socks: 10000 made, 1151 left. Sockets: 64982 made, 5755 left [...]
result: namespaces: 1000 made, 923 taken down, 77 left

Fixes 76e2a55d1625 ("mptcp: better msk-level shutdown.")

[ Shorter comment. Add a Fixes tag. Commit message: how the issue was
found, and its symptoms. ]

---
 net/mptcp/protocol.c | 5 +++++
 1 file changed, 5 insertions(+)
net/mptcp/protocol.c +5 -0
index b375da1f17fe..fd88104f0631 100644
@@ -4340,6 +4340,11 @@ static int mptcp_listen(struct socket *sock, int backlog)
 
 	lock_sock(ssk);
 	err = __inet_listen_sk(ssk, backlog);
+	if (err && ssk->sk_state == TCP_CLOSE) {
+		/* inet_csk_listen_start() leaves this drain to MPTCP */
+		mptcp_subflow_queue_clean(sk, ssk);
+		inet_csk_listen_stop(ssk);
+	}
 	release_sock(ssk);
 	mptcp_set_state(sk, inet_sk_state_load(ssk));
 
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 3/10 mptcp: clear inet_opt in the child of an IPv6 listener

In reply to Matthieu Baerts (NGI0)

From: Chris Mason <[email protected]>

With SELinux, an IPv6 MPTCP listener and its children can cause a double free of one CIPSO option, and then a use-after-free read of it, if NetLabel uses CIPSO for IPv4 and selects the IPv6 labels by address.

mptcp_sk_clone_init() creates the child of a listener with sk_clone_lock(), as a byte copy of the listener, so each pointer in the copy that the socket destructor frees has to be replaced or cleared. mptcp_copy_ip_options() duplicates inet_opt, the IPv4 options, for AF_INET, and mptcp_copy_ip6_options() duplicates the IPv6 options for AF_INET6. But an AF_INET6 socket has an inet_opt pointer too, and its child keeps the listener's pointer.

MPTCP does not support setsockopt(IP_OPTIONS), so that pointer is usually NULL. But if connect() gets a struct sockaddr_in, netlbl_conn_setattr() does not look at the family of the socket, so the connect() hook of SELinux can store a CIPSO option in inet_opt of an AF_INET6 MPTCP listener. inet_sock_destruct() then passes that option to kfree_rcu() for each child and again for the listener, and after a child has freed it, another such connect() on the listener reads it in cipso_v4_sock_setattr().

The fix used here is to clear inet_opt of the child in mptcp_copy_ip6_options().

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, with SELinux, NetLabel -- CIPSO for some IPv4 addresses, and the address-based selection used for IPv6 too -- and KASAN: an unprivileged user calls connect() with an IPv4 address on an IPv6 MPTCP listener. Without this patch, an object is freed twice:

  BUG: KASAN: double-free in rcu_free_sheaf+0x23/0x100
  Free of addr ffff888012c27a80 by task kworker/1:1/50
  CPU: 1 UID: 0 PID: 50 Comm: kworker/1:1 Not tainted 7.4.0-rc1+ #1 PREEMPT(full)
  Workqueue: events mptcp_worker
  Call Trace:
   <IRQ>
   kasan_report_invalid_free+0xd4/0x1b0
   check_slab_allocation+0xc2/0xf0
   __rcu_free_sheaf_prepare+0x102/0x2a0
   rcu_free_sheaf+0x23/0x100
   rcu_do_batch+0x29a/0xf80
   rcu_core+0x33c/0x830
   handle_softirqs+0x212/0x980
   do_softirq+0xb4/0xf0
   </IRQ>
   <TASK>
   __local_bh_enable_ip+0x11c/0x150
   mptcp_token_destroy+0x1c9/0x360
   mptcp_destroy_common+0x2d9/0x430
   mptcp_destroy+0x86/0x110
   __mptcp_destroy_sock+0x176/0x350
   mptcp_worker+0xbb5/0xf80
  [...]
  Allocated by task 176:
  [...]
   cipso_v4_sock_setattr+0x9b/0x3d0
   netlbl_conn_setattr+0x3ac/0x500
   selinux_netlbl_socket_connect_locked+0x102/0x210
   selinux_netlbl_socket_connect+0x23/0x40
   security_socket_connect+0x2e/0x60
   __sys_connect_file+0xcb/0x200
  [...]

In another case, the CIPSO option is used after having been freed:

  BUG: KASAN: slab-use-after-free in cipso_v4_sock_setattr+0x3c7/0x3d0
  Read of size 1 at addr ffff8880161c2f18 by task python3/177
  CPU: 1 UID: 65534 PID: 177 Comm: python3 Not tainted 7.4.0-rc1+ #1 PREEMPT(full)
  Call Trace:
   <TASK>
   kasan_report+0x101/0x140
   cipso_v4_sock_setattr+0x3c7/0x3d0
   netlbl_conn_setattr+0x3ac/0x500
   selinux_netlbl_socket_connect_locked+0x102/0x210
   selinux_netlbl_socket_connect+0x23/0x40
   security_socket_connect+0x2e/0x60
   __sys_connect_file+0xcb/0x200
   __sys_connect+0xfd/0x130
   __x64_sys_connect+0x72/0xd0
  [...]
Fixes cf7da0d66cc1 ("mptcp: Create SUBFLOW socket for incoming connections")
[ Commit message: how the issue was found, and its symptoms ]
---
 net/mptcp/protocol.c | 3 +++
 1 file changed, 3 insertions(+)
net/mptcp/protocol.c +3 -0
index fd88104f0631..50c294d925b0 100644
@@ -3677,6 +3677,9 @@ static void mptcp_copy_ip6_options(struct sock *newsk, const struct sock *sk)
 
 	newnp = inet6_sk(newsk);
 
+	/* The IPv4 options belong to the listener, which frees them */
+	RCU_INIT_POINTER(inet_sk(newsk)->inet_opt, NULL);
+
 	rcu_read_lock();
 	opt = rcu_dereference(np->opt);
 	if (opt) {
Signature · 1 line
2.55.0
author maintainer
#6 lore raw

PATCH 4/10 mptcp: fix use-after-free of the subflow context on the delegated list

In reply to Matthieu Baerts (NGI0)

From: Hyunwoo Kim <[email protected]>

mptcp_subflow_delegate() links the subflow context into a per-CPU napi list and takes a reference on the subflow socket. The context is a separate allocation, reached through icsk_ulp_data, and the list holds no reference to it.

inet_csk_destroy_sock() runs sk_prot->destroy() regardless of the outstanding references. That path ends in tcp_cleanup_ulp(), which calls subflow_ulp_release(). subflow_ulp_release() frees the context with kfree_rcu() but does not remove it from the list. Once the grace period elapses and the context is actually freed, the next mptcp_napi_poll() dequeues that entry and mptcp_subflow_delegated_next() writes to the freed context with list_del_init(). That is the use-after-free.

The decoded KASAN trace is:

==================================================================

 BUG: KASAN: slab-use-after-free in __list_del_entry_valid_or_report (lib/list_debug.c:49)
 Read of size 8 at addr ffff88800cc13cc8 by task poc/88

 CPU: 3 UID: 65534 PID: 88 Comm: poc Not tainted 7.2.0-rc7 #1 PREEMPT(lazy)
 Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
 Call Trace:
  <IRQ>
  dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120)
  print_report (mm/kasan/report.c:378 mm/kasan/report.c:482)
  ? __pfx__raw_spin_lock_irqsave (include/asm-generic/qrwlock.h:122 (discriminator 4))
  ? __list_del_entry_valid_or_report (lib/list_debug.c:49)
  kasan_report (mm/kasan/report.c:595)
  ? __list_del_entry_valid_or_report (lib/list_debug.c:49)
  __list_del_entry_valid_or_report (lib/list_debug.c:49)
  mptcp_napi_poll (include/linux/list.h:132 include/linux/list.h:246 include/linux/list.h:318 net/mptcp/protocol.h:787 net/mptcp/protocol.c:4620)
  ? call_timer_fn (kernel/time/timer.c:1748)
  __napi_poll (net/core/dev.c:7735)
  ? __pfx_delayed_work_timer_fn (include/linux/list.h:404 (discriminator 2))
  net_rx_action (net/core/dev.c:7798 net/core/dev.c:7955)
  ? __pfx_net_rx_action (include/net/gro.h:525)
  ? _raw_spin_lock_irq (include/linux/instrumented.h:55 include/linux/atomic/atomic-instrumented.h:1301 include/asm-generic/qspinlock.h:111 include/linux/spinlock.h:187 include/linux/spinlock_api_smp.h:143 kernel/locking/spinlock.c:174)
  ? __pfx__raw_spin_lock_irq (kernel/locking/spinlock.c:183)
  ? try_to_wake_up (include/linux/spinlock.h:571 kernel/sched/core.c:4292)
  handle_softirqs (kernel/softirq.c:622)
  ? __pfx_handle_softirqs (include/linux/wait_bit.h:77)
  ? __hrtimer_rearm_deferred (kernel/time/hrtimer.c:707 kernel/time/hrtimer.c:2141 kernel/time/hrtimer.c:2161)
  do_softirq (kernel/softirq.c:523 (discriminator 20) kernel/softirq.c:510 (discriminator 20))
  </IRQ>
  <TASK>
  __local_bh_enable_ip (kernel/softirq.c:450)
  inet_stream_connect (net/ipv4/af_inet.c:756)
  __sys_connect (net/socket.c:2154)
  ? __pfx___sys_connect (net/socket.c:2136)
  ? __sys_setsockopt (include/linux/file.h:63 include/linux/file.h:83 net/socket.c:2385)
  __x64_sys_connect (net/socket.c:2160 net/socket.c:2157 net/socket.c:2157)
  ? fpregs_assert_state_consistent (include/linux/instrumented.h:82 include/asm-generic/bitops/instrumented-non-atomic.h:141 include/linux/thread_info.h:133 arch/x86/kernel/fpu/core.c:912)
  do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
 RIP: 0033:0x4216eb
 Code: 48 89 e5 48 83 ec 10 89 55 f8 48 89 75 f0 89 7d fc e8 b9 ec 02 00 8b 55 f8 48 8b 75 f0 41 89 c0 8b 7d fc b8 2a 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 2d 44 89 c7 89 45 fc e8 12 ed 02 00 8b 45 fc
All code
========
   0:	48 89 e5             	mov    %rsp,%rbp
   3:	48 83 ec 10          	sub    $0x10,%rsp
   7:	89 55 f8             	mov    %edx,-0x8(%rbp)
   a:	48 89 75 f0          	mov    %rsi,-0x10(%rbp)
   e:	89 7d fc             	mov    %edi,-0x4(%rbp)
  11:	e8 b9 ec 02 00       	call   0x2eccf
  16:	8b 55 f8             	mov    -0x8(%rbp),%edx
  19:	48 8b 75 f0          	mov    -0x10(%rbp),%rsi
  1d:	41 89 c0             	mov    %eax,%r8d
  20:	8b 7d fc             	mov    -0x4(%rbp),%edi
  23:	b8 2a 00 00 00       	mov    $0x2a,%eax
  28:	0f 05                	syscall
  2a:*	48 3d 00 f0 ff ff    	cmp    $0xfffffffffffff000,%rax		<-- trapping instruction
  30:	77 2d                	ja     0x5f
  32:	44 89 c7             	mov    %r8d,%edi
  35:	89 45 fc             	mov    %eax,-0x4(%rbp)
  38:	e8 12 ed 02 00       	call   0x2ed4f
  3d:	8b 45 fc             	mov    -0x4(%rbp),%eax

Code starting with the faulting instruction
===========================================

   0:	48 3d 00 f0 ff ff    	cmp    $0xfffffffffffff000,%rax
   6:	77 2d                	ja     0x35
   8:	44 89 c7             	mov    %r8d,%edi
   b:	89 45 fc             	mov    %eax,-0x4(%rbp)
   e:	e8 12 ed 02 00       	call   0x2ed25
  13:	8b 45 fc             	mov    -0x4(%rbp),%eax
 RSP: 002b:00007fbe0147b1d0 EFLAGS: 00000293 ORIG_RAX: 000000000000002a
 RAX: ffffffffffffffda RBX: 000000000000000a RCX: 00000000004216eb
 RDX: 0000000000000010 RSI: 00007fbe0147b200 RDI: 000000000000000a
 RBP: 00007fbe0147b1e0 R08: 0000000000000000 R09: 0000000000000000
 R10: 00007fbe0147b1f8 R11: 0000000000000293 R12: 00007fbe0147b1f8
 R13: 00007fbe0157b678 R14: 00007fbe0147b1f4 R15: 00007fffd4bc16c0
  </TASK>

 Allocated by task 90:
  kasan_save_stack (mm/kasan/common.c:57)
  kasan_save_track (mm/kasan/common.c:78)
  __kasan_kmalloc (mm/kasan/common.c:398 mm/kasan/common.c:415)
  __kmalloc_cache_noprof (include/linux/kasan.h:263 mm/slub.c:5489)
  subflow_create_ctx (include/linux/slab.h:988 include/linux/slab.h:1309 net/mptcp/subflow.c:1845)
  subflow_ulp_init (net/mptcp/subflow.c:1977)
  tcp_set_ulp (net/ipv4/tcp_ulp.c:146 net/ipv4/tcp_ulp.c:167)
  mptcp_subflow_create_socket (net/mptcp/subflow.c:1806)
  __mptcp_nmpc_sk.part.0 (net/mptcp/protocol.c:111 net/mptcp/protocol.c:143)
  mptcp_connect (net/mptcp/protocol.c:139 net/mptcp/protocol.c:4009)
  __inet_stream_connect (net/ipv4/af_inet.c:684)
  inet_stream_connect (net/ipv4/af_inet.c:755)
  __sys_connect (net/socket.c:2154)
  __x64_sys_connect (net/socket.c:2160 net/socket.c:2157 net/socket.c:2157)
  do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)

 Freed by task 22:
  kasan_save_stack (mm/kasan/common.c:57)
  kasan_save_track (mm/kasan/common.c:78)
  kasan_save_free_info (mm/kasan/generic.c:584)
  __kasan_slab_free (mm/kasan/common.c:253 mm/kasan/common.c:285)
  kvfree_rcu_cb (include/linux/kasan.h:235 mm/slub.c:2677 mm/slub.c:6377 mm/slub.c:6662)
  rcu_core (kernel/rcu/tree.c:2645 kernel/rcu/tree.c:2897)
  handle_softirqs (kernel/softirq.c:622)
  run_ksoftirqd (kernel/softirq.c:1076 kernel/softirq.c:1068)
  smpboot_thread_fn (kernel/smpboot.c:160)
  kthread (kernel/kthread.c:436)
  ret_from_fork (arch/x86/kernel/process.c:158)
  ret_from_fork_asm (arch/x86/entry/entry_64.S:245)

 Last potentially related work creation:
  kasan_save_stack (mm/kasan/common.c:57)
  kasan_record_aux_stack (mm/kasan/generic.c:556)
  __call_rcu_common.constprop.0 (kernel/rcu/tree.c:3159)
  tcp_cleanup_ulp (net/ipv4/tcp_ulp.c:124)
  tcp_v4_destroy_sock (net/ipv4/tcp_ipv4.c:2437)
  inet_csk_destroy_sock (net/ipv4/inet_connection_sock.c:1282)
  __tcp_close (net/ipv4/tcp.c:3299)
  __mptcp_close_ssk (net/mptcp/protocol.c:2648)
  mptcp_destroy_common (net/mptcp/protocol.c:3442 (discriminator 1))
  mptcp_destroy (net/mptcp/protocol.c:3651)
  __mptcp_destroy_sock (net/mptcp/protocol.c:3284)
  __mptcp_close (net/mptcp/protocol.c:3386)
  mptcp_close (net/mptcp/protocol.c:3401)
  inet_release (net/ipv4/af_inet.c:442)
  __sock_release (net/socket.c:710)
  sock_close (net/socket.c:1501 (discriminator 1))
  __fput (fs/file_table.c:512)
  fput_close_sync (fs/file_table.c:617)
  __x64_sys_close (fs/open.c:1511 fs/open.c:1496 fs/open.c:1496)
  do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)

 Second to last potentially related work creation:
  kasan_save_stack (mm/kasan/common.c:57)
  kasan_record_aux_stack (mm/kasan/generic.c:556)
  kvfree_call_rcu (mm/slab_common.c:1288)
  tcp_cleanup_ulp (net/ipv4/tcp_ulp.c:124)
  tcp_v4_destroy_sock (net/ipv4/tcp_ipv4.c:2437)
  inet_csk_destroy_sock (net/ipv4/inet_connection_sock.c:1282)
  __tcp_close (net/ipv4/tcp.c:3299)
  __mptcp_close_ssk (net/mptcp/protocol.c:2648)
  mptcp_destroy_common (net/mptcp/protocol.c:3442 (discriminator 1))
  mptcp_destroy (net/mptcp/protocol.c:3651)
  __mptcp_destroy_sock (net/mptcp/protocol.c:3284)
  __mptcp_close (net/mptcp/protocol.c:3386)
  mptcp_close (net/mptcp/protocol.c:3401)
  inet_release (net/ipv4/af_inet.c:442)
  __sock_release (net/socket.c:710)
  sock_close (net/socket.c:1501 (discriminator 1))
  __fput (fs/file_table.c:512)
  fput_close_sync (fs/file_table.c:617)
  __x64_sys_close (fs/open.c:1511 fs/open.c:1496 fs/open.c:1496)
  do_syscall_64 (arch/x86/entry/syscall_64.c:63 arch/x86/entry/syscall_64.c:94)
  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)

 The buggy address belongs to the object at ffff88800cc13c00
  which belongs to the cache kmalloc-512 of size 512
 The buggy address is located 200 bytes inside of
  freed 512-byte region [ffff88800cc13c00, ffff88800cc13e00)

 The buggy address belongs to the physical page:
 page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0xcc10
 head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
 flags: 0x100000000000040(head|node=0|zone=1)
 page_type: f5(slab)
 raw: 0100000000000040 ffff888007042c80 dead000000000100 dead000000000122
 raw: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000
 head: 0100000000000040 ffff888007042c80 dead000000000100 dead000000000122
 head: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000
 head: 0100000000000002 ffffffffffffff01 00000000ffffffff 00000000ffffffff
 head: 0000000000000000 0000000000000000 00000000ffffffff 0000000000000000
 page dumped because: kasan: bad access detected

 Memory state around the buggy address:
  ffff88800cc13b80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
  ffff88800cc13c00: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 >ffff88800cc13c80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                               ^
  ffff88800cc13d00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
  ffff88800cc13d80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ==================================================================

The freeing path cannot dequeue the entry itself: the list is per-CPU and only the CPU that queued the subflow may touch it, as tcp_release_cb_override() already notes.

Delay the free instead. Add MPTCP_DELEGATE_DEAD. mptcp_subflow_free_ctx() sets it and frees the context only if the subflow is not scheduled, while mptcp_napi_poll() frees it if the dead bit is set when it clears the scheduled bit. Either mptcp_subflow_free_ctx() gets there first, sees the scheduled bit and leaves the free to mptcp_napi_poll(), or mptcp_napi_poll() gets there first, does not see the dead bit and mptcp_subflow_free_ctx() does the free.

Also skip mptcp_subflow_process_delegated() once the dead bit is set. subflow_ulp_release() has already dropped the msk reference by then, so the pending actions must not run.

Finally, move delegated_status out of struct_group(reset). mptcp_subflow_ctx_reset() clears that group on disconnect, and clearing the scheduled bit while the entry is still queued would let mptcp_subflow_free_ctx() free a context the napi list still points to. delegated_node is already outside the group.

Fixes b19bc2945b40 ("mptcp: implement delegated actions")
---
 net/mptcp/protocol.c | 13 ++++++++++---
 net/mptcp/protocol.h | 18 ++++++++++++++++--
 net/mptcp/subflow.c  |  4 ++--
 3 files changed, 28 insertions(+), 7 deletions(-)
net/mptcp/protocol.c +10 -3
index 50c294d925b0..23d35f43b566 100644
@@ -2708,7 +2708,7 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
 	 */
 	if (!inet_csk(ssk)->icsk_ulp_ops) {
 		WARN_ON_ONCE(!sock_flag(ssk, SOCK_DEAD));
-		kfree_rcu(subflow, rcu);
+		mptcp_subflow_free_ctx(subflow);
 	} else {
 		/* otherwise tcp will dispose of the ssk and subflow ctx */
 		__tcp_close(ssk, 0);
@@ -4803,10 +4803,13 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget)
 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+		long status;
 
 		bh_lock_sock_nested(ssk);
 		if (!sock_owned_by_user(ssk)) {
-			mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
+			status = xchg(&subflow->delegated_status, 0);
+			if (!(status & BIT(MPTCP_DELEGATE_DEAD)))
+				mptcp_subflow_process_delegated(ssk, status);
 		} else {
 			/* tcp_release_cb_override already processed
 			 * the action or will do at next release_sock().
@@ -4814,11 +4817,15 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget)
 			 * CPU that scheduled it.
 			 */
 			smp_wmb();
-			clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
+			status = set_mask_bits(&subflow->delegated_status,
+					       BIT(MPTCP_DELEGATE_SCHEDULED), 0);
 		}
 		bh_unlock_sock(ssk);
 		sock_put(ssk);
 
+		if (status & BIT(MPTCP_DELEGATE_DEAD))
+			kfree_rcu(subflow, rcu);
+
 		if (++work_done == budget)
 			return budget;
 	}
net/mptcp/protocol.h +16 -2
index 7d226ee38315..7644651f01a8 100644
@@ -553,8 +553,10 @@ DECLARE_PER_CPU(struct mptcp_delegated_action, mptcp_delegated_actions);
 #define MPTCP_DELEGATE_SEND		1
 #define MPTCP_DELEGATE_ACK		2
 #define MPTCP_DELEGATE_SNDBUF		3
+#define MPTCP_DELEGATE_DEAD		4
 
-#define MPTCP_DELEGATE_ACTIONS_MASK	(~BIT(MPTCP_DELEGATE_SCHEDULED))
+#define MPTCP_DELEGATE_ACTIONS_MASK	(~(BIT(MPTCP_DELEGATE_SCHEDULED) | \
+					   BIT(MPTCP_DELEGATE_DEAD)))
 /* MPTCP subflow context */
 struct mptcp_subflow_context {
 	struct	list_head node;/* conn_list of subflows */
@@ -628,11 +630,11 @@ struct mptcp_subflow_context {
 
 	u32	subflow_id;
 
-	long	delegated_status;
 	unsigned long	fail_tout;
 
 	);
 
+	long	delegated_status;
 	struct	list_head delegated_node;   /* link into delegated_action, protected by local BH */
 
 	u32	setsockopt_seq;
@@ -671,6 +673,8 @@ mptcp_subflow_ctx_reset(struct mptcp_subflow_context *subflow)
 	memset(&subflow->reset, 0, sizeof(subflow->reset));
 	subflow->request_mptcp = 1;
 	WRITE_ONCE(subflow->local_id, -1);
+	set_mask_bits(&subflow->delegated_status,
+		      MPTCP_DELEGATE_ACTIONS_MASK, 0);
 }
 
 /* Convert reset reasons in MPTCP to enum sk_rst_reason type */
@@ -808,6 +812,16 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated)
 	return ret;
 }
 
+static inline void mptcp_subflow_free_ctx(struct mptcp_subflow_context *subflow)
+{
+	long old = set_mask_bits(&subflow->delegated_status, 0,
+				 BIT(MPTCP_DELEGATE_DEAD));
+
+	/* a scheduled subflow is owned by mptcp_napi_poll(), which will free it */
+	if (!(old & BIT(MPTCP_DELEGATE_SCHEDULED)))
+		kfree_rcu(subflow, rcu);
+}
+
 void __mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp);
 void __mptcp_inherit_cgrp_data(struct sock *sk, struct sock *ssk);
 
net/mptcp/subflow.c +2 -2
index 6f07544bfe40..20d506235074 100644
@@ -832,7 +832,7 @@ void mptcp_subflow_drop_ctx(struct sock *ssk)
 			sock_put(ctx->conn);
 	}
 
-	kfree_rcu(ctx, rcu);
+	mptcp_subflow_free_ctx(ctx);
 }
 
 void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
@@ -2098,7 +2098,7 @@ static void subflow_ulp_release(struct sock *ssk)
 
 	mptcp_subflow_ops_undo_override(ssk);
 	if (release)
-		kfree_rcu(ctx, rcu);
+		mptcp_subflow_free_ctx(ctx);
 }
 
 static void subflow_ulp_clone(const struct request_sock *req,
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH net 04/10] mptcp: fix use-after-free of the subflow context on the delegated list

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The comment in the sock_owned_by_user() branch of mptcp_napi_poll() (net/mptcp/protocol.c) is now wrong for the case…

--

mptcp: fix use-after-free of the subflow context on the delegated list

This fixes a use-after-free. subflow_ulp_release() frees the subflow context with kfree_rcu() while the context is still linked on the per-CPU delegated napi list. A new MPTCP_DELEGATE_DEAD bit hands the free to mptcp_napi_poll() when the subflow is scheduled, skips delegated actions for dead contexts, and moves delegated_status out of struct_group(reset).

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 50c294d925b0c..23d35f43b566e 100644

[ ... ]

Quoted 16 lines
net/mptcp/protocol.c +4 -1
@@ -4803,10 +4803,13 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget)
 	delegated = container_of(napi, struct mptcp_delegated_action, napi);
 	while ((subflow = mptcp_subflow_delegated_next(delegated)) != NULL) {
 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+		long status;
 
 		bh_lock_sock_nested(ssk);
 		if (!sock_owned_by_user(ssk)) {
-			mptcp_subflow_process_delegated(ssk, xchg(&subflow->delegated_status, 0));
+			status = xchg(&subflow->delegated_status, 0);
+			if (!(status & BIT(MPTCP_DELEGATE_DEAD)))
+				mptcp_subflow_process_delegated(ssk, status);
 		} else {
 			/* tcp_release_cb_override already processed
 			 * the action or will do at next release_sock().

net/mptcp/protocol.c static int mptcp_napi_poll(struct napi_struct *napi, int budget) · line 4821

Quoted 6 lines
net/mptcp/protocol.c +2 -1
@@ -4814,11 +4817,15 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget)
 			 * CPU that scheduled it.
 			 */
 			smp_wmb();
-			clear_bit(MPTCP_DELEGATE_SCHEDULED, &subflow->delegated_status);
+			status = set_mask_bits(&subflow->delegated_status,
+					       BIT(MPTCP_DELEGATE_SCHEDULED), 0);

[Severity: Low]
This isn't a bug, but is the comment above this branch still accurate once MPTCP_DELEGATE_DEAD is set?

The comment says tcp_release_cb_override "already processed the action or will do at next release_sock()". However, every path that reaches mptcp_subflow_free_ctx() runs after mptcp_subflow_ops_undo_override() has restored ssk->sk_prot to plain tcp_prot/tcpv6_prot:

net/mptcp/subflow.c:subflow_ulp_release() {
    ...
	mptcp_subflow_ops_undo_override(ssk);
	if (release)
		mptcp_subflow_free_ctx(ctx);
}

mptcp_subflow_drop_ctx() reaches the same undo through subflow_ulp_fallback(). __mptcp_close_ssk() only calls mptcp_subflow_free_ctx() when icsk_ulp_ops is already NULL.

So for a dead context, the next release_sock() calls tcp_release_cb() rather than tcp_release_cb_override(). Any pending action bits are dropped, and the commit message says that is intended ("the pending actions must not run").

A dead context can reach this branch. __mptcp_close_ssk() holds lock_sock_nested(ssk) across this chain:

__tcp_close()->inet_csk_destroy_sock()->tcp_cleanup_ulp()->

    subflow_ulp_release()->mptcp_subflow_free_ctx()

That means a concurrent mptcp_napi_poll() can see sock_owned_by_user(ssk) after DEAD has been set.

Could the comment be updated to say that dead contexts have their pending actions discarded here? It could also mention that status is captured so the context can be freed with kfree_rcu() after bh_unlock_sock() and sock_put().

Quoted 10 lines
net/mptcp/protocol.c +3 -0
 		}
 		bh_unlock_sock(ssk);
 		sock_put(ssk);
 
+		if (status & BIT(MPTCP_DELEGATE_DEAD))
+			kfree_rcu(subflow, rcu);
+
 		if (++work_done == budget)
 			return budget;
 	}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-net-mptcp-misc-fixes-7-4-rc5-v1-0-278042217e8d%40kernel.org
author maintainer
#8 lore raw

PATCH 5/10 mptcp: leave an orphan's DATA_FIN to the worker

In reply to Matthieu Baerts (NGI0)

From: Chris Mason <[email protected]>

Closed MPTCP sockets can leak until reboot, with the data in their receive queues, which can exhaust tcp_mem and stall other TCP connections.

Once close() has returned, only mptcp_worker() destroys an orphaned msk, and only in the run that moves it to TCP_CLOSE: a later run returns at once, and mptcp_schedule_work() queues no work for an msk in that state. The receive path, in softirq, puts data on the backlog while the msk is owned, and release_sock() calls mptcp_release_cb(), which passes the backlog to __mptcp_move_skbs(). But __mptcp_move_skbs() calls mptcp_check_data_fin(), which can move the msk to TCP_CLOSE, and by then the lock owner, here the worker, no longer looks at the state:

receive path, softirq mptcp_worker()
===================== ==============
not in TCP_CLOSE: no destroy
last data, with the DATA_FIN
the msk is owned: backlog
release_sock()
mptcp_release_cb()
__mptcp_move_skbs()
mptcp_check_data_fin()
TCP_CLOSE, never destroyed

The fix used here is to queue the worker in __mptcp_move_skbs() if an orphaned msk has a DATA_FIN to handle, instead of calling mptcp_check_data_fin().

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
200,000 connections in which the server accept()s and close()s, while the client reads the end of file, sends one byte, and closes. Without this patch, some MPTCP sockets of the server stayed in the CLOSE state for good, with their network namespace:

=== connections so far : MPTCP sockets of the server in the state CLOSE: 200000:722, [...]
RESULT [...] | left 300 s behind the end: slab_MPTCP=722 sockets_MPTCP=722 [...] [...] | veth_end_left=1 slab_net_namespace_left=2 | [...]

Fixes bc68b0efa1bf ("mptcp: move the whole rx path under msk socket lock protection")

[ Shorter comment. Commit message: how the issue was found, and its
symptoms. ]

---
 net/mptcp/protocol.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +9 -3
index 23d35f43b566..17451240c105 100644
@@ -2312,9 +2312,15 @@ static bool __mptcp_move_skbs(struct sock *sk, struct list_head *skbs, u32 *delt
 	}
 
 	__mptcp_ofo_queue(msk);
-	if (moved)
-		mptcp_check_data_fin((struct sock *)msk);
-	return moved;
+	if (!moved)
+		return false;
+
+	/* Only the worker can close an orphan, as it must then destroy it */
+	if (!sock_flag(sk, SOCK_DEAD))
+		mptcp_check_data_fin(sk);
+	else if (mptcp_pending_data_fin(sk, NULL))
+		mptcp_schedule_work(sk);
+	return true;
 }
 
 static bool mptcp_can_spool_backlog(struct sock *sk, struct list_head *skbs)
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH 6/10 mptcp: don't leak an orphan when the PM closes its last subflow

In reply to Matthieu Baerts (NGI0)

Once close() has returned, an orphaned MPTCP socket is only destroyed by mptcp_worker(), in the run that sees it in TCP_CLOSE: later on, the worker returns at once for an msk in this state, and mptcp_schedule_work() no longer queues it.

__mptcp_close_ssk() moves an orphan to TCP_CLOSE when, after the closure of a subflow, only the initial one remains, and it is closed. This function is not only called by the worker: path manager commands also close subflows of any msk of the netns, orphans included, under the msk
lock: e.g. 'ip mptcp endpoint delete' or 'flush' with the in-kernel PM, or MPTCP_PM_CMD_SUBFLOW_DESTROY with the userspace one. In this case, nothing destroys the orphan: it stays in TCP_CLOSE until the reboot, with its token, its initial subflow, and its reference to the netns, which is then never freed.

This can happen when the initial subflow has been reset or has timed out, and the connection is closed -- the orphan then waits for the end of the DATA_FIN exchange, up to TCP_TIMEWAIT_LEN -- before the endpoint used by another subflow is removed, e.g. when an interface goes down.

Only the worker can close an orphan, as it must then destroy it. So here, mark its close timeout as expired, and queue the worker, like it is done for an msk in the accept queue. The worker then closes the orphan with mptcp_do_fastclose() -- nothing is sent: the only subflow left is closed -- and destroys it. If __mptcp_close_ssk() was called by the worker, this happens later in the same run.

This issue has been found by an AI code review agent, while reviewing the code of the commit "mptcp: leave an orphan's DATA_FIN to the worker", and confirmed with a new Packetdrill test: the initial subflow of an orphan waiting for the end of the DATA_FIN exchange is reset, then the endpoint of the other subflow is removed. Without this patch, the orphan stayed:

UNCONN 0 0 192.168.0.1:8080 192.0.2.1:39933
kernel_orphan_last_subflow.pkt:39: error executing ! ss -HMan | grep -v LISTEN command: non-zero status 1

Fixes 27e5ccc2d5a5 ("mptcp: fix dangling connection hang-up")
---
 net/mptcp/protocol.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +7 -2
index 17451240c105..5339fd6a8d81 100644
@@ -2744,8 +2744,13 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk,
 	 */
 	if (list_is_singular(&msk->conn_list) && msk->first &&
 	    inet_sk_state_load(msk->first) == TCP_CLOSE) {
-		if (sk->sk_state != TCP_ESTABLISHED ||
-		    msk->in_accept_queue || sock_flag(sk, SOCK_DEAD)) {
+		if (sock_flag(sk, SOCK_DEAD)) {
+			/* let the worker close and destroy the orphan */
+			mptcp_set_close_tout(sk, tcp_jiffies32 -
+					     (mptcp_close_timeout(sk) + 1));
+			mptcp_schedule_work(sk);
+		} else if (sk->sk_state != TCP_ESTABLISHED ||
+			   msk->in_accept_queue) {
 			mptcp_set_state(sk, TCP_CLOSE);
 			mptcp_close_wake_up(sk);
 		} else {
Signature · 1 line
2.55.0
author maintainer
#10 lore raw

PATCH 7/10 net: add __sk_net_refcnt_upgrade() with a GFP mask

In reply to Matthieu Baerts (NGI0)

sk_net_refcnt_upgrade() gives a kernel socket a reference on its network namespace, with a netns tracker allocated with GFP_KERNEL. The next commit needs to do that for a socket created in atomic context: the child of a kernel MPTCP listener, created when an MP_JOIN request is accepted.

Add __sk_net_refcnt_upgrade(), taking the GFP mask to use, and make sk_net_refcnt_upgrade() call it with GFP_KERNEL: no functional change for the existing callers.

The 'Fixes' tag here below is the same as the one from the next commit:
this patch is needed to fix the issue introduced by this commit ID.

Fixes 1729cf186d8a ("mptcp: create the listening socket for new port")

---
Cc: Neal Cardwell <[email protected]>
Cc: Kuniyuki Iwashima <[email protected]>

---
 include/net/sock.h | 1 +
 net/core/sock.c    | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
include/net/sock.h +1 -0
index 3144efad8c36..de23cdc89477 100644
@@ -1843,6 +1843,7 @@ static inline bool sock_allow_reclassification(const struct sock *csk)
 struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
 		      struct proto *prot, int kern);
 void sk_free(struct sock *sk);
+void __sk_net_refcnt_upgrade(struct sock *sk, gfp_t gfp);
 void sk_net_refcnt_upgrade(struct sock *sk);
 void sk_destruct(struct sock *sk);
 struct sock *sk_clone(const struct sock *sk, const gfp_t priority, bool lock);
net/core/sock.c +7 -2
index 5675053625e6..5ed63d2d8976 100644
@@ -2422,7 +2422,7 @@ static void __sk_destruct(struct rcu_head *head)
 	sk_prot_free(sk->sk_prot_creator, sk);
 }
 
-void sk_net_refcnt_upgrade(struct sock *sk)
+void __sk_net_refcnt_upgrade(struct sock *sk, gfp_t gfp)
 {
 	struct net *net = sock_net(sk);
 
@@ -2430,9 +2430,14 @@ void sk_net_refcnt_upgrade(struct sock *sk)
 	__netns_tracker_free(net, &sk->ns_tracker, false);
 	net_passive_dec(net);
 	sk->sk_net_refcnt = 1;
-	get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
+	get_net_track(net, &sk->ns_tracker, gfp);
 	sock_inuse_add(net, 1);
 }
+
+void sk_net_refcnt_upgrade(struct sock *sk)
+{
+	__sk_net_refcnt_upgrade(sk, GFP_KERNEL);
+}
 EXPORT_SYMBOL_GPL(sk_net_refcnt_upgrade);
 
 void sk_destruct(struct sock *sk)
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 7/10
#11 lore raw

Re: [PATCH net 07/10] net: add __sk_net_refcnt_upgrade() with a GFP mask

Quoted 10 lines · On Mon, 23 Nov 2026 14:24:55 +0100, Matthieu Baerts (NGI0) wrote:

sk_net_refcnt_upgrade() gives a kernel socket a reference on its network namespace, with a netns tracker allocated with GFP_KERNEL. The next commit needs to do that for a socket created in atomic context: the child of a kernel MPTCP listener, created when an MP_JOIN request is accepted.

Add __sk_net_refcnt_upgrade(), taking the GFP mask to use, and make sk_net_refcnt_upgrade() call it with GFP_KERNEL: no functional change for the existing callers.

Quoted 6 lines

The 'Fixes' tag here below is the same as the one from the next commit:
this patch is needed to fix the issue introduced by this commit ID.

Fixes 1729cf186d8a ("mptcp: create the listening socket for new port")
author maintainer
#12 lore raw

PATCH 8/10 mptcp: pm: don't let listeners pin their netns

In reply to Matthieu Baerts (NGI0)

From: Josef Bacik <[email protected]>

For a "signal" endpoint with a port, the in-kernel path manager creates a listening MPTCP socket with sock_create_kern(). This socket is released when the endpoint is deleted or, failing that, by pm_nl_exit_net() when the network namespace is dismantled.

The MPTCP socket itself holds no reference on the namespace. But its first subflow is created by mptcp_subflow_create_socket(), which always calls sk_net_refcnt_upgrade(). So the namespace cannot be dismantled before the listener is released, and the listener is not released before the namespace is dismantled: unless somebody deletes the endpoint, both stay around for ever once the last process of the namespace is gone.

unshare -Urn sh -c 'ip link set lo up;

                      ip mptcp endpoint add 127.0.0.1 port 4321 signal'

leaks a namespace, with everything it contains, each time it is run. It remains charged to the user's max_net_namespaces limit.

Only take the reference if the MPTCP socket has one. A listening TCP socket has no timer of its own, and request sockets of a dead namespace are taken care of by inet_twsk_purge().

The sockets cloned from that listener for incoming MP_JOIN requests do need the reference, for their timers, but inherit sk_net_refcnt == 0. Take it when they are created, with __sk_net_refcnt_upgrade(), from the previous commit, as this is done in atomic context, and drop the connection request if the namespace is already going away.

The TCP listener of such an endpoint is now a plain kernel socket, like the MPTCP socket it belongs to: it is not counted in "sockets: used" in /proc/net/sockstat any more, and its destruction is not broadcast to the listeners of the SKNLGRP_INET*_TCP_DESTROY sock_diag groups.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
the command above, run 1000 times by an unprivileged user. Without this patch, all the network namespaces were leaked:

result: 1000 network namespaces made, 0 taken down, 1000 are left

Fixes 1729cf186d8a ("mptcp: create the listening socket for new port")

[ Use __sk_net_refcnt_upgrade(), from the previous commit, instead of an
open-coded copy of sk_net_refcnt_upgrade(), with maybe_get_net() like rds_tcp_tune(). Shorter comments, and describe the condition: any kernel msk. Commit message: how the issue was found, and its symptoms. ]

---
 net/mptcp/subflow.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
net/mptcp/subflow.c +24 -1
index 20d506235074..3e674c21bdb2 100644
@@ -844,6 +844,22 @@ void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
 	WRITE_ONCE(msk->fully_established, true);
 }
 
+/* Children of a kernel listener need a netns ref for their timers */
+static bool subflow_child_get_net(struct sock *child)
+{
+	struct net *net = sock_net(child);
+
+	if (likely(child->sk_net_refcnt))
+		return true;
+
+	if (!maybe_get_net(net))
+		return false;
+
+	__sk_net_refcnt_upgrade(child, GFP_ATOMIC);
+	put_net(net);
+	return true;
+}
+
 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					  struct sk_buff *skb,
 					  struct request_sock *req,
@@ -912,6 +928,11 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 
 		tcp_rsk(req)->drop_req = false;
 
+		if (!subflow_child_get_net(child)) {
+			subflow_add_reset_reason(skb, MPTCP_RST_EUNSPEC);
+			goto dispose_child;
+		}
+
 		/* we need to fallback on ctx allocation failure and on pre-reqs
 		 * checking above. In the latter scenario we additionally need
 		 * to reset the context to non MPTCP status.
@@ -1860,8 +1881,10 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
 	/* kernel sockets do not by default acquire net ref, but TCP timer
 	 * needs it.
 	 * Update ns_tracker to current stack trace and refcounted tracker.
+	 * Not for a kernel msk, e.g. a PM listener: it would pin the netns.
 	 */
-	sk_net_refcnt_upgrade(sf->sk);
+	if (sk->sk_net_refcnt)
+		sk_net_refcnt_upgrade(sf->sk);
 	err = tcp_set_ulp(sf->sk, "mptcp");
 	if (err)
 		goto err_free;
Signature · 1 line
2.55.0
author maintainer
#13 lore raw

Re: [PATCH net 08/10] mptcp: pm: don't let listeners pin their netns

Hello,

Quoted 66 lines · On 23/11/2026 14:24, Matthieu Baerts (NGI0) wrote:

From: Josef Bacik <[email protected]>

For a "signal" endpoint with a port, the in-kernel path manager creates a listening MPTCP socket with sock_create_kern(). This socket is released when the endpoint is deleted or, failing that, by pm_nl_exit_net() when the network namespace is dismantled.

The MPTCP socket itself holds no reference on the namespace. But its first subflow is created by mptcp_subflow_create_socket(), which always calls sk_net_refcnt_upgrade(). So the namespace cannot be dismantled before the listener is released, and the listener is not released before the namespace is dismantled: unless somebody deletes the endpoint, both stay around for ever once the last process of the namespace is gone.

unshare -Urn sh -c 'ip link set lo up;

                      ip mptcp endpoint add 127.0.0.1 port 4321 signal'

leaks a namespace, with everything it contains, each time it is run. It remains charged to the user's max_net_namespaces limit.

Only take the reference if the MPTCP socket has one. A listening TCP socket has no timer of its own, and request sockets of a dead namespace are taken care of by inet_twsk_purge().

The sockets cloned from that listener for incoming MP_JOIN requests do need the reference, for their timers, but inherit sk_net_refcnt == 0. Take it when they are created, with __sk_net_refcnt_upgrade(), from the previous commit, as this is done in atomic context, and drop the connection request if the namespace is already going away.

The TCP listener of such an endpoint is now a plain kernel socket, like the MPTCP socket it belongs to: it is not counted in "sockets: used" in /proc/net/sockstat any more, and its destruction is not broadcast to the listeners of the SKNLGRP_INET*_TCP_DESTROY sock_diag groups.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
the command above, run 1000 times by an unprivileged user. Without this patch, all the network namespaces were leaked:

result: 1000 network namespaces made, 0 taken down, 1000 are left

Fixes 1729cf186d8a ("mptcp: create the listening socket for new port")

[ Use __sk_net_refcnt_upgrade(), from the previous commit, instead of an
open-coded copy of sk_net_refcnt_upgrade(), with maybe_get_net() like rds_tcp_tune(). Shorter comments, and describe the condition: any kernel msk. Commit message: how the issue was found, and its symptoms. ]

---
 net/mptcp/subflow.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
net/mptcp/subflow.c +3 -0
index 20d506235074..3e674c21bdb2 100644
@@ -844,6 +844,22 @@ void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
 	WRITE_ONCE(msk->fully_established, true);
 }
 
+/* Children of a kernel listener need a netns ref for their timers */
+static bool subflow_child_get_net(struct sock *child)
+{

net/mptcp/subflow.c void __mptcp_subflow_fully_established(struct mptcp_sock *msk, · line 859

Quoted 10 lines
net/mptcp/subflow.c +10 -0
+	struct net *net = sock_net(child);
+
+	if (likely(child->sk_net_refcnt))
+		return true;
+
+	if (!maybe_get_net(net))
+		return false;
+
+	__sk_net_refcnt_upgrade(child, GFP_ATOMIC);
+	put_net(net);

I just noticed put_net() is somehow deprecated (according to netdev/deprecated_api test on PW). In net_namespace.h, above the declaration of "put_net()", it says "Try using put_net_track() instead", but I don't think it makes sense here: I would need to allocate a new tracker here. Indeed, I cannot use child->ns_tracker (it belongs to the reference __sk_net_refcnt_upgrade()) or NULL (no_tracker was never incremented) from what I see.

If a new tracker is needed, it would be allocated before __sk_net_refcnt_upgrade() and released just after. The fix here follows what is done in rds_tcp_tune(), with the same sequence from 5c70eb5c593d ("net: better track kernel sockets lifetime").

So I think there is nothing to change here.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#14 lore raw

Re: [PATCH net 08/10] mptcp: pm: don't let listeners pin their netns

In reply to Matthieu Baerts (NGI0)
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mptcp: netns leak in subflow_syn_recv_sock() on non-MP_JOIN syncookie ACK --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mptcp: netns leak in subflow_syn_recv_sock() on non-MP_JOIN syncookie ACK
--

commit 6179aee66fd5371bdb738c5d1e0414c3ccde4437
Author: Josef Bacik <[email protected]>

mptcp: pm: don't let listeners pin their netns

This commit skips upgrading the network namespace reference count on subflows created for kernel MPTCP sockets, and instead takes a network namespace reference in subflow_syn_recv_sock() when cloning a child socket from a kernel listener.

Quoted 29 lines
net/mptcp/subflow.c +16 -0
index 20d5062350742..3e674c21bdb2a 100644
@@ -844,6 +844,22 @@ void __mptcp_subflow_fully_established(struct mptcp_sock *msk,
 	WRITE_ONCE(msk->fully_established, true);
 }
 
+/* Children of a kernel listener need a netns ref for their timers */
+static bool subflow_child_get_net(struct sock *child)
+{
+	struct net *net = sock_net(child);
+
+	if (likely(child->sk_net_refcnt))
+		return true;
+
+	if (!maybe_get_net(net))
+		return false;
+
+	__sk_net_refcnt_upgrade(child, GFP_ATOMIC);
+	put_net(net);
+	return true;
+}
+
 static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 					  struct sk_buff *skb,
 					  struct request_sock *req,
@@ -912,6 +928,11 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 

net/mptcp/subflow.c static struct sock *subflow_syn_recv_sock(const struct sock *sk, · line 938

Quoted 10 lines
net/mptcp/subflow.c +5 -0
 		tcp_rsk(req)->drop_req = false;
 
+		if (!subflow_child_get_net(child)) {
+			subflow_add_reset_reason(skb, MPTCP_RST_EUNSPEC);
+			goto dispose_child;
+		}
+
 		/* we need to fallback on ctx allocation failure and on pre-reqs
 		 * checking above. In the latter scenario we additionally need
 		 * to reset the context to non MPTCP status.

[Severity: High]
Can this still leak the network namespace when SYN cookies are active on an in-kernel PM listener?

subflow_check_req() rejects non-MP_JOIN SYNs when listener->pm_listener is true, and for an MP_JOIN SYN under SYN cookies tcp_conn_request() sends a SYN+ACK with a valid TCP syncookie and frees the request_sock. If the peer replies with a third ACK carrying that syncookie but omitting OPTION_MPTCP_MPJ_ACK (for example, a plain TCP ACK or one carrying OPTION_MPTCP_MPC_ACK), cookie_tcp_reqsk_init() calls mptcp_subflow_init_cookie_req(), which does not check listener->pm_listener and returns 0 with subflow_req->mp_join == 0.

When tcp_get_cookie_sock() then calls subflow_syn_recv_sock(), fallback_is_fatal is false because subflow_req->mp_join is 0. subflow_child_get_net(child) upgrades child->sk_net_refcnt to 1 before the fallback and ctx->mp_join checks, and subflow_syn_recv_sock() returns child with tcp_rsk(req)->drop_req == false (via goto fallback for a plain TCP ACK, or via ctx->mp_capable after cloning listener->conn). tcp_get_cookie_sock() then queues child into the PM listener's icsk_accept_queue, where nothing ever accepts it, pinning the network namespace indefinitely.

Checking listener->pm_listener in mptcp_subflow_init_cookie_req() (or rejecting non-MP_JOIN children of a PM listener before upgrading child->sk_net_refcnt) would prevent queueing unaccepted children with a netns reference.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-net-mptcp-misc-fixes-7-4-rc5-v1-0-278042217e8d@kernel.org?part=8
#15 lore raw

Re: [PATCH net 08/10] mptcp: pm: don't let listeners pin their netns

In reply to Matthieu Baerts (NGI0)
Thank 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 · High: 1 · Medium: 0 · 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).

- [High] The netns leak this patch claims to fix can still be triggered through the SYN-cookie path.

--

mptcp: pm: don't let listeners pin their netns

This patch stops the subflow of an in-kernel PM listener from taking a netns reference when its MPTCP socket is a kernel socket. As a result, a "signal" endpoint with a port no longer pins its namespace. Children cloned from such a listener in subflow_syn_recv_sock() now take the reference themselves, using __sk_net_refcnt_upgrade() in atomic context.

Quoted 4 lines
net/mptcp/subflow.c +0 -0
index 20d5062350742..3e674c21bdb2a 100644

[ ... ]

net/mptcp/subflow.c static struct sock *subflow_syn_recv_sock(const struct sock *sk, · line 935

Quoted 9 lines
net/mptcp/subflow.c +5 -0
@@ -912,6 +928,11 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,
 
 		tcp_rsk(req)->drop_req = false;
 
+		if (!subflow_child_get_net(child)) {
+			subflow_add_reset_reason(skb, MPTCP_RST_EUNSPEC);
+			goto dispose_child;
+		}
+

[Severity: High]
Can the namespace still get pinned through the syncookie path here?

mptcp_subflow_create_socket() no longer upgrades the PM listener ssk, so every child cloned from it inherits sk_net_refcnt == 0. Here subflow_child_get_net() runs for every owned child, before the !ctx || fallback check and the mp_capable / mp_join branching. That means plain-TCP fallback children and MP_CAPABLE children of a PM listener also get a real netns reference. The commit message only mentions MP_JOIN children needing one.

On a pm_listener, MPC and plain requests are rejected only at SYN time, in subflow_check_req():

	if (opt_mp_capable) {
		...
		if (unlikely(listener->pm_listener))
			return subflow_reset_req_endp(req, skb);
	} else if (opt_mp_join) {
		...
	} else if (unlikely(listener->pm_listener)) {
		return subflow_reset_req_endp(req, skb);
	}

mptcp_subflow_init_cookie_req() has no such check. It picks mp_capable or mp_join from the options in the third ACK only:

	opt_mp_capable = !!(mp_opt.suboptions & OPTION_MPTCP_MPC_ACK);
	opt_mp_join = !!(mp_opt.suboptions & OPTION_MPTCP_MPJ_ACK);
	if (opt_mp_capable && listener->request_mptcp) {

Syncookies are in effect when net.ipv4.tcp_syncookies=2 (a per-netns setting) or when the SYN queue is full. In that case, could this sequence happen?

1. An MP_JOIN SYN with a valid token passes subflow_check_req() and gets a cookie SYN-ACK.
2. The third ACK has no MPJ_ACK, or has MPC_ACK instead.
3. cookie_tcp_reqsk_init() leaves mp_join = 0, so fallback_is_fatal is false. In the plain case the child falls back with ctx == NULL. In the MPC case it clones the kernel listener msk.
4. subflow_child_get_net() sets sk_net_refcnt = 1 on the child, which is then returned with drop_req == false.

tcp_get_cookie_sock() then puts the child on the PM listener's accept queue:

	if (rsk_drop_req(req)) {
		reqsk_put(req);
		return child;
	}

	if (inet_csk_reqsk_queue_add(sk, req, child))
		return child;

Nothing ever accepts from that queue. Only inet_csk_listen_stop() drains it, either when the endpoint is deleted or from pm_nl_exit_net(). But pm_nl_exit_net() can't run while the queued child holds the netns reference. Doesn't that leak the namespace the same way as before this patch?

The setup is the same as the commit's reproducer: an unprivileged user in their own user and net namespace. That user can set tcp_syncookies, get a token from a loopback connection via MPTCP_INFO, and use CAP_NET_RAW to send the MP_JOIN SYN and a bare ACK. While cookies are active, a remote peer that knows a token could also trigger it, and so could a middlebox that strips MPJ_ACK.

The later patches in this series, "mptcp: initialize drop_req in subflow_init_req()" and "tcp: do not pass a BPF reqsk to an MPTCP listener", don't seem to change this.

Could the reference be taken only for MP_JOIN children that get attached to an msk? Another option would be to reject non-MP_JOIN cookie ACKs on a pm_listener in mptcp_subflow_init_cookie_req().

Quoted 3 lines
net/mptcp/subflow.c +0 -0
 		/* we need to fallback on ctx allocation failure and on pre-reqs
 		 * checking above. In the latter scenario we additionally need
 		 * to reset the context to non MPTCP status.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-net-mptcp-misc-fixes-7-4-rc5-v1-0-278042217e8d%40kernel.org
author maintainer
#16 lore raw

Re: [PATCH net 08/10] mptcp: pm: don't let listeners pin their netns

Hello,

Quoted 2 lines · On 24/11/2026 15:26, [email protected] wrote:

- [High] The netns leak this patch claims to fix can still be triggered through the SYN-cookie path.

(...)

Quoted 1 line

Doesn't that leak the namespace the same way as before this patch?

Yep! But this is not a regression: before this patch, these children also held a netns reference, and the listener itself always pinned the netns.

(...)

Quoted 3 lines

Could the reference be taken only for MP_JOIN children that get attached to an msk? Another option would be to reject non-MP_JOIN cookie ACKs on a pm_listener in mptcp_subflow_init_cookie_req().

The second option is already pending on the MPTCP ML, for a separate *pre-existing* issue. It will be sent with the next batch:

https://lore.kernel.org/[email protected]

So no need to change this patch, it can be applied as is.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#17 lore raw

PATCH 9/10 mptcp: initialize drop_req in subflow_init_req()

In reply to Matthieu Baerts (NGI0)

From: Chris Mason <[email protected]>

With CONFIG_MPTCP and SO_REUSEPORT migration, which net.ipv4.tcp_migrate_req=1 or a BPF program enables, TCP sockets can leak until reboot, and each keeps its network namespace alive.

tcp_check_req() keeps a child off the accept queue if drop_req is true in a request socket that an MPTCP listener created. drop_req has one writer, subflow_syn_recv_sock(), which tcp_check_req() calls before that test only for an MPTCP listener. But if SO_REUSEPORT migration moves the request socket to a plain TCP listener, the test reads what the allocation left in drop_req, and nothing drops the last reference of a child that stays off the accept queue.

The fix used here is to set drop_req to false in subflow_init_req(), which runs when the request socket of an MPTCP listener is set up.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch, run as an unprivileged user in a user namespace, with net.ipv4.tcp_migrate_req=1: an MPTCP listener and a TCP one sharing a port with SO_REUSEPORT, the MPTCP one being closed while handshakes complete. Without this patch, the children were leaked, with their network namespace, without any message from the kernel:

repro: step 3 to 6: 32 SYNs, 16 went to A. A closed, 16 third ACKs. accept() on B returned 0 [...]
repro: RESULT: 16 children never reached an accept queue
repro: one RST for each connection. TCP: inuse 0 orphan 0 tw 0 alloc 16 mem 0 === exit code 0. The process and its namespaces are gone
=== after: TCP sockets allocated, all namespaces: 16 [...]

With UBSAN, the original version of this patch also reported:

  UBSAN: invalid-load in [...]/include/net/mptcp.h:144:47
  load of value 14 is not a valid value for type '_Bool'
   tcp_check_req.cold+0xb6/0x189
   tcp_v4_rcv+0x11f4/0x4230
Fixes c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")

[ Shorter comment. Commit message: how the issue was found, and its
symptoms. ]

---
 net/mptcp/subflow.c | 3 +++
 1 file changed, 3 insertions(+)
net/mptcp/subflow.c +3 -0
index 3e674c21bdb2..054ee992904e 100644
@@ -151,6 +151,9 @@ static void subflow_init_req(struct request_sock *req, const struct sock *sk_lis
 	subflow_req->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk_listener));
 	subflow_req->msk = NULL;
 	mptcp_token_init_request(req);
+
+	/* read by tcp_check_req() even after migration to a TCP listener */
+	tcp_rsk(req)->drop_req = false;
 }
 
 static bool subflow_use_different_sport(struct mptcp_sock *msk, const struct sock *sk)
Signature · 1 line
2.55.0
author maintainer
#18 lore raw

PATCH 10/10 tcp: do not pass a BPF reqsk to an MPTCP listener

In reply to Matthieu Baerts (NGI0)

From: Chris Mason <[email protected]>

A slab-out-of-bounds write and a panic can occur if a tc BPF program that validates SYN cookies assigns an ACK to a TCP listener, and the ACK then goes to an MPTCP listener of the same SO_REUSEPORT group.

bpf_sk_assign_tcp_reqsk() attaches a plain TCP request sock to the skb, and returns -EINVAL if the program passes in an MPTCP listener. But inet_steal_sock() and inet6_steal_sock() select a listener again from the SO_REUSEPORT group of the listener that was passed in, and the result can be an MPTCP listener. cookie_bpf_check() then passes the request sock to mptcp_subflow_init_cookie_req(), which writes to members of the larger struct mptcp_subflow_request_sock, so the writes can land in the next object of the slab.

The fix used here is to drop the segment in cookie_bpf_check() if the listener is an MPTCP listener. The connection then fails, instead of corrupting the memory.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
a tc BPF program validating SYN cookies -- the one from the BPF selftests -- and an SO_REUSEPORT group with a TCP listener and an MPTCP one, the latter being selected. With KASAN, the original version of this patch reported:

  BUG: KASAN: slab-out-of-bounds in mptcp_subflow_init_cookie_req
  Write of size 1 at addr ... by task python3
   mptcp_subflow_init_cookie_req
   cookie_bpf_check
   cookie_v4_check
   tcp_v4_do_rcv
   tcp_v4_rcv

Without debug options, and with request sockets of half-open connections next to the corrupted one, the kernel panicked a second later, when the timer of a request socket fired:

  BUG: kernel NULL pointer dereference, address: 000000000000058f
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 3 UID: 0 PID: 0 Comm: swapper/3 Not tainted 7.4.0-rc1+ #1 PREEMPT(lazy)
  RIP: 0010:tcp_syn_ack_timeout+0xd/0x30
  Call Trace:
   <IRQ>
   reqsk_timer_handler+0x22e/0x500
   call_timer_fn+0x2a/0x140
   __run_timers+0x1f7/0x280
   run_timer_softirq+0x49/0xf0
   handle_softirqs+0xfa/0x300
   __irq_exit_rcu+0xb5/0xf0
   sysvec_apic_timer_interrupt+0x73/0x80
   </IRQ>
  Kernel panic - not syncing: Fatal exception in interrupt
Fixes e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")

[ Shorter comment. Commit message: how the issue was found, its
symptoms, and that the connection then fails. ]

---
Cc: Neal Cardwell <[email protected]>
Cc: Kuniyuki Iwashima <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: [email protected]

---
 net/ipv4/syncookies.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/syncookies.c +2 -1
index 89169c82431c..f39600ee3f17 100644
@@ -312,7 +312,8 @@ struct request_sock *cookie_bpf_check(struct sock *sk, struct sk_buff *skb)
 	skb->sk = NULL;
 	skb->destructor = NULL;
 
-	if (cookie_tcp_reqsk_init(sk, skb, req)) {
+	/* reuseport can pick an MPTCP listener: req is too small for it */
+	if (sk_is_mptcp(sk) || cookie_tcp_reqsk_init(sk, skb, req)) {
 		reqsk_free(req);
 		req = NULL;
 	}
Signature · 1 line
2.55.0
Reviewed-by on v1 10/10
#19 lore raw

Re: [PATCH net 10/10] tcp: do not pass a BPF reqsk to an MPTCP listener

Quoted 56 lines · On 11/23/26 9:24 PM, Matthieu Baerts (NGI0) wrote:

From: Chris Mason <[email protected]>

A slab-out-of-bounds write and a panic can occur if a tc BPF program that validates SYN cookies assigns an ACK to a TCP listener, and the ACK then goes to an MPTCP listener of the same SO_REUSEPORT group.

bpf_sk_assign_tcp_reqsk() attaches a plain TCP request sock to the skb, and returns -EINVAL if the program passes in an MPTCP listener. But inet_steal_sock() and inet6_steal_sock() select a listener again from the SO_REUSEPORT group of the listener that was passed in, and the result can be an MPTCP listener. cookie_bpf_check() then passes the request sock to mptcp_subflow_init_cookie_req(), which writes to members of the larger struct mptcp_subflow_request_sock, so the writes can land in the next object of the slab.

The fix used here is to drop the segment in cookie_bpf_check() if the listener is an MPTCP listener. The connection then fails, instead of corrupting the memory.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
a tc BPF program validating SYN cookies -- the one from the BPF selftests -- and an SO_REUSEPORT group with a TCP listener and an MPTCP one, the latter being selected. With KASAN, the original version of this patch reported:

  BUG: KASAN: slab-out-of-bounds in mptcp_subflow_init_cookie_req
  Write of size 1 at addr ... by task python3
   mptcp_subflow_init_cookie_req
   cookie_bpf_check
   cookie_v4_check
   tcp_v4_do_rcv
   tcp_v4_rcv

Without debug options, and with request sockets of half-open connections next to the corrupted one, the kernel panicked a second later, when the timer of a request socket fired:

  BUG: kernel NULL pointer dereference, address: 000000000000058f
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 3 UID: 0 PID: 0 Comm: swapper/3 Not tainted 7.4.0-rc1+ #1 PREEMPT(lazy)
  RIP: 0010:tcp_syn_ack_timeout+0xd/0x30
  Call Trace:
   <IRQ>
   reqsk_timer_handler+0x22e/0x500
   call_timer_fn+0x2a/0x140
   __run_timers+0x1f7/0x280
   run_timer_softirq+0x49/0xf0
   handle_softirqs+0xfa/0x300
   __irq_exit_rcu+0xb5/0xf0
   sysvec_apic_timer_interrupt+0x73/0x80
   </IRQ>
  Kernel panic - not syncing: Fatal exception in interrupt
Quoted 6 lines
Fixes e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")

[ Shorter comment. Commit message: how the issue was found, its
symptoms, and that the connection then fails. ]

Actually, we still have a few issues with bpf_sk_assign semantics...

Quoted 26 lines

---
Cc: Neal Cardwell <[email protected]>
Cc: Kuniyuki Iwashima <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: [email protected]

---
 net/ipv4/syncookies.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/syncookies.c +2 -1
index 89169c82431c..f39600ee3f17 100644
@@ -312,7 +312,8 @@ struct request_sock *cookie_bpf_check(struct sock *sk, struct sk_buff *skb)
 	skb->sk = NULL;
 	skb->destructor = NULL;
 
-	if (cookie_tcp_reqsk_init(sk, skb, req)) {
+	/* reuseport can pick an MPTCP listener: req is too small for it */
+	if (sk_is_mptcp(sk) || cookie_tcp_reqsk_init(sk, skb, req)) {
 		reqsk_free(req);
 		req = NULL;
 	}
#20 lore raw

Re: [PATCH net 10/10] tcp: do not pass a BPF reqsk to an MPTCP listener

Quoted 9 lines · Jiayuan Chen
Quoted 6 lines · On 11/23/26 9:24 PM, Matthieu Baerts (NGI0) wrote:

From: Chris Mason <[email protected]>

A slab-out-of-bounds write and a panic can occur if a tc BPF program that validates SYN cookies assigns an ACK to a TCP listener, and the ACK then goes to an MPTCP listener of the same SO_REUSEPORT group.

Quoted 5 lines
Quoted 5 lines

bpf_sk_assign_tcp_reqsk() attaches a plain TCP request sock to the skb, and returns -EINVAL if the program passes in an MPTCP listener. But inet_steal_sock() and inet6_steal_sock() select a listener again from the SO_REUSEPORT group of the listener that was passed in, and the result can be an MPTCP listener.

Hmm... do we want to support this setup ... ?

Maybe only useful when someone wants to migrate TCP to MPTCP gradually ?

Quoted 86 lines
Quoted 46 lines

cookie_bpf_check() then passes the
request sock to mptcp_subflow_init_cookie_req(), which writes to members of the larger struct mptcp_subflow_request_sock, so the writes can land in the next object of the slab.

The fix used here is to drop the segment in cookie_bpf_check() if the listener is an MPTCP listener. The connection then fails, instead of corrupting the memory.

This issue has been found with the help of an LLM, and reproduced in a VM, with the reproducer shared with the original version of this patch:
a tc BPF program validating SYN cookies -- the one from the BPF selftests -- and an SO_REUSEPORT group with a TCP listener and an MPTCP one, the latter being selected. With KASAN, the original version of this patch reported:

  BUG: KASAN: slab-out-of-bounds in mptcp_subflow_init_cookie_req
  Write of size 1 at addr ... by task python3
   mptcp_subflow_init_cookie_req
   cookie_bpf_check
   cookie_v4_check
   tcp_v4_do_rcv
   tcp_v4_rcv

Without debug options, and with request sockets of half-open connections next to the corrupted one, the kernel panicked a second later, when the timer of a request socket fired:

  BUG: kernel NULL pointer dereference, address: 000000000000058f
  #PF: supervisor read access in kernel mode
  #PF: error_code(0x0000) - not-present page
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 3 UID: 0 PID: 0 Comm: swapper/3 Not tainted 7.4.0-rc1+ #1 PREEMPT(lazy)
  RIP: 0010:tcp_syn_ack_timeout+0xd/0x30
  Call Trace:
   <IRQ>
   reqsk_timer_handler+0x22e/0x500
   call_timer_fn+0x2a/0x140
   __run_timers+0x1f7/0x280
   run_timer_softirq+0x49/0xf0
   handle_softirqs+0xfa/0x300
   __irq_exit_rcu+0xb5/0xf0
   sysvec_apic_timer_interrupt+0x73/0x80
   </IRQ>
  Kernel panic - not syncing: Fatal exception in interrupt
Quoted 6 lines
Fixes e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")

[ Shorter comment. Commit message: how the issue was found, its
symptoms, and that the connection then fails. ]

Actually, we still have a few issues with bpf_sk_assign semantics...

Quoted 26 lines

---
Cc: Neal Cardwell <[email protected]>
Cc: Kuniyuki Iwashima <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: [email protected]

---
 net/ipv4/syncookies.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/syncookies.c +2 -1
index 89169c82431c..f39600ee3f17 100644
@@ -312,7 +312,8 @@ struct request_sock *cookie_bpf_check(struct sock *sk, struct sk_buff *skb)
 	skb->sk = NULL;
 	skb->destructor = NULL;
 
-	if (cookie_tcp_reqsk_init(sk, skb, req)) {
+	/* reuseport can pick an MPTCP listener: req is too small for it */
+	if (sk_is_mptcp(sk) || cookie_tcp_reqsk_init(sk, skb, req)) {
 		reqsk_free(req);
 		req = NULL;
 	}
author maintainer
#21 lore raw
In reply to Matthieu Baerts (NGI0)

Hello,

Quoted 15 lines · On 23/11/2026 14:24, Matthieu Baerts (NGI0) wrote:

Here are some unrelated fixes:

- Patches 1-2: drain the queues of a TCP or MPTCP socket when listen() fails, not to leak sockets and their netns. Fixes for v4.4 and v5.11.

- Patch 3: clear inet_opt in the child of an IPv6 listener, to avoid a double free with SELinux and NetLabel. A fix for v5.6.

- Patch 4: fix UaF in corner cases from delegated actions. A fix for v5.12.

- Patches 5-6: do not leak an orphan moved to the CLOSE state outside the worker. Fixes for v6.15, and for another fix introduced in v6.6 and targeting v5.11.

Quoted 8 lines

- Patches 7-8: do not let PM listeners pin their netns, leaked otherwise. A fix for v5.12.

- Patch 9: initialize drop_req, not to leak sockets with SO_REUSEPORT migration. A fix for v5.14.

- Patch 10: do not pass a BPF request sock to an MPTCP listener, to avoid a slab-out-of-bounds write. A fix for v6.9.

TL;DR regarding Clashiko's reviews: no new versions needed I think.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#22 lore raw
In reply to Matthieu Baerts (NGI0)
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Mon, 23 Nov 2026 14:24:48 +0100 you wrote:

Here are some unrelated fixes:

- Patches 1-2: drain the queues of a TCP or MPTCP socket when listen() fails, not to leak sockets and their netns. Fixes for v4.4 and v5.11.

- Patch 3: clear inet_opt in the child of an IPv6 listener, to avoid a double free with SELinux and NetLabel. A fix for v5.6.

[...]

Here is the summary with links:
- [net,01/10] tcp: drain the queues of a listen() that fails

    https://git.kernel.org/netdev/net/c/90fecf474972
  - [net,02/10] mptcp: drain the queues of a listen() that fails
    https://git.kernel.org/netdev/net/c/767e6df88441
  - [net,03/10] mptcp: clear inet_opt in the child of an IPv6 listener
    https://git.kernel.org/netdev/net/c/0eee4a53c8b2
  - [net,04/10] mptcp: fix use-after-free of the subflow context on the delegated list
    https://git.kernel.org/netdev/net/c/308bf0d44e1d
  - [net,05/10] mptcp: leave an orphan's DATA_FIN to the worker
    https://git.kernel.org/netdev/net/c/7f1c51dce304
  - [net,06/10] mptcp: don't leak an orphan when the PM closes its last subflow
    https://git.kernel.org/netdev/net/c/78289c2e39ef
  - [net,07/10] net: add __sk_net_refcnt_upgrade() with a GFP mask
    https://git.kernel.org/netdev/net/c/fc9aa6e5c0fa
  - [net,08/10] mptcp: pm: don't let listeners pin their netns
    https://git.kernel.org/netdev/net/c/724ee40d2dda
  - [net,09/10] mptcp: initialize drop_req in subflow_init_req()
    https://git.kernel.org/netdev/net/c/ff3a60db8ee8
  - [net,10/10] tcp: do not pass a BPF reqsk to an MPTCP listener
    https://git.kernel.org/netdev/net/c/84050f6a6892

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html