Debian Codegraph @

testsuite/rrsync-pull-delivers-content_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A pull through rrsync must deliver the file's CONTENT.
3
4 rrsync inode-pins each validated transfer argument and rewrites the argument to
5 /proc/self/fd/N so the exec'd rsync re-resolves it to the pinned inode. That is
6 right for the receiver, which plain-open()s its destination -- but the sender
7 never plain-opens the transfer argument: send_file_list() lstat()s it, and
8 lstat() of a /proc/self/fd magic link always reports S_IFLNK. So the sender
9 described the argument as a symlink instead of sending the file.
10
11 The observable damage depends on how much the receiver checks. It has been seen
12 both as a delivered dangling symlink holding the server's absolute in-tree path
13 and no content, and -- with the current receiver's file-list name check -- as an
14 outright refusal:
15
16 ERROR: rejecting unrequested file-list name: 4
17
18 Either way the canonical `rsync -a host:file dest/` through a restricted account
19 does not deliver the file, so this asserts the user-visible outcome (content
20 arrives, as a regular file) rather than any one symptom.
21
22 The push case is checked too: the fix must not be achievable by abandoning the
23 receiver-side pinning that the same code does for real.
24 """
25
26 import os
27 import shlex
28 import subprocess
29
30 from rsyncfns import (
31 RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, test_fail, rsync_path_arg,
32 )
33
34 PULL_DATA = 'REAL-FILE-CONTENT\n'
35 PUSH_DATA = 'PUSHED-FILE-CONTENT\n'
36
37 base = SCRATCHDIR / 'rrsync-pull-content'
38 rmtree(base)
39 restricted = base / 'restricted'
40 dest = base / 'dest'
41 src = base / 'src'
42 makepath(restricted, dest, src)
43
44 (restricted / 'f1').write_text(PULL_DATA)
45 (src / 'up1').write_text(PUSH_DATA)
46 # rrsync refuses a --sender arg that does not already exist, so the push target
47 # directory has to be there before the transfer.
48 makepath(restricted / 'inbox')
49
50 # $RSYNC may carry a wrapper (valgrind, --protocol=N); rrsync wants a single
51 # executable, so hand it a shim that re-expands the whole command line.
52 shim = base / 'rsync-shim'
53 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
54 shim.chmod(0o755)
55
56 rrsync = patched_rrsync(base, rsync_path=str(shim))
57
58 # Stand in for ssh: drop the host argument and pass the rest to rrsync exactly
59 # as sshd would, through SSH_ORIGINAL_COMMAND.
60 rsh = base / 'fake-rsh'
61 rsh.write_text(
62 '#!/bin/sh\n'
63 'shift\n'
64 'SSH_ORIGINAL_COMMAND="$*"\n'
65 'export SSH_ORIGINAL_COMMAND\n'
66 'exec %s %s\n' % (shlex.quote(str(rrsync)), shlex.quote(str(restricted))))
67 rsh.chmod(0o755)
68
69
70 def transfer(*args):
71 return subprocess.run(rsync_argv('-a', '-e', str(rsh), *args),
72 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
73 text=True)
74
75
76 # --- the regression: a plain pull of a file ---------------------------------
77 proc = transfer('dummy:f1', str(dest) + '/')
78 ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:300]!r}'
79 got = dest / 'f1'
80
81 if got.is_symlink():
82 test_fail(f'pull delivered a symlink instead of the file: '
83 f'{os.readlink(got)!r} ({ctx})')
84 if not got.exists():
85 test_fail(f'pull delivered nothing ({ctx})')
86 if not got.is_file():
87 test_fail(f'pull delivered a non-regular file ({ctx})')
88 if got.read_text() != PULL_DATA:
89 test_fail(f'pull delivered the wrong content: {got.read_text()!r} ({ctx})')
90 if proc.returncode != 0:
91 test_fail(f'pull delivered the content but failed ({ctx})')
92
93 # --- control: the receiver direction still works ----------------------------
94 # Without this, "stop handing the sender a pinned fd" could be satisfied by
95 # dropping the pinning altogether, which is the hardening this must preserve.
96 proc = transfer(str(src) + '/up1', 'dummy:inbox/')
97 ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:300]!r}'
98 landed = restricted / 'inbox' / 'up1'
99
100 if proc.returncode != 0:
101 test_fail(f'push through rrsync failed ({ctx})')
102 if not landed.is_file() or landed.read_text() != PUSH_DATA:
103 test_fail(f'push did not deliver the content ({ctx})')
104
105 print('rrsync pull delivers file content; push still works')