Debian Codegraph @

testsuite/recv-generator-acl-leak_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """KI-23: recv_generator leaks real_sx ACL data on the directory path.
3
4 In generator.c:recv_generator the directory branch calls set_file_attrs() on
5 &real_sx, which (under --acls) loads the destination's current ACL into
6 real_sx via get_acl_fdat(), then does `goto cleanup`. The cleanup label only
7 frees sx, never real_sx (real_sx is freed solely on the regular-file path), so
8 each pre-existing ACL-bearing destination directory leaks its ACL data.
9
10 ASan/LSan reproducer: sync --acls onto a destination directory that already
11 carries an ACL, then assert no leak report names recv_generator. Gated on an
12 AddressSanitizer build and on setfacl being available.
13 """
14
15 import glob
16 import os
17 import shutil
18 import subprocess
19
20 from rsyncfns import (
21 SCRATCHDIR, RSYNC,
22 acls_supported, require_asan, rmtree, rsync_argv, test_fail, test_skipped,
23 )
24
25 require_asan("KI-23 real_sx ACL leak is only observable under AddressSanitizer/LSan", RSYNC)
26 if not acls_supported():
27 test_skipped("rsync built without ACL support, or filesystem rejects ACLs")
28 if not shutil.which('setfacl'):
29 test_skipped("setfacl not available to plant a destination-directory ACL")
30
31 base = SCRATCHDIR / 'acl-leak'
32 rmtree(base)
33 src = base / 'src'
34 dst = base / 'dst'
35 (src / 'sub').mkdir(parents=True)
36 (src / 'sub' / 'f.txt').write_text("updated-content\n") # different size -> always transferred
37 (dst / 'sub').mkdir(parents=True)
38 (dst / 'sub' / 'f.txt').write_text("old\n")
39
40 # Plant an ACL on the pre-existing destination directory so recv_generator's
41 # directory branch loads it into real_sx (the leaked allocation).
42 if subprocess.run(['setfacl', '-m', 'u:nobody:rwx', str(dst / 'sub')]).returncode != 0:
43 test_skipped("setfacl could not set an ACL on the destination directory")
44
45 asan_log = base / 'acl-leak-asan'
46 for stale in glob.glob(f"{asan_log}.*"):
47 os.unlink(stale)
48 os.environ['ASAN_OPTIONS'] = (
49 f"detect_leaks=1:abort_on_error=0:log_path={asan_log}"
50 )
51
52 p = subprocess.run(rsync_argv('-a', '--acls', f'{src}/', f'{dst}/'),
53 capture_output=True, text=True)
54
55 # Non-vacuity: the transfer must have actually run through recv_generator's
56 # directory branch (the dest dir pre-exists with an ACL, and its child file is
57 # updated). We can't check the exit code: under detect_leaks=1 rsync's
58 # intentional at-exit leaks already force a nonzero status.
59 if (dst / 'sub' / 'f.txt').read_text() != "updated-content\n":
60 test_fail(f"--acls transfer did not update the destination; leak path not exercised:\n{p.stderr}")
61
62 reports = ''.join(open(r, errors='replace').read()
63 for r in glob.glob(f"{asan_log}.*"))
64 if 'recv_generator' in reports:
65 test_fail("recv_generator leaked real_sx ACL data on the directory path (KI-23):\n"
66 + reports[:1500])
67
68 print("recv-generator-acl-leak: recv_generator does not leak real_sx ACL data")