// 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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; }