Netdev List
 help / color / mirror / Atom feed
* [PATCH net] tcp: fix stale per-CPU tcp_tw_isn leak enabling ISN prediction
@ 2026-05-11 15:15 Eric Dumazet
  2026-05-11 20:23 ` Jakub Kicinski
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Dumazet @ 2026-05-11 15:15 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni
  Cc: Simon Horman, Neal Cardwell, Kuniyuki Iwashima, netdev,
	eric.dumazet, Eric Dumazet

Blamed commit moved the TIME_WAIT-derived ISN from the skb control
block to a per-CPU variable, assuming the value would always be consumed
by tcp_conn_request() for the same packet that wrote it. That assumption
is violated by multiple drop paths between the producer
(__this_cpu_write(tcp_tw_isn, isn) in tcp_v{4,6}_rcv()) and the consumer
(tcp_conn_request()):

 - min_ttl / min_hopcount check
 - xfrm policy check
 - tcp_inbound_hash() MD5/AO mismatch
 - tcp_filter() eBPF/SO_ATTACH_FILTER drop
 - th->syn && th->fin discard in tcp_rcv_state_process() TCP_LISTEN
 - psp_sk_rx_policy_check() in tcp_v{4,6}_do_rcv()
 - tcp_checksum_complete() in tcp_v{4,6}_do_rcv()
 - tcp_v{4,6}_cookie_check() returning NULL

When a packet is dropped on any of these paths, tcp_tw_isn is left set.

The next SYN processed on the same CPU then consumes the non zero value in
tcp_conn_request(), receiving a predictable ISN.

We could fix this by clearing tcp_tw_isn at tcp_v{4,6}_do_rcv() start,
at the expense of slower fast path.

This patch clears tcp_tw_isn only when drops are happening.

It also adds two DEBUG_NET_WARN_ON_ONCE() to detect if any drop paths
are missed in the future, as they check the state of tcp_tw_isn at
the beginning of processing a packet that could potentially be a new SYN.

Fixes: 41eecbd712b7 ("tcp: replace TCP_SKB_CB(skb)->tcp_tw_isn with a per-cpu field")
Reported-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/ipv4/tcp_ipv4.c | 3 +++
 net/ipv6/tcp_ipv6.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index c0526cc0398049fb34b5de20a1175d54942e80cd..529f10a768183f74b85fcfdbee94d35c88c55122 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -1878,6 +1878,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
 reset:
 	tcp_v4_send_reset(sk, skb, sk_rst_convert_drop_reason(reason));
 discard:
+	__this_cpu_write(tcp_tw_isn, 0);
 	sk_skb_reason_drop(sk, skb, reason);
 	/* Be careful here. If this function gets more complicated and
 	 * gcc suffers from register pressure on the x86, sk (in %ebx)
@@ -2198,6 +2199,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
 		}
 	}
 
+	DEBUG_NET_WARN_ON_ONCE(__this_cpu_read(tcp_tw_isn));
 process:
 	if (static_branch_unlikely(&ip4_min_ttl)) {
 		/* min_ttl can be changed concurrently from do_ip_setsockopt() */
@@ -2274,6 +2276,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
 	}
 
 discard_it:
+	__this_cpu_write(tcp_tw_isn, 0);
 	SKB_DR_OR(drop_reason, NOT_SPECIFIED);
 	/* Discard frame. */
 	sk_skb_reason_drop(sk, skb, drop_reason);
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index d13d49bfef19457cc5902cb556605a80f4c0ab2c..079f2324a3a777cfe30dd59777c9d3d6d9a6bae0 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1641,6 +1641,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
 reset:
 	tcp_v6_send_reset(sk, skb, sk_rst_convert_drop_reason(reason));
 discard:
+	__this_cpu_write(tcp_tw_isn, 0);
 	if (opt_skb)
 		__kfree_skb(opt_skb);
 	sk_skb_reason_drop(sk, skb, reason);
@@ -1839,6 +1840,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
 		}
 	}
 
+	DEBUG_NET_WARN_ON_ONCE(__this_cpu_read(tcp_tw_isn));
 process:
 	if (static_branch_unlikely(&ip6_min_hopcount)) {
 		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
@@ -1913,6 +1915,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
 	}
 
 discard_it:
+	__this_cpu_write(tcp_tw_isn, 0);
 	SKB_DR_OR(drop_reason, NOT_SPECIFIED);
 	sk_skb_reason_drop(sk, skb, drop_reason);
 	return 0;
-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] tcp: fix stale per-CPU tcp_tw_isn leak enabling ISN prediction
  2026-05-11 15:15 [PATCH net] tcp: fix stale per-CPU tcp_tw_isn leak enabling ISN prediction Eric Dumazet
@ 2026-05-11 20:23 ` Jakub Kicinski
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Kicinski @ 2026-05-11 20:23 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: David S . Miller, Paolo Abeni, Simon Horman, Neal Cardwell,
	Kuniyuki Iwashima, netdev, eric.dumazet

