Debian Codegraph @

testsuite/operator-path-insecure-links-daemon_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # A daemon must NEVER honor a peer-forwarded --insecure-links: a client cannot be
3 # allowed to switch off the daemon's symlink confinement (otherwise a malicious
4 # client just always sends it). rsync no longer forwards --insecure-links to the
5 # server (it is a local-only opt-out), but a client can still inject it explicitly
6 # with -M/--remote-option, so this guards that every daemon-side confinement gate
7 # ignores it -- the opt-out predicate is am_daemon ? lp_insecure_links(module_id)
8 # : insecure_links, which never reads the peer-controllable insecure_links on a
9 # daemon. (A daemon also hard-refuses an explicitly-injected --insecure-links via
10 # the refused-options path; see operator-path-insecure-links-refused.)
11 #
12 # Exercised via the module-root chdir (change_dir): the daemon serves a module
13 # whose configured `path =` is a foreign-owned symlink to an escape dir. The
14 # daemon's chdir must refuse it EVEN when the client forwards -M--insecure-links.
15 # (change_dir's daemon branch is already always-confined; this locks that in and
16 # guards the dir-sink/file-open fixes that add the opt-out gate.)
17 #
18 # Requires root for the cross-uid plant.
19
20 import os
21 import subprocess
22
23 from rsyncfns import (
24 SCRATCHDIR, find_attacker_uid, rmtree, rsync_argv, start_test_daemon,
25 test_fail, test_skipped, write_daemon_conf,
26 )
27
28 DAEMON_PORT = 12901
29 CANARY = "audit_daemon_optout_canary"
30
31 if os.geteuid() != 0:
32 test_skipped("requires root to plant a symlink owned by a non-self uid")
33 ATT = find_attacker_uid()
34 if ATT is None:
35 test_skipped("no untrusted-uid user available for cross-uid plant")
36
37 base = SCRATCHDIR / 'daemonoptout'
38 rmtree(base)
39 base.mkdir()
40 escape_target = base / 'escape_target'
41 escape_target.mkdir()
42 (escape_target / CANARY).write_text("must-not-be-served\n")
43
44 plant = base / 'plant' # the module-configured `path =`
45 os.symlink(escape_target, plant)
46 os.lchown(plant, ATT, ATT) # foreign-owned (attacker) symlink
47
48 conf = write_daemon_conf([('mod', {'path': plant, 'read only': 'yes'})])
49 url = start_test_daemon(conf, DAEMON_PORT)
50
51 # The client forwards --insecure-links to the daemon (-M / --remote-option).
52 proc = subprocess.run(
53 rsync_argv('-M--insecure-links', f'{url}mod/'),
54 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
55
56 if CANARY in proc.stdout:
57 test_fail(
58 "the daemon honored a peer-forwarded --insecure-links and disabled its "
59 f"module-chdir confinement: the foreign-owned symlink {plant} -> "
60 f"{escape_target} was followed (canary {CANARY!r} in the listing). A "
61 "daemon must never let a client switch off its symlink confinement; the "
62 "opt-out must be gated on !am_daemon, not bare insecure_links.")
63 print("daemon ignores peer-forwarded --insecure-links: confinement held")