| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import os |
| 18 |
import subprocess |
| 19 |
import time |
| 20 |
|
| 21 |
from rsyncfns import ( |
| 22 |
SCRATCHDIR, race_budget, find_attacker_uid, |
| 23 |
rmtree, rsync_argv, start_c_flipper, stop_flipper, test_fail, test_skipped, |
| 24 |
) |
| 25 |
|
| 26 |
if os.geteuid() != 0: |
| 27 |
test_skipped("requires root to plant a copy-dest symlink owned by a non-self uid") |
| 28 |
ATT_UID = find_attacker_uid() |
| 29 |
if ATT_UID is None: |
| 30 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 31 |
|
| 32 |
NFILES = 60 |
| 33 |
PINNED = 1000000000 |
| 34 |
|
| 35 |
base = SCRATCHDIR / 'copydest-readleak' |
| 36 |
src = base / 'src' |
| 37 |
dest = base / 'dest' |
| 38 |
cdroot = base / 'cdroot' |
| 39 |
cd = cdroot / 'cd' |
| 40 |
cdlink = cdroot / '.cdlink' |
| 41 |
outside = base / 'outside' |
| 42 |
|
| 43 |
SECRET = 'OUT-OF-TREE-SECRET\n' |
| 44 |
|
| 45 |
|
| 46 |
def build(): |
| 47 |
rmtree(base) |
| 48 |
for d in (src, dest, cdroot, outside): |
| 49 |
d.mkdir(parents=True) |
| 50 |
cd.mkdir() |
| 51 |
for i in range(NFILES): |
| 52 |
(src / f'f{i}').write_text('SRC\n') |
| 53 |
(cd / f'f{i}').write_text('BAS\n') |
| 54 |
(outside / f'f{i}').write_text(SECRET) |
| 55 |
for p in (src / f'f{i}', cd / f'f{i}', outside / f'f{i}'): |
| 56 |
os.utime(p, (PINNED, PINNED)) |
| 57 |
os.symlink(str(outside), cdlink) |
| 58 |
os.lchown(cdlink, ATT_UID, ATT_UID) |
| 59 |
|
| 60 |
|
| 61 |
def push(): |
| 62 |
return subprocess.run( |
| 63 |
rsync_argv('-rt', f'--copy-dest={cd}', f'{src}/', f'{dest}/'), |
| 64 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 65 |
|
| 66 |
|
| 67 |
def leaked(): |
| 68 |
for i in range(NFILES): |
| 69 |
try: |
| 70 |
if (dest / f'f{i}').read_text() == SECRET: |
| 71 |
return f'f{i}' |
| 72 |
except OSError: |
| 73 |
pass |
| 74 |
return None |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
build() |
| 79 |
proc = push() |
| 80 |
if proc.returncode != 0: |
| 81 |
test_fail("positive control: clean --copy-dest transfer failed " |
| 82 |
f"(rc={proc.returncode}):\n{proc.stdout or ''}") |
| 83 |
for i in range(NFILES): |
| 84 |
target = dest / f'f{i}' |
| 85 |
if not target.is_file(): |
| 86 |
test_fail(f"positive control: --copy-dest did not create {target}") |
| 87 |
got = target.read_text() |
| 88 |
if got != 'BAS\n': |
| 89 |
test_fail( |
| 90 |
"positive control: clean --copy-dest did not copy the in-tree " |
| 91 |
f"basis for {target} (got {got!r}); the test would not exercise " |
| 92 |
"copy_file()'s basis read path") |
| 93 |
|
| 94 |
flip = start_c_flipper(cd, cdlink) |
| 95 |
try: |
| 96 |
deadline = time.monotonic() + race_budget(10.0) |
| 97 |
while time.monotonic() < deadline: |
| 98 |
rmtree(dest) |
| 99 |
dest.mkdir() |
| 100 |
push() |
| 101 |
hit = leaked() |
| 102 |
if hit: |
| 103 |
test_fail( |
| 104 |
"--copy-dest parent symlink race: out-of-tree basis content was " |
| 105 |
f"read into the destination ({hit}) -- copy_file followed a " |
| 106 |
"flipped foreign-owned --copy-dest parent symlink (read-leak).") |
| 107 |
finally: |
| 108 |
stop_flipper(flip) |
| 109 |
|
| 110 |
|