Debian Codegraph @

testsuite/msg-io-timeout-zero_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A malicious server must not be able to disable the client's --timeout.
3 #
4 # MSG_IO_TIMEOUT lets a peer ask the client to adopt a SHORTER I/O timeout (a
5 # stricter cap). A crafted server that sends MSG_IO_TIMEOUT(0) would, pre-fix,
6 # zero the client's --timeout via set_io_timeout(0) and could then hang the
7 # client indefinitely. The fix ignores a non-positive value, so the client
8 # keeps its --timeout and exits on its own.
9 #
10 # This drives a tiny crafted rsync daemon that completes the handshake, sends
11 # MSG_IO_TIMEOUT(0) as the first multiplex frame, then holds the socket open
12 # well past the client's --timeout. Fixed: the client times out and self-exits.
13 # Vulnerable: the client hangs until the watchdog kills it -> test_fail.
14
15 import subprocess
16 import sys
17 import time
18
19 from rsyncfns import (
20 SCRATCHDIR, claim_ports, rmtree, rsync_argv, test_fail, test_skipped,
21 )
22
23 # No require_tcp: this connects to our own local crafted server over a
24 # rsync:// URL (a plain TCP connect to 127.0.0.1), independent of the test's
25 # own pipe/tcp transport, so it runs in either mode.
26 PORT = 18367
27 claim_ports(PORT)
28
29 CLIENT_TIMEOUT = 3
30 HOLD = 25 # server holds the socket this long after sending the message
31 WATCHDOG = 12 # kill the client if it hasn't self-exited by now (< HOLD)
32
33 dst = SCRATCHDIR / 'ki47dst'
34 rmtree(dst)
35 dst.mkdir(parents=True)
36
37 # Minimal crafted daemon: greet, accept module, drain args, do the pre-multiplex
38 # setup_protocol exchange, then emit MSG_IO_TIMEOUT(0) and hold.
39 SERVER = r'''
40 import socket, sys, time, select
41 MPLEX_BASE, MSG_IO_TIMEOUT = 7, 33
42 def hdr(t, n):
43 v = ((MPLEX_BASE + t) << 24) | (n & 0xFFFFFF)
44 return bytes([v & 0xff, (v >> 8) & 0xff, (v >> 16) & 0xff, (v >> 24) & 0xff])
45 def le32(v):
46 return bytes([v & 0xff, (v >> 8) & 0xff, (v >> 16) & 0xff, (v >> 24) & 0xff])
47 port, hold = int(sys.argv[1]), float(sys.argv[2])
48 srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
50 srv.bind(("127.0.0.1", port)); srv.listen(1); srv.settimeout(20.0)
51 sys.stderr.write("ready\n"); sys.stderr.flush()
52 conn, _ = srv.accept(); conn.settimeout(10.0)
53 f = conn.makefile("rwb")
54 f.write(b"@RSYNCD: 32.0 sha256\n"); f.flush()
55 f.readline(); f.readline()
56 f.write(b"@RSYNCD: OK\n"); f.flush()
57 conn.setblocking(False)
58 dl, quiet = time.time() + 4.0, 0.0
59 while time.time() < dl:
60 r, _, _ = select.select([conn], [], [], 0.1)
61 if r:
62 try:
63 if not conn.recv(4096): break
64 quiet = 0.0
65 except BlockingIOError:
66 pass
67 else:
68 quiet += 0.1
69 if quiet >= 0.8: break
70 conn.setblocking(True); conn.settimeout(5.0)
71 f.write(b"\x00"); f.write(le32(42)); f.flush() # compat_flags=0, checksum_seed
72 f.write(hdr(MSG_IO_TIMEOUT, 4)); f.write(le32(0)); f.flush()
73 time.sleep(hold)
74 f.close(); conn.close(); srv.close()
75 '''
76
77 srv = subprocess.Popen([sys.executable, '-c', SERVER, str(PORT), str(HOLD)],
78 stderr=subprocess.PIPE)
79 try:
80 # wait for the server to be listening
81 line = srv.stderr.readline()
82 if b'ready' not in line:
83 test_skipped("crafted MSG_IO_TIMEOUT server failed to start")
84
85 t0 = time.monotonic()
86 try:
87 proc = subprocess.run(
88 rsync_argv(f'--timeout={CLIENT_TIMEOUT}', '--info=misc2',
89 f'rsync://127.0.0.1:{PORT}/mod/', f'{dst}/'),
90 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=WATCHDOG)
91 except subprocess.TimeoutExpired:
92 test_fail(
93 f"client did not self-exit within {WATCHDOG}s despite --timeout="
94 f"{CLIENT_TIMEOUT}: a MSG_IO_TIMEOUT(0) from the server disabled the "
95 "client timeout (it would hang indefinitely).")
96
97 out = (proc.stdout or b'').decode('utf-8', 'replace')
98 elapsed = time.monotonic() - t0
99 # The security property is that the client does NOT hang: it self-exited
100 # within the watchdog above (a vulnerable client would have been killed and
101 # raised TimeoutExpired -> test_fail). On the branch the crafted handshake
102 # reaches the inject point and the client exits via its own --timeout; if a
103 # platform's handshake didn't engage, the fast self-exit is still safe.
104 how = "timed out (kept --timeout)" if 'timeout' in out.lower() \
105 else "exited without timing out (crafted handshake may not have engaged)"
106 print(f"msg-io-timeout-zero: client self-exited in {elapsed:.1f}s -- {how}; "
107 "MSG_IO_TIMEOUT(0) did not disable the client timeout")
108 finally:
109 srv.terminate()
110 try:
111 srv.wait(timeout=5)
112 except subprocess.TimeoutExpired:
113 srv.kill()
114 srv.wait()