Debian Codegraph @

testsuite/daemon-module-private-parent_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon must serve a module whose path lies under a directory the SERVED
3 # (dropped-privilege) uid cannot traverse.
4 #
5 # The daemon chdir's into the module root while still root, then drops to the
6 # module uid; both the directory scan AND the file-content open must resolve
7 # relative to that pinned root, NOT re-walk the absolute module path -- which
8 # re-traverses the module's ancestors as the unprivileged uid and EACCESes. The
9 # common real-world shape is a module under a 0700 home (path = /home/backup/data);
10 # here we plant an explicit 0700 parent so the test is deterministic and
11 # independent of where the suite is built.
12 #
13 # RED before the privilege-drop-safe anchoring (the daemon's directory scan, or
14 # send_files' content open, fails with "Permission denied"); GREEN after.
15 # Requires root (a 0700 parent the served uid cannot traverse, and a daemon that
16 # drops to an unprivileged uid).
17
18 import os
19 import pwd
20 import subprocess
21
22 from rsyncfns import (
23 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon,
24 test_fail, test_skipped, write_daemon_conf,
25 )
26
27 DAEMON_PORT = 12951
28 CONTENT = "served-from-under-a-private-parent\n"
29
30
31 if os.geteuid() != 0:
32 test_skipped("requires root (a 0700 parent the served uid cannot traverse, "
33 "and a daemon that drops to an unprivileged uid)")
34
35 UNPRIV = None
36 for name in ('nobody', 'nfsnobody', 'daemon'):
37 try:
38 u = pwd.getpwnam(name)
39 if u.pw_uid != 0 and u.pw_uid != os.geteuid():
40 UNPRIV = u
41 break
42 except KeyError:
43 continue
44 if UNPRIV is None:
45 test_skipped("no unprivileged uid available to drop the daemon to")
46
47 base = SCRATCHDIR / 'priv-parent'
48 rmtree(base)
49 # A 0700 root-owned parent the served uid cannot search. The module lives under
50 # it; its own contents are world-readable so serving works once the daemon is
51 # inside (it chdir'd in while still root).
52 private = base / 'private'
53 modpath = private / 'mod'
54 sub = modpath / 'sub'
55 makepath(sub)
56 (sub / 'file').write_text(CONTENT)
57 os.chmod(modpath, 0o755)
58 os.chmod(sub, 0o755)
59 os.chmod(sub / 'file', 0o644)
60 os.chmod(private, 0o700) # <-- the barrier: unsearchable to UNPRIV
61
62 dest = base / 'dest'
63 makepath(dest)
64
65 # Numeric uid/gid so we don't depend on a group named like the user (e.g.
66 # "nobody" is a user but the group is "nogroup" on Debian/Ubuntu).
67 conf = write_daemon_conf([
68 ('m', {'path': modpath, 'read only': 'yes',
69 'uid': str(UNPRIV.pw_uid), 'gid': str(UNPRIV.pw_gid)}),
70 ])
71 url = start_test_daemon(conf, DAEMON_PORT)
72
73 def pull(*opts):
74 """Pull the module to a fresh dest with the given options; assert it serves
75 sub/file (the content open under the 0700 parent must not EACCES)."""
76 rmtree(dest)
77 makepath(dest)
78 proc = subprocess.run(
79 rsync_argv(*opts, f'{url}m/', f'{dest}/'),
80 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
81 if proc.returncode != 0:
82 test_fail(
83 f"daemon failed to serve a module under a 0700 parent as uid "
84 f"{UNPRIV.pw_name} ({' '.join(opts)}): {proc.stderr.strip()!r}. The "
85 "scan and the content open must resolve relative to the module root "
86 "the daemon pinned while privileged, not re-walk the absolute module "
87 "path (which EACCESes on the 0700 ancestor under the dropped uid).")
88 got = dest / 'sub' / 'file'
89 if not got.is_file() or got.read_text() != CONTENT:
90 test_fail(f"the under-0700-parent module did not serve its file with "
91 f"'{' '.join(opts)}' (dest/sub/file missing/wrong)")
92
93
94 # Default mode: content open via sender_open_confined() -> held_dir_path_fd()
95 # (the dpc base open).
96 pull('-r')
97 # Symlink-following mode: content open via sender_open_copylinks_confined() ->
98 # secure_relative_open(module_dir, ...) -- the other re-walk site (covers the
99 # absolute-basedir branch of the resolver).
100 pull('-rL')
101
102 print("daemon-module-private-parent: served a module under a 0700 parent as "
103 f"uid {UNPRIV.pw_name} (default + --copy-links content opens)")