| 1 |
From 195b4c6d305f7ad9b8b4b9ff1a62d525d907e241 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Zen Dodd <[email protected]> |
| 3 |
Date: Sat, 15 Aug 2026 16:19:15 +1000 |
| 4 |
Subject: [PATCH] testsuite: make basis xname oracle deterministic (#1051) |
| 5 |
|
| 6 |
--- |
| 7 |
testsuite/basis-xname-traversal_test.py | 198 ++++++++---------------- |
| 8 |
1 file changed, 61 insertions(+), 137 deletions(-) |
| 9 |
|
| 10 |
diff --git a/testsuite/basis-xname-traversal_test.py b/testsuite/basis-xname-traversal_test.py |
| 11 |
index de004c0e..1ab28c4b 100644 |
| 12 |
--- a/testsuite/basis-xname-traversal_test.py |
| 13 |
+++ b/testsuite/basis-xname-traversal_test.py |
| 14 |
@@ -16,30 +16,16 @@ |
| 15 |
# *symlink* components. The fix sanitizes the wire xname itself (for basis |
| 16 |
# types only, leaving the hard-link "=> target" xname alone). |
| 17 |
# |
| 18 |
-# Test: build an instrumented daemon-sender (env-gated sender.c edit that, when |
| 19 |
+# Test: build an instrumented rsync (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 |
+# fnamecmp_type=FNAMECMP_FUZZY+1 (== basis_dir[0]) + xname onto each transfer). |
| 37 |
+# An env-gated receiver.c edit records the exact basedir and relpath passed to |
| 38 |
+# secure_basis_open(). This observes the security decision directly without |
| 39 |
+# relying on timing-sensitive FIFO rendezvous behaviour across operating systems. |
| 40 |
|
| 41 |
import os |
| 42 |
import shlex |
| 43 |
import subprocess |
| 44 |
-import time |
| 45 |
-from pathlib import Path |
| 46 |
-import sys |
| 47 |
|
| 48 |
from rsyncfns import ( |
| 49 |
SCRATCHDIR, build_patched_rsync, forced_protocol, makepath, rmtree, |
| 50 |
@@ -53,11 +39,7 @@ from rsyncfns import ( |
| 51 |
_proto = forced_protocol() |
| 52 |
if _proto is not None and _proto < 29: |
| 53 |
test_skipped("basis-xname-traversal: xname/item flags need protocol >= 29") |
| 54 |
-if not hasattr(os, 'mkfifo'): |
| 55 |
- test_skipped("basis-xname-traversal: os.mkfifo unavailable on this platform") |
| 56 |
- |
| 57 |
- |
| 58 |
-# -- Build the instrumented sender (shared helper: Cygwin skip, CCACHE_DISABLE, |
| 59 |
+# -- Build the instrumented peer (shared helper: Cygwin skip, CCACHE_DISABLE, |
| 60 |
# forced rebuild of the patched unit) ------------------------------------- |
| 61 |
PATCH_OLD = ("\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n" |
| 62 |
"\t\twrite_sum_head(f_xfer, s);") |
| 63 |
@@ -68,14 +50,32 @@ PATCH_NEW = ("\t\tif (getenv(\"RSYNC_MAL_XNAME\")) { /* basis-xname-traversal Po |
| 64 |
"\t\t}\n" |
| 65 |
"\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n" |
| 66 |
"\t\twrite_sum_head(f_xfer, s);") |
| 67 |
-mal_rsync = build_patched_rsync('mal-xname-rsync', [('sender.c', PATCH_OLD, PATCH_NEW)]) |
| 68 |
+TRACE_OLD = ("static int secure_basis_open(const char *basedir, const char *relpath, int flags, mode_t mode)\n" |
| 69 |
+ "{\n" |
| 70 |
+ "\textern int am_daemon, am_chrooted;") |
| 71 |
+TRACE_NEW = ("static int secure_basis_open(const char *basedir, const char *relpath, int flags, mode_t mode)\n" |
| 72 |
+ "{\n" |
| 73 |
+ "\tconst char *trace_path = getenv(\"RSYNC_BASIS_TRACE\");\n" |
| 74 |
+ "\tif (trace_path) {\n" |
| 75 |
+ "\t\tFILE *trace = fopen(trace_path, \"a\");\n" |
| 76 |
+ "\t\tif (trace) {\n" |
| 77 |
+ "\t\t\tfprintf(trace, \"%s\\t%s\\n\", basedir ? basedir : \"\", relpath);\n" |
| 78 |
+ "\t\t\tfclose(trace);\n" |
| 79 |
+ "\t\t}\n" |
| 80 |
+ "\t}\n" |
| 81 |
+ "\textern int am_daemon, am_chrooted;") |
| 82 |
+mal_rsync = build_patched_rsync( |
| 83 |
+ 'mal-xname-rsync', |
| 84 |
+ [('sender.c', PATCH_OLD, PATCH_NEW), |
| 85 |
+ ('receiver.c', TRACE_OLD, TRACE_NEW)], |
| 86 |
+) |
| 87 |
|
| 88 |
|
| 89 |
# -- Workspace ---------------------------------------------------------------- |
| 90 |
# base/serversrc/file the file the instrumented daemon offers |
| 91 |
# base/linkdest/ the client's --link-dest (basis_dir[0]) |
| 92 |
-# base/linkdest/secret DECOY fifo -- where a sanitized "secret" resolves |
| 93 |
-# base/secret ESCAPE fifo -- where an unsanitized "../secret" lands |
| 94 |
+# base/linkdest/secret where a sanitized "secret" resolves |
| 95 |
+# base/secret where an unsanitized "../secret" resolves |
| 96 |
# base/dest/ the client's destination |
| 97 |
base = SCRATCHDIR / 'xname-race' |
| 98 |
rmtree(base) |
| 99 |
@@ -84,133 +84,57 @@ linkdest = base / 'linkdest' |
| 100 |
dest = base / 'dest' |
| 101 |
escape = base / 'secret' # linkdest/../secret |
| 102 |
decoy = linkdest / 'secret' # linkdest/secret |
| 103 |
-esc_flag = base / 'escape.flag' |
| 104 |
-dec_flag = base / 'decoy.flag' |
| 105 |
+trace_file = base / 'basis.trace' |
| 106 |
makepath(serversrc) |
| 107 |
makepath(linkdest) |
| 108 |
makepath(dest) |
| 109 |
(serversrc / 'file').write_text("from the server\n") |
| 110 |
+escape.write_text("escaped basis\n") |
| 111 |
+decoy.write_text("confined basis\n") |
| 112 |
|
| 113 |
- |
| 114 |
-# A helper that blocks in open(fifo, O_WRONLY) until some reader opens the FIFO, |
| 115 |
-# then records the flag. Terminated below if no reader ever appears. |
| 116 |
-WRITER = ("import os,sys\n" |
| 117 |
- "open(sys.argv[3],'w').close()\n" # ready: about to block in open() |
| 118 |
- "fd=os.open(sys.argv[1],os.O_WRONLY)\n" |
| 119 |
- "open(sys.argv[2],'w').close()\n" |
| 120 |
- "os.close(fd)\n") |
| 121 |
- |
| 122 |
- |
| 123 |
-def spawn(fifo, flag): |
| 124 |
- ready = Path(str(flag) + '.ready') |
| 125 |
- if ready.exists(): |
| 126 |
- ready.unlink() |
| 127 |
- proc = subprocess.Popen( |
| 128 |
- [sys.executable, '-c', WRITER, str(fifo), str(flag), str(ready)]) |
| 129 |
- # Wait until the helper is actually at its blocking open(). Starting the |
| 130 |
- # transfer before that lets the receiver come and go while nothing is |
| 131 |
- # watching the FIFO, and the run reports a vacuous result -- which is what |
| 132 |
- # made this test flaky on the slower fleet VMs. |
| 133 |
- deadline = time.time() + 30 |
| 134 |
- while not ready.exists() and proc.poll() is None and time.time() < deadline: |
| 135 |
- time.sleep(0.02) |
| 136 |
- return proc |
| 137 |
- |
| 138 |
- |
| 139 |
-def settle(w): |
| 140 |
- """Give a rendezvoused helper a bounded chance to record its flag; a still- |
| 141 |
- blocked one just times out. (Closes the terminate-before-flag race.)""" |
| 142 |
- try: |
| 143 |
- w.wait(timeout=15) |
| 144 |
- except subprocess.TimeoutExpired: |
| 145 |
- pass |
| 146 |
- |
| 147 |
- |
| 148 |
-def reap(w): |
| 149 |
- if w.poll() is None: |
| 150 |
- w.terminate() |
| 151 |
- try: |
| 152 |
- w.wait(timeout=10) |
| 153 |
- except subprocess.TimeoutExpired: |
| 154 |
- w.kill() |
| 155 |
- w.wait() |
| 156 |
- |
| 157 |
- |
| 158 |
-def attempt(): |
| 159 |
- """One injection run. Returns the receiver's CompletedProcess. |
| 160 |
- |
| 161 |
- Re-creates the FIFOs and flags each time so a retry starts clean. |
| 162 |
- """ |
| 163 |
- for f in (escape, decoy, esc_flag, dec_flag): |
| 164 |
- if os.path.lexists(f): |
| 165 |
- os.unlink(f) |
| 166 |
- rmtree(dest) |
| 167 |
- makepath(dest) |
| 168 |
- os.mkfifo(escape) |
| 169 |
- os.mkfifo(decoy) |
| 170 |
- esc_w = spawn(escape, esc_flag) |
| 171 |
- dec_w = spawn(decoy, dec_flag) |
| 172 |
- proc = None |
| 173 |
- try: |
| 174 |
- conf = write_daemon_conf( |
| 175 |
- [('m', {'path': str(serversrc), 'read only': 'yes', 'use chroot': 'no'})], |
| 176 |
- name='mal-xname-rsyncd.conf') |
| 177 |
- os.environ['RSYNC_CONNECT_PROG'] = f'{shlex.quote(str(mal_rsync))} --config={shlex.quote(str(conf))} --daemon' |
| 178 |
- os.environ['RSYNC_MAL_XNAME'] = '../secret' # from basis_dir[0] == linkdest |
| 179 |
- proc = subprocess.run( |
| 180 |
- rsync_argv('-a', f'--link-dest={linkdest}', |
| 181 |
- 'rsync://localhost/m/file', str(dest) + '/'), |
| 182 |
- stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120) |
| 183 |
- settle(esc_w) |
| 184 |
- settle(dec_w) |
| 185 |
- finally: |
| 186 |
- os.environ.pop('RSYNC_MAL_XNAME', None) |
| 187 |
- os.environ.pop('RSYNC_CONNECT_PROG', None) |
| 188 |
- reap(esc_w) |
| 189 |
- reap(dec_w) |
| 190 |
- for f in (escape, decoy): |
| 191 |
- if os.path.lexists(f): |
| 192 |
- os.unlink(f) |
| 193 |
- return proc |
| 194 |
- |
| 195 |
- |
| 196 |
-# A run where NEITHER fifo was opened proves nothing: the injection did not |
| 197 |
-# take effect, so there was no traversal attempt to confine. That is a setup |
| 198 |
-# failure, not a security signal, and on the slower fleet VMs it happens often |
| 199 |
-# enough to make the test unusable -- so retry it. An ESCAPE is never retried: |
| 200 |
-# the loop stops the moment the escape flag appears. |
| 201 |
-attempts = 0 |
| 202 |
-for _try in range(6): |
| 203 |
- attempts += 1 |
| 204 |
- proc = attempt() |
| 205 |
- if esc_flag.is_file() or dec_flag.is_file(): |
| 206 |
- break |
| 207 |
+conf = write_daemon_conf( |
| 208 |
+ [('m', {'path': str(serversrc), 'read only': 'yes', 'use chroot': 'no'})], |
| 209 |
+ name='mal-xname-rsyncd.conf') |
| 210 |
+os.environ['RSYNC_CONNECT_PROG'] = f'{shlex.quote(str(mal_rsync))} --config={shlex.quote(str(conf))} --daemon' |
| 211 |
+os.environ['RSYNC_MAL_XNAME'] = '../secret' # from basis_dir[0] == linkdest |
| 212 |
+os.environ['RSYNC_BASIS_TRACE'] = str(trace_file) |
| 213 |
+try: |
| 214 |
+ argv = rsync_argv('-a', f'--link-dest={linkdest}', |
| 215 |
+ 'rsync://localhost/m/file', str(dest) + '/') |
| 216 |
+ argv[0] = str(mal_rsync) |
| 217 |
+ proc = subprocess.run( |
| 218 |
+ argv, |
| 219 |
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120) |
| 220 |
+finally: |
| 221 |
+ os.environ.pop('RSYNC_BASIS_TRACE', None) |
| 222 |
+ os.environ.pop('RSYNC_MAL_XNAME', None) |
| 223 |
+ os.environ.pop('RSYNC_CONNECT_PROG', None) |
| 224 |
|
| 225 |
|
| 226 |
# -- Oracle ------------------------------------------------------------------- |
| 227 |
-out_tail = '\n'.join((proc.stdout if proc else '').splitlines()[-20:]) |
| 228 |
+out_tail = '\n'.join(proc.stdout.splitlines()[-20:]) |
| 229 |
+trace = trace_file.read_text().splitlines() if trace_file.is_file() else [] |
| 230 |
+escaped = f'{linkdest}\t../secret' |
| 231 |
+confined = f'{linkdest}\tsecret' |
| 232 |
|
| 233 |
-if esc_flag.is_file(): |
| 234 |
+if escaped in trace: |
| 235 |
test_fail( |
| 236 |
"malicious server traversed the client's filesystem via the alt-dest " |
| 237 |
- f"xname: the receiver opened {escape} (one level above the --link-dest " |
| 238 |
+ f"xname: the receiver attempted {escape} (one level above the --link-dest " |
| 239 |
"dir) as the delta basis. A server-supplied xname of '../secret' was " |
| 240 |
"not sanitized on the client (sanitize_paths==0 off-daemon). Fix: " |
| 241 |
"sanitize a basis-type xname in read_ndx_and_attrs(). Receiver output " |
| 242 |
f"tail:\n{out_tail}") |
| 243 |
|
| 244 |
-# The decoy flag proves the crafted xname reached the receiver AND was confined |
| 245 |
-# to the basedir (sanitized "../secret" -> "secret" -> linkdest/secret). Its |
| 246 |
-# absence means the injection never took effect (e.g. a stale patched build), |
| 247 |
-# so a clear escape flag alone would be a vacuous pass. |
| 248 |
-if not dec_flag.is_file(): |
| 249 |
+# The trace proves the crafted xname reached the receiver and was confined to |
| 250 |
+# the basedir (sanitized "../secret" -> "secret" -> linkdest/secret). Its |
| 251 |
+# absence means the injection never took effect (e.g. a stale patched build). |
| 252 |
+if confined not in trace: |
| 253 |
test_fail( |
| 254 |
- "the crafted xname never reached the receiver's basis open (neither the " |
| 255 |
- "escape nor the decoy FIFO was opened) -- the instrumented-sender " |
| 256 |
- f"injection did not take effect, so this run is vacuous after " |
| 257 |
- f"{attempts} attempt(s). This is a harness failure, NOT a traversal: " |
| 258 |
- "an escape is reported separately and is never retried. Receiver rc=" |
| 259 |
- f"{proc.returncode if proc else 'n/a'}. Output tail:\n{out_tail}") |
| 260 |
+ "the crafted xname never reached the receiver's confined basis open; " |
| 261 |
+ "the instrumented injection did not take effect, so this run is " |
| 262 |
+ f"vacuous. Trace={trace!r}. Receiver rc={proc.returncode}. " |
| 263 |
+ f"Output tail:\n{out_tail}") |
| 264 |
|
| 265 |
if proc.returncode != 0: |
| 266 |
test_fail( |
| 267 |
-- |
| 268 |
2.53.0 |
| 269 |
|