| 1 |
|
| 2 |
import os |
| 3 |
import platform |
| 4 |
import subprocess |
| 5 |
import time |
| 6 |
|
| 7 |
from rsyncfns import ( |
| 8 |
race_budget, SCRATCHDIR, rmtree, rsync_argv, |
| 9 |
start_path_flipper, start_test_daemon, stop_flipper, test_fail, test_xfail, |
| 10 |
write_daemon_conf, |
| 11 |
) |
| 12 |
|
| 13 |
_CYGWIN = platform.system().startswith('CYGWIN') |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
base = SCRATCHDIR / 'sender-remove-source' |
| 20 |
mod = base / 'module' |
| 21 |
outside = base / 'outside' |
| 22 |
dest = base / 'dest' |
| 23 |
rmtree(base) |
| 24 |
(mod / 'real').mkdir(parents=True) |
| 25 |
outside.mkdir(parents=True) |
| 26 |
dest.mkdir(parents=True) |
| 27 |
|
| 28 |
inside_file = mod / 'real' / 'file' |
| 29 |
outside_file = outside / 'file' |
| 30 |
inside_file.write_text('payload\n') |
| 31 |
outside_file.write_text('payload\n') |
| 32 |
st = inside_file.stat() |
| 33 |
os.utime(outside_file, (st.st_atime, st.st_mtime)) |
| 34 |
os.symlink(outside, mod / 'evil') |
| 35 |
|
| 36 |
conf = write_daemon_conf([ |
| 37 |
('src', {'path': str(mod), 'read only': 'no', 'use chroot': 'no'}), |
| 38 |
]) |
| 39 |
url = start_test_daemon(conf, 12937) |
| 40 |
|
| 41 |
flip = start_path_flipper(mod / 'real', mod / 'evil') |
| 42 |
deadline = time.monotonic() + race_budget() |
| 43 |
try: |
| 44 |
while time.monotonic() < deadline and outside_file.exists(): |
| 45 |
if not inside_file.exists() and (mod / 'real').is_dir() and not os.path.islink(mod / 'real'): |
| 46 |
try: |
| 47 |
inside_file.write_text('payload\n') |
| 48 |
os.utime(inside_file, (st.st_atime, st.st_mtime)) |
| 49 |
except FileNotFoundError: |
| 50 |
pass |
| 51 |
subprocess.run( |
| 52 |
rsync_argv('-a', '--remove-source-files', f'{url}src/real/file', str(dest) + '/'), |
| 53 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True) |
| 54 |
finally: |
| 55 |
stop_flipper(flip) |
| 56 |
|
| 57 |
if not outside_file.exists(): |
| 58 |
if _CYGWIN: |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
test_xfail("cygwin: --remove-source-files parent-flip race still unlinks " |
| 67 |
"the outside victim -- documented Cygwin platform residual") |
| 68 |
test_fail("daemon sender --remove-source-files cleanup unlinked the outside victim through a raced parent symlink") |
| 69 |
|
| 70 |
print("sender-remove-source-secure: remove-source cleanup did not unlink outside the module") |