| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import os |
| 14 |
import subprocess |
| 15 |
|
| 16 |
from rsyncfns import ( |
| 17 |
SCRATCHDIR, |
| 18 |
rsync_argv, get_testuid, get_rootuid, get_rootgid, |
| 19 |
rmtree, start_test_daemon, test_fail, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
DAEMON_PORT = 12881 |
| 24 |
|
| 25 |
|
| 26 |
mod = SCRATCHDIR / 'module' |
| 27 |
outside = SCRATCHDIR / 'outside' |
| 28 |
listfile = SCRATCHDIR / 'listed.txt' |
| 29 |
conf = SCRATCHDIR / 'test-rsyncd.conf' |
| 30 |
|
| 31 |
rmtree(mod) |
| 32 |
rmtree(outside) |
| 33 |
mod.mkdir(parents=True) |
| 34 |
outside.mkdir(parents=True) |
| 35 |
|
| 36 |
(outside / 'leak_marker.txt').write_text( |
| 37 |
"OUTSIDE_PROTECTED_FILE_USED_AS_LEAK_DETECTOR\n" |
| 38 |
) |
| 39 |
os.chmod(outside / 'leak_marker.txt', 0o644) |
| 40 |
|
| 41 |
os.symlink(str(outside), mod / 'cd') |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
(mod / 'realdir').mkdir() |
| 46 |
(mod / 'realdir' / 'in_module.txt').write_text("INSIDE_THE_MODULE\n") |
| 47 |
|
| 48 |
my_uid = get_testuid() |
| 49 |
root_uid = get_rootuid() |
| 50 |
root_gid = get_rootgid() |
| 51 |
uid_line = f"uid = {root_uid}" |
| 52 |
gid_line = f"gid = {root_gid}" |
| 53 |
if my_uid != root_uid: |
| 54 |
uid_line = '#' + uid_line |
| 55 |
gid_line = '#' + gid_line |
| 56 |
|
| 57 |
conf.write_text(f"""\ |
| 58 |
use chroot = no |
| 59 |
{uid_line} |
| 60 |
{gid_line} |
| 61 |
log file = {SCRATCHDIR}/rsyncd.log |
| 62 |
[upload] |
| 63 |
path = {mod} |
| 64 |
use chroot = no |
| 65 |
read only = no |
| 66 |
""") |
| 67 |
|
| 68 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
ctl = subprocess.run( |
| 74 |
rsync_argv('-nrv', f'{url}upload/realdir/', f'{SCRATCHDIR}/dst/'), |
| 75 |
capture_output=True, text=True, |
| 76 |
) |
| 77 |
if ctl.returncode != 0 or 'in_module.txt' not in ctl.stdout: |
| 78 |
test_fail("positive control: listing an in-module path did not enumerate " |
| 79 |
f"in_module.txt (rc={ctl.returncode}); leak check would be vacuous" |
| 80 |
f"\n{ctl.stdout}{ctl.stderr}") |
| 81 |
|
| 82 |
proc = subprocess.run( |
| 83 |
rsync_argv('-nrv', f'{url}upload/cd/', f'{SCRATCHDIR}/dst/'), |
| 84 |
capture_output=True, text=True, |
| 85 |
) |
| 86 |
if proc.returncode >= 128: |
| 87 |
test_fail(f"leak pull: rsync died from a signal (rc={proc.returncode})") |
| 88 |
listfile.write_text(proc.stdout + proc.stderr) |
| 89 |
|
| 90 |
if 'leak_marker.txt' in listfile.read_text(): |
| 91 |
import sys |
| 92 |
sys.stderr.write("----- leaked listing follows\n") |
| 93 |
for line in listfile.read_text().splitlines(): |
| 94 |
sys.stderr.write(f" {line}\n") |
| 95 |
sys.stderr.write("----- leaked listing ends\n") |
| 96 |
test_fail( |
| 97 |
"sender flist leak: outside/leak_marker.txt was enumerated to " |
| 98 |
"the client (daemon's chdir followed the cd symlink during flist " |
| 99 |
"generation)" |
| 100 |
) |