Debian Codegraph @

testsuite/rrsync-sender-parent-pin_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A sender leaf rrsync never opens must still resolve beneath a pinned parent.
3
4 rrsync deliberately does not content-open a sender argument whose leaf is not a
5 regular file or a directory: O_RDONLY on a FIFO blocks until a writer appears,
6 and a dangling symlink resolves to nothing. What it must NOT do is then hand
7 rsync the bare name, because every component goes back into play and a parent
8 flipped after the realpath() check redirects the read out of the tree -- which
9 is CVE-2026-53783. The leaf is instead spelled beneath a pinned directory.
10
11 rrsync-sender-leaf-flip races this with the real binary, but a race only ever
12 samples the window: zero leaks in 104 attempts still leaves a few per cent of
13 per-attempt risk unmeasured. This test closes the window deterministically
14 instead. A stub standing in for rsync inherits the pinned descriptor, then
15 blocks; the parent is swapped for a symlink pointing outside the tree while it
16 is blocked, so the substitution is guaranteed to be in place before the stub
17 resolves anything; the stub then reports what the argument actually names.
18
19 With the parent pinned, the stub sees the original in-tree leaf no matter what
20 the tree now looks like. Without it -- the bare name -- it sees whatever the
21 attacker's symlink points at. So this fails outright if the pin is removed,
22 rather than depending on winning a race.
23
24 Not asserted here: a --relative argument with no client "/./" anchors at the
25 restricted root rather than the leaf's parent, so its intermediate components
26 stay raceable. That limit predates this and is stated in NEWS.
27 """
28
29 import os
30 import shlex
31 import stat
32 import subprocess
33 import time
34
35 from rsyncfns import (
36 SCRATCHDIR, makepath, patched_rrsync, proc_self_fd_pins, rmtree, test_fail,
37 test_skipped,
38 )
39
40 if not proc_self_fd_pins():
41 test_skipped("rrsync's inode pin needs /proc/self/fd (Linux); the "
42 "unhardened fallback elsewhere is by design")
43
44 MARKER = 'AUDIT_PARENT_PIN_MARKER_DO_NOT_DISCLOSE'
45
46 base = SCRATCHDIR / 'rrsync-parent-pin'
47 rmtree(base)
48 restricted = base / 'restricted'
49 outside = base / 'outside'
50 makepath(restricted, outside)
51
52 # What the attacker wants reached: same names as in the tree, but regular
53 # files holding the marker, so a leak is unmistakable in the stub's report.
54 outdir = outside / 'dir'
55 makepath(outdir)
56 (outdir / 'target').write_text(MARKER + '\n')
57 (outdir / 'afifo').write_text(MARKER + '\n')
58
59 # In-tree: a dangling symlink and a FIFO, the two shapes rrsync never opens.
60 realdir = restricted / 'dir'
61 makepath(realdir)
62 os.symlink('missing-sibling', realdir / 'target')
63 os.mkfifo(realdir / 'afifo')
64
65 ready = base / 'stub-ready'
66 go = base / 'stub-go'
67 report = base / 'stub-report'
68
69 # The stub replaces rsync. It signals that it is running -- with the pinned
70 # descriptor already inherited -- waits to be released, and only then resolves
71 # its source argument, which is the whole point: the swap below is guaranteed
72 # to have happened first.
73 stub = base / 'rsync-stub'
74 stub.write_text(f'''#!/usr/bin/env python3
75 import os, stat, sys, time
76 arg = sys.argv[-1]
77 open({str(ready)!r}, 'w').close()
78 for _ in range(600):
79 if os.path.exists({str(go)!r}):
80 break
81 time.sleep(0.05)
82 try:
83 st = os.lstat(arg)
84 except OSError as e:
85 out = 'error %s' % e.strerror
86 else:
87 if stat.S_ISLNK(st.st_mode):
88 out = 'symlink %s' % os.readlink(arg)
89 elif stat.S_ISFIFO(st.st_mode):
90 out = 'fifo'
91 elif stat.S_ISREG(st.st_mode):
92 out = 'regular %s' % open(arg).read().strip()
93 elif stat.S_ISDIR(st.st_mode):
94 out = 'directory'
95 else:
96 out = 'other'
97 with open({str(report)!r}, 'w') as f:
98 f.write('%s\\n%s\\n' % (arg, out))
99 ''')
100 stub.chmod(0o755)
101
102 rrsync = patched_rrsync(base, rsync_path=str(stub))
103
104 evil = restricted / 'evil'
105 os.symlink(str(outdir), evil)
106
107
108 def resolve_with_parent_swapped(source):
109 """Run rrsync, swap `dir` for the outside symlink mid-flight, report."""
110 for p in (ready, go, report):
111 if p.exists():
112 p.unlink()
113
114 cmd = ('rsync --server --sender -logDtpre.iLsfxC . %s' % source)
115 proc = subprocess.Popen(
116 [str(rrsync), '-ro', str(restricted)],
117 env={**os.environ, 'SSH_ORIGINAL_COMMAND': cmd},
118 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
119
120 deadline = time.monotonic() + 30
121 while time.monotonic() < deadline and not ready.exists():
122 if proc.poll() is not None:
123 break
124 time.sleep(0.02)
125 if not ready.exists():
126 out = proc.communicate()[0]
127 test_fail(f'the stub never ran for {source!r}, so nothing was tested '
128 f'(rc={proc.returncode}, output={out.strip()[:300]!r})')
129
130 # The stub is blocked with the pinned fd already inherited. Swap the
131 # parent for a symlink out of the tree -- guaranteed to be in place before
132 # the stub resolves the argument.
133 os.rename(realdir, base / 'stash')
134 os.rename(evil, realdir)
135
136 go.touch()
137 out = proc.communicate(timeout=60)[0]
138
139 # Put the tree back for the next case.
140 os.rename(realdir, evil)
141 os.rename(base / 'stash', realdir)
142
143 if not report.exists():
144 test_fail(f'the stub produced no report for {source!r} '
145 f'(rc={proc.returncode}, output={out.strip()[:300]!r})')
146 handed, saw = report.read_text().splitlines()
147 return handed, saw
148
149
150 # --- control: the swap really does redirect an unpinned name -----------------
151 # Resolve the same relative name the way rsync would without a pin, while the
152 # substitution is in place. If this does NOT reach the outside file, the
153 # construction is broken and the assertions below would prove nothing.
154 os.rename(realdir, base / 'stash')
155 os.rename(evil, realdir)
156 try:
157 probe = os.path.join(str(restricted), 'dir/target')
158 st = os.lstat(probe)
159 if not stat.S_ISREG(st.st_mode) or MARKER not in open(probe).read():
160 test_fail('control failed: with "dir" swapped, the bare name '
161 'dir/target does not reach the outside file, so this test '
162 'cannot tell a pinned resolution from an unpinned one')
163 finally:
164 os.rename(realdir, evil)
165 os.rename(base / 'stash', realdir)
166
167 # --- the dangling symlink leaf ----------------------------------------------
168 handed, saw = resolve_with_parent_swapped('dir/target')
169 if saw != 'symlink missing-sibling':
170 test_fail(f'a dangling-symlink leaf resolved to {saw!r} after its parent '
171 f'was swapped for a symlink out of the tree; expected the '
172 f'in-tree "symlink missing-sibling". rsync was handed '
173 f'{handed!r} -- an unpinned name puts every component back in '
174 'play, which is CVE-2026-53783')
175
176 # --- the FIFO leaf -----------------------------------------------------------
177 handed, saw = resolve_with_parent_swapped('dir/afifo')
178 if saw != 'fifo':
179 test_fail(f'a FIFO leaf resolved to {saw!r} after its parent was swapped '
180 f'for a symlink out of the tree; expected the in-tree "fifo". '
181 f'rsync was handed {handed!r}')
182
183 print('a sender leaf rrsync never opens still resolves beneath its pinned '
184 'parent when the parent is swapped out from under it')