Debian Codegraph @

testsuite/proto-hlink-flag-oob_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for the heap out-of-bounds WRITE in recv_file_entry()
3 # (flist.c): a malicious sender that sets XMIT_HLINKED on a file even though
4 # the receiver was not invoked with -H. recv_file_entry() reserves the
5 # hard-link extras slots only under preserve_hard_links, but used to set
6 # FLAG_HLINKED straight from the peer's xflags. HLINK_BUMP() then offsets
7 # F_SUM() past the start of the pool_alloc()'d region, and the subsequent
8 # read_buf(f, F_SUM(file), flist_csum_len) writes attacker-controlled checksum
9 # bytes below the allocation. The fix gates FLAG_HLINKED on preserve_hard_links.
10 #
11 # The shift is HLINK_BUMP()*EXTRA_LEN = (inc_recurse+1)*EXTRA_LEN. With
12 # inc_recurse OFF the 8-byte shift is absorbed by extra-slot rounding and the
13 # write stays in bounds; with inc_recurse ON the shift is 16 bytes and the
14 # write underflows the chunk for real. So this drives the daemon with -r and an
15 # inc-recurse-capable -e string (the 'i' flag), no -H, and --checksum so the
16 # receiver reaches the F_SUM read.
17 #
18 # The corruption stays inside one 32 KiB pool malloc, so vanilla ASan can't see
19 # it; lib/pool_alloc.c fences each chunk with a poisoned redzone (under ASan).
20 # This test therefore needs an ASan build and a real TCP daemon, and is skipped
21 # otherwise.
22 #
23 # Entry: one regular file flagged XMIT_HLINKED|XMIT_HLINK_FIRST (HLINK_FIRST
24 # makes recv_file_entry skip the gnum read, keeping the stream in sync) plus a
25 # checksum trailer.
26 #
27 # Oracle: pre-fix -> the receiver aborts with an ASan use-after-poison report
28 # (captured via ASAN_OPTIONS=log_path). fixed -> no report.
29
30 import glob
31 import os
32
33 from rsyncfns import (
34 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
35 )
36 import rsync_proto as rp
37
38 PORT = 12953
39 require_tcp("the pure-Python sender needs a real TCP daemon; run with --use-tcp")
40 require_asan("the FLAG_HLINKED pool underflow is only observable under "
41 "AddressSanitizer + the lib/pool_alloc.c redzone")
42 claim_ports(PORT)
43
44 mod = SCRATCHDIR / 'hlink-oob-mod'
45 mod.mkdir(parents=True, exist_ok=True)
46 conf = SCRATCHDIR / 'hlink-oob.conf'
47 conf.write_text(f"""\
48 pid file = {SCRATCHDIR}/hlink-oob-rsyncd.pid
49 use chroot = no
50
51 [mod]
52 path = {mod}
53 read only = no
54 """)
55
56 # Route the daemon's ASan reports to files we can scan (start_rsyncd sends the
57 # daemon's stderr to /dev/null). The daemon inherits this env.
58 asan_log = SCRATCHDIR / 'hlink-oob-asan'
59 for stale in glob.glob(f"{asan_log}.*"):
60 os.unlink(stale)
61 prev = os.environ.get('ASAN_OPTIONS', '')
62 os.environ['ASAN_OPTIONS'] = (
63 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
64
65 start_rsyncd(conf, PORT)
66
67 s = rp.DaemonSender('127.0.0.1', PORT)
68 # -r + 'i' => inc_recurse on the daemon (HLINK_BUMP == 2); --checksum =>
69 # always_checksum; NO -H => preserve_hard_links off.
70 s.handshake('mod', ['--server', '-re.iLsfxCIu', '--checksum', '.', 'mod/'],
71 greeting_version=30)
72 evil = rp.FileEntry('h.txt', mode=rp.S_IFREG | 0o644, length=0,
73 extra_flags=rp.XMIT_HLINKED | rp.XMIT_HLINK_FIRST,
74 csum=b'\x41' * 64)
75 s.send_flat_flist([evil])
76 s.drain(timeout=3.0)
77 s.close()
78
79 reports = glob.glob(f"{asan_log}.*")
80 text = ''.join(open(r, errors='replace').read() for r in reports)
81 if 'AddressSanitizer' in text:
82 test_fail(
83 "receiver hit an AddressSanitizer error parsing an XMIT_HLINKED entry "
84 "sent without -H -- the FLAG_HLINKED pool underflow is unguarded:\n"
85 + text[:1500])
86
87 print("proto-hlink-flag-oob: XMIT_HLINKED-without-H entry parsed with no "
88 "pool underflow (FLAG_HLINKED gated on preserve_hard_links).")