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