Debian Codegraph @

testsuite/proto-cleared-ndx_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for a receiver NULL-deref on a transfer-phase index that
3 # targets a cleared file-list entry (receiver.c recv_files).
4 #
5 # A peer can send duplicate file-list entries; flist_sort_and_clean() then
6 # clear_file()s one of them (basename and mode zeroed) but leaves its slot
7 # indexable. If the transfer phase sends an ndx for that slot, recv_files()
8 # resolves it to the cleared file_struct and f_name() returns NULL, which flows
9 # into the daemon filter check / set_file_attrs() -> full_fname() and crashes
10 # the receiver child (NULL deref). The fix refuses an inactive entry
11 # (!F_IS_ACTIVE) at the ndx->file_struct resolution with a clean RERR_PROTOCOL.
12 #
13 # This drives a real daemon receiver with the pure-Python sender: push a file
14 # list with the SAME name twice (so flist_sort_and_clean clears the second
15 # slot, index 1), then send a transfer-phase ndx for that cleared slot.
16 #
17 # Oracle: fixed -> the receiver refuses with "refusing transfer of cleared file
18 # index 1" and exits RERR_PROTOCOL. pre-fix -> the receiver SIGSEGVs before
19 # logging anything, so that message never appears (the generator just reports
20 # the connection dropped). This is a normal-build crash (no ASan needed); it
21 # only needs a real TCP daemon.
22
23 import time
24
25 from rsyncfns import (
26 SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail,
27 )
28 import rsync_proto as rp
29
30 PORT = 12957
31 CLEARED_NDX = 1 # flist_sort_and_clean() deterministically clears the 2nd dup
32 require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp")
33 claim_ports(PORT)
34
35 mod = SCRATCHDIR / 'cn-mod'
36 mod.mkdir(parents=True, exist_ok=True)
37 conf = SCRATCHDIR / 'cn.conf'
38 log = SCRATCHDIR / 'cn.log'
39 conf.write_text(f"""\
40 pid file = {SCRATCHDIR}/cn-rsyncd.pid
41 use chroot = no
42 log file = {log}
43
44 [mod]
45 path = {mod}
46 read only = no
47 """)
48 start_rsyncd(conf, PORT)
49
50 before = log.read_text(errors='replace') if log.exists() else ''
51 s = rp.DaemonSender('127.0.0.1', PORT)
52 s.handshake('mod', ['--server', '-e.LsfxCIu', '.', 'mod/'], greeting_version=30)
53 # Same name twice -> the 2nd entry's slot is clear_file()'d.
54 dup = rp.FileEntry('dup.txt', mode=rp.S_IFREG | 0o644, length=0)
55 s.send_flat_flist([dup, dup])
56 # Transfer-phase index for the cleared slot.
57 s.send_transfer_ndx(CLEARED_NDX, iflags=0)
58 s.drain(timeout=2.0)
59 s.close()
60
61 refused = False
62 for _ in range(50):
63 new = log.read_text(errors='replace')[len(before):]
64 if 'refusing transfer of cleared file index' in new:
65 refused = True
66 break
67 time.sleep(0.1)
68
69 if not refused:
70 new = log.read_text(errors='replace')[len(before):]
71 test_fail(
72 "receiver did not refuse a transfer ndx targeting a cleared (dup) "
73 "file-list entry -- it dereferenced the NULL f_name() and crashed.\n"
74 f"daemon log:\n{new}")
75
76 print("proto-cleared-ndx: receiver refused a transfer ndx for a cleared "
77 "file-list slot (no NULL-deref crash).")