Debian Codegraph @

testsuite/io-readargs-argv-nullwrite_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for an off-by-one heap write in read_args() (io.c). After the
3 # '.' separator, read_args delegates argv growth to glob_expand(), which does
4 # ENSURE_MEMSPACE(glob.argv, char *, glob.maxargs, glob.argc + 1) -- reserving
5 # room for the entry it adds but not for the trailing NULL that read_args writes
6 # after the loop. ENSURE_MEMSPACE only grows when argc+1 > maxargs, so when a
7 # single post-dot glob lands argc exactly on maxargs (initially MAX_ARGS = 1000),
8 # no growth happens and the final 'argv[argc] = NULL' is an 8-byte NULL write one
9 # slot past the argv allocation. The pre-dot growth check (argc == maxargs-1)
10 # never fires once dot_pos is set, and it cannot pre-empt a single glob that
11 # crosses the boundary within one iteration.
12 #
13 # A daemon client sends '--server --sender -e... . *' against a module holding
14 # exactly 995 files: read_args adds "rsyncd" + 3 options + "." (argc 5), the lone
15 # post-dot '*' globs to 995 entries -> argc lands on exactly 1000 == maxargs, and
16 # argv[1000] is written one past the 1000-slot array. Reachable post-chroot/setuid
17 # in the per-connection daemon child. The fix reserves the +1 slot before the
18 # NULL store.
19 #
20 # Oracle: pre-fix -> the daemon child hits an ASan heap-buffer-overflow report;
21 # fixed -> none. Needs an ASan build + a real TCP daemon, and is skipped
22 # otherwise.
23
24 import glob as globmod
25 import os
26 import time
27
28 from rsyncfns import (
29 SCRATCHDIR, claim_ports, require_asan, require_tcp, start_rsyncd, test_fail,
30 )
31 import rsync_proto as rp
32
33 PORT = 12956
34 require_tcp("the pure-Python client needs a real TCP daemon; run with --use-tcp")
35 require_asan("the read_args trailing-NULL argv overflow is only observable under "
36 "AddressSanitizer")
37 claim_ports(PORT)
38
39 mod = SCRATCHDIR / 'readargs-mod'
40 mod.mkdir(parents=True, exist_ok=True)
41 for stale in mod.iterdir():
42 stale.unlink()
43 # read_args pre-dot args are: "rsyncd" + --server + --sender + -e.LsfxCIu + "."
44 # = 5; the lone post-dot '*' must glob to 995 so argc lands on MAX_ARGS (1000).
45 NFILES = 995
46 for i in range(NFILES):
47 (mod / ('f%04d' % i)).write_text('x')
48
49 conf = SCRATCHDIR / 'readargs.conf'
50 conf.write_text(f"""\
51 pid file = {SCRATCHDIR}/readargs-rsyncd.pid
52 use chroot = no
53
54 [mod]
55 path = {mod}
56 read only = no
57 """)
58
59 asan_log = SCRATCHDIR / 'readargs-asan'
60 for stale in globmod.glob(f"{asan_log}.*"):
61 os.unlink(stale)
62 prev = os.environ.get('ASAN_OPTIONS', '')
63 os.environ['ASAN_OPTIONS'] = (
64 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
65
66 start_rsyncd(conf, PORT)
67
68 c = rp.DaemonClient('127.0.0.1', PORT)
69 # A single post-dot '*' that globs to NFILES entries lands argc on maxargs.
70 c.handshake('mod', ['--server', '--sender', '-e.LsfxCIu', '.', '*'],
71 greeting_version=30)
72 c.drain(timeout=3.0)
73 c.close()
74
75 reports = ''
76 for _ in range(30):
77 reports = ''.join(open(r, errors='replace').read()
78 for r in globmod.glob(f"{asan_log}.*"))
79 if 'AddressSanitizer' in reports:
80 break
81 time.sleep(0.1)
82
83 if 'AddressSanitizer' in reports:
84 test_fail(
85 "read_args() wrote the trailing argv NULL one slot past the argv heap "
86 "allocation when a post-dot glob landed argc exactly on maxargs:\n"
87 + reports[:1500])
88
89 print("io-readargs-argv-nullwrite: the trailing argv NULL stays in bounds when a "
90 "post-dot glob fills argv to maxargs.")