Debian Codegraph @

testsuite/inband-modname-leak_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """KI-17: start_inband_exchange leaks modname on early error returns.
3
4 clientserver.c:start_inband_exchange allocates `modname` (new_array) and frees
5 it only on the success path; the early `return -1` error paths (e.g. the
6 daemon replying @ERROR for an unknown module) leak it. Client-side, one-shot
7 per failed connection.
8
9 ASan/LSan reproducer: connect to a daemon requesting a non-existent module so
10 the @ERROR path is taken, then assert no client leak report names
11 start_inband_exchange. Gated on an AddressSanitizer build and --use-tcp.
12 """
13
14 import glob
15 import os
16 import subprocess
17
18 from rsyncfns import (
19 FROMDIR, SCRATCHDIR, RSYNC,
20 make_tree, require_asan, require_tcp, rmtree, rsync_argv,
21 start_test_daemon, test_fail,
22 )
23
24 DAEMON_PORT = 12896
25 require_tcp("the daemon @ERROR handshake needs a real TCP peer")
26 require_asan("KI-17 modname leak is only observable under AddressSanitizer/LSan", RSYNC)
27
28 src = FROMDIR
29 rmtree(src)
30 make_tree(src, depth=1)
31
32 conf = SCRATCHDIR / 'modname-leak.conf'
33 conf.write_text(
34 f"pid file = {SCRATCHDIR}/rsyncd.pid\n"
35 "use chroot = no\n"
36 f"log file = {SCRATCHDIR}/rsyncd.log\n"
37 f"\n[realmod]\n\tpath = {src}\n\tread only = yes\n"
38 )
39 url = start_test_daemon(conf, DAEMON_PORT)
40
41 asan_log = SCRATCHDIR / 'modname-leak-asan'
42 for stale in glob.glob(f"{asan_log}.*"):
43 os.unlink(stale)
44 os.environ['ASAN_OPTIONS'] = (
45 f"detect_leaks=1:abort_on_error=0:log_path={asan_log}"
46 )
47
48 # Request a module that does not exist: the daemon replies @ERROR and the
49 # client's start_inband_exchange takes the early `return -1` that leaks modname.
50 p = subprocess.run(rsync_argv('-r', f'{url}no-such-module/'),
51 capture_output=True, text=True)
52
53 # Non-vacuity: the connection must have been refused via the @ERROR path that
54 # leaks modname (rc != 0 and an "Unknown module" / @ERROR diagnostic).
55 if p.returncode == 0:
56 test_fail("connection to a non-existent module unexpectedly succeeded; "
57 "the modname-leak path was not exercised")
58 if 'ERROR' not in p.stderr and 'nknown module' not in p.stderr:
59 test_fail(f"expected an @ERROR/unknown-module rejection; got:\n{p.stderr}")
60
61 reports = ''.join(open(r, errors='replace').read()
62 for r in glob.glob(f"{asan_log}.*"))
63 if 'start_inband_exchange' in reports:
64 test_fail("start_inband_exchange leaked modname on the @ERROR path (KI-17):\n"
65 + reports[:1500])
66
67 print("inband-modname-leak: start_inband_exchange does not leak modname")