Debian Codegraph @

testsuite/io-nosend-flood-recursion_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Sibling of io-noop-flood-recursion: the same read_a_msg() re-entry bug reached
3 # through MSG_NO_SEND instead of MSG_NOOP. The original fix deferred
4 # iobuf.in_multiplexed = 1 only for MSG_NOOP/MSG_IO_ERROR, but the MSG_NO_SEND
5 # handler also reset it to 1 *before* its send_msg_int(MSG_NO_SEND, val) call-out
6 # (the !am_generator branch). At a daemon sender (am_sender), send_msg() reaches
7 # perform_io(PIO_NEED_MSGROOM), which re-enters read_a_msg() while
8 # in_multiplexed > 0 and iobuf.in.len > 512 -- so a MSG_NO_SEND flood recurses to
9 # stack exhaustion exactly like the MSG_NOOP one. (Codex review of the run4 0002
10 # fix flagged this sibling path.)
11 #
12 # Pull from a daemon (so it is the sender, not the generator -> the send_msg_int
13 # branch runs) and flood MSG_NO_SEND frames in one burst. The fix defers
14 # in_multiplexed = 1 past the call-out here too.
15 #
16 # Oracle: pre-fix -> ASan stack-overflow report from the daemon child; fixed ->
17 # none. Needs an ASan build + a real TCP daemon, and is skipped otherwise.
18
19 import glob
20 import os
21 import socket
22 import struct
23 import time
24
25 from rsyncfns import (
26 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
27 )
28 import rsync_proto as rp
29
30 PORT = 12958
31 require_tcp("the pure-Python client needs a real TCP daemon; run with --use-tcp")
32 require_asan("the read_a_msg MSG_NO_SEND recursion is observed via the daemon's "
33 "AddressSanitizer stack-overflow report")
34 claim_ports(PORT)
35
36 mod = SCRATCHDIR / 'nosend-mod'
37 mod.mkdir(parents=True, exist_ok=True)
38 (mod / 'f').write_text("hello\n")
39
40 conf = SCRATCHDIR / 'nosend.conf'
41 # Tiny SO_SNDBUF so the daemon's re-sent MSG_NO_SEND output blocks almost
42 # immediately (loopback send buffers are otherwise large), forcing send_msg()'s
43 # perform_io(PIO_NEED_MSGROOM) -- and thus the input-draining re-entry.
44 conf.write_text(f"""\
45 pid file = {SCRATCHDIR}/nosend-rsyncd.pid
46 use chroot = no
47 socket options = SO_SNDBUF=2048
48
49 [mod]
50 path = {mod}
51 read only = no
52 """)
53
54 asan_log = SCRATCHDIR / 'nosend-asan'
55 for stale in glob.glob(f"{asan_log}.*"):
56 os.unlink(stale)
57 prev = os.environ.get('ASAN_OPTIONS', '')
58 os.environ['ASAN_OPTIONS'] = (
59 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
60
61 start_rsyncd(conf, PORT)
62
63 c = rp.DaemonClient('127.0.0.1', PORT)
64 # Shrink our receive buffer so the daemon's re-sent MSG_NO_SEND output blocks
65 # almost immediately (on loopback the default buffers are huge). Once send_msg()
66 # can't drain, perform_io(PIO_NEED_MSGROOM) does the input-draining re-entry --
67 # unlike MSG_NOOP, whose maybe_send_keepalive() flushes unconditionally.
68 c.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2048)
69 # Pull => daemon is the sender (am_sender, not am_generator) => MSG_NO_SEND takes
70 # the send_msg_int() branch.
71 c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '.', 'mod/'],
72 greeting_version=30)
73 # A MSG_NO_SEND frame: header ((MPLEX_BASE + MSG_NO_SEND) << 24) | 4, 4-byte body.
74 NOSEND = struct.pack('<I', ((rp.MPLEX_BASE + 102) << 24) | 4) + b'\x00\x00\x00\x00'
75 try:
76 c._send_raw(NOSEND * 60000)
77 except OSError:
78 pass # daemon crashed mid-write, or send timed out
79 try:
80 c.drain(timeout=5.0)
81 except OSError:
82 pass # connection reset by the crashed daemon child
83 c.close()
84
85 reports = ''
86 for _ in range(30):
87 reports = ''.join(open(r, errors='replace').read()
88 for r in glob.glob(f"{asan_log}.*"))
89 if 'AddressSanitizer' in reports:
90 break
91 time.sleep(0.1)
92
93 if 'AddressSanitizer' in reports:
94 test_fail(
95 "daemon sender recursed through read_a_msg on a MSG_NO_SEND flood and hit "
96 "an AddressSanitizer stack-overflow -- the MSG_NO_SEND handler re-armed "
97 "in_multiplexed before the send_msg_int flush:\n" + reports[:1500])
98
99 print("io-nosend-flood-recursion: a MSG_NO_SEND flood does not re-enter "
100 "read_a_msg (no stack exhaustion).")