tcp_fastopen_active_should_disable() calculates the active-disable interval as multiplier * tfo_bh_timeout * HZ. Since all operands are 32-bit, a large tcp_fastopen_blackhole_timeout_sec can wrap the delay, even on 64-bit kernels, and re-enable active Fast Open too soon. With HZ=1000 and the maximum multiplier of 64, any timeout above 67108 seconds wraps. For example, 67109 seconds should produce 4,294,976,000 jiffies (about 50 days), but the calculation truncates it to 8,704 jiffies (8.7 seconds). This issue was found using a static analysis tool. It was confirmed with a loopback reproducer that induces 12 Fast Open blackholes and opens another connection 10 seconds later: with a 67109 second timeout, the unpatched kernel retried active Fast Open. The reproducer needs neither kernel modifications nor error injection. Compute the interval in u64 and cap it at MAX_JIFFY_OFFSET before adding it to the saved timestamp. On 32-bit kernels, the cap keeps the interval within the range that time_before() can compare, which limits the longest backoff there to about 12.4 days at HZ=1000. Also limit the writable initial timeout to one day, so the exponential backoff does not start from an impractically long period. Writes above 86400 seconds, which were previously accepted, now fail with -EINVAL and leave the current value unchanged. The wider calculation is still needed below that limit, as the 67109 second example shows. Suggested-by: Kuniyuki Iwashima Signed-off-by: Yingjie Wang --- Changes since v3: - Drop Fixes, as requested by Matthieu. - Leave the existing comments unchanged, as requested by Matthieu. - Add Kuniyuki's Suggested-by for requesting the input limit. - Move the test notes below the separator, as requested by Eric. - Rebase on current net-next. Testing: the loopback reproducer kept active Fast Open disabled, and the sysctl accepted 86400 and rejected 86401. The TCP selftests were not run. Documentation/networking/ip-sysctl.rst | 1 + net/ipv4/sysctl_net_ipv4.c | 1 + net/ipv4/tcp_fastopen.c | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst index 3cc80d9b024b..c2b05ca14ff8 100644 --- a/Documentation/networking/ip-sysctl.rst +++ b/Documentation/networking/ip-sysctl.rst @@ -1055,6 +1055,7 @@ tcp_fastopen_blackhole_timeout_sec - INTEGER get detected right after Fastopen is re-enabled and will reset to initial value when the blackhole issue goes away. 0 to disable the blackhole detection. + The maximum value is one day (86400 seconds). By default, it is set to 0 (feature is disabled). diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c index 9df93ad219db..fb0f3124836d 100644 --- a/net/ipv4/sysctl_net_ipv4.c +++ b/net/ipv4/sysctl_net_ipv4.c @@ -1152,6 +1152,7 @@ static const struct ctl_table ipv4_net_table[] = { .mode = 0644, .proc_handler = proc_tfo_blackhole_detect_timeout, .extra1 = SYSCTL_ZERO, + .extra2 = &one_day_secs, }, #ifdef CONFIG_IP_ROUTE_MULTIPATH { diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c index 7f7eb3cc1c04..aeefd605414f 100644 --- a/net/ipv4/tcp_fastopen.c +++ b/net/ipv4/tcp_fastopen.c @@ -613,6 +613,7 @@ bool tcp_fastopen_active_should_disable(struct sock *sk) unsigned long timeout; int tfo_da_times; int multiplier; + u64 delay; if (!tfo_bh_timeout) return false; @@ -626,10 +627,11 @@ bool tcp_fastopen_active_should_disable(struct sock *sk) /* Limit timeout to max: 2^6 * initial timeout */ multiplier = 1 << min(tfo_da_times - 1, 6); + delay = (u64)multiplier * tfo_bh_timeout * HZ; /* Paired with the WRITE_ONCE() in tcp_fastopen_active_disable(). */ timeout = READ_ONCE(sock_net(sk)->ipv4.tfo_active_disable_stamp) + - multiplier * tfo_bh_timeout * HZ; + min(delay, MAX_JIFFY_OFFSET); if (time_before(jiffies, timeout)) return true; -- 2.43.0