| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import os |
| 12 |
import subprocess |
| 13 |
import time |
| 14 |
|
| 15 |
from rsyncfns import ( |
| 16 |
SCRATCHDIR, race_budget, find_attacker_uid, rmtree, |
| 17 |
start_c_flipper, stop_flipper, test_fail, test_skipped, |
| 18 |
) |
| 19 |
|
| 20 |
if os.geteuid() != 0: |
| 21 |
test_skipped("requires root to plant a backup-dir symlink owned by a non-self uid") |
| 22 |
|
| 23 |
ATT_UID = find_attacker_uid() |
| 24 |
if ATT_UID is None: |
| 25 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
NFILES = 50 |
| 30 |
base = SCRATCHDIR / 'bdir-race' |
| 31 |
src = base / 'src' |
| 32 |
dest = base / 'dest' |
| 33 |
backup = base / 'backup' |
| 34 |
outside = base / 'outside' |
| 35 |
sub = backup / 'sub' |
| 36 |
sublink = backup / '.sublink' |
| 37 |
|
| 38 |
|
| 39 |
def build(): |
| 40 |
"""Reset the workspace. Call only while the flipper is stopped.""" |
| 41 |
rmtree(base) |
| 42 |
for d in (src / 'sub', dest / 'sub', backup, outside): |
| 43 |
d.mkdir(parents=True, exist_ok=True) |
| 44 |
|
| 45 |
for i in range(NFILES): |
| 46 |
|
| 47 |
|
| 48 |
(sub / f'sub{i}').mkdir(parents=True, exist_ok=True) |
| 49 |
|
| 50 |
|
| 51 |
(outside / f'sub{i}').mkdir(exist_ok=True) |
| 52 |
(src / 'sub' / f'sub{i}').write_text('payload') |
| 53 |
(dest / 'sub' / f'sub{i}').write_text('different') |
| 54 |
|
| 55 |
os.symlink(str(outside), sublink) |
| 56 |
os.lchown(sublink, ATT_UID, ATT_UID) |
| 57 |
|
| 58 |
|
| 59 |
def push(): |
| 60 |
"""Runs a blocking local rsync push invocation.""" |
| 61 |
return subprocess.run( |
| 62 |
['./rsync', '-a', '-b', f'--backup-dir={backup}', |
| 63 |
f'{src}/', f'{dest}/'], |
| 64 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
def escaped() -> bool: |
| 69 |
"""True if any outside sentinel directory was removed (escaped rmdir).""" |
| 70 |
for i in range(NFILES): |
| 71 |
if not (outside / f'sub{i}').is_dir(): |
| 72 |
return True |
| 73 |
return False |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
build() |
| 81 |
proc = push() |
| 82 |
if proc.returncode != 0: |
| 83 |
test_fail(f"positive control: clean --backup-dir run failed (rc={proc.returncode}):\n{proc.stdout or ''}") |
| 84 |
if not (sub / 'sub0').is_file() or (sub / 'sub0').read_text() != 'different': |
| 85 |
test_fail("positive control: the old destination file was not backed up over the " |
| 86 |
"pre-existing backup directory; the backup rmdir path was not exercised") |
| 87 |
if escaped(): |
| 88 |
test_fail("positive control: an outside sentinel dir vanished during a no-flipper run") |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
deadline = time.monotonic() + race_budget(10.0) |
| 94 |
flip = None |
| 95 |
|
| 96 |
try: |
| 97 |
while time.monotonic() < deadline: |
| 98 |
if flip is not None: |
| 99 |
stop_flipper(flip) |
| 100 |
flip = None |
| 101 |
build() |
| 102 |
flip = start_c_flipper(sub, sublink) |
| 103 |
push() |
| 104 |
|
| 105 |
if escaped(): |
| 106 |
test_fail( |
| 107 |
"--backup-dir parent symlink race: a backup rmdir escaped the tree, " |
| 108 |
f"removing a sentinel directory under {outside}; rsync followed the " |
| 109 |
"flipped attacker-owned backup/sub component instead of refusing it." |
| 110 |
) |
| 111 |
finally: |
| 112 |
if flip is not None: |
| 113 |
stop_flipper(flip) |
| 114 |
|
| 115 |
print("operator-path-backup-rmdir: backup rmdir confined under parent-swap race") |