Debian Codegraph @

testsuite/io-noop-flood-recursion_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for unbounded recursion in read_a_msg() (io.c). read_a_msg()
3 # sets iobuf.in_multiplexed = -1 on entry so that any perform_io() reached while
4 # the message body is consumed will not re-enter read_a_msg(). The MSG_NOOP (and
5 # MSG_IO_ERROR) handlers reset it to 1 *before* calling maybe_send_keepalive() /
6 # send_msg_int(), both of which can reach perform_io(PIO_NEED_MSGROOM). With more
7 # frames already buffered (iobuf.in.len > 512), perform_io() then loops back into
8 # read_a_msg() -- each frame stacks a fresh BIGPATHBUFLEN local, so a hostile peer
9 # that floods MSG_NOOP at a daemon sender exhausts the stack.
10 #
11 # Drive it with the pure-Python client: pull from a daemon (so the daemon is the
12 # sender, am_sender => maybe_send_keepalive runs) and, instead of a filter list,
13 # flood MSG_NOOP frames in one burst. The daemon's first read of our stream
14 # recurses through read_a_msg until the stack guard page is hit.
15 #
16 # The fix defers iobuf.in_multiplexed = 1 to *after* the call-out, keeping the
17 # re-entry guard armed across the write-side flush.
18 #
19 # Oracle: pre-fix -> the daemon child hits an ASan stack-overflow report; fixed
20 # -> none. Needs an ASan build + a real TCP daemon, and is skipped otherwise.
21
22 import glob
23 import os
24 import struct
25 import time
26
27 from rsyncfns import (
28 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
29 )
30 import rsync_proto as rp
31
32 PORT = 12955
33 require_tcp("the pure-Python client needs a real TCP daemon; run with --use-tcp")
34 require_asan("the read_a_msg recursion is observed via the daemon's "
35 "AddressSanitizer stack-overflow report")
36 claim_ports(PORT)
37
38 mod = SCRATCHDIR / 'noop-mod'
39 mod.mkdir(parents=True, exist_ok=True)
40 (mod / 'f').write_text("hello\n")
41
42 conf = SCRATCHDIR / 'noop.conf'
43 conf.write_text(f"""\
44 pid file = {SCRATCHDIR}/noop-rsyncd.pid
45 use chroot = no
46
47 [mod]
48 path = {mod}
49 read only = no
50 """)
51
52 asan_log = SCRATCHDIR / 'noop-asan'
53 for stale in glob.glob(f"{asan_log}.*"):
54 os.unlink(stale)
55 prev = os.environ.get('ASAN_OPTIONS', '')
56 os.environ['ASAN_OPTIONS'] = (
57 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
58
59 start_rsyncd(conf, PORT)
60
61 c = rp.DaemonClient('127.0.0.1', PORT)
62 # Pull => the daemon is the sender (am_sender, so the MSG_NOOP handler reaches
63 # maybe_send_keepalive).
64 c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '.', 'mod/'],
65 greeting_version=30)
66 # A MSG_NOOP frame: header ((MPLEX_BASE + MSG_NOOP) << 24) | 0, no payload.
67 NOOP = struct.pack('<I', (rp.MPLEX_BASE + 42) << 24)
68 try:
69 c._send_raw(NOOP * 40000) # one big burst => iobuf.in.len >> 512
70 except OSError:
71 pass # the daemon may die mid-write
72 c.drain(timeout=5.0)
73 c.close()
74
75 reports = ''
76 for _ in range(30):
77 reports = ''.join(open(r, errors='replace').read()
78 for r in glob.glob(f"{asan_log}.*"))
79 if 'AddressSanitizer' in reports:
80 break
81 time.sleep(0.1)
82
83 if 'AddressSanitizer' in reports:
84 test_fail(
85 "daemon sender recursed through read_a_msg on a MSG_NOOP flood and hit "
86 "an AddressSanitizer stack-overflow -- the MSG_NOOP handler re-armed "
87 "in_multiplexed before the keepalive flush:\n" + reports[:1500])
88
89 print("io-noop-flood-recursion: a MSG_NOOP flood does not re-enter read_a_msg "
90 "(no stack exhaustion).")