Debian Codegraph @

testsuite/exclude-implied-trailing-backslash_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for a 1-byte heap overflow in add_implied_include()
3 # (exclude.c). Phase 1 classifies a '\' via !strchr("*[?", cp[1]); strchr()
4 # returns the literal's NUL terminator when cp[1] == '\0', so a TRAILING
5 # backslash takes neither branch -- it is copied into new_pat but backslash_cnt
6 # is not incremented. Phase 2's recurse/xfer_dirs '/**' rule allocates
7 # arg_len + backslash_cnt + 3 + 1 and doubles every '\' in new_pat (including the
8 # uncounted trailing one), so the closing '*p = '\0'' lands one byte past the
9 # heap buffer.
10 #
11 # Reachable on a standard network 'rsync --daemon' (the per-module
12 # parse_arguments() runs with am_server == 0, so trust_sender_args stays 0 and
13 # add_implied_include is active) by a remote unauthenticated client that sends
14 # '-r --files-from=<file>' with a files-from name carrying both an interior and a
15 # trailing backslash ('a\b\'): forward_filesfrom_data() feeds it to
16 # add_implied_include() on the daemon. The fix counts the trailing backslash.
17 #
18 # Oracle: pre-fix -> the daemon hits an ASan heap-buffer-overflow report; fixed
19 # -> none. Needs an ASan build + a real TCP daemon, and is skipped otherwise.
20
21 import glob
22 import os
23 import subprocess
24
25 from rsyncfns import (
26 SCRATCHDIR, claim_ports, makepath, require_asan, require_tcp, rsync_argv,
27 start_rsyncd, test_fail,
28 )
29
30 PORT = 12954
31 require_tcp("a real network rsync daemon is needed (am_server==0 per-module); "
32 "run with --use-tcp")
33 require_asan("the 1-byte heap overflow in add_implied_include is only observable "
34 "under AddressSanitizer")
35 claim_ports(PORT)
36
37 base = SCRATCHDIR / 'implied-bslash'
38 mod = base / 'mod'
39 dest = base / 'dest'
40 makepath(mod, dest)
41 (mod / 'file.txt').write_text("hello\n")
42
43 # A files-from entry with an interior AND a trailing backslash.
44 ff = base / 'files-from.txt'
45 ff.write_text('a\\b\\\n')
46
47 conf = base / 'implied.conf'
48 conf.write_text(f"""\
49 pid file = {SCRATCHDIR}/implied-rsyncd.pid
50 use chroot = no
51
52 [mod]
53 path = {mod}
54 read only = yes
55 """)
56
57 asan_log = base / 'implied-asan'
58 for stale in glob.glob(f"{asan_log}.*"):
59 os.unlink(stale)
60 prev = os.environ.get('ASAN_OPTIONS', '')
61 os.environ['ASAN_OPTIONS'] = (
62 (prev + ':' if prev else '') + f'detect_leaks=0:abort_on_error=1:log_path={asan_log}')
63
64 start_rsyncd(conf, PORT)
65
66 # A real client forwards the files-from names to the daemon, which parses each
67 # through add_implied_include() (recurse via -r so the vulnerable '/**' path runs).
68 proc = subprocess.run(
69 rsync_argv('-r', f'--files-from={ff}', f'rsync://127.0.0.1:{PORT}/mod/',
70 str(dest) + '/'),
71 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=30)
72
73
74 def asan_report():
75 text = ''.join(open(r, errors='replace').read()
76 for r in glob.glob(f"{asan_log}.*"))
77 return text if 'AddressSanitizer' in text else (
78 proc.stdout if 'AddressSanitizer' in (proc.stdout or '') else '')
79
80
81 # The overflowing process (the forwarding client child) may flush its report a
82 # beat after the client exits, so poll briefly.
83 import time
84 report = ''
85 for _ in range(30):
86 report = asan_report()
87 if report:
88 break
89 time.sleep(0.1)
90
91 if report:
92 test_fail(
93 "add_implied_include() overflowed its heap buffer on a files-from name "
94 "with a trailing backslash -- the trailing '\\' was doubled but not "
95 "counted in the allocation:\n" + report[:1500])
96
97 print("exclude-implied-trailing-backslash: a trailing backslash in a files-from "
98 "name is counted; no heap overflow in add_implied_include.")