Debian Codegraph @

testsuite/rrsync-merge-file-confine_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """An rrsync restricted dir must bound the merge files the peer can name.
3
4 Filter rules travel over the protocol rather than in the argv rrsync validates,
5 so a dir-merge rule can name a file outside the restricted dir and have the
6 server read it in as rules. Redacting the resulting diagnostics is not enough:
7 an exclude-only merge (the "-" modifier) turns every line into a pattern, so
8 nothing fails to parse and the client reads the file's contents off its own
9 file list instead -- no error, no verbosity, and on a pull no --delete either.
10 """
11
12 import os
13 import shlex
14 import signal
15 import subprocess
16 import sys
17
18 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
19 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
20 if '--shell' in sys.argv:
21 i = sys.argv.index('--shell') + 2
22 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])}
23 signal.signal(signal.SIGUSR1, signal.SIG_DFL)
24 signal.signal(signal.SIGUSR2, signal.SIG_DFL)
25 wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT']
26 os.execve(wrapper, [wrapper, '-ro', '-no-lock', root], env)
27
28 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg
29
30 base = SCRATCHDIR / 'rrsync-merge-file-confine'
31 rmtree(base)
32 root = base / 'root'
33 sub = root / 'sub'
34 dest = base / 'dest'
35 makepath(sub, dest)
36
37 # The secret sits BESIDE the restricted dir, so "../" reaches it from the root.
38 # Its line is the name of a file inside the root: if the server reads it as an
39 # exclude list, that file silently vanishes from the transfer and the client has
40 # confirmed the content without ever seeing it.
41 (base / 'outside-secret').write_text('secretword\n')
42 (root / 'secretword').write_bytes(b'X')
43 (root / 'keep').write_bytes(b'X')
44 # An ordinary in-tree merge file, for the control below.
45 (sub / 'intree-dropped').write_bytes(b'X')
46 (sub / 'intree-kept').write_bytes(b'X')
47 (sub / '.rsync-filter').write_text('intree-dropped\n')
48
49 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
50 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
51 # has to be wrapped before patched_rrsync() sees it.
52 shim = base / 'rsync-shim'
53 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
54 shim.chmod(0o755)
55 wrapper = patched_rrsync(base, rsync_path=str(shim))
56 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
57 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
58
59
60 def pull(filt, into):
61 into.mkdir(parents=True, exist_ok=True)
62 return subprocess.run(
63 rsync_argv('-r', f'--filter={filt}', '-e', rsh, 'ignored:.', f'{into}/'),
64 env=env, capture_output=True, text=True, timeout=20)
65
66
67 # The escape: an exclude-only dir-merge naming a file outside the restricted
68 # dir. No --delete and no -v are involved -- the answer is the file list.
69 esc = dest / 'escape'
70 got = pull(':-s ../outside-secret', esc)
71 assert got.returncode == 0, (
72 'confining the merge open must not cost the transfer: refusing outright '
73 f'would hand the peer a denial of service. rc={got.returncode}, '
74 f'stderr={got.stderr!r}')
75 assert (esc / 'secretword').exists(), (
76 'the server read a merge file outside the restricted dir: "secretword" was '
77 f'excluded, so its presence in that file is confirmed to the client. '
78 f'rc={got.returncode}, stdout={got.stdout!r}, stderr={got.stderr!r}')
79 assert 'secretword' not in got.stderr, (
80 f'the out-of-root merge file\'s text reached the peer: stderr={got.stderr!r}')
81
82 # Control: an ordinary in-tree .rsync-filter must still be read and obeyed.
83 # Without this, refusing every merge file would pass the assertion above.
84 ctl = dest / 'control'
85 got = pull(':-s .rsync-filter', ctl)
86 assert got.returncode == 0, (
87 f'the in-tree dir-merge failed the transfer: rc={got.returncode}, '
88 f'stderr={got.stderr!r}')
89 assert (ctl / 'sub' / 'intree-kept').exists(), (
90 f'the in-tree dir-merge broke the transfer: rc={got.returncode}, '
91 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
92 assert not (ctl / 'sub' / 'intree-dropped').exists(), (
93 'the in-tree .rsync-filter was not applied, so this control does not show '
94 f'that merge files still work: stdout={got.stdout!r}, stderr={got.stderr!r}')
95
96 # --confine-root direct, no wrapper: a source argument reached through a symlink
97 # makes rsync's tracked cwd deeper than the kernel's, so a merge file's own
98 # relative symlink resolves from one directory while the confinement check walks
99 # from another. Seeded lexically, a ".." that really escapes looks like it
100 # landed back inside the root. rrsync happens to reject the argument shapes
101 # that reach this, so it is driven directly rather than through the wrapper.
102 alias_root = base / 'aliased'
103 makepath(alias_root)
104 (base / 'aliased-secret').write_text('aliased-word\n')
105 (alias_root / 'aliased-word').write_bytes(b'X')
106 (alias_root / 'plain').write_bytes(b'X')
107 os.symlink('.', alias_root / 'self-alias')
108 os.symlink('../aliased-secret', alias_root / 'outside-link')
109 (alias_root / 'intree-list').write_text('plain\n')
110
111
112 def confined_pull(filter_text, into):
113 (alias_root / '.rsync-filter').write_text(filter_text + '\n')
114 rmtree(into)
115 into.mkdir(parents=True, exist_ok=True)
116 return subprocess.run(
117 rsync_argv('-r', f'--confine-root={alias_root}',
118 '--filter=:s .rsync-filter',
119 f'{alias_root}/self-alias/', f'{into}/'),
120 capture_output=True, text=True, timeout=20)
121
122
123 esc2 = dest / 'aliased-escape'
124 got = confined_pull('merge,- outside-link', esc2)
125 assert not (got.returncode == 0 and not (esc2 / 'aliased-word').exists()), (
126 'a merge file outside the root was read through a symlinked source arg, so '
127 f'"aliased-word" was excluded: rc={got.returncode}, stderr={got.stderr!r}')
128
129 # Control: the same symlinked source arg with an IN-TREE merge target must still
130 # work, or the assertion above would also pass if confinement simply broke every
131 # transfer that reaches a directory through a symlink.
132 ctl2 = dest / 'aliased-control'
133 got = confined_pull('merge,- intree-list', ctl2)
134 assert got.returncode == 0 and (ctl2 / 'aliased-word').exists(), (
135 'a symlinked source arg with an in-tree merge file broke the transfer: '
136 f'rc={got.returncode}, stderr={got.stderr!r}')
137 assert not (ctl2 / 'plain').exists(), (
138 'the in-tree merge file was not applied through the symlinked source arg, '
139 f'so the escape check above proves nothing: stderr={got.stderr!r}')