Debian Codegraph @

testsuite/rrsync-alt-dest-inband-pivot_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """An in-band symlink at an alt-dest name must not read outside the restricted dir.
3
4 --link-dest/--compare-dest/--copy-dest name a basis directory rsync only reads
5 through. A missing one is the ordinary first-run case, so it cannot simply be
6 refused -- but leaving the name for rsync to resolve lets the same transfer
7 plant a symlink there and have the basis lookup read out of the tree. With
8 --copy-dest a matching basis file is copied into the destination, so the
9 out-of-tree content lands where it can be read back.
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 index = sys.argv.index('--shell') + 2
22 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[index:])}
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, '-wo', root], env)
27
28 from rsyncfns import (RSYNC, SCRATCHDIR, makepath, patched_rrsync,
29 proc_self_fd_pins, rmtree, rsync_argv, rsync_path_arg,
30 )
31
32 T = 1234567890
33
34 base = SCRATCHDIR / 'rrsync-alt-dest-inband-pivot'
35 rmtree(base)
36 src = base / 'src'
37 root = base / 'restricted'
38 outside = base / 'outside'
39 makepath(src, root, outside)
40
41 # Same length and mtime as the source file, so rsync's quick check calls the
42 # basis a match and --copy-dest copies it instead of transferring.
43 (outside / 'f0').write_bytes(b'ADMIN-SECRET\n')
44 (src / 'f0').write_bytes(b'CLIENT-DATA0\n')
45 # The pivot has to be INSTALLED before the generator does f0's basis lookup,
46 # and the generator runs ahead of the receiver -- sorting 'basis' first is not
47 # enough on its own, and a run where the generator wins would pass vacuously.
48 # Put a few thousand files in between: the receiver has only the symlink to
49 # write while the generator has to work through all of them, so by the time it
50 # reaches f0 the symlink is long since in place. The assertion after the run
51 # checks that it really was, rather than trusting the ordering.
52 os.symlink('../outside', src / 'basis')
53 filler = src / 'c'
54 filler.mkdir()
55 for i in range(3000):
56 (filler / ('%04d' % i)).write_bytes(b'.')
57 os.utime(src / 'f0', (T, T))
58 os.utime(outside / 'f0', (T, T))
59
60 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
61 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
62 # has to be wrapped before patched_rrsync() sees it.
63 shim = base / 'rsync-shim'
64 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
65 shim.chmod(0o755)
66 wrapper = patched_rrsync(base, rsync_path=str(shim))
67 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
68 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
69
70 got = subprocess.run(
71 rsync_argv('-rlt', '--copy-dest=basis', '-e', rsh, f'{src}/', 'ignored:'),
72 env=env, capture_output=True, text=True, timeout=60)
73
74 landed = (root / 'f0').read_bytes() if (root / 'f0').exists() else None
75 pivot = os.readlink(root / 'basis') if (root / 'basis').is_symlink() else None
76 print(f'rc={got.returncode}, landed={landed!r}, pivot={pivot!r}, '
77 f'stderr={got.stderr!r}', flush=True)
78 assert landed != b'ADMIN-SECRET\n', (
79 'rrsync let a --copy-dest symlink created by the same transfer read a file '
80 'outside the restricted directory and deliver it to the client')
81
82 if proc_self_fd_pins():
83 # rrsync stands in an empty unlinked directory: the basis lookup finds
84 # nothing, so the file transfers normally and first-run alt-dest keeps
85 # working. That it SUCCEEDS is the point -- refusing would have been the
86 # easy answer and would break every first-run --link-dest.
87 assert pivot == '../outside', (
88 'the transfer did not install the pivot symlink, so nothing was in '
89 f'place for the basis lookup to follow and this run proves nothing: '
90 f'pivot={pivot!r}')
91 assert got.returncode == 0, (
92 f'the transfer failed instead of finding an empty basis: '
93 f'rc={got.returncode}, stderr={got.stderr!r}')
94 assert landed == b'CLIENT-DATA0\n', (
95 f'the source file did not transfer: landed={landed!r}')
96 else:
97 # Without the /proc/self/fd primitive there is no way to hand rsync an
98 # inode instead of a name, so the option is refused BEFORE rsync runs --
99 # which is why there is no pivot to find here.
100 assert got.returncode != 0 and 'receiver option path does not exist' in got.stderr, (
101 f'a missing alt-dest was neither pinned nor refused: '
102 f'rc={got.returncode}, stderr={got.stderr!r}')
103 assert pivot is None and landed is None, (
104 f'the refusal did not stop the transfer: pivot={pivot!r}, '
105 f'landed={landed!r}')
106
107 # A basis directory that really exists must still be usable as one.
108 makepath(root / 'real-basis')
109 (root / 'real-basis' / 'f1').write_bytes(b'BASIS-MATCH\n')
110 (src / 'f1').write_bytes(b'BASIS-MATCH\n')
111 os.utime(src / 'f1', (T, T))
112 os.utime(root / 'real-basis' / 'f1', (T, T))
113 ok = subprocess.run(
114 rsync_argv('-rlt', '--copy-dest=real-basis', '-e', rsh,
115 f'{src}/f1', 'ignored:'),
116 env=env, capture_output=True, text=True, timeout=20)
117 assert ok.returncode == 0 and (root / 'f1').read_bytes() == b'BASIS-MATCH\n', (
118 f'an existing --copy-dest basis stopped working: rc={ok.returncode}, '
119 f'stderr={ok.stderr!r}')
120
121 # --compare-dest takes the same route and must be covered too: a match there
122 # makes rsync skip the file entirely rather than copy it, so the tell is the
123 # file going MISSING from the destination.
124 rmtree(root)
125 makepath(root)
126 got = subprocess.run(
127 rsync_argv('-rlt', '--compare-dest=basis', '-e', rsh, f'{src}/', 'ignored:'),
128 env=env, capture_output=True, text=True, timeout=60)
129 landed = (root / 'f0').read_bytes() if (root / 'f0').exists() else None
130 pivot = os.readlink(root / 'basis') if (root / 'basis').is_symlink() else None
131 if proc_self_fd_pins():
132 assert pivot == '../outside' and got.returncode == 0, (
133 'the --compare-dest run did not install the pivot or did not complete, '
134 f'so it proves nothing: pivot={pivot!r}, rc={got.returncode}, '
135 f'stderr={got.stderr!r}')
136 assert landed == b'CLIENT-DATA0\n', (
137 'the out-of-tree basis was consulted through the --compare-dest '
138 f'symlink, so rsync skipped transferring the file: landed={landed!r}, '
139 f'stderr={got.stderr!r}')
140 else:
141 assert got.returncode != 0 and 'receiver option path does not exist' in got.stderr, (
142 f'a missing --compare-dest was neither pinned nor refused: '
143 f'rc={got.returncode}, stderr={got.stderr!r}')
144 assert pivot is None, f'the refusal did not stop the transfer: pivot={pivot!r}'