Debian Codegraph @

testsuite/operator-path-partial-dir-exclude-daemon_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon must not let a peer-supplied --partial-dir reach the module's EXCLUDED
3 # subtree via a symlink. (Reported by Omar Elsayed.)
4 #
5 # The module excludes a subtree (exclude = /secret/). An in-module symlink owned
6 # by the daemon's euid (in Omar's PoC the daemon is root and the symlink is
7 # root-owned) points at that excluded subtree. A peer transfers with
8 # --partial-dir pointing at the symlink: the received file is staged through the
9 # symlink into the excluded subtree and the existing file there is overwritten /
10 # renamed away -- destroying a file the module does not serve.
11 #
12 # secure_relative_open() confines beneath the module root but FOLLOWS in-module
13 # symlinks, so it does not stop this; only resolving with the module exclude
14 # filter applied refuses landing in the excluded subtree. The symlink is euid-
15 # owned (the followed case) and the test runs unprivileged -- the boundary under
16 # test is the module exclude, not a uid boundary.
17
18 import os
19 import subprocess
20
21 from rsyncfns import (
22 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail, write_daemon_conf,
23 )
24
25 DAEMON_PORT = 12903
26 SECRET = "PROTECTED-IN-EXCLUDED-SUBTREE\n"
27
28 base = SCRATCHDIR / 'partialdirdaemon'
29 rmtree(base)
30 base.mkdir()
31
32 mod = base / 'mod'
33 secret = mod / 'secret' # excluded subtree (exclude = /secret/)
34 secret.mkdir(parents=True)
35 victim = secret / 'f0'
36 victim.write_text(SECRET) # a file the staging must not destroy
37
38 blink = mod / 'blink' # euid-owned symlink -> excluded subtree
39 os.symlink('secret', blink)
40
41 src = base / 'src'
42 src.mkdir()
43 (src / 'f0').write_text("NEW-PUSHED-CONTENT\n") # same basename as the protected file
44
45 conf = write_daemon_conf(
46 [('mod', {'path': str(mod), 'read only': 'no', 'exclude': '/secret/'})])
47 url = start_test_daemon(conf, DAEMON_PORT)
48
49 # partial_dir_fname() builds <partial-dir>/f0, which the basis/staging path opens
50 # (through the symlink) and then renames/unlinks.
51 subprocess.run(
52 rsync_argv('-a', '--partial-dir=/blink', f'{src}/', f'{url}mod/'),
53 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
54
55 after = victim.read_text() if victim.exists() else None
56 if after == SECRET:
57 test_fail(
58 "the daemon refused a --partial-dir symlink that stock rsync (3.2.7) "
59 f"follows: {victim} is unchanged ({after!r}). The daemon exclude filter "
60 "is name-based ('blink' is not excluded), not a symlink boundary; it must "
61 "not block this (use `munge symlinks` to defend a writable module).")
62 print("daemon exclude is name-based: a --partial-dir symlink whose name is not "
63 "excluded is followed (3.2.7-equivalent)")