Debian Codegraph @

testsuite/daemon-module-chdir-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon module-root chdir under `use chroot = no` must not follow planted
3 # symlinks at attacker-controlled path components.
4 #
5 # Threat: when a daemon is
6 # configured with `use chroot = no`, it calls change_dir(module_chdir)
7 # where module_chdir is the absolute path from rsyncd.conf `path =`.
8 # change_dir's absolute-path branch (util1.c:1193) is plain chdir() with
9 # NO symlink confinement. If the admin's configured path includes a
10 # component the attacker controls (e.g. `path = /home/$user/share`, or any
11 # /srv-style path under a non-root-owned parent), an attacker-planted
12 # symlink redirects the daemon's CWD out of the configured module before
13 # any transfer begins.
14 #
15 # This is the daemon-side analog of the symlink-race-dest XFAIL (the
16 # non-daemon receiver chdir); the daemon path has a clear fix here without
17 # the chdir-of-named-dest scope concern.
18 #
19 # Leak signal: configure the daemon with `path = $plant` where $plant is
20 # a planted symlink to an "escape" directory containing a unique-named
21 # canary file that the legitimate module doesn't have. List the module
22 # from a client; if the canary appears in the listing, the daemon's chdir
23 # followed the planted symlink and is serving files from the escape
24 # target. With the fix the chdir is refused and the daemon errors on
25 # connection.
26 #
27 # Requires root for the cross-uid plant; skips otherwise.
28
29 import os
30 import pwd
31 import subprocess
32
33 from rsyncfns import (
34 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail,
35 test_skipped, write_daemon_conf,
36 )
37
38 DAEMON_PORT = 12900
39 CANARY = "audit_escape_canary"
40
41
42 if os.geteuid() != 0:
43 test_skipped("requires root to plant a symlink owned by a non-self uid "
44 "(the attacker simulation)")
45
46 NOBODY_UID = None
47 for name in ('nobody', 'nfsnobody', 'daemon'):
48 try:
49 u = pwd.getpwnam(name).pw_uid
50 if u != 0 and u != os.geteuid():
51 NOBODY_UID = u
52 break
53 except KeyError:
54 continue
55 if NOBODY_UID is None:
56 test_skipped("no untrusted-uid user available for cross-uid plant")
57
58
59 base = SCRATCHDIR / 'modchdir'
60 rmtree(base)
61 base.mkdir()
62
63 # The "escape" directory the attacker wants the daemon to serve from.
64 # Owned by us, with a canary file the legitimate module doesn't have.
65 escape_target = base / 'escape_target'
66 escape_target.mkdir()
67 (escape_target / CANARY).write_text("you-should-not-see-this\n")
68
69 # The planted symlink: appears at the daemon-configured 'path =' and
70 # redirects to the escape target. The symlink itself is owned by NOBODY_UID
71 # (the attacker), so the fix's owned-by-trusted-uid path walk refuses it.
72 plant = base / 'plant'
73 os.symlink(escape_target, plant)
74 os.lchown(plant, NOBODY_UID, NOBODY_UID)
75
76 # Daemon config: `path = $plant`. With `use chroot = no` (the default in
77 # write_daemon_conf) the daemon will chdir into the resolved path before
78 # serving the module.
79 conf = write_daemon_conf(
80 [('mod', {'path': plant, 'read only': 'yes'})],
81 )
82 url = start_test_daemon(conf, DAEMON_PORT)
83
84 # Client lists the module's contents (rsync $url/mod/). Without the fix the
85 # daemon's chdir followed the planted symlink and is serving from
86 # escape_target -> canary appears in the listing.
87 proc = subprocess.run(
88 rsync_argv(f'{url}mod/'),
89 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
90
91 if CANARY in proc.stdout:
92 test_fail(
93 f"daemon module-root chdir followed a planted parent-component "
94 f"symlink (owned by uid {NOBODY_UID}): {plant} -> {escape_target} "
95 f"was traversed by the plain chdir() in change_dir(), and the daemon "
96 f"is now serving files from the escape target (canary "
97 f"{CANARY!r} appeared in the module listing). Fix: per-component "
98 "owned-by-trusted-uid path walk in change_dir's absolute-path branch "
99 "under am_daemon && !am_chrooted.")