tcp: shift-out-of-bounds in inet_csk_rto_backoff() for an orphaned zero-window probe
Subsystems (2)
Hi Eric, Neal,
While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that an orphaned socket that keeps sending zero-window probes makes inet_csk_rto_backoff() shift by 64 or more. An unprivileged user can set this up in its own user and network namespace:
UBSAN: shift-out-of-bounds in include/net/inet_connection_sock.h:258:40 shift exponent 64 is too large for 64-bit type 'long long unsigned int' CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc3 #1 PREEMPT(lazy) Call Trace: <IRQ> tcp_write_timer_handler.cold+0x54/0xc4 tcp_write_timer+0xf3/0x210 call_timer_fn+0x15c/0x4b0 __run_timers+0x5f4/0x970 run_timer_softirq+0x141/0x240 handle_softirqs+0x22d/0x980 __irq_exit_rcu+0x153/0x1e0 irq_exit_rcu+0x9/0x20 sysvec_apic_timer_interrupt+0x6c/0x80 </IRQ>
The probe path raises icsk_backoff up to tcp_retries2, which can be up to 255:
if (icsk->icsk_backoff < READ_ONCE(net->ipv4.sysctl_tcp_retries2)) icsk->icsk_backoff++;
6d4634d1b091 ("net: Limit logical shift left of TCP probe0 timeout") clamps the shift in tcp_probe0_when(), but tcp_probe_timer() also calls inet_csk_rto_backoff() for an orphan, and that one is not clamped:
if (sock_flag(sk, SOCK_DEAD)) {
unsigned int rto_max = tcp_rto_max(sk);
const bool alive = inet_csk_rto_backoff(icsk, rto_max) < rto_max;
u64 when = (u64)icsk->icsk_rto << icsk->icsk_backoff;On x86 the shift count is masked, so from 64 on alive can come out true again and the orphan keeps probing instead of being aborted.
The reproducer runs as an unprivileged user (uid 65534). It creates a user and network namespace, brings up lo, and sets tcp_retries2 and tcp_orphan_retries to 255 there. A loopback server with a small receive buffer never reads. The client sets TCP_RTO_MAX_MS to 1000 so probes come about once a second, fills its send queue against the zero window, and closes the socket. On an x86_64 QEMU guest with 2 vCPUs and KASAN and UBSAN (gcc 13.3, config attached), the report fires in every run (4 of 4), after about two minutes of probing.
Checked against v7.4-rc3 (aebb72744764); the code is the same in net-next (6ff4c5942a30). We found no earlier report of this call site.
The reproducer (repro.c) and the kernel config are attached.
Thanks,
Kyuwon Cho
// Reproducer for the tcp_probe_timer() orphan branch: inet_csk_rto_backoff() // shifts a u64 by icsk_backoff (a u8) without the clamp that tcp_probe0_when() // applies. Reaching icsk_backoff >= 64 makes the shift undefined (UBSAN // shift-out-of-bounds).
//
// Steps, all as an unprivileged user inside a fresh user+net namespace:
// 1. unshare(CLONE_NEWUSER|CLONE_NEWNET), bring lo up.
// 2. Raise this netns' tcp_retries2 and tcp_orphan_retries to 255, so the
// zero-window probe backoff may grow past 64 and the orphan path keeps
// probing.
// 3. Loopback server with a tiny receive buffer that never reads, so the
// advertised window goes to zero.
// 4. Client sets TCP_RTO_MAX_MS=1000 (per-socket icsk_rto_max, minimum 1 s)
// so each zero-window probe fires about once a second, fills its send // queue behind the zero window, then close()s: the socket becomes an // orphan (SOCK_DEAD) with unsent data, and the probe timer keeps firing.
// 5. Wait ~120 s for ~100 probe timer expirations.
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#ifndef TCP_RTO_MAX_MS
#define TCP_RTO_MAX_MS 44
#endif
#define PORT 4242
#define WAIT_SECS 120
static void die(const char *what)
{
fprintf(stderr, "repro: %s: %s\n", what, strerror(errno));
exit(1);
}static void write_file(const char *path, const char *val)
{
int fd = open(path, O_WRONLY);
if (fd < 0)
die(path);
if (write(fd, val, strlen(val)) < 0)
die(path);
close(fd);
}int main(void)
{
uid_t uid = getuid();
gid_t gid = getgid();
char buf[64];
struct sockaddr_in addr;
struct ifreq ifr;
int ls, cs, s, rcvbuf = 2048, sndbuf = 4096, rto_max = 1000;
int on = 1;
char chunk[1024];
long total = 0;
time_t t0;
memset(chunk, 'x', sizeof(chunk));
if (unshare(CLONE_NEWUSER | CLONE_NEWNET))
die("unshare(CLONE_NEWUSER|CLONE_NEWNET)");
write_file("/proc/self/setgroups", "deny");
snprintf(buf, sizeof(buf), "0 %u 1\n", (unsigned)gid);
write_file("/proc/self/gid_map", buf);
snprintf(buf, sizeof(buf), "0 %u 1\n", (unsigned)uid);
write_file("/proc/self/uid_map", buf);
/* Bring loopback up (no ip binary in the VM). */
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
die("socket");
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "lo");
if (ioctl(s, SIOCGIFFLAGS, &ifr))
die("SIOCGIFFLAGS");
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
if (ioctl(s, SIOCSIFFLAGS, &ifr))
die("SIOCSIFFLAGS lo");
close(s);
/* Per-netns sysctls: allow backoff past 64 and keep the orphan alive. */
write_file("/proc/sys/net/ipv4/tcp_retries2", "255");
write_file("/proc/sys/net/ipv4/tcp_orphan_retries", "255");
puts("repro: netns sysctls set (tcp_retries2=255, tcp_orphan_retries=255)");
fflush(stdout);
/* Listener: small receive buffer, never read from. */
ls = socket(AF_INET, SOCK_STREAM, 0);
if (ls < 0)
die("socket listen");
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt(ls, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(ls, (struct sockaddr *)&addr, sizeof(addr)))
die("bind");
if (listen(ls, 4))
die("listen");
/* Client: per-socket RTO max of 1 s, small send buffer. */
cs = socket(AF_INET, SOCK_STREAM, 0);
if (cs < 0)
die("socket client");
setsockopt(cs, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
if (setsockopt(cs, IPPROTO_TCP, TCP_RTO_MAX_MS, &rto_max, sizeof(rto_max)))
die("TCP_RTO_MAX_MS");
if (connect(cs, (struct sockaddr *)&addr, sizeof(addr)))
die("connect");
{
int as = accept(ls, NULL, NULL);
if (as < 0)
die("accept");
/* Server never reads: its receive queue fills, window -> 0. */
}
/* Fill the send queue behind the zero window. */
{
int fl = fcntl(cs, F_GETFL);
fcntl(cs, F_SETFL, fl | O_NONBLOCK);
}
for (;;) {
ssize_t n = write(cs, chunk, sizeof(chunk));
if (n < 0) {
if (errno == EAGAIN)
break;
die("write");
}
total += n;
if (total > (1 << 20))
break;
}
printf("repro: queued %ld bytes behind zero window\n", total);
fflush(stdout);
usleep(500 * 1000); /* let ACKs and the zero window settle */
/* Orphan the socket with unsent data: the probe timer now runs the
* SOCK_DEAD branch of tcp_probe_timer() on every expiration. */
close(cs);
puts("repro: client closed (orphan with unsent data); waiting for probe timers");
fflush(stdout);
t0 = time(NULL);
while (time(NULL) - t0 < WAIT_SECS) {
sleep(10);
printf("repro: %ld s elapsed\n", (long)(time(NULL) - t0));
fflush(stdout);
}
puts("repro: done");
return 0;
}Eric Dumazet
maintainerQuoted 8 lines · Kyuwon Cho
Hi Eric, Neal,
While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that an orphaned socket that keeps sending zero-window probes makes
inet_csk_rto_backoff()shift by 64 or more. An unprivileged user can set this up in its own user and network namespace:
Thanks for the report and the reproducer.
This is a known issue, a fix is under review:
[PATCH net v2 2/2] tcp: fix shift-out-of-bounds in inet_csk_rto_backoff()
https://lore.kernel.org/netdev/[email protected]/
It limits the shift to 32 in inet_csk_rto_backoff() itself, so it covers both callers: tcp_ld_RTO_revert(), which is where Jiayuan hit the splat, and the orphan check in tcp_probe_timer() from your report.
Quoted 1 line
We found no earlier report of this call site.
Searching lore for the function name would have found it:
https://lore.kernel.org/netdev/?q=inet_csk_rto_backoff
Please do this before sending a report, especially for findings coming from automated tools.
Note that the netns sysctls are not needed. tcp_retransmit_timer() increments icsk_backoff without any limit, so TCP_USER_TIMEOUT and TCP_RTO_MAX_MS on a socket with a silent peer are enough to get icsk_backoff above 63, then an ICMP unreachable reaches the shift from tcp_ld_RTO_revert().
Quoted 2 lines
On x86 the shift count is masked, so from 64 on alive can come out true again and the orphan keeps probing instead of being aborted.
Right, and this starts before 64: with icsk_rto == 200, the u64 is already zero when icsk_backoff reaches 61.
Jiayuan, I have not seen a v3 addressing Neal's feedback.
When you send it, could you please add:
and mention the zero window probe path in the changelog ?
Kyuwon, it would be nice if you could test Jiayuan's patch with your reproducer and send a Tested-by: tag in that thread.
Thanks.
Quoted 38 lines · Eric Dumazet
Quoted 7 lines · Kyuwon Cho
Hi Eric, Neal,
While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that an orphaned socket that keeps sending zero-window probes makes
inet_csk_rto_backoff()shift by 64 or more. An unprivileged user can set this up in its own user and network namespace:Thanks for the report and the reproducer.
This is a known issue, a fix is under review:
[PATCH net v2 2/2] tcp: fix shift-out-of-bounds in
inet_csk_rto_backoff()
https://lore.kernel.org/netdev/[email protected]/It limits the shift to 32 in
inet_csk_rto_backoff()itself, so it covers both callers:tcp_ld_RTO_revert(), which is where Jiayuan hit the splat, and the orphan check intcp_probe_timer()from your report.Quoted 1 line
We found no earlier report of this call site.
Searching lore for the function name would have found it:
https://lore.kernel.org/netdev/?q=inet_csk_rto_backoff
Please do this before sending a report, especially for findings coming from automated tools.
Note that the netns sysctls are not needed.
tcp_retransmit_timer()incrementsicsk_backoffwithout any limit, soTCP_USER_TIMEOUTandTCP_RTO_MAX_MSon a socket with a silent peer are enough to geticsk_backoffabove 63, then an ICMP unreachable reaches the shift fromtcp_ld_RTO_revert().Quoted 2 lines
On x86 the shift count is masked, so from 64 on alive can come out true again and the orphan keeps probing instead of being aborted.
Right, and this starts before 64: with
icsk_rto== 200, the u64 is already zero whenicsk_backoffreaches 61.
Quoted 8 lines
Jiayuan, I have not seen a v3 addressing Neal's feedback.
When you send it, could you please add:
Reported-by Kyuwon Cho <[email protected]>and mention the zero window probe path in the changelog ?
Sure I can do that.
Quoted 5 lines
Kyuwon, it would be nice if you could test Jiayuan's patch with your reproducer and send a Tested-by: tag in that thread.
Thanks.
Quoted 6 lines · Eric Dumazet
Searching lore for the function name would have found it:
https://lore.kernel.org/netdev/?q=inet_csk_rto_backoff
Please do this before sending a report, especially for findings coming from automated tools.
You are right, sorry for the noise. We now search lore for the functions involved before sending a report.
Quoted 2 lines
Kyuwon, it would be nice if you could test Jiayuan's patch with your reproducer and send a Tested-by: tag in that thread.
Sure. I'll run it against Jiayuan's v3 once it is posted and send the Tested-by in that thread.
Thanks,
Kyuwon Cho