| 1 | #!/usr/bin/env python3 |
| 2 | # A near-INT_MAX I/O timeout must not overflow set_io_timeout() into a tight CPU |
| 3 | # loop. Reported by z3r0s (github z3r0s6), 2026-07. |
| 4 | # |
| 5 | # set_io_timeout() computed "allowed_lull = (io_timeout + 1) / 2", a signed |
| 6 | # overflow for io_timeout near INT_MAX that yields a negative allowed_lull / |
| 7 | # select_timeout; select() then returns EINVAL on the negative tv_sec, which |
| 8 | # isn't EBADF, so the read loop spins at 100% CPU forever (io_timeout ~= 68 years |
| 9 | # never fires check_timeout). io_timeout reaches INT_MAX two ways, both fixed on |
| 10 | # this branch: a malicious daemon's MSG_IO_TIMEOUT (PR 50 caps the wire value at |
| 11 | # 86400) and an operator's --timeout (options.c parses it unbounded). The root |
| 12 | # fix computes allowed_lull overflow-safe in a wider type in set_io_timeout(), |
| 13 | # covering both. |
| 14 | # |
| 15 | # Because "(io_timeout + 1)" is undefined behaviour, whether it manifests depends |
| 16 | # on the build: plain -O2 gcc/clang optimize the compare so select_timeout stays |
| 17 | # 60 (no spin, though allowed_lull is still stored negative), while -fwrapv / |
| 18 | # -fno-strict-overflow (common hardening) wrap deterministically to the negative |
| 19 | # select_timeout and the spin (-ftrapv aborts). So to exercise the DoS |
| 20 | # deterministically on any target we build the rsync-under-test with -fwrapv and |
| 21 | # drive set_io_timeout(INT_MAX) through the --timeout path -- the same overflow a |
| 22 | # capped MSG_IO_TIMEOUT would otherwise reach. (The wire path sets the timeout |
| 23 | # mid-stream, so whether the client next blocks in select() is timing-dependent; |
| 24 | # --timeout sets it at startup, so the very first select() spins -- deterministic.) |
| 25 | # |
| 26 | # Oracle: run the -fwrapv build with --timeout=INT_MAX on a normal local copy. |
| 27 | # * transfer completes -> overflow-safe (PASS / GREEN) |
| 28 | # * transfer runs past the deadline -> tight select()-EINVAL loop (FAIL / RED) |
| 29 | |
| 30 | import os |
| 31 | import signal |
| 32 | import subprocess |
| 33 | |
| 34 | from rsyncfns import ( |
| 35 | SCRATCHDIR, build_patched_rsync, makepath, rmtree, test_fail, |
| 36 | ) |
| 37 | |
| 38 | INT_MAX = 2147483647 |
| 39 | DEADLINE = 25 # a normal small copy finishes in well under a second |
| 40 | |
| 41 | # -fwrapv build of the tree under test (no source patch); makes the signed |
| 42 | # overflow deterministic on every platform instead of compiler-optimized away. |
| 43 | victim = build_patched_rsync('io-timeout-fwrapv', [], append_cflags='-fwrapv') |
| 44 | |
| 45 | base = SCRATCHDIR / 'io-timeout' |
| 46 | rmtree(base) |
| 47 | src = base / 'src' |
| 48 | dst = base / 'dst' |
| 49 | makepath(src) |
| 50 | makepath(dst) |
| 51 | (src / 'file').write_text("payload\n") |
| 52 | |
| 53 | # --timeout=INT_MAX -> set_io_timeout(INT_MAX) at startup. An unfixed -fwrapv |
| 54 | # build wraps select_timeout negative and the first select() spins forever; the |
| 55 | # fixed build keeps it sane and the copy completes. |
| 56 | proc = subprocess.Popen( |
| 57 | [str(victim), '--timeout=%d' % INT_MAX, '-a', str(src) + '/', str(dst) + '/'], |
| 58 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 59 | start_new_session=True) |
| 60 | try: |
| 61 | out, _ = proc.communicate(timeout=DEADLINE) |
| 62 | except subprocess.TimeoutExpired: |
| 63 | # RED: trapped in the tight select()-EINVAL loop, will never return. |
| 64 | try: |
| 65 | os.killpg(os.getpgid(proc.pid), signal.SIGKILL) |
| 66 | except ProcessLookupError: |
| 67 | pass |
| 68 | proc.communicate() |
| 69 | test_fail( |
| 70 | f"--timeout={INT_MAX} trapped rsync in a tight loop: the copy did not " |
| 71 | f"finish within {DEADLINE}s. A near-INT_MAX timeout overflowed " |
| 72 | "set_io_timeout()'s (io_timeout + 1) / 2 to a negative select_timeout " |
| 73 | "(select() -> EINVAL -> 100% CPU spin). Fix: compute allowed_lull " |
| 74 | "overflow-safe (wider type) in set_io_timeout(), and cap a peer's " |
| 75 | "MSG_IO_TIMEOUT in read_a_msg().") |
| 76 | |
| 77 | # -- Oracle ------------------------------------------------------------------- |
| 78 | if proc.returncode != 0: |
| 79 | test_fail(f"rsync did not spin under --timeout={INT_MAX} but exited " |
| 80 | f"non-zero (rc={proc.returncode}). Output tail:\n" |
| 81 | + '\n'.join(out.splitlines()[-20:])) |
| 82 | if not (dst / 'file').is_file(): |
| 83 | test_fail(f"rsync returned 0 under --timeout={INT_MAX} but did not copy the " |
| 84 | f"file. Output tail:\n" + '\n'.join(out.splitlines()[-20:])) |
| 85 | |
| 86 | print("msg-io-timeout-overflow: -fwrapv rsync absorbed --timeout=INT_MAX without " |
| 87 | "spinning; the copy completed normally.") |