* [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash
@ 2026-10-28 16:25 Eric Dumazet
2026-10-28 19:45 ` Kuniyuki Iwashima
2026-10-29 6:29 ` Jiayuan Chen
0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-10-28 16:25 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima,
Neal Cardwell
Cc: Simon Horman, netdev, Kyle Zeng, Eric Dumazet
From: Kyle Zeng <kylebot@openai.com>
inet_twsk_kill() removes a TIME_WAIT socket from the established hash
before it finishes using the ehash lock, hashinfo and bind tables, and
before it drops the death-row reference. A running timer can therefore
unhash its socket before inet_twsk_purge() reaches it. The purge cannot
synchronize with that timer, and tcp_sk_exit_batch() can free the
private ehash and hashinfo while inet_twsk_kill() is still using them.
An unprivileged reproducer using user and network namespaces, a private
TCP ehash and a FIN_WAIT2 timer triggers a KASAN slab-use-after-free in
inet_twsk_kill(). The allocation and free stacks point to
inet_pernet_hashinfo_alloc() and inet_pernet_hashinfo_free().
Wait for an RCU grace period after purging the whole batch.
TIME_WAIT removal runs with bottom halves disabled, so this also waits
for removers that were already invisible to the purge. Keep the wait
under tcp_exit_batch_mutex, before freeing any private hashinfo.
Fixes: d1e5e6408b30 ("tcp: Introduce optional per-netns ehash.")
Signed-off-by: Kyle Zeng <kylebot@openai.com>
[edumazet: only call synchronize_net() if one netns in the batch uses a private ehash]
Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
net/ipv4/tcp_ipv4.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index ebe5aa70374a3fd9e3305377efebec3004989945..3f8563a36db1e9ea853699ecb547af7ee9491b11 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -3561,6 +3561,17 @@ static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
tcp_twsk_purge(net_exit_list);
+ /* inet_twsk_kill() can unhash a socket before the purge sees it.
+ * Wait for such BH-disabled callers to finish before freeing a
+ * private ehash. This is not needed for the shared tcp_hashinfo.
+ */
+ list_for_each_entry(net, net_exit_list, exit_list) {
+ if (net->ipv4.tcp_death_row.hashinfo->pernet) {
+ synchronize_net();
+ break;
+ }
+ }
+
list_for_each_entry(net, net_exit_list, exit_list) {
inet_pernet_hashinfo_free(net->ipv4.tcp_death_row.hashinfo);
WARN_ON_ONCE(!refcount_dec_and_test(&net->ipv4.tcp_death_row.tw_refcount));
--
2.53.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash
2026-10-28 16:25 [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash Eric Dumazet
@ 2026-10-28 19:45 ` Kuniyuki Iwashima
2026-10-29 6:29 ` Jiayuan Chen
1 sibling, 0 replies; 3+ messages in thread
From: Kuniyuki Iwashima @ 2026-10-28 19:45 UTC (permalink / raw)
To: Eric Dumazet
Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Neal Cardwell,
Simon Horman, netdev, Kyle Zeng
On Wed, 28 Oct 2026 17:25:10 +0100, Eric Dumazet wrote:
> From: Kyle Zeng <kylebot@openai.com>
>
> inet_twsk_kill() removes a TIME_WAIT socket from the established hash
> before it finishes using the ehash lock, hashinfo and bind tables, and
> before it drops the death-row reference. A running timer can therefore
> unhash its socket before inet_twsk_purge() reaches it. The purge cannot
> synchronize with that timer, and tcp_sk_exit_batch() can free the
> private ehash and hashinfo while inet_twsk_kill() is still using them.
>
> An unprivileged reproducer using user and network namespaces, a private
> TCP ehash and a FIN_WAIT2 timer triggers a KASAN slab-use-after-free in
> inet_twsk_kill(). The allocation and free stacks point to
> inet_pernet_hashinfo_alloc() and inet_pernet_hashinfo_free().
>
> Wait for an RCU grace period after purging the whole batch.
> TIME_WAIT removal runs with bottom halves disabled, so this also waits
> for removers that were already invisible to the purge. Keep the wait
> under tcp_exit_batch_mutex, before freeing any private hashinfo.
>
> Fixes: d1e5e6408b30 ("tcp: Introduce optional per-netns ehash.")
> Signed-off-by: Kyle Zeng <kylebot@openai.com>
> [edumazet: only call synchronize_net() if one netns in the batch uses a private ehash]
> Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Thanks !
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash
2026-10-28 16:25 [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash Eric Dumazet
2026-10-28 19:45 ` Kuniyuki Iwashima
@ 2026-10-29 6:29 ` Jiayuan Chen
1 sibling, 0 replies; 3+ messages in thread
From: Jiayuan Chen @ 2026-10-29 6:29 UTC (permalink / raw)
To: Eric Dumazet, David S . Miller, Jakub Kicinski, Paolo Abeni,
Kuniyuki Iwashima, Neal Cardwell
Cc: Simon Horman, netdev, Kyle Zeng
On 10/29/26 12:25 AM, Eric Dumazet wrote:
> From: Kyle Zeng <kylebot@openai.com>
>
> inet_twsk_kill() removes a TIME_WAIT socket from the established hash
> before it finishes using the ehash lock, hashinfo and bind tables, and
> before it drops the death-row reference. A running timer can therefore
> unhash its socket before inet_twsk_purge() reaches it. The purge cannot
> synchronize with that timer, and tcp_sk_exit_batch() can free the
> private ehash and hashinfo while inet_twsk_kill() is still using them.
>
> An unprivileged reproducer using user and network namespaces, a private
> TCP ehash and a FIN_WAIT2 timer triggers a KASAN slab-use-after-free in
> inet_twsk_kill(). The allocation and free stacks point to
> inet_pernet_hashinfo_alloc() and inet_pernet_hashinfo_free().
>
> Wait for an RCU grace period after purging the whole batch.
> TIME_WAIT removal runs with bottom halves disabled, so this also waits
> for removers that were already invisible to the purge. Keep the wait
> under tcp_exit_batch_mutex, before freeing any private hashinfo.
>
> Fixes: d1e5e6408b30 ("tcp: Introduce optional per-netns ehash.")
> Signed-off-by: Kyle Zeng <kylebot@openai.com>
> [edumazet: only call synchronize_net() if one netns in the batch uses a private ehash]
> Signed-off-by: Eric Dumazet <edumazet@kernel.org>
> ---
> net/ipv4/tcp_ipv4.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
> index ebe5aa70374a3fd9e3305377efebec3004989945..3f8563a36db1e9ea853699ecb547af7ee9491b11 100644
> --- a/net/ipv4/tcp_ipv4.c
> +++ b/net/ipv4/tcp_ipv4.c
> @@ -3561,6 +3561,17 @@ static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
>
> tcp_twsk_purge(net_exit_list);
>
> + /* inet_twsk_kill() can unhash a socket before the purge sees it.
> + * Wait for such BH-disabled callers to finish before freeing a
> + * private ehash. This is not needed for the shared tcp_hashinfo.
> + */
> + list_for_each_entry(net, net_exit_list, exit_list) {
> + if (net->ipv4.tcp_death_row.hashinfo->pernet) {
> + synchronize_net();
> + break;
> + }
> + }
Calling synchronize_net() only for the private ehash is nice and cheap
> +
> list_for_each_entry(net, net_exit_list, exit_list) {
> inet_pernet_hashinfo_free(net->ipv4.tcp_death_row.hashinfo);
> WARN_ON_ONCE(!refcount_dec_and_test(&net->ipv4.tcp_death_row.tw_refcount));
But I think this WARN_ON_ONCE can still be triggered, is that intentional?
tcp_sk_exit_batch inet_twsk_kill (timer)
unlink
tcp_twsk_purge() // does not see tw
refcount_dec_and_test() // tw_refcount 2 -> 1, WARN
refcount_dec(&tw->tw_dr->tw_refcount)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-10-29 6:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-28 16:25 [PATCH net] tcp: wait for TIME_WAIT teardown before freeing per-netns ehash Eric Dumazet
2026-10-28 19:45 ` Kuniyuki Iwashima
2026-10-29 6:29 ` Jiayuan Chen
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.