| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
import filecmp |
| 11 |
import os |
| 12 |
import subprocess |
| 13 |
|
| 14 |
from rsyncfns import ( |
| 15 |
SCRATCHDIR, |
| 16 |
get_rootgid, get_rootuid, get_testuid, |
| 17 |
make_data_file, rmtree, rsync_argv, start_test_daemon, |
| 18 |
test_fail, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
DAEMON_PORT = 12885 |
| 23 |
|
| 24 |
mod = SCRATCHDIR / 'module' |
| 25 |
outside = SCRATCHDIR / 'outside' |
| 26 |
src = SCRATCHDIR / 'src_files' |
| 27 |
conf = SCRATCHDIR / 'test-rsyncd.conf' |
| 28 |
|
| 29 |
for d in (mod, outside, src): |
| 30 |
rmtree(d) |
| 31 |
d.mkdir(parents=True) |
| 32 |
(src / 'subdir').mkdir() |
| 33 |
|
| 34 |
|
| 35 |
(outside / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n") |
| 36 |
os.chmod(outside / 'target.txt', 0o600) |
| 37 |
outside_pristine = SCRATCHDIR / 'outside-pristine.txt' |
| 38 |
import shutil |
| 39 |
shutil.copy2(outside / 'target.txt', outside_pristine) |
| 40 |
|
| 41 |
|
| 42 |
os.symlink(str(outside), mod / 'subdir') |
| 43 |
|
| 44 |
|
| 45 |
sz = (outside / 'target.txt').stat().st_size |
| 46 |
make_data_file(src / 'target.txt', sz) |
| 47 |
make_data_file(src / 'subdir' / 'target.txt', sz) |
| 48 |
os.chmod(src / 'target.txt', 0o666) |
| 49 |
os.chmod(src / 'subdir' / 'target.txt', 0o666) |
| 50 |
|
| 51 |
conf.write_text(f"""\ |
| 52 |
use chroot = no |
| 53 |
log file = {SCRATCHDIR}/rsyncd.log |
| 54 |
[upload] |
| 55 |
path = {mod} |
| 56 |
use chroot = no |
| 57 |
read only = no |
| 58 |
""") |
| 59 |
|
| 60 |
|
| 61 |
def reset_outside() -> None: |
| 62 |
os.chmod(outside / 'target.txt', 0o600) |
| 63 |
(outside / 'target.txt').write_text("OUTSIDE_SECRET_DATA\n") |
| 64 |
os.chmod(outside / 'target.txt', 0o600) |
| 65 |
|
| 66 |
|
| 67 |
def verify_unchanged(label: str) -> None: |
| 68 |
mode = (outside / 'target.txt').stat().st_mode & 0o777 |
| 69 |
if mode != 0o600: |
| 70 |
test_fail( |
| 71 |
f"{label}: outside file mode changed from 600 to {oct(mode)[2:]} " |
| 72 |
"(chmod escape)" |
| 73 |
) |
| 74 |
if not filecmp.cmp(outside / 'target.txt', outside_pristine, shallow=False): |
| 75 |
test_fail(f"{label}: outside file content changed (write escape)") |
| 76 |
|
| 77 |
|
| 78 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 79 |
|
| 80 |
|
| 81 |
def run_attack(label: str, *args) -> None: |
| 82 |
reset_outside() |
| 83 |
rc = subprocess.run( |
| 84 |
rsync_argv(*args), |
| 85 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 86 |
).returncode |
| 87 |
if rc >= 128: |
| 88 |
test_fail(f"{label}: rsync died from a signal (rc={rc})") |
| 89 |
verify_unchanged(label) |
| 90 |
|
| 91 |
|
| 92 |
def positive_control() -> None: |
| 93 |
"""Confirm the receiver writes into an ordinary in-module subdirectory, so |
| 94 |
the symlink-escape scenarios below genuinely exercise the chdir path rather |
| 95 |
than passing because the daemon refused (or failed) before reaching it.""" |
| 96 |
real = mod / 'realdir' |
| 97 |
rmtree(real) |
| 98 |
real.mkdir() |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
os.chmod(real, 0o777) |
| 104 |
rc = subprocess.run( |
| 105 |
rsync_argv(f'{src}/subdir/target.txt', f'{url}upload/realdir/'), |
| 106 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 107 |
).returncode |
| 108 |
landed = real / 'target.txt' |
| 109 |
if rc != 0 or not landed.is_file() \ |
| 110 |
or not filecmp.cmp(landed, src / 'subdir' / 'target.txt', shallow=False): |
| 111 |
test_fail(f"positive control: receiver did not write into an ordinary " |
| 112 |
f"in-module subdir (rc={rc}); attack scenarios would be vacuous") |
| 113 |
|
| 114 |
|
| 115 |
positive_control() |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
run_attack("single-file --size-only", |
| 121 |
'-tp', '--size-only', |
| 122 |
f'{src}/target.txt', |
| 123 |
f'{url}upload/subdir/target.txt') |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
run_attack("-r --size-only into subdir/", |
| 128 |
'-rtp', '--size-only', |
| 129 |
f'{src}/subdir/', |
| 130 |
f'{url}upload/subdir/') |
| 131 |
|
| 132 |
|
| 133 |
run_attack("-r without --size-only into subdir/", |
| 134 |
'-rtp', |
| 135 |
f'{src}/subdir/', |
| 136 |
f'{url}upload/subdir/') |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
run_attack("-r --size-only into upload/ root", |
| 141 |
'-rtp', '--size-only', |
| 142 |
f'{src}/', |
| 143 |
f'{url}upload/') |