Debian Codegraph @

testsuite/basis-xname-traversal_test.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 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 # An env-gated receiver.c edit records the exact basedir and relpath passed to
23 # secure_basis_open(). This observes the security decision directly without
24 # relying on timing-sensitive FIFO rendezvous behaviour across operating systems.
25
26 import os
27 import shlex
28 import subprocess
29
30 from rsyncfns import (
31 SCRATCHDIR, build_patched_rsync, forced_protocol, makepath, rmtree,
32 rsync_argv, test_fail, test_skipped, write_daemon_conf,
33 )
34
35 # Gates ----------------------------------------------------------------------
36 # xname only exists in the protocol >= 29 item stream; below that
37 # write_ndx_and_attrs() returns before any flags/xname, so the injection can't
38 # cross the wire and the test would be vacuous. (The fix is a no-op there.)
39 _proto = forced_protocol()
40 if _proto is not None and _proto < 29:
41 test_skipped("basis-xname-traversal: xname/item flags need protocol >= 29")
•42 # -- Build the instrumented peer (shared helper: Cygwin skip, CCACHE_DISABLE,
43 # forced rebuild of the patched unit) -------------------------------------
44 PATCH_OLD = ("\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n"
45 "\t\twrite_sum_head(f_xfer, s);")
46 PATCH_NEW = ("\t\tif (getenv(\"RSYNC_MAL_XNAME\")) { /* basis-xname-traversal PoC */\n"
47 "\t\t\tiflags |= ITEM_XNAME_FOLLOWS | ITEM_BASIS_TYPE_FOLLOWS;\n"
48 "\t\t\tfnamecmp_type = FNAMECMP_FUZZY + 1;\n"
49 "\t\t\txlen = strlcpy(xname, getenv(\"RSYNC_MAL_XNAME\"), MAXPATHLEN);\n"
50 "\t\t}\n"
51 "\t\twrite_ndx_and_attrs(f_out, ndx, iflags, fname, file, fnamecmp_type, xname, xlen);\n"
52 "\t\twrite_sum_head(f_xfer, s);")
•53 TRACE_OLD = ("static int secure_basis_open(const char *basedir, const char *relpath, int flags, mode_t mode)\n"
54 "{\n"
55 "\textern int am_daemon, am_chrooted;")
56 TRACE_NEW = ("static int secure_basis_open(const char *basedir, const char *relpath, int flags, mode_t mode)\n"
57 "{\n"
58 "\tconst char *trace_path = getenv(\"RSYNC_BASIS_TRACE\");\n"
59 "\tif (trace_path) {\n"
60 "\t\tFILE *trace = fopen(trace_path, \"a\");\n"
61 "\t\tif (trace) {\n"
62 "\t\t\tfprintf(trace, \"%s\\t%s\\n\", basedir ? basedir : \"\", relpath);\n"
63 "\t\t\tfclose(trace);\n"
64 "\t\t}\n"
65 "\t}\n"
66 "\textern int am_daemon, am_chrooted;")
67 mal_rsync = build_patched_rsync(
68 'mal-xname-rsync',
69 [('sender.c', PATCH_OLD, PATCH_NEW),
70 ('receiver.c', TRACE_OLD, TRACE_NEW)],
71 )
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 where a sanitized "secret" resolves
78 # base/secret where an unsanitized "../secret" resolves
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 trace_file = base / 'basis.trace'
88 makepath(serversrc)
89 makepath(linkdest)
90 makepath(dest)
91 (serversrc / 'file').write_text("from the server\n")
•92 escape.write_text("escaped basis\n")
93 decoy.write_text("confined basis\n")
94
•95 conf = write_daemon_conf(
96 [('m', {'path': str(serversrc), 'read only': 'yes', 'use chroot': 'no'})],
97 name='mal-xname-rsyncd.conf')
98 os.environ['RSYNC_CONNECT_PROG'] = f'{shlex.quote(str(mal_rsync))} --config={shlex.quote(str(conf))} --daemon'
99 os.environ['RSYNC_MAL_XNAME'] = '../secret' # from basis_dir[0] == linkdest
100 os.environ['RSYNC_BASIS_TRACE'] = str(trace_file)
101 try:
102 argv = rsync_argv('-a', f'--link-dest={linkdest}',
103 'rsync://localhost/m/file', str(dest) + '/')
104 argv[0] = str(mal_rsync)
105 proc = subprocess.run(
106 argv,
107 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=120)
108 finally:
109 os.environ.pop('RSYNC_BASIS_TRACE', None)
110 os.environ.pop('RSYNC_MAL_XNAME', None)
111 os.environ.pop('RSYNC_CONNECT_PROG', None)
112
113
114 # -- Oracle -------------------------------------------------------------------
•115 out_tail = '\n'.join(proc.stdout.splitlines()[-20:])
116 trace = trace_file.read_text().splitlines() if trace_file.is_file() else []
117 escaped = f'{linkdest}\t../secret'
118 confined = f'{linkdest}\tsecret'
119
•120 if escaped in trace:
121 test_fail(
122 "malicious server traversed the client's filesystem via the alt-dest "
•123 f"xname: the receiver attempted {escape} (one level above the --link-dest "
124 "dir) as the delta basis. A server-supplied xname of '../secret' was "
125 "not sanitized on the client (sanitize_paths==0 off-daemon). Fix: "
126 "sanitize a basis-type xname in read_ndx_and_attrs(). Receiver output "
127 f"tail:\n{out_tail}")
128
•129 # The trace proves the crafted xname reached the receiver and was confined to
130 # the basedir (sanitized "../secret" -> "secret" -> linkdest/secret). Its
131 # absence means the injection never took effect (e.g. a stale patched build).
132 if confined not in trace:
133 test_fail(
•134 "the crafted xname never reached the receiver's confined basis open; "
135 "the instrumented injection did not take effect, so this run is "
136 f"vacuous. Trace={trace!r}. Receiver rc={proc.returncode}. "
137 f"Output tail:\n{out_tail}")
138
139 if proc.returncode != 0:
140 test_fail(
141 f"xname confined to the basedir, but the pull failed (rc={proc.returncode}). "
142 f"Output tail:\n{out_tail}")
143
144 print("basis-xname-traversal: receiver confined the server-supplied alt-dest "
145 "xname to the --link-dest dir; '../secret' resolved to linkdest/secret, "
146 "not the out-of-tree sibling.")