Debian Codegraph @

testsuite/operator-path-dir-daemon-outside_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon must not let a peer-supplied --partial-dir/--backup-dir symlink resolve
3 # OUTSIDE the served module at all (not just into an excluded subtree).
4 # (Companion to the exclude-bypass tests; reported via Omar Elsayed's findings.)
5 #
6 # An in-module symlink owned by the daemon euid whose target is ABSOLUTE (or
7 # climbs out with ../) points to a sibling directory outside the module root.
8 # An unprivileged peer overwrites a dest file with --backup/--partial-dir aimed
9 # at that symlink, moving/staging the data OUTSIDE the served set -- modifying
10 # files the module never exposed. The module-confined resolver must refuse a
11 # resolved path that leaves the module root.
12 #
13 # Runs unprivileged: the boundary under test is the module root, not a uid.
14
15 import os
16 import subprocess
17
18 from rsyncfns import (
19 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail, write_daemon_conf,
20 )
21
22 def run_case(label, opt, extra, port):
23 base = SCRATCHDIR / f'outside-{label}'
24 rmtree(base)
25 base.mkdir()
26 mod = base / 'mod'
27 mod.mkdir()
28 outside = base / 'outside' # sibling of the module -- NOT served
29 outside.mkdir()
30 (mod / 'f0').write_text("OLD-DEST-CONTENT\n") # existing dest -> overwritten
31 src = base / 'src'
32 src.mkdir()
33 (src / 'f0').write_text("NEW\n")
34 # euid-owned symlink inside the module, absolute target outside the module.
35 os.symlink(str(outside), mod / 'elink')
36
37 conf = write_daemon_conf(
38 [('mod', {'path': str(mod), 'read only': 'no'})],
39 {'pid file': str(SCRATCHDIR / f'rsyncd-outside-{label}.pid')},
40 name=f'outside-{label}.conf')
41 url = start_test_daemon(conf, port)
42 subprocess.run(
43 rsync_argv('-a', *extra, f'{opt}=/elink', f'{src}/', f'{url}mod/'),
44 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45 leaked = [os.path.join(r, f) for r, _d, fs in os.walk(outside) for f in fs]
46 if leaked:
47 test_fail(
48 f"{label}: the daemon followed an in-module symlink whose target is "
49 f"OUTSIDE the module root and wrote there: {leaked} (a peer modified "
50 "files the module does not serve). The resolver must confine an "
51 "operator/peer path to the module root.")
52
53
54 run_case('backup', '--backup-dir', ['--backup'], 12905)
55 run_case('partial', '--partial-dir', [], 12909)
56 print("daemon confines peer --partial-dir/--backup-dir symlinks to the module root")