Debian Codegraph @

testsuite/match-want-i-nolen_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for a NULL-pointer deref in hash_search() (match.c): the
3 # check_want_i adjacent-match optimisation substitutes i = want_i after matching
4 # sum1 and sum2, but -- unlike the main chain loop and the aligned_i path -- did
5 # NOT re-check s->sums[want_i].len == l.
6 #
7 # A hostile generator pulling from a daemon-as-sender forges a sum header against
8 # a served regular file: count=2, blength=len+1, remainder=len, s2length=0 (so
9 # the sum2 memcmp is a no-op), with BOTH block sum1 set to the file's rolling
10 # checksum get_checksum1(file, len). At offset 0 the chain skips block 0
11 # (.len=blength=len+1 != l=len) and matches block 1 (.len=remainder=len); then
12 # check_want_i substitutes want_i=0 (.len=len+1) unchecked. After matched(),
13 # offset advances to len, k = MIN(blength, len-offset) = 0, map_ptr(...,0)
14 # returns NULL, and match.c:323 dereferences map[0] -- a remote unauthenticated
15 # SEGV of the per-connection daemon child against any non-empty module file.
16 #
17 # We use a 1-byte file so get_checksum1 is trivial. The fix adds the missing
18 # `l == s->sums[want_i].len` gate (matching the chain loop / aligned_i path).
19 #
20 # Oracle: pre-fix -> the daemon child hits an ASan SEGV report (NULL deref);
21 # fixed -> the forged want_i substitution is rejected and no report appears.
22 # Needs an ASan build + a real TCP daemon, and is skipped otherwise.
23
24 import glob
25 import os
26
27 from rsyncfns import (
28 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
29 )
30 import rsync_proto as rp
31
32 PORT = 12951
33 require_tcp("the pure-Python generator needs a real TCP daemon; run with --use-tcp")
34 require_asan("the hash_search want_i NULL-deref is observed via the daemon's "
35 "AddressSanitizer SEGV report")
36 claim_ports(PORT)
37
38 mod = SCRATCHDIR / 'wanti-mod'
39 mod.mkdir(parents=True, exist_ok=True)
40 CONTENT = b'A' # 1-byte file => trivial rolling checksum
41 (mod / 'f').write_bytes(CONTENT)
42 LEN = len(CONTENT)
43
44 conf = SCRATCHDIR / 'wanti.conf'
45 conf.write_text(f"""\
46 pid file = {SCRATCHDIR}/wanti-rsyncd.pid
47 use chroot = no
48
49 [mod]
50 path = {mod}
51 read only = no
52 """)
53
54 asan_log = SCRATCHDIR / 'wanti-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 # Pull the single file: we are the receiver/generator, the daemon is the sender.
65 c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '.', 'mod/f'],
66 greeting_version=30)
67 entries = rp.sort_entries(c.recv_flist(preserve_links=False))
68 ndx = next(i for i, e in enumerate(entries) if e.name == b'f')
69
70 # Forged sum header: count=2, blength=len+1, remainder=len, s2length=0, both
71 # block sum1 = get_checksum1(file). No sum2 bytes follow (s2length=0).
72 sum1 = rp.get_checksum1(CONTENT)
73 req = (c.w_ndx(ndx) + rp.w_shortint(rp.ITEM_TRANSFER)
74 + rp.w_sum_head(2, LEN + 1, 0, LEN)
75 + rp.w_int(sum1) + rp.w_int(sum1))
76 c.send_data(req)
77 # Trailing data so the sender's last block-sum read completes (its read-ahead
78 # wants more than the lone request frame), after which it runs the vulnerable
79 # match_sums/hash_search.
80 c.send_data(c.w_ndx(rp.NDX_DONE))
81 c.drain(timeout=3.0)
82 c.close()
83
84 reports = glob.glob(f"{asan_log}.*")
85 text = ''.join(open(r, errors='replace').read() for r in reports)
86 if 'AddressSanitizer' in text:
87 test_fail(
88 "daemon sender hit an AddressSanitizer error on a forged adjacent-match "
89 "sum header -- check_want_i substituted want_i without re-checking its "
90 "length, so map_ptr() returned NULL and hash_search dereferenced it:\n"
91 + text[:1500])
92
93 print("match-want-i-nolen: forged want_i adjacent match is length-checked; no "
94 "NULL-deref in hash_search.")