Debian Codegraph @

testsuite/daemon-strict-modes-matrix_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Pin the EXACT secrets-file permission contract enforced by `strict modes`.
3
4 The rsyncd.conf(5) `strict modes` entry says the secrets file "must not be
5 readable by any user ID other than the one that the rsync daemon is running
6 under". That is not what the code does. check_secret() (authenticate.c) only
7 rejects the file when:
8
9 (st.st_mode & 06) != 0 -> other-READ or other-WRITE bit set
10 OR (daemon running as root AND the file is not owned by root)
11
12 So group permissions are never consulted, and even an other-EXECUTE-only bit is
13 ignored. A group-readable secrets file is ACCEPTED, directly contradicting the
14 "any user ID other than" wording. This test measures accept/reject across a
15 mode sweep so the behaviour is unambiguous and the doc can be made to match it.
16
17 Auth runs inside the daemon protocol, so the default stdio-pipe transport is
18 fine (no TCP needed). The owner-mismatch row needs the daemon to run as root
19 plus an unprivileged uid to chown to, so it is gated on both.
20
21 Cross-version: the `& 06` test is long-standing, so running this with
22 `--rsync-bin=old_versions/rsync_3.2.7` is expected to reproduce the same grid --
23 i.e. the doc has been imprecise for many releases, this is not a regression.
24 Set REPORT_MODES=1 to dump the observed grid (via test_fail) for eyeballing.
25 """
26
27 import os
28 import subprocess
29
30 from rsyncfns import (
31 FROMDIR, SCRATCHDIR,
32 find_attacker_uid, make_tree, makepath, rmtree, rsync_argv,
33 start_test_daemon, test_fail, write_daemon_conf,
34 )
35
36 DAEMON_PORT = 12910
37
38 # Never let a rejected secrets file fall through to an interactive getpass()
39 # on /dev/tty (which would hang an automated run): give a wrong env password so
40 # the client always has something to send and never prompts.
41 os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong'
42
43 src = FROMDIR
44 rmtree(src)
45 make_tree(src, depth=2)
46
47 authdir = SCRATCHDIR / 'strictdest'
48 secrets = SCRATCHDIR / 'rsyncd.strict.secrets'
49 secrets.write_text('tuser:secretpass\n')
50 secrets.chmod(0o600)
51
52 # Two modules: strict modes on (default) and off, same secrets file.
53 conf = write_daemon_conf([
54 ('strict', {'path': str(authdir), 'read only': 'no',
55 'auth users': 'tuser', 'secrets file': str(secrets),
56 'strict modes': 'yes'}),
57 ('loose', {'path': str(authdir), 'read only': 'no',
58 'auth users': 'tuser', 'secrets file': str(secrets),
59 'strict modes': 'no'}),
60 ], name='strict-modes.conf')
61 url = start_test_daemon(conf, DAEMON_PORT)
62
63
64 def pwfile():
65 p = SCRATCHDIR / 'pw.strict'
66 p.write_text('secretpass\n')
67 p.chmod(0o600)
68 return p
69
70
71 PW = pwfile()
72
73
74 def accepted(module):
75 """Push with the correct password; True == the daemon honoured the secrets
76 file (auth succeeded), False == it ignored/rejected it (auth failed)."""
77 rmtree(authdir)
78 makepath(authdir)
79 userurl = url.replace('rsync://', 'rsync://tuser@', 1)
80 proc = subprocess.run(
81 rsync_argv('-a', f'--password-file={PW}', f'{src}/', f'{userurl}{module}/'),
82 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
83 stdin=subprocess.DEVNULL)
84 return proc.returncode in (0, 23)
85
86
87 # (mode, expected-accepted-under-strict-modes, human note)
88 MODE_GRID = [
89 (0o600, True, 'owner rw only'),
90 (0o400, True, 'owner r only'),
91 (0o640, True, 'group READ -> still accepted (group bits unchecked)'),
92 (0o660, True, 'group rw -> accepted'),
93 (0o610, True, 'group exec -> accepted'),
94 (0o601, True, 'other EXEC only -> accepted (& 06 ignores the x bit)'),
95 (0o604, False, 'other READ -> rejected'),
96 (0o602, False, 'other WRITE -> rejected'),
97 (0o606, False, 'other rw -> rejected'),
98 (0o644, False, 'world-readable -> rejected'),
99 (0o666, False, 'world rw -> rejected'),
100 ]
101
102 grid, mism = [], []
103 for mode, want, note in MODE_GRID:
104 secrets.chmod(mode)
105 got = accepted('strict')
106 grid.append(f"strict 0{mode:04o} accepted={int(got)} want={int(want)} {note}")
107 if got != want:
108 mism.append(f"strict modes, 0{mode:04o}: accepted={got}, expected={want} ({note})")
109
110 # strict modes = no: even a world-rw secrets file is honoured.
111 secrets.chmod(0o666)
112 got = accepted('loose')
113 grid.append(f"loose 00666 accepted={int(got)} want=1 strict modes=no skips the check")
114 if not got:
115 mism.append("strict modes=no still rejected a 0666 secrets file")
116
117 # Owner mismatch is only enforced when the daemon runs as root.
118 ATT = find_attacker_uid() if os.geteuid() == 0 else None
119 if ATT is not None:
120 secrets.chmod(0o600)
121 os.chown(secrets, ATT, ATT) # 0600 but NOT owned by root
122 got = accepted('strict')
123 grid.append(f"strict 00600 owner={ATT} accepted={int(got)} want=0 "
124 f"non-root owner rejected when daemon is root")
125 if got:
126 mism.append("strict modes accepted a non-root-owned secrets file under a root daemon")
127 os.chown(secrets, 0, 0)
128 else:
129 grid.append("strict owner-mismatch row SKIPPED (needs root daemon + unprivileged uid)")
130
131 secrets.chmod(0o600)
132
133 if os.environ.get('REPORT_MODES'):
134 test_fail("REPORT-MODES grid:\n " + "\n ".join(grid))
135 if mism:
136 test_fail("strict-modes contract deviated:\n " + "\n ".join(mism))
137
138 print("daemon-strict-modes-matrix: secrets-file accept/reject pinned -- only "
139 "other-read/other-write (st_mode & 06) and (under a root daemon) a "
140 "non-root owner are rejected; group bits and other-exec are NOT checked, "
141 "so a group-readable secrets file is accepted")