| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| •19 |
|
| 20 |
|
| •21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 43 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| •77 |
|
| 78 |
|
| 79 |
|
| 80 |
base = SCRATCHDIR / 'xname-race' |
| 81 |
rmtree(base) |
| 82 |
serversrc = base / 'serversrc' |
| 83 |
linkdest = base / 'linkdest' |
| 84 |
dest = base / 'dest' |
| 85 |
escape = base / 'secret' |
| 86 |
decoy = 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' |
| 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 |
|
| •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 |
|
| 130 |
|
| 131 |
|
| 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.") |