On Mon, 11 May 2026 15:15:59 +0000 Eric Dumazet wrote:
> diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
> index d13d49bfef19457cc5902cb556605a80f4c0ab2c..079f2324a3a777cfe30dd59777c9d3d6d9a6bae0 100644
> --- a/net/ipv6/tcp_ipv6.c
> +++ b/net/ipv6/tcp_ipv6.c
> @@ -1641,6 +1641,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
>  reset:
>  	tcp_v6_send_reset(sk, skb, sk_rst_convert_drop_reason(reason));
>  discard:
> +	__this_cpu_write(tcp_tw_isn, 0);
>  	if (opt_skb)
>  		__kfree_skb(opt_skb);
>  	sk_skb_reason_drop(sk, skb, reason);
> @@ -1839,6 +1840,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
>  		}
>  	}
>  
> +	DEBUG_NET_WARN_ON_ONCE(__this_cpu_read(tcp_tw_isn));
>  process:
>  	if (static_branch_unlikely(&ip6_min_hopcount)) {
>  		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
> @@ -1913,6 +1915,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
>  	}
>  
>  discard_it:
> +	__this_cpu_write(tcp_tw_isn, 0);
>  	SKB_DR_OR(drop_reason, NOT_SPECIFIED);
>  	sk_skb_reason_drop(sk, skb, drop_reason);
>  	return 0;

CI says:

[  288.637769][ T3492] BUG: using __this_cpu_write() in preemptible [00000000] code: python3/3492
[  288.638116][ T3492] caller is tcp_v6_do_rcv+0x204/0x1cb0
[  288.638240][ T3492] CPU: 1 UID: 0 PID: 3492 Comm: python3 Not tainted 7.1.0-rc2-virtme #1 PREEMPT(full) 
[  288.638245][ T3492] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[  288.638247][ T3492] Call Trace:
[  288.638248][ T3492]  <TASK>
[  288.638250][ T3492]  dump_stack_lvl+0x6f/0xa0
[  288.638257][ T3492]  check_preemption_disabled+0xdb/0xf0
[  288.638261][ T3492]  tcp_v6_do_rcv+0x204/0x1cb0
[  288.638265][ T3492]  __release_sock+0x135/0x3a0
[  288.638272][ T3492]  release_sock+0x19c/0x240
[  288.638275][ T3492]  tcp_sendmsg+0x39/0x50
[  288.638279][ T3492]  __sys_sendto+0x2c9/0x400
[  288.638282][ T3492]  ? __ia32_sys_getpeername+0xd0/0xd0
[  288.638288][ T3492]  ? sock_ioctl+0x3cb/0x5f0
[  288.638294][ T3492]  ? find_held_lock+0x2b/0x80
[  288.638299][ T3492]  ? __lock_release.isra.0+0x6b/0x1a0
[  288.638302][ T3492]  __x64_sys_sendto+0xe4/0x1f0
[  288.638304][ T3492]  ? trace_irq_enable.constprop.0+0x9b/0x180
[  288.638308][ T3492]  ? lockdep_hardirqs_on+0x8c/0x130
[  288.638310][ T3492]  ? do_syscall_64+0x82/0xfc0
[  288.638312][ T3492]  do_syscall_64+0x117/0xfc0
[  288.638314][ T3492]  ? irq_exit_rcu+0x1a/0x30
[  288.638318][ T3492]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[  288.638321][ T3492] RIP: 0033:0x7f895f8fe08e

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-11 20:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-11 15:15 [PATCH net] tcp: fix stale per-CPU tcp_tw_isn leak enabling ISN prediction Eric Dumazet
2026-05-11 20:23 ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox