🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19613

[PATCH net v4 1/2] tcp: fix shift-out-of-bounds in SYN-ACK retransmit timers

#19613
From: Jiayuan Chen
Date: Fri, 20 Nov 2026 04:04
175 lines
15985 bytes
req->num_timeout counts the SYN-ACK timeouts of a request and is used as
a shift amount:

	/* tcp_reqsk_timeout() */
	u64 timeout = (u64)req->timeout << req->num_timeout;
	/* tcp_fastopen_synack_timer() */
	req->timeout << req->num_timeout

Once it climbs past the width of the shifted type the shift is UB. Seen
with tcp_synack_retries=255 and a peer that never completes the
handshake:

  UBSAN: shift-out-of-bounds in ./include/net/tcp.h:917:34
  shift exponent 64 is too large for 64-bit type 'long long unsigned int'
  Call Trace:
   <TASK>
   dump_stack_lvl+0xc1/0xf0
   dump_stack+0x10/0x20
   __ubsan_handle_shift_out_of_bounds+0x1d1/0x390
   reqsk_timer_handler.cold+0x1c/0x5d
   call_timer_fn+0x17d/0x510
   __run_timers+0x662/0xab0
   run_timer_softirq+0xdf/0x260
   handle_softirqs+0x204/0x9b0
   __irq_exit_rcu+0x13c/0x220
   irq_exit_rcu+0xe/0x20
   sysvec_apic_timer_interrupt+0x40/0xd0
   asm_sysvec_apic_timer_interrupt+0x1b/0x20

The sysctl is not needed: TCP_SYNCNT=127 on the listener gets there
too, and the TFO shift is 32-bit, so it is UB from num_timeout 32.

The value can also wrap to 0 before the shift count is out of range,
which arms a zero-length timer. E.g. with HZ=1000 and req->timeout ==
1000, (u64)1000 << 61 and (u32)1000 << 29 are both 0.

num_timeout is a 7-bit field. Once a retransmit limit is above 127 the
"num_timeout >= limit" check can never be true, the request is never
expired by that limit, and after the field wraps to 0
reqsk_timer_handler() decrements queue->young again on the next expiry.

 - split tcp_reqsk_timeout() into tcp_reqsk_timeout_sk() and cap the
   shift at 32 there, same as inet_csk_rto_backoff() in the next patch.
   req->timeout << 32 is already above tcp_rto_max(), so the clamped
   result does not change.
 - cap max_retries / max_syn_ack_retries at MAX_TCP_SYNCNT (127), the
   largest value the field can hold. The sysctl range is left alone:
   narrowing it would make setups that write 128..255 fail with
   -EINVAL, and the caps are needed anyway for TCP_DEFER_ACCEPT and the
   TFO "+ 1". 128..255 now act as 127.
 - cap the TCP_DEFER_ACCEPT conversion in secs_to_retrans() at 127 too;
   larger values kept an acked request from ever expiring. getsockopt()
   now reports at most 14527 instead of 29887 seconds.
 - route the TFO path through the same helper.
 - document that tcp_synack_retries values above 127 act as 127.

Found by checking sysctl ranges against their users. Reproduced in a VM
with UBSAN: tcp_synack_retries=255 and tcp_rto_max_ms=1000 on the
server, and an iptables rule dropping the final ACK of the handshake.
The splat shows up after about 64 seconds, and is gone with this patch.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: e6c022a4fa2d ("tcp: better retrans tracking for defer-accept")
Cc: stable@vger.kernel.org
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
---
v3 -> v4: rebase, no code change.
v2 -> v3:
 - drop READ_ONCE(req->timeout), cap the shift at 32, reuse
   MAX_TCP_SYNCNT, simplify the doc text (Neal)
 - changelog: mention TCP_SYNCNT, the wrap to 0, why the sysctl range
   is not narrowed, how it was found and tested (Neal)
 - add Fixes: 1da177e4c3f4, the shift was already there
v1 -> v2: reworded the commit message, no code change.
v3: https://lore.kernel.org/netdev/20261118144249.358481-1-jiayuan.chen@linux.dev/
v2: https://lore.kernel.org/netdev/20261026123244.239766-1-jiayuan.chen@linux.dev/
v1: https://lore.kernel.org/netdev/20261023064915.401112-1-jiayuan.chen@linux.dev/
---
 Documentation/networking/ip-sysctl.rst |  2 +-
 include/net/tcp.h                      | 14 ++++++++++----
 net/ipv4/inet_connection_sock.c        |  1 +
 net/ipv4/tcp.c                         |  2 +-
 net/ipv4/tcp_timer.c                   |  3 ++-
 5 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst
index d3523c9064a2..0c48c29fbeeb 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -956,7 +956,7 @@ tcp_stdurg - BOOLEAN
 
 tcp_synack_retries - INTEGER
 	Number of times SYNACKs for a passive TCP connection attempt will
