| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for a remotely-reachable abort in rwrite() (log.c): a daemon |
| 3 | # receiver child (am_server=1, am_generator=0, send_msgs_to_gen=1) that receives |
| 4 | # a MSG_INFO/MSG_ERROR frame from the wire calls rwrite() with |
| 5 | # is_utf8 = !am_generator = 1 (io.c read_a_msg). The send_msgs_to_gen branch |
| 6 | # used to assert(!is_utf8), so a peer that sends such a message to the receiver |
| 7 | # triggered a SIGABRT of the per-connection child. Legitimate senders never put |
| 8 | # these tags on the wire. The fix drops the assert and forwards the bytes raw to |
| 9 | # the generator, which logs them. |
| 10 | # |
| 11 | # This drives a real daemon receiver with the pure-Python sender: push a |
| 12 | # one-entry file list (so the receiver enters recv_files and reads our stream), |
| 13 | # then inject a MSG_INFO frame carrying a unique marker. In recv_files the |
| 14 | # receiver's read_ndx_and_attrs() reads our stream and read_a_msg() dispatches |
| 15 | # the MSG_INFO inline -> rwrite() with is_utf8=1. |
| 16 | # |
| 17 | # Oracle: fixed -> the receiver forwards the message to the generator, which |
| 18 | # logs the marker (and echoes a MSG_INFO frame back to us). pre-fix -> the |
| 19 | # receiver SIGABRTs on the assert *before* forwarding, so the marker never |
| 20 | # appears and the generator reports the connection dropped. |
| 21 | # |
| 22 | # Asserts are compiled in on the default (debug) build, so this is a normal- |
| 23 | # build RED/GREEN; it only needs a real TCP daemon (--use-tcp). |
| 24 | |
| 25 | import time |
| 26 | |
| 27 | from rsyncfns import ( |
| 28 | SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail, |
| 29 | ) |
| 30 | import rsync_proto as rp |
| 31 | |
| 32 | PORT = 12955 |
| 33 | MARKER = b'SCANNER-0009-FORWARDED' |
| 34 | require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp") |
| 35 | claim_ports(PORT) |
| 36 | |
| 37 | mod = SCRATCHDIR / 'mi-mod' |
| 38 | mod.mkdir(parents=True, exist_ok=True) |
| 39 | conf = SCRATCHDIR / 'mi.conf' |
| 40 | log = SCRATCHDIR / 'mi.log' |
| 41 | conf.write_text(f"""\ |
| 42 | pid file = {SCRATCHDIR}/mi-rsyncd.pid |
| 43 | use chroot = no |
| 44 | log file = {log} |
| 45 | |
| 46 | [mod] |
| 47 | path = {mod} |
| 48 | read only = no |
| 49 | """) |
| 50 | start_rsyncd(conf, PORT) |
| 51 | |
| 52 | s = rp.DaemonSender('127.0.0.1', PORT) |
| 53 | s.handshake('mod', ['--server', '-e.LsfxCIu', '.', 'mod/'], greeting_version=30) |
| 54 | # One regular file so the receiver enters recv_files and reads our stream. |
| 55 | s.send_flat_flist([rp.FileEntry('hello.txt', mode=rp.S_IFREG | 0o644, length=0)]) |
| 56 | # Inject a peer MSG_INFO -- the receiver child's read_a_msg() -> rwrite(is_utf8=1). |
| 57 | s.send_message(rp.MSG_INFO, MARKER + b'\n') |
| 58 | back = s.drain(timeout=3.0) |
| 59 | s.close() |
| 60 | |
| 61 | # The forwarded message reaches us as an echoed MSG_INFO frame and/or the |
| 62 | # generator's log. Either presence means the receiver did NOT abort. |
| 63 | forwarded = MARKER in back |
| 64 | for _ in range(50): |
| 65 | if log.exists() and MARKER.decode() in log.read_text(errors='replace'): |
| 66 | forwarded = True |
| 67 | break |
| 68 | if forwarded: |
| 69 | break |
| 70 | time.sleep(0.1) |
| 71 | |
| 72 | if not forwarded: |
| 73 | log_text = log.read_text(errors='replace') if log.exists() else '(no log)' |
| 74 | test_fail( |
| 75 | "daemon receiver did not forward a peer MSG_INFO -- it aborted on the " |
| 76 | "rwrite() assert(!is_utf8) (the marker never reached the generator).\n" |
| 77 | f"daemon log:\n{log_text}") |
| 78 | |
| 79 | print("proto-msg-info-assert: daemon receiver forwarded a peer MSG_INFO without " |
| 80 | "aborting (the rwrite assert is gone).") |