| 1 |
#!/usr/bin/env python3 |
| 2 |
# Server-controlled alternate-basis xname must stay confined to the operator |
| 3 |
# basedir on the client. Reported by z3r0s (github z3r0s6), 2026-07. |
| 4 |
# |
| 5 |
# When the client pulls with an alt-dest flag (--link-dest / --copy-dest / |
| 6 |
# --compare-dest, or --fuzzy), the sender sends an fnamecmp_type + xname naming |
| 7 |
# which alternate basis the receiver should reconstruct against. The receiver |
| 8 |
# joins the wire-supplied xname to the operator's basedir (basis_dir[]) and |
| 9 |
# opens it as the delta basis. read_ndx_and_attrs() sanitized xname only when |
| 10 |
# sanitize_paths was set (daemon side); a *client* receiver has |
| 11 |
# sanitize_paths == 0, so a malicious server could send xname="../../etc/shadow" |
| 12 |
# and make the client open an out-of-tree file (client-side arbitrary-read / |
| 13 |
# file-existence-oracle / FIFO-hang). secure_basis_open()'s ownership walk does |
| 14 |
# not stop this: it deliberately follows a plain ".." to a regular file (the |
| 15 |
# legitimate --link-dest=../01 sibling, #915) and only refuses foreign-owned |
| 16 |
# *symlink* components. The fix sanitizes the wire xname itself (for basis |
| 17 |
# types only, leaving the hard-link "=> target" xname alone). |
| 18 |
# |
| 19 |
# Test: build an instrumented daemon-sender (env-gated sender.c edit that, when |
| 20 |
# RSYNC_MAL_XNAME is set, injects ITEM_XNAME_FOLLOWS|ITEM_BASIS_TYPE_FOLLOWS + |
| 21 |
# fnamecmp_type=FNAMECMP_FUZZY+1 (== basis_dir[0]) + xname onto each transfer), |
| 22 |
# run it via RSYNC_CONNECT_PROG with the production rsync as the receiver pulling |
| 23 |
# with --link-dest, and observe where the receiver opens the basis. |
| 24 |
# |
| 25 |
# Two FIFOs, each with a helper blocked in open(O_WRONLY) that drops a flag when |
| 26 |
# some reader opens it, tell RED from GREEN without hanging the receiver (it |
| 27 |
# reads EOF and finishes): |
| 28 |
# * ESCAPE base/secret reached only by an unsanitized "../secret" |
| 29 |
# * DECOY linkdest/secret where the SANITIZED "secret" lands |
| 30 |
# Injected xname is "../secret": |
| 31 |
# - vulnerable receiver opens ESCAPE -> escape flag -> FAIL (traversal) |
| 32 |
# - fixed receiver sanitizes to "secret", opens DECOY -> decoy flag -> PASS |
| 33 |
# (the decoy flag also proves the crafted xname actually crossed the wire, |
| 34 |
# so a stale/failed injection build can't false-PASS as "confined") |
| 35 |
# - neither flag -> the injection never took effect -> FAIL (vacuous) |
| 36 |
|
| 37 |
import os |
| 38 |
import shlex |
| 39 |
import subprocess |
| 40 |
import time |
| 41 |
from pathlib import Path |
| 42 |
import sys |
| 43 |
|
| 44 |
from rsyncfns import ( |
| 45 |
SCRATCHDIR, build_patched_rsync, forced_protocol, makepath, rmtree, |
| 46 |
rsync_argv, test_fail, test_skipped, write_daemon_conf, |
| 47 |
) |
| 48 |
|
| 49 |
# Gates ---------------------------------------------------------------------- |
| 50 |
# xname only exists in the protocol >= 29 item stream; below that |
| 51 |
# write_ndx_and_attrs() returns before any flags/xname, so the injection can't |
| 52 |
# cross the wire and the test would be vacuous. (The fix is a no-op there.) |
| 53 |
_proto = forced_protocol() |
| 54 |
if _proto is not None and _proto < 29: |
| 55 |
test_skipped("basis-xname-traversal: xname/item flags need protocol >= 29") |
| 56 |
if not hasattr(os, 'mkfifo'): |
| 57 |
test_skipped("basis-xname-traversal: os.mkfifo unavailable on this platform") |
| 58 |
|
| 59 |
|
| 60 |
# -- Build the instrumented sender (shared helper: Cygwin skip, CCACHE_DISABLE, |
| 61 |
# forced rebuild of the patched unit) ------------------------------------- |
| 62 |
PATCH_OLD = ("\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n" |
| 63 |
"\t\twrite_sum_head(f_xfer, s);") |
| 64 |
PATCH_NEW = ("\t\tif (getenv(\"RSYNC_MAL_XNAME\")) { /* basis-xname-traversal PoC */\n" |
| 65 |
"\t\t\tiflags |= ITEM_XNAME_FOLLOWS | ITEM_BASIS_TYPE_FOLLOWS;\n" |
| 66 |
"\t\t\tfnamecmp_type = FNAMECMP_FUZZY + 1;\n" |
| 67 |
"\t\t\txlen = strlcpy(xname, getenv(\"RSYNC_MAL_XNAME\"), MAXPATHLEN);\n" |
| 68 |
"\t\t}\n" |
| 69 |
"\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n" |
| 70 |
"\t\twrite_sum_head(f_xfer, s);") |
| 71 |
mal_rsync = build_patched_rsync('mal-xname-rsync', [('sender.c', PATCH_OLD, PATCH_NEW)]) |
| 72 |
|
| 73 |
|
| 74 |
# -- Workspace ---------------------------------------------------------------- |
| 75 |
# base/serversrc/file the file the instrumented daemon offers |
| 76 |
# base/linkdest/ the client's --link-dest (basis_dir[0]) |
| 77 |
# base/linkdest/secret DECOY fifo -- where a sanitized "secret" resolves |
| 78 |
# base/secret ESCAPE fifo -- where an unsanitized "../secret" lands |
| 79 |
# base/dest/ the client's destination |
| 80 |
base = SCRATCHDIR / 'xname-race' |
| 81 |
rmtree(base) |
| 82 |
serversrc = base / 'serversrc' |
| 83 |
linkdest = base / 'linkdest' |
| 84 |
dest = base / 'dest' |
| 85 |
escape = base / 'secret' # linkdest/../secret |
| 86 |
decoy = linkdest / 'secret' # linkdest/secret |
| 87 |
esc_flag = base / 'escape.flag' |
| 88 |
dec_flag = base / 'decoy.flag' |
| 89 |
makepath(serversrc) |
| 90 |
makepath(linkdest) |
| 91 |
makepath(dest) |
| 92 |
(serversrc / 'file').write_text("from the server\n") |
| 93 |
|
| 94 |
|
| 95 |
# A helper that blocks in open(fifo, O_WRONLY) until some reader opens the FIFO, |
| 96 |
# then records the flag. Terminated below if no reader ever appears. |
| 97 |
WRITER = ("import os,sys\n" |
| 98 |
"open(sys.argv[3],'w').close()\n" # ready: about to block in open() |
| 99 |
"fd=os.open(sys.argv[1],os.O_WRONLY)\n" |
| 100 |
"open(sys.argv[2],'w').close()\n" |
| 101 |
"os.close(fd)\n") |
| 102 |
|
| 103 |
|
| 104 |
def spawn(fifo, flag): |
| 105 |
ready = Path(str(flag) + '.ready') |
| 106 |
if ready.exists(): |
| 107 |
ready.unlink() |
| 108 |
proc = subprocess.Popen( |
| 109 |
[sys.executable, '-c', WRITER, str(fifo), str(flag), str(ready)]) |
| 110 |
# Wait until the helper is actually at its blocking open(). Starting the |
| 111 |
# transfer before that lets the receiver come and go while nothing is |
| 112 |
# watching the FIFO, and the run reports a vacuous result -- which is what |
| 113 |
# made this test flaky on the slower fleet VMs. |
| 114 |
deadline = time.time() + 30 |
| 115 |
while not ready.exists() and proc.poll() is None and time.time() < deadline: |
| 116 |
time.sleep(0.02) |
| 117 |
return proc |
| 118 |
|
| 119 |
|
| 120 |
def settle(w): |
| 121 |
"""Give a rendezvoused helper a bounded chance to record its flag; a still- |
| 122 |
blocked one just times out. (Closes the terminate-before-flag race.)""" |
| 123 |
try: |
| 124 |
w.wait(timeout=15) |
| 125 |
except subprocess.TimeoutExpired: |
| 126 |
pass |
| 127 |
|
| 128 |
|
| 129 |
def reap(w): |
| 130 |
if w.poll() is None: |
| 131 |
w.terminate() |
| 132 |
try: |
| 133 |
w.wait(timeout=10) |
| 134 |
except subprocess.TimeoutExpired: |
| 135 |
w.kill() |
| 136 |
w.wait() |
| 137 |
|
| 138 |
|
| 139 |
def attempt(): |
| 140 |
"""One injection run. Returns the receiver's CompletedProcess. |
| 141 |
|
| 142 |
Re-creates the FIFOs and flags each time so a retry starts clean. |
| 143 |
""" |
| 144 |
for f in (escape, decoy, esc_flag, dec_flag): |
| 145 |
if os.path.lexists(f): |
| 146 |
os.unlink(f) |
| 147 |
rmtree(dest) |
| 148 |
makepath(dest) |
| 149 |
os.mkfifo(escape) |
| 150 |
os.mkfifo(decoy) |
| 151 |
esc_w = spawn(escape, esc_flag) |
| 152 |
dec_w = spawn(decoy, dec_flag) |
| 153 |
proc = None |
| 154 |
try: |
| 155 |
conf = write_daemon_conf( |
| 156 |
[('m', {'path': str(serversrc), 'read only': 'yes', 'use chroot': 'no'})], |
| 157 |
name='mal-xname-rsyncd.conf') |
| 158 |
os.environ['RSYNC_CONNECT_PROG'] = f'{shlex.quote(str(mal_rsync))} --config={shlex.quote(str(conf))} --daemon' |
| 159 |
os.environ['RSYNC_MAL_XNAME'] = '../secret' # from basis_dir[0] == linkdest |
| 160 |
proc = subprocess.run( |
| 161 |
rsync_argv('-a', f'--link-dest={linkdest}', |
| 162 |
'rsync://localhost/m/file', str(dest) + '/'), |
| 163 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120) |
| 164 |
settle(esc_w) |
| 165 |
settle(dec_w) |
| 166 |
finally: |
| 167 |
os.environ.pop('RSYNC_MAL_XNAME', None) |
| 168 |
os.environ.pop('RSYNC_CONNECT_PROG', None) |
| 169 |
reap(esc_w) |
| 170 |
reap(dec_w) |
| 171 |
for f in (escape, decoy): |
| 172 |
if os.path.lexists(f): |
| 173 |
os.unlink(f) |
| 174 |
return proc |
| 175 |
|
| 176 |
|
| 177 |
# A run where NEITHER fifo was opened proves nothing: the injection did not |
| 178 |
# take effect, so there was no traversal attempt to confine. That is a setup |
| 179 |
# failure, not a security signal, and on the slower fleet VMs it happens often |
| 180 |
# enough to make the test unusable -- so retry it. An ESCAPE is never retried: |
| 181 |
# the loop stops the moment the escape flag appears. |
| 182 |
attempts = 0 |
| 183 |
for _try in range(6): |
| 184 |
attempts += 1 |
| 185 |
proc = attempt() |
| 186 |
if esc_flag.is_file() or dec_flag.is_file(): |
| 187 |
break |
| 188 |
|
| 189 |
|
| 190 |
# -- Oracle ------------------------------------------------------------------- |
| 191 |
out_tail = '\n'.join((proc.stdout if proc else '').splitlines()[-20:]) |
| 192 |
|
| 193 |
if esc_flag.is_file(): |
| 194 |
test_fail( |
| 195 |
"malicious server traversed the client's filesystem via the alt-dest " |
| 196 |
f"xname: the receiver opened {escape} (one level above the --link-dest " |
| 197 |
"dir) as the delta basis. A server-supplied xname of '../secret' was " |
| 198 |
"not sanitized on the client (sanitize_paths==0 off-daemon). Fix: " |
| 199 |
"sanitize a basis-type xname in read_ndx_and_attrs(). Receiver output " |
| 200 |
f"tail:\n{out_tail}") |
| 201 |
|
| 202 |
# The decoy flag proves the crafted xname reached the receiver AND was confined |
| 203 |
# to the basedir (sanitized "../secret" -> "secret" -> linkdest/secret). Its |
| 204 |
# absence means the injection never took effect (e.g. a stale patched build), |
| 205 |
# so a clear escape flag alone would be a vacuous pass. |
| 206 |
if not dec_flag.is_file(): |
| 207 |
test_fail( |
| 208 |
"the crafted xname never reached the receiver's basis open (neither the " |
| 209 |
"escape nor the decoy FIFO was opened) -- the instrumented-sender " |
| 210 |
f"injection did not take effect, so this run is vacuous after " |
| 211 |
f"{attempts} attempt(s). This is a harness failure, NOT a traversal: " |
| 212 |
"an escape is reported separately and is never retried. Receiver rc=" |
| 213 |
f"{proc.returncode if proc else 'n/a'}. Output tail:\n{out_tail}") |
| 214 |
|
| 215 |
if proc.returncode != 0: |
| 216 |
test_fail( |
| 217 |
f"xname confined to the basedir, but the pull failed (rc={proc.returncode}). " |
| 218 |
f"Output tail:\n{out_tail}") |
| 219 |
|
| 220 |
print("basis-xname-traversal: receiver confined the server-supplied alt-dest " |
| 221 |
"xname to the --link-dest dir; '../secret' resolved to linkdest/secret, " |
| 222 |
"not the out-of-tree sibling.") |