-	be retransmitted. Should not be higher than 255. Default value
+	be retransmitted. Values above 127 are treated as 127. Default value
 	is 5, which corresponds to 31seconds till the last retransmission
 	with the current initial RTO of 1second. With this the final timeout
 	for a passive TCP connection will happen after 63seconds.
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 27de546dc802..175052cc04ce 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -922,12 +922,18 @@ static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
 	return max(rto, 1U);
 }
 
-static inline unsigned long tcp_reqsk_timeout(struct request_sock *req)
+static inline unsigned long tcp_reqsk_timeout_sk(const struct sock *sk,
+						 const struct request_sock *req)
 {
-	u64 timeout = (u64)req->timeout << req->num_timeout;
+	/* timeout << 32 is already above tcp_rto_max(). */
+	u64 timeout = (u64)req->timeout << min_t(u8, req->num_timeout, 32);
+
+	return (unsigned long)min_t(u64, timeout, tcp_rto_max(sk));
+}
 
-	return (unsigned long)min_t(u64, timeout,
-				    tcp_rto_max(req->rsk_listener));
+static inline unsigned long tcp_reqsk_timeout(struct request_sock *req)
+{
+	return tcp_reqsk_timeout_sk(req->rsk_listener, req);
 }
 
 u32 tcp_delack_max(const struct sock *sk);
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 9a3b05edb62b..aa0678a1823d 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -1082,6 +1082,7 @@ static void reqsk_timer_handler(struct timer_list *t)
 	net = sock_net(sk_listener);
 	max_syn_ack_retries = READ_ONCE(icsk->icsk_syn_retries) ? :
 		READ_ONCE(net->ipv4.sysctl_tcp_synack_retries);
+	max_syn_ack_retries = min_t(int, max_syn_ack_retries, MAX_TCP_SYNCNT);
 	/* Normally all the openreqs are young and become mature
 	 * (i.e. converted to established socket) for first timeout.
 	 * If synack was not acknowledged for 1 second, it means
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 832b25ddd3c2..418842a2dfd9 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -358,7 +358,7 @@ static u8 secs_to_retrans(int seconds, int timeout, int rto_max)
 		int period = timeout;
 
 		res = 1;
-		while (seconds > period && res < 255) {
+		while (seconds > period && res < MAX_TCP_SYNCNT) {
 			res++;
 			timeout <<= 1;
 			if (timeout > rto_max)
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index 00debcba122b..814fadb30749 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -468,6 +468,7 @@ static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
 	 */
 	max_retries = READ_ONCE(icsk->icsk_syn_retries) ? :
 		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_synack_retries) + 1;
