Debian Codegraph @

testsuite/match-append-empty-nullmap_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for a NULL-pointer deref in match_sums() (match.c): in
3 # daemon-as-sender mode, send_files() leaves mbuf == NULL for an empty served
4 # file (st.st_size == 0). For append_mode == 2 (--append-verify), match_sums()
5 # runs a verify loop driven by s->flength, which receive_sums() derives from the
6 # peer's sum header (count * blength) -- NOT the daemon's own flist size. The
7 # diminished-file guard compares st.st_size against F_LENGTH(file) (both 0), so
8 # it does not catch a peer that claims a non-empty length.
9 #
10 # A hostile client pulls an EMPTY module file with --append-verify and forges a
11 # sum header of count=1, blength=1 (remainder=0) -> s->flength = 1. The
12 # append_mode==2 loop then calls sum_update(map_ptr(NULL, 0, 1), 1), and
13 # map_ptr() (len != 0) dereferences the NULL map -- a remote unauthenticated SEGV
14 # of the per-connection daemon child. (receive_sums() returns early in append
15 # mode, so no block sums follow the header.) The fix clamps s->flength to len
16 # before the verify loop.
17 #
18 # Oracle: pre-fix -> the daemon child hits an ASan SEGV report; fixed -> none.
19 # Needs an ASan build + a real TCP daemon, and is skipped otherwise.
20
21 import glob
22 import os
23
24 from rsyncfns import (
25 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
26 )
27 import rsync_proto as rp
28
29 PORT = 12952
30 require_tcp("the pure-Python generator needs a real TCP daemon; run with --use-tcp")
31 require_asan("the append-mode NULL map_ptr deref is observed via the daemon's "
32 "AddressSanitizer SEGV report")
33 claim_ports(PORT)
34
35 mod = SCRATCHDIR / 'append-mod'
36 mod.mkdir(parents=True, exist_ok=True)
37 (mod / 'f').write_bytes(b'') # empty file => mbuf is NULL on the sender
38
39 conf = SCRATCHDIR / 'append.conf'
40 conf.write_text(f"""\
41 pid file = {SCRATCHDIR}/append-rsyncd.pid
42 use chroot = no
43
44 [mod]
45 path = {mod}
46 read only = no
47 """)
48
49 asan_log = SCRATCHDIR / 'append-asan'
50 for stale in glob.glob(f"{asan_log}.*"):
51 os.unlink(stale)
52 prev = os.environ.get('ASAN_OPTIONS', '')
53 os.environ['ASAN_OPTIONS'] = (
54 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
55
56 start_rsyncd(conf, PORT)
57
58 c = rp.DaemonClient('127.0.0.1', PORT)
59 # --append-verify => the daemon sender runs with append_mode == 2.
60 c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '--append-verify',
61 '.', 'mod/f'], greeting_version=30)
62 entries = rp.sort_entries(c.recv_flist(preserve_links=False))
63 ndx = next(i for i, e in enumerate(entries) if e.name == b'f')
64
65 # Forged sum header claiming a non-empty verified prefix (flength = 1*1 = 1)
66 # against the empty file. In append mode receive_sums() returns after the
67 # header, so no block sums follow.
68 req = (c.w_ndx(ndx) + rp.w_shortint(rp.ITEM_TRANSFER)
69 + rp.w_sum_head(1, 1, 0, 0))
70 c.send_data(req)
71 c.send_data(c.w_ndx(rp.NDX_DONE)) # trailing data so the header read completes
72 c.drain(timeout=3.0)
73 c.close()
74
75 reports = glob.glob(f"{asan_log}.*")
76 text = ''.join(open(r, errors='replace').read() for r in reports)
77 if 'AddressSanitizer' in text:
78 test_fail(
79 "daemon sender hit an AddressSanitizer error on a forged append-mode sum "
80 "header against an empty file -- s->flength was taken from the peer and "
81 "the append verify loop dereferenced the NULL map:\n" + text[:1500])
82
83 print("match-append-empty-nullmap: forged append-mode flength is clamped to the "
84 "local length; no NULL-deref in match_sums.")