Debian Codegraph @

testsuite/daemon-secrets-file-symlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon 'secrets file = PATH' must not follow planted symlinks at attacker-
3 # controlled path components.
4 #
5 # Companion to password-file-symlink (which covers the CLIENT --password-file);
6 # this covers the DAEMON-side 'secrets file' open in authenticate.c. Same path-
7 # walk helper (open_no_attacker_symlinks), exercised directly here because
8 # the daemon secrets read is a distinct call site with its own leak signal.
9 #
10 # Threat: the daemon opens 'secrets file' to authenticate a client. A symlink
11 # planted at that path (or a parent component) by another uid redirects the read
12 # to an attacker-chosen file. The strict-modes fstat() runs on the *target*
13 # inode, so a symlink to e.g. /etc/shadow (root-owned, not other-readable) would
14 # pass the owner/mode check and the daemon would auth against the target's
15 # contents.
16 #
17 # Leak signal: the planted symlink points at a victim secrets file (root:0600)
18 # whose contents ARE a known user:password. If the daemon follows it, a client
19 # authenticating with that password SUCCEEDS -> leak observed. With the fix the
20 # planted symlink is refused ("no secrets file"), so auth fails.
21 #
22 # Both leaf and parent-component variants are exercised (the parent case is the
23 # /tmp/somedir/ class that a leaf-only O_NOFOLLOW does not defend).
24 #
25 # Requires root (to lchown the planted symlink to a non-self uid -- the attacker
26 # simulation); skips otherwise.
27
28 import os
29 import pwd
30 import subprocess
31
32 from rsyncfns import (
33 FROMDIR, SCRATCHDIR, make_tree, makepath, rmtree, rsync_argv,
34 start_test_daemon, test_fail, test_skipped, write_daemon_conf,
35 )
36
37 DAEMON_PORT = 12903
38 USER = 'tuser'
39 CORRECT_PASSWORD = 'correctpass'
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 # Prevent rsync's interactive getpass() fallback if no password is read.
59 os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong'
60
61 src = FROMDIR
62 rmtree(src)
63 make_tree(src, depth=1)
64
65 base = SCRATCHDIR / 'secretsplant'
66 rmtree(base)
67 base.mkdir()
68
69 # The victim secrets file: root-owned, 0600, contents == a valid credential.
70 # A daemon that follows a planted symlink to this file authenticates the client.
71 victim = base / 'victim_secrets'
72 victim.write_text(f'{USER}:{CORRECT_PASSWORD}\n')
73 os.chmod(victim, 0o600)
74 os.chown(victim, 0, 0)
75
76 # Leaf plant: a symlink AT the secrets-file path, owned by the attacker uid.
77 leaf_plant = base / 'secrets_leaf'
78 os.symlink(victim, leaf_plant)
79 os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID)
80
81 # Parent-component plant: secrets file = <parent_link>/victim_in_target, where
82 # parent_link is an attacker-owned symlink to a real directory holding the
83 # victim secrets. A leaf-only O_NOFOLLOW does not defend this; only the per-
84 # component owner-checked walk does.
85 parent_real = base / 'parent_real'
86 parent_real.mkdir()
87 victim_in_target = parent_real / 'victim_in_target'
88 victim_in_target.write_text(f'{USER}:{CORRECT_PASSWORD}\n')
89 os.chmod(victim_in_target, 0o600)
90 os.chown(victim_in_target, 0, 0)
91 parent_plant = base / 'parent_link'
92 os.symlink(parent_real, parent_plant)
93 os.lchown(parent_plant, NOBODY_UID, NOBODY_UID)
94
95 authreal = base / 'authreal'
96 authleaf = base / 'authleaf'
97 authparent = base / 'authparent'
98 makepath(authreal, authleaf, authparent)
99
100 conf = write_daemon_conf([
101 # Sanity module: a real (non-symlinked) root:0600 secrets file authenticates.
102 ('authreal', {'path': authreal, 'read only': 'no',
103 'auth users': USER, 'secrets file': victim}),
104 # Leak modules: the secrets file is reached through an attacker-owned symlink.
105 ('authleaf', {'path': authleaf, 'read only': 'no',
106 'auth users': USER, 'secrets file': leaf_plant}),
107 ('authparent', {'path': authparent, 'read only': 'no',
108 'auth users': USER,
109 'secrets file': parent_plant / 'victim_in_target'}),
110 ])
111 url = start_test_daemon(conf, DAEMON_PORT)
112 userurl = url.replace('rsync://', f'rsync://{USER}@', 1)
113
114 pwfile = SCRATCHDIR / 'pw.ok'
115 pwfile.write_text(CORRECT_PASSWORD + '\n')
116 pwfile.chmod(0o600)
117
118
119 def push(module, dest):
120 rmtree(dest)
121 makepath(dest)
122 return subprocess.run(
123 rsync_argv('-a', f'--password-file={pwfile}',
124 f'{src}/', f'{userurl}{module}/'),
125 stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True)
126
127
128 # Sanity: the real secrets file authenticates (demonstrates the machinery;
129 # success against a planted symlink is then the leak signal).
130 sanity = push('authreal', authreal)
131 if sanity.returncode != 0:
132 test_fail(
133 "sanity check failed: a non-symlinked root:0600 secrets file with the "
134 f"correct credential could not authenticate. stderr: {sanity.stderr!r}")
135
136 # Leaf plant: success means the daemon followed the attacker-owned symlink.
137 leaf = push('authleaf', authleaf)
138 if leaf.returncode == 0:
139 test_fail(
140 f"daemon 'secrets file' followed a planted LEAF symlink (owned by uid "
141 f"{NOBODY_UID}): {leaf_plant} -> {victim} was read as the secrets DB and "
142 f"the client authenticated -> transfer succeeded. Fix: refuse planted "
143 f"symlinks at the daemon secrets-file open.")
144
145 # Parent-component plant: success means the daemon traversed the attacker-owned
146 # parent symlink.
147 parent = push('authparent', authparent)
148 if parent.returncode == 0:
149 test_fail(
150 f"daemon 'secrets file' followed a planted PARENT-component symlink "
151 f"(owned by uid {NOBODY_UID}): {parent_plant} -> {parent_real} was "
152 f"traversed and victim_in_target read as the secrets DB; the client "
153 f"authenticated -> transfer succeeded. This is the /tmp/somedir/-class "
154 f"parent-flip case. Fix: per-component path walk refusing parent "
155 f"symlinks owned by untrusted uids.")
156
157 print("daemon-secrets-file-symlink: leaf + parent plants refused")