+	max_retries = min_t(int, max_retries, MAX_TCP_SYNCNT);
 
 	if (req->num_timeout >= max_retries) {
 		tcp_write_err(sk);
@@ -489,7 +490,7 @@ static void tcp_fastopen_synack_timer(struct sock *sk, struct request_sock *req)
 	if (!tp->retrans_stamp)
 		tp->retrans_stamp = tcp_time_stamp_ts(tp);
 	tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
-			  req->timeout << req->num_timeout, false);
+			     tcp_reqsk_timeout_sk(sk, req), false);
 }
 
 static bool tcp_rtx_probe0_timed_out(const struct sock *sk,
-- 
2.43.0
Technical Details
Message-ID: <20261120040447.195446-1-jiayuan.chen@linux.dev>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367629-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367629-linux-kernel-netdev=dewy.ru
From: jiayuan.chen@linux.dev (Jiayuan Chen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net v4 1/2] tcp: fix shift-out-of-bounds in SYN-ACK retransmit timers
Date: Fri, 20 Nov 2026 12:04:35 +0800
Organization: Usenet Network
Message-ID: <20261120040447.195446-1-jiayuan.chen@linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3056689"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:w9w/HyITgvNLEyxsqPH9EIeSpRc= sha256:b/+pSYCWidUD+WhIrj1c+NmV0s0b6vQuXElGne29k+g=
	sha1:RH8CB54No4kSYiFmovKAUCHHnw0= sha256:ncRJT/9WtamyvzrBzF/YYOoyfMbzWiximj1rQu9pUJk=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 049FB1158A3; Fri, 20 Nov 2026 07:05:34 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.051
X-Spam-Status: No, score=-2.051 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	HK_RANDOM_FROM=1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3,
	SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id mzX3Qdi_sMpY;
	Fri, 20 Nov 2026 07:05:22 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-367629-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 53F43115875; Fri, 20 Nov 2026 07:05:12 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 30A60E9B9C; Fri, 20 Nov 2026 04:05:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795147508;
	b=ZLIrpqsnO9PI7/yTQ9A9JrhguaL6MH26UiqsjLl6G0lE3Te9NcoPZqNiRdYodKyCOYet
	 xfu/DpmfGbnxlXw3RO8LbF3DlOFNh+SFmKWz43UOIdx70dGmZDnFe6/PpNracYDCcf+WS
	 4RAlzhzqE/dZfKe+rvldRmKkf7OSXjZUvc9G6LLDBSnjCtoJlffBHYNIpuRnQtUgw1Xyu
	 +TkAtJ90zCJQRV4Hk2lZv3GCMyEwmDJQk0X46MfonIgTwcWKixeHmowmMuzrLVgLFuZ/p
	 eWVBUOGsCYnIC/2ayRW8ETokKhzcHyKi1Gti9Ep8KsfossnC+TjM/tfcl8WGo6ZvPHg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795147508;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding;
	bh=agAySXnXvXLwBctyLqodLU4Ieq96j9f3PlYra6z6qAs=;
	b=wTQYhqnMYRGmyLLnninQVNuj8O6JVKmYft7zmB4KbNf2amvZ1kLgh39WOovjoN2gYC9P
	 4mYOM5cFlfQvaPP8OPvP/n3ujNPUrvqz8Y5ozKS64P96kcnBLWj3RoAsO3KA7j0ODMpU1
	 OahSBUODyJf/GpXMCI6hMXYHtAuk7ehWYKyv3nw4pe8eRYDIk1aclkTTmaU8fnQ+hOp37
	 ZY76aNooADSeGzMBJ/Hx5fHQTIhti4QHij5v/aopiOheXJmKJSvzonItSIIvIYGnxKI5o
	 qc/pGIqU7712TzQ59pWq+6o2R9cNFcI0kpglGSzBB2W8nupRqdcsBACeQD0GR3zmTeg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=vNGZl2JF;
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id A4EF4606A334;
	Fri, 20 Nov 2026 04:05:08 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=vNGZl2JF
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.95
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795147502;
	b=QssCiyrizW4ui/4676eYEM4XCfLwb1FJa8PmPy1YxxpMHc8Ok/LnZZSnAvK2aFe5NcUz
	 377hwpMZWY11GG6JA34KX6Cz50UxfxCMS7rm545Jg7H2aRTs5Rk+zVE4p4N1iaFCFpCGr
	 3qIywgpGczXUmTq5MtWSTtO3RM+glUVuPqZZJj1skMafTR/Kk8YBE9hdHPCoBE5yFx6dN
	 3YwJM5YOh/rfw/bkZ+4oh+tMffDxdOJGzUdLr/lkBcTdfkORqZu4tN0g6GUpOLzvYIVJ9
	 A4OP2yoiuWJzXrHBGCbT0XV6ENpnfYSp6hrZ7t+xPeGfWUGCwCoAmOouz/x89X7+U5A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795147502;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:MIME-Version:Content-Transfer-Encoding;
	bh=agAySXnXvXLwBctyLqodLU4Ieq96j9f3PlYra6z6qAs=;
	b=kbHrTIauSO52YTX6kPMM/2PO1RQ15EuqGaOxKfmbJ5CptbaRDJupTnj+n7p0K7nFl0Ej
	 SOJuWUcmCdW72YnhdSAgyx0NOgHzYQenWh3geJEqZG4iONZBmp7B8108dEuAQB5UlgKhD
	 X6/5AQtfm0XFBgWFyVazFsilvf44fx8wrsjQeqY6Gj2eFd1MOeGQHfFm0sL6OlObtWeDL
	 Qc8hgaDw5ZCYqyKVN/E7ZNz8jk7nIrRrWKcQCWn9jwZlT3Uf3OK865LATjRF+kvwiG2gq
	 SN5K3IhCk8O38s27i1Iah/fdgC+hE2+2gAsvLomo3pcs5MfqlFf3d6dgJl25uFVtytQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=linux.dev;
	spf=pass smtp.mailfrom=linux.dev;
	dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=vNGZl2JF;
	arc=none smtp.remote-ip=95.215.58.95
X-Received: from mta1.migadu.com (out-95.mta1.migadu.com [95.215.58.95])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id E687B606A326
	for <netdev@vger.kernel.org>; Fri, 20 Nov 2026 04:05:01 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E687B606A326
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=LRtH9AQ+fNUKM2YiFfL7S6RROg82Tv1oGoxfQzc1sOk=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1795147500; v=1; x=1795752300;
 b=vNGZl2JFYGGaaxHGFcJrXhYWIbC2doLeRb5w80NL/12xMC+8WDnlqdA/uYk8shGYrG3NcODc
 IMRDYUPWNRfaVfNzutZbnolDWjy2biCILHxoktR4pzGYj61Cs6YeS0InkWrDjPgI7VEAx9dpA2z
 kJlZptrG16EdfU2Zx7jxrHjA=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id 74e913414ee01686;
	Fri, 20 Nov 2026 04:04:59 +0000
X-Mizu-Trace-ID: 74e913414ee01686
X-Migadu-Flow: FLOW_OUT
X-Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	stable@vger.kernel.org,
	Eric Dumazet <edumazet@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	linux-doc@vger.kernel.org
X-Mailer: git-send-email 2.43.0
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list