Debian Codegraph @

testsuite/rrsync-sender-leaf-flip_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A raced LAST component must not leak outside content through an rrsync pull.
3
4 rrsync inode-pins the components above a sender argument's last one, but not
5 always the last one itself: rsync lstat()s a source argument, and lstat of a
6 /proc/self/fd magic link is always S_IFLNK, so handing it the leaf's pin makes
7 the sender describe the argument as a symlink and send no data at all (see
8 rrsync-pull-delivers-content).
9
10 What keeps that safe is rsync's own behaviour rather than the wrapper's:
11
12 * a symlink at a plain source argument is transmitted AS a symlink -- the
13 sender does not read through it;
14 * the options that would change that (-L, -k, --copy-unsafe-links) are
15 disabled by rrsync for a restricted dir, and --insecure-links is not in its
16 allowlist at all;
17 * the sender's own opens are O_NOFOLLOW, and directories go through
18 secure_opendir(), so a flip landing between rsync's lstat() and its open is
19 refused by the kernel.
20
21 So this drives the real binary and asserts the property that actually holds --
22 no outside CONTENT reaches the client -- rather than requiring a particular
23 symptom from a race that may or may not be won on any given run.
24
25 Note what is NOT claimed: an outside symlink's target string can still be
26 disclosed, since transmitting the symlink is the correct behaviour for the
27 shape. rrsync's -munge blunts that. Under --relative the argument is anchored
28 higher, so the intermediate components are exposed to the same window; that is
29 a known limitation of the wrapper, recorded in the plan notes, not something
30 this test asserts away.
31 """
32
33 import os
34 import shlex
35 import subprocess
36 import time
37
38 from rsyncfns import (
39 race_budget, RSYNC, SCRATCHDIR, makepath, patched_rrsync,
40 proc_self_fd_pins, rmtree, rsync_argv, start_path_flipper, stop_flipper,
41 test_fail, test_skipped, rsync_path_arg,
42 )
43
44 if not proc_self_fd_pins():
45 test_skipped("rrsync's realpath-vs-exec inode-pin needs /proc/self/fd "
46 "(Linux); unhardened fallback elsewhere by design")
47
48 MARKER = 'AUDIT_RRSYNC_LEAF_FLIP_MARKER_DO_NOT_DISCLOSE'
49
50 base = SCRATCHDIR / 'rrsync-leaf-flip'
51 rmtree(base)
52 restricted = base / 'restricted'
53 outside = base / 'outside'
54 dest = base / 'dest'
55 makepath(restricted, outside / 'dir', dest)
56
57 (outside / 'secret').write_text(MARKER + '\n')
58 (outside / 'dir' / 'loot').write_text(MARKER + '\n')
59
60 shim = base / 'rsync-shim'
61 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
62 shim.chmod(0o755)
63
64 rrsync = patched_rrsync(base, rsync_path=str(shim))
65
66 rsh = base / 'fake-rsh'
67 rsh.write_text(
68 '#!/bin/sh\n'
69 'shift\n'
70 'SSH_ORIGINAL_COMMAND="$*"\n'
71 'export SSH_ORIGINAL_COMMAND\n'
72 'exec %s -ro %s\n' % (shlex.quote(str(rrsync)), shlex.quote(str(restricted))))
73 rsh.chmod(0o755)
74
75
76 def leaked_content():
77 """True if any delivered regular file holds the outside marker."""
78 for path in dest.rglob('*'):
79 if path.is_file() and not path.is_symlink():
80 try:
81 if MARKER in path.read_text():
82 return str(path.relative_to(dest))
83 except (OSError, UnicodeDecodeError):
84 pass
85 return None
86
87
88 def race_pull(source, real_name, evil_name):
89 flip = start_path_flipper(real_name, evil_name)
90 try:
91 deadline = time.monotonic() + race_budget()
92 while time.monotonic() < deadline:
93 rmtree(dest)
94 dest.mkdir()
95 subprocess.run(rsync_argv('-a', '-e', str(rsh), source,
96 str(dest) + '/'),
97 stdout=subprocess.DEVNULL,
98 stderr=subprocess.DEVNULL)
99 got = leaked_content()
100 if got:
101 return got
102 finally:
103 stop_flipper(flip)
104 return None
105
106
107 # --- a plain file argument whose leaf flips to an outside symlink ------------
108 real_file = restricted / 'target'
109 real_file.write_text('benign_in_tree_content\n')
110 evil_file = restricted / 'evil'
111 os.symlink(str(outside / 'secret'), evil_file)
112
113 got = race_pull('dummy:target', real_file, evil_file)
114 if got:
115 test_fail(f'a raced leaf flip leaked outside content to {got!r}: the '
116 'sender read through a symlink substituted at the source '
117 'argument after rrsync validated it')
118
119 rmtree(real_file)
120 rmtree(evil_file)
121
122 # --- a trailing-slash directory, which rsync DOES follow ---------------------
123 # This shape keeps the leaf pin precisely because rsync opens it rather than
124 # lstat()ing it; without the pin the flip below transfers outside/dir/loot.
125 real_dir = restricted / 'dir'
126 makepath(real_dir)
127 (real_dir / 'ok').write_text('benign\n')
128 evil_dir = restricted / 'evildir'
129 os.symlink(str(outside / 'dir'), evil_dir)
130
131 got = race_pull('dummy:dir/', real_dir, evil_dir)
132 if got:
133 test_fail(f'a raced trailing-slash directory flip leaked outside content '
134 f'to {got!r}: the leaf pin did not hold')
135
136 print('rrsync sender pulls leak no outside content when the last component is raced')