Debian Codegraph @

testsuite/daemon-scan-cwd-desync_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon directory scan must anchor confinement at the module root by IDENTITY,
3 # not by climbing ".." up from the cwd by the lexical curr_dir depth.
4 #
5 # rsync tracks the path it descended (curr_dir) lexically, but the daemon's
6 # change_dir follows in-tree directory symlinks. So when the requested path is an
7 # in-module directory symlink whose target nets SHALLOWER -- e.g. x/jump -> ../y,
8 # which from module/x lands at module/y -- the real kernel cwd ends up at module/y
9 # (depth 1) while curr_dir records module/x/jump (depth 2). A confinement that
10 # reaches the module root by climbing ".." up by the lexical depth then climbs too
11 # far (to module/y's grandparent, ABOVE the module) and resolves the scan there:
12 # in-module content goes missing, and were a matching path to exist above the
13 # module it could be enumerated out of the module. Anchoring at a module-root fd
14 # pinned by identity resolves the same logical path correctly and stays confined.
15 #
16 # Layout (module path = area/mod):
17 # mod/y/real.txt (the in-module content that must be served)
18 # mod/x/jump -> ../y (in-module symlink: mod/x/jump resolves to mod/y)
19 # Pull mod/x/jump/ with --copy-dirlinks: the daemon change_dir's through jump,
20 # desyncing curr_dir (mod/x/jump) from the real cwd (mod/y). A correct, identity-
21 # anchored scan serves mod/y/real.txt; the buggy lexical climb mis-anchors above
22 # the module and the file goes missing.
23 #
24 # Deterministic (static symlinks). Requires root for the daemon uid/gid lines;
25 # runs unprivileged too.
26
27 import os
28
29 from rsyncfns import (
30 SCRATCHDIR, get_rootgid, get_rootuid, get_testuid, makepath, rmtree,
31 run_rsync, start_test_daemon, test_fail,
32 )
33
34 DAEMON_PORT = 12950
35 CONTENT = "served-through-a-cwd-desyncing-symlink\n"
36
37 base = SCRATCHDIR / 'scan-desync'
38 rmtree(base)
39 area = base / 'area'
40 mod = area / 'mod'
41 dest = base / 'dest'
42 makepath(mod / 'y', mod / 'x', dest)
43 (mod / 'y' / 'real.txt').write_text(CONTENT)
44 # In-module symlink whose target nets shallower: mod/x/jump -> ../y == mod/y.
45 os.symlink('../y', mod / 'x' / 'jump')
46
47 root = get_testuid() == get_rootuid()
48 ids = f"uid = {get_rootuid()}\ngid = {get_rootgid()}" if root else ""
49
50 conf = base / 'd.conf'
51 conf.write_text(f"""\
52 pid file = {base}/rsyncd.pid
53 use chroot = no
54 {ids}
55 log file = {base}/rsyncd.log
56
57 [m]
58 path = {mod}
59 read only = yes
60 hosts allow = 127.0.0.1
61 """)
62 url = start_test_daemon(conf, DAEMON_PORT)
63
64 # Pull the in-module net-shallower symlinked path; the daemon descends through it.
65 run_rsync('-a', '--copy-dirlinks', f'{url}m/x/jump/', f'{dest}/', check=False)
66
67 got = dest / 'real.txt'
68 if not got.is_file() or got.read_text() != CONTENT:
69 test_fail(
70 "daemon scan failed to serve in-module content reached through a "
71 "cwd-desyncing in-module symlink (mod/x/jump -> ../y): dest/real.txt "
72 "missing/wrong. The scan must anchor at the module root by identity "
73 "(module_dirfd), not by climbing the lexical curr_dir depth -- the latter "
74 "over-climbs above the module when an in-module symlink nets shallower.")
75 print("daemon-scan-cwd-desync-escape: in-module content served correctly through "
76 "a cwd-desyncing symlink (scan stayed anchored at the module root)")