Debian Codegraph @

testsuite/password-file-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # --password-file=PATH must not follow planted symlinks at attacker-
3 # controlled path components.
4 #
5 # Threat: a privileged client runs
6 # rsync with --password-file=PATH where PATH lives in an attacker-
7 # influenceable directory. A planted symlink redirects the read to an
8 # arbitrary file (e.g. /etc/shadow); rsync reads the symlinked content and
9 # sends it as the daemon auth response. A malicious daemon (or simply
10 # error/timing channels) observes the leaked content.
11 #
12 # The leak signal in this test is direct: the daemon is configured with a
13 # known correct password, and the planted symlink points at a victim file
14 # whose content IS that correct password. With the vulnerability rsync
15 # reads the victim, sends the correct password, and the daemon accepts ->
16 # transfer SUCCEEDS (returncode 0) -> LEAK observed. With the fix the
17 # planted symlink is refused, rsync sends no password, and auth fails ->
18 # transfer fails (returncode != 0).
19 #
20 # Both leaf and parent-component variants exercised (the parent case is
21 # the /tmp/somedir/ class that plain O_NOFOLLOW does NOT defend).
22 #
23 # Requires root (to lchown the planted symlink to a non-self uid -- the
24 # attacker simulation); skips otherwise.
25
26 import os
27 import pwd
28 import subprocess
29
30 from rsyncfns import (
31 FROMDIR, SCRATCHDIR, make_tree, makepath, rmtree, rsync_argv,
32 start_test_daemon, test_fail, test_skipped, write_daemon_conf,
33 )
34
35 DAEMON_PORT = 12902
36 USER = 'tuser'
37 CORRECT_PASSWORD = 'correctpass'
38
39
40 if os.geteuid() != 0:
41 test_skipped("requires root to plant a symlink owned by a non-self uid "
42 "(the attacker simulation)")
43
44 NOBODY_UID = None
45 for name in ('nobody', 'nfsnobody', 'daemon'):
46 try:
47 u = pwd.getpwnam(name).pw_uid
48 if u != 0 and u != os.geteuid():
49 NOBODY_UID = u
50 break
51 except KeyError:
52 continue
53 if NOBODY_UID is None:
54 test_skipped("no untrusted-uid user available for cross-uid plant")
55
56 # Match daemon-auth_test.py: prevent rsync's interactive getpass() fallback
57 # if no password is read.
58 os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong'
59
60 # Daemon setup
61 src = FROMDIR
62 rmtree(src)
63 make_tree(src, depth=1)
64
65 authdir = SCRATCHDIR / 'authdest'
66 secrets = SCRATCHDIR / 'rsyncd.secrets'
67 secrets.write_text(f'{USER}:{CORRECT_PASSWORD}\n')
68 secrets.chmod(0o600)
69
70 conf = write_daemon_conf([
71 ('auth', {'path': authdir, 'read only': 'no',
72 'auth users': USER, 'secrets file': secrets}),
73 ])
74 url = start_test_daemon(conf, DAEMON_PORT)
75 userurl = url.replace('rsync://', f'rsync://{USER}@', 1)
76
77 # Victim file the attacker wants rsync to read. Owned by us, mode 0600.
78 # Crucially: content == the correct daemon password, so a SUCCESSFUL
79 # transfer is the leak signal.
80 victim = SCRATCHDIR / 'victim_password'
81 victim.write_text(CORRECT_PASSWORD + '\n')
82 victim.chmod(0o600)
83
84
85 def push_with_pwfile(pwfile_path):
86 """Run an rsync push via the daemon with --password-file=pwfile_path."""
87 rmtree(authdir)
88 makepath(authdir)
89 return subprocess.run(
90 rsync_argv('-a', f'--password-file={pwfile_path}',
91 f'{src}/', f'{userurl}auth/'),
92 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
93
94
95 # ---------------------------------------------------------------------------
96 # Sanity: a real password file with the correct password authenticates.
97 # Demonstrates the test machinery: success == leak when we later use a planted
98 # symlink that resolves to this same content.
99 # ---------------------------------------------------------------------------
100 real_pwfile = SCRATCHDIR / 'real_pwfile'
101 real_pwfile.write_text(CORRECT_PASSWORD + '\n')
102 real_pwfile.chmod(0o600)
103
104 sanity = push_with_pwfile(real_pwfile)
105 if sanity.returncode != 0:
106 test_fail(
107 "sanity check failed: a non-symlinked password file with the correct "
108 f"password could not authenticate. stderr: {sanity.stderr!r}")
109
110
111 # ---------------------------------------------------------------------------
112 # LEAF plant.
113 # ---------------------------------------------------------------------------
114 plants = SCRATCHDIR / 'plants'
115 rmtree(plants); plants.mkdir()
116 leaf_plant = plants / 'pwfile_leaf'
117 os.symlink(victim, leaf_plant)
118 os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID)
119
120 leaf = push_with_pwfile(leaf_plant)
121 if leaf.returncode == 0:
122 test_fail(
123 f"--password-file followed a planted LEAF symlink (owned by uid "
124 f"{NOBODY_UID}): rsync read {victim} via the symlink and sent its "
125 f"content as the auth password; the daemon accepted -> transfer "
126 f"succeeded. This is a content-disclosure primitive (the leaked "
127 f"bytes are sent on the wire). Fix: refuse planted symlinks at "
128 f"--password-file.")
129
130
131 # ---------------------------------------------------------------------------
132 # PARENT-component plant.
133 # ---------------------------------------------------------------------------
134 real_target = plants / 'parent_real_target'
135 real_target.mkdir()
136 (real_target / 'victim_in_target').write_text(CORRECT_PASSWORD + '\n')
137 os.chmod(real_target / 'victim_in_target', 0o600)
138
139 parent_plant = plants / 'parent_link'
140 os.symlink(real_target, parent_plant)
141 os.lchown(parent_plant, NOBODY_UID, NOBODY_UID)
142
143 # --password-file=$parent_plant/victim_in_target. Parent is the symlink; the
144 # kernel resolves the parent and reads victim_in_target inside. Plain O_NOFOLLOW
145 # does NOT defend (leaf-only); only the path walk with parent-symlink
146 # ownership checks defends here.
147 parent = push_with_pwfile(parent_plant / 'victim_in_target')
148 if parent.returncode == 0:
149 test_fail(
150 f"--password-file followed a planted PARENT-component symlink (owned "
151 f"by uid {NOBODY_UID}): {parent_plant} -> {real_target} was traversed "
152 f"and rsync read victim_in_target via the planted parent; the daemon "
153 f"accepted the leaked password -> transfer succeeded. This is the "
154 "/tmp/somedir/-class parent-flip case. Fix: per-component path walk "
155 "refusing parent symlinks owned by untrusted uids.")