Debian Codegraph @

testsuite/insecure-links-admin-optout_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Admin per-module opt-out: `insecure links = yes` (rsyncd.conf).
3 #
4 # A CLIENT can never switch off a daemon's symlink confinement -- that is the
5 # operator-path-insecure-links-{daemon,refused} guard. But an ADMIN may, for an
6 # isolated/trusted module, restore the legacy follow-any-symlink behaviour with
7 # `insecure links = yes` (the daemon analogue of the local --insecure-links).
8 # This re-opens the symlink-escape vectors for that module ON PURPOSE, so the
9 # test asserts both halves of the rule using two modules over one daemon, both
10 # rooted at the same tree with the same planted escape:
11 #
12 # - mod_secure (default) -> the escape is REFUSED (confined)
13 # - mod_insecure (insecure links=yes) -> the escape is SERVED (opt-out honored)
14 #
15 # The escape is a directory symlink inside the module pointing OUT of the module;
16 # a --copy-dirlinks pull enumerates through it (flist secure_opendir) and reads
17 # the out-of-tree file (sender content open). Both gates consult the per-module
18 # opt-out, so the out-of-module file arrives only for the insecure module.
19 #
20 # Requires root: the planted symlink is foreign-owned, matching the threat model
21 # (an attacker-owned in-module symlink), and the module is served as root.
22
23 import os
24 import subprocess
25
26 from rsyncfns import (
27 SCRATCHDIR, find_attacker_uid, rmtree, rsync_argv, rsync_supports,
28 start_test_daemon, test_fail, test_skipped, write_daemon_conf,
29 )
30
31 DAEMON_PORT = 12906
32 MARKER = "OUT-OF-MODULE-SECRET\n"
33
34 if os.geteuid() != 0:
35 test_skipped("requires root to plant a foreign-owned symlink and serve as root")
36 ATT = find_attacker_uid()
37 if ATT is None:
38 test_skipped("no untrusted-uid user available for cross-uid plant")
39 if not rsync_supports('--copy-dirlinks'):
40 test_skipped("rsync lacks --copy-dirlinks")
41
42 base = SCRATCHDIR / 'insecure-admin'
43 rmtree(base)
44 mod = base / 'mod'
45 outside = base / 'outside'
46 mod.mkdir(parents=True)
47 outside.mkdir(parents=True)
48
49 # In-module content (always servable) plus the escaping directory symlink.
50 (mod / 'intree.txt').write_text("in-module\n")
51 (outside / 'secret.txt').write_text(MARKER)
52 os.symlink('../outside', mod / 'sub') # dir symlink -> out of module
53 os.lchown(mod / 'sub', ATT, ATT) # foreign-owned (attacker) plant
54
55 # Serve as root (uid/gid 0) so the daemon euid differs from the attacker uid.
56 # Use numeric 0, not the name "root": the root *group* is "wheel" on the BSDs
57 # and macOS, so `gid = root` is rejected there ("Invalid gid root").
58 conf = write_daemon_conf([
59 ('mod_secure', {'path': str(mod), 'use chroot': 'no', 'read only': 'yes',
60 'uid': '0', 'gid': '0'}),
61 ('mod_insecure', {'path': str(mod), 'use chroot': 'no', 'read only': 'yes',
62 'uid': '0', 'gid': '0', 'insecure links': 'yes'}),
63 ])
64 url = start_test_daemon(conf, DAEMON_PORT)
65
66
67 def pull(modname):
68 out = base / f'dest_{modname}'
69 rmtree(out)
70 out.mkdir()
71 subprocess.run(
72 rsync_argv('-r', '--copy-dirlinks', f'{url}{modname}/', f'{out}/'),
73 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
74 served = out / 'sub' / 'secret.txt'
75 return served.is_file() and served.read_text() == MARKER
76
77
78 # Default module: the foreign-owned escaping symlink must be refused.
79 if pull('mod_secure'):
80 test_fail("mod_secure served the out-of-module file through the planted "
81 "directory symlink: the daemon followed it without `insecure "
82 "links = yes` -- confinement failed.")
83
84 # Opt-out module: the admin asked for legacy following, so it must be honored.
85 if not pull('mod_insecure'):
86 test_fail("mod_insecure did NOT serve the out-of-module file even with "
87 "`insecure links = yes`: the per-module admin opt-out was not "
88 "honored by the sender enumeration/content gates.")
89
90 print("insecure links = yes: refused by default, honored as an admin opt-out")