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

From: Eric Dumazet

Date: Wed Nov 18 2026 - 10:05:28 EST


Le mer. 18 nov. 2026 à 15:43, Jiayuan Chen <jiayuan.chen@xxxxxxxxx> a écrit :
>
> 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:
>

> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
> ---
> 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.
> v2: https://lore.kernel.org/netdev/20261026123244.239766-1-jiayuan.chen@xxxxxxxxx/
> v1: https://lore.kernel.org/netdev/20261023064915.401112-1-jiayuan.chen@xxxxxxxxx/
> ---

Note this might conflict with Chris series, which changes __tcp_set_rto():

https://lore.kernel.org/netdev/20261118005048.53393-2-mason@xxxxxxxxxx/

No big deal, just FYI.

Reviewed-by: Eric Dumazet <edumazet@xxxxxxxxxx>