Debian Codegraph @

testsuite/daemon-config-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon ancillary opens (motd / early-input / lock / --config rsyncd.conf)
3 # must not follow planted symlinks at attacker-controlled path components.
4 #
5 # Threat: admin-configured paths in
6 # rsyncd.conf or on the daemon's command line are operator-supplied, but the
7 # /tmp-class trust model applies (admin reasonably picks /tmp or another
8 # shared-parent path for one-off use; the attacker plants in that parent).
9 # Each of these opens is plain fopen()/open() without O_NOFOLLOW or path
10 # walk:
11 # clientserver.c:160 fopen(motd, "r")
12 # clientserver.c:269 fopen(early_input_file, "rb")
13 # connection.c:33 open(lock_file, O_RDWR|O_CREAT, 0600)
14 # params.c:583 fopen(rsyncd.conf, "r")
15 #
16 # This test exercises the MOTD case as the representative -- the leak signal
17 # is direct: when the planted symlink is followed, its target's bytes are
18 # read and sent to the connecting client as the message of the day. With the
19 # fix the open is refused; the daemon either sends no motd or errors, but
20 # the leaked content does not reach the client. The other three call sites
21 # (early-input, lock, --config) are covered by the SAME path-walk helper
22 # applied uniformly; their behaviour is not separately exercised here.
23 #
24 # Requires root for the cross-uid plant; skips otherwise.
25
26 import os
27 import pwd
28 import subprocess
29
30 from rsyncfns import (
31 FROMDIR, SCRATCHDIR, make_tree, rmtree, rsync_argv, start_test_daemon,
32 test_fail, test_skipped, write_daemon_conf,
33 )
34
35 DAEMON_PORT = 12901
36 MARKER = "AUDIT_MOTD_LEAK_MARKER_DO_NOT_DISCLOSE"
37
38
39 if os.geteuid() != 0:
40 test_skipped("requires root to plant a symlink owned by a non-self uid "
41 "(the attacker simulation)")
42
43 NOBODY_UID = None
44 for name in ('nobody', 'nfsnobody', 'daemon'):
45 try:
46 u = pwd.getpwnam(name).pw_uid
47 if u != 0 and u != os.geteuid():
48 NOBODY_UID = u
49 break
50 except KeyError:
51 continue
52 if NOBODY_UID is None:
53 test_skipped("no untrusted-uid user available for cross-uid plant")
54
55
56 # Daemon setup
57 src = FROMDIR
58 rmtree(src)
59 make_tree(src, depth=1)
60
61 base = SCRATCHDIR / 'daemoncfg'
62 rmtree(base)
63 base.mkdir()
64 plants = base / 'plants'
65 plants.mkdir()
66 modpath = base / 'mod'
67 modpath.mkdir()
68
69 # The victim file. The attacker plants a symlink at the motd path pointing
70 # here; the file's content is the unique marker. If the marker reaches the
71 # connecting client, the leak fired.
72 victim = plants / 'motd_victim'
73 victim.write_text(MARKER + "\n")
74 os.chmod(victim, 0o600)
75
76 motd_plant = plants / 'motd_plant'
77 os.symlink(victim, motd_plant)
78 os.lchown(motd_plant, NOBODY_UID, NOBODY_UID)
79
80 # Lock-file plant. The daemon uses 'lock file =' for max-connections gating
81 # (connection.c:33: open(fname, O_RDWR|O_CREAT, 0600)) -- only when
82 # max_connections > 0, so we set max_connections=5 below to force the open.
83 # A planted symlink at the lock-file path redirects the daemon's
84 # O_CREAT-open to an attacker-chosen path. We plant a symlink pointing at
85 # a victim-directory entry inside our scratch tree; if the daemon followed
86 # it, the file gets created at the planted target on first client connect.
87 lock_victim_dir = base / 'lock_victim'
88 lock_victim_dir.mkdir()
89 lock_plant = plants / 'lock_plant'
90 os.symlink(lock_victim_dir / 'leaked_lockfile', lock_plant)
91 os.lchown(lock_plant, NOBODY_UID, NOBODY_UID)
92
93 conf = write_daemon_conf(
94 [('mod', {'path': modpath, 'read only': 'yes',
95 'max connections': '5'})],
96 globals={'motd file': str(motd_plant),
97 'lock file': str(lock_plant)},
98 )
99 url = start_test_daemon(conf, DAEMON_PORT)
100
101 # Connect with the rsync client; the daemon will (or won't) send the motd
102 # content to the client during connection setup. Listing the module triggers
103 # motd, and -- since max_connections > 0 on `mod` -- ALSO triggers the lock-
104 # file open via claim_connection().
105 proc = subprocess.run(
106 rsync_argv(url + 'mod/'),
107 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
108
109 if MARKER in proc.stdout or MARKER in proc.stderr:
110 test_fail(
111 f"daemon followed a planted motd symlink (owned by uid {NOBODY_UID}): "
112 f"the marker {MARKER!r} from {victim} was read via the symlink and "
113 "sent to the connecting client as the message of the day. Fix: refuse "
114 "planted symlinks at the daemon motd open (and analogously for "
115 "early-input / lock / --config).")
116
117 # Did the daemon's claim_connection() follow the planted lock-file symlink
118 # and create a file at the attacker-chosen location?
119 leaked_lockfile = lock_victim_dir / 'leaked_lockfile'
120 if leaked_lockfile.exists():
121 test_fail(
122 f"daemon followed a planted lock-file symlink (owned by uid "
123 f"{NOBODY_UID}): {lock_plant} -> {leaked_lockfile} was traversed by "
124 f"claim_connection()'s open(O_CREAT) at connection.c:33, creating a "
125 f"root-owned file at the attacker-chosen path. Fix: refuse planted "
126 "symlinks at the daemon lock-file open.")