Debian Codegraph @

testsuite/scanner-argv-bounds_test.py

raw symbols SCIP
1 #!/usr/bin/python3
2 # Regression tests for the run5 option/argv bound fixes: a hostile or
3 # wrapper-built command line must not overflow a fixed buffer, underflow a
4 # stack array, or exhaust the stack. Each case used to crash (global-buffer-
5 # overflow / stack-buffer-underflow / stack exhaustion under ASAN) and now must
6 # complete a clean transfer.
7 import os
8 import subprocess
9 from rsyncfns import (
10 FROMDIR, RSYNC_PEER, SRCDIR, SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail, rsync_path_arg, rsh_cmd,
11 )
12
13 LSH = rsh_cmd()
14 os.environ['RSYNC_RSH'] = LSH
15
16 src = FROMDIR
17 rmtree(src)
18 makepath(src)
19 (src / 'f').write_text('payload\n')
20
21
22 def run(label, extra, local=False):
23 dest = SCRATCHDIR / ('dest-' + label)
24 rmtree(dest)
25 makepath(dest)
26 if local:
27 argv = rsync_argv('-a', *extra, f'{src}/', f'{dest}/')
28 else:
29 argv = rsync_argv('-a', *extra, '-e', LSH,
30 f'--rsync-path={rsync_path_arg()}', f'{src}/', f'localhost:{dest}/')
31 r = subprocess.run(argv, capture_output=True, text=True)
32 if r.returncode < 0 or r.returncode >= 128:
33 test_fail(f'{label}: rsync crashed (rc={r.returncode}): {r.stderr.strip()[:200]}')
34 if not (dest / 'f').exists() or (dest / 'f').read_text() != 'payload\n':
35 test_fail(f'{label}: transfer did not complete (rc={r.returncode}): {r.stderr.strip()[:200]}')
36
37
38 # 0014: ~60 '-v' overran server_options()'s static argstr[64] (global-buffer-
39 # overflow WRITE) before the count was bounded.
40 run('verbose-flood', ['-v'] * 60)
41
42 # 0015: --info=NAME<overflowing-digits> -> atoi() returns a negative int on
43 # LP64 -> make_output_option() indexed counts[-1] (stack underflow).
44 run('info-overflow', ['--info=BACKUP99999999999999999999'])
45
46 # 0023: --skip-compress=<one very long token> recursed add_suffix() once per
47 # character, exhausting the stack; truncated to 32 chars now.
48 run('skip-compress-long', ['-z', '--skip-compress=' + 'a' * 5000], local=True)
49
50 print("scanner-argv-bounds: -v flood, --info overflow, --skip-compress long token all clean")