| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import os |
| 25 |
import shlex |
| 26 |
import pwd |
| 27 |
import subprocess |
| 28 |
import time |
| 29 |
|
| 30 |
from rsyncfns import ( |
| 31 |
FROMDIR, SCRATCHDIR, make_tree, makepath, rmtree, rsync_argv, |
| 32 |
start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 33 |
) |
| 34 |
|
| 35 |
DAEMON_PORT = 12904 |
| 36 |
SANITY_MARKER = "EARLY_INPUT_SANITY_DELIVERED" |
| 37 |
LEAK_MARKER = "EARLY_INPUT_LEAK_MARKER_DO_NOT_DISCLOSE" |
| 38 |
|
| 39 |
|
| 40 |
if os.geteuid() != 0: |
| 41 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 42 |
"(the attacker simulation)") |
| 43 |
|
| 44 |
NOBODY_UID = None |
| 45 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 46 |
try: |
| 47 |
u = pwd.getpwnam(name).pw_uid |
| 48 |
if u != 0 and u != os.geteuid(): |
| 49 |
NOBODY_UID = u |
| 50 |
break |
| 51 |
except KeyError: |
| 52 |
continue |
| 53 |
if NOBODY_UID is None: |
| 54 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 55 |
|
| 56 |
src = FROMDIR |
| 57 |
rmtree(src) |
| 58 |
make_tree(src, depth=1) |
| 59 |
|
| 60 |
base = SCRATCHDIR / 'earlyinput' |
| 61 |
rmtree(base) |
| 62 |
base.mkdir() |
| 63 |
moddir = base / 'mod' |
| 64 |
moddir.mkdir() |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
capture = base / 'early_capture' |
| 69 |
hook = base / 'earlyhook.sh' |
| 70 |
hook.write_text(f'#!/bin/sh\ncat > {shlex.quote(str(capture))} 2>/dev/null\nexit 0\n') |
| 71 |
hook.chmod(0o755) |
| 72 |
|
| 73 |
conf = write_daemon_conf([ |
| 74 |
('mod', {'path': moddir, 'read only': 'yes', 'early exec': str(hook)}), |
| 75 |
]) |
| 76 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 77 |
|
| 78 |
|
| 79 |
def list_with_early_input(path): |
| 80 |
"""List the module with --early-input=path; return the CompletedProcess.""" |
| 81 |
if capture.exists(): |
| 82 |
capture.unlink() |
| 83 |
return subprocess.run( |
| 84 |
rsync_argv('--early-input=' + str(path), f'{url}mod/'), |
| 85 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 86 |
|
| 87 |
|
| 88 |
def captured_contains(marker, secs=5): |
| 89 |
deadline = time.monotonic() + secs |
| 90 |
while time.monotonic() < deadline: |
| 91 |
if capture.is_file(): |
| 92 |
try: |
| 93 |
if marker in capture.read_text(errors='replace'): |
| 94 |
return True |
| 95 |
except OSError: |
| 96 |
pass |
| 97 |
time.sleep(0.05) |
| 98 |
return False |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
real = base / 'real_early' |
| 103 |
real.write_text(SANITY_MARKER + '\n') |
| 104 |
real.chmod(0o600) |
| 105 |
list_with_early_input(real) |
| 106 |
if not captured_contains(SANITY_MARKER): |
| 107 |
test_fail( |
| 108 |
"sanity check failed: a non-symlinked --early-input file was not " |
| 109 |
"delivered to the daemon's early-exec hook; the test machinery is " |
| 110 |
f"broken. stderr/capture: {capture.read_text(errors='replace') if capture.exists() else '(none)'!r}") |
| 111 |
|
| 112 |
|
| 113 |
victim = base / 'victim_early' |
| 114 |
victim.write_text(LEAK_MARKER + '\n') |
| 115 |
os.chmod(victim, 0o600) |
| 116 |
|
| 117 |
|
| 118 |
leaf_plant = base / 'early_leaf' |
| 119 |
os.symlink(victim, leaf_plant) |
| 120 |
os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID) |
| 121 |
list_with_early_input(leaf_plant) |
| 122 |
if captured_contains(LEAK_MARKER, secs=2): |
| 123 |
test_fail( |
| 124 |
f"--early-input followed a planted LEAF symlink (owned by uid " |
| 125 |
f"{NOBODY_UID}): the client read {victim} via the symlink and forwarded " |
| 126 |
f"its contents to the daemon, where the marker {LEAK_MARKER!r} landed in " |
| 127 |
f"the early-exec hook's stdin. This is a content-disclosure primitive. " |
| 128 |
f"Fix: refuse planted symlinks at the --early-input open.") |
| 129 |
|
| 130 |
|
| 131 |
parent_real = base / 'parent_real' |
| 132 |
parent_real.mkdir() |
| 133 |
(parent_real / 'victim_in_target').write_text(LEAK_MARKER + '\n') |
| 134 |
os.chmod(parent_real / 'victim_in_target', 0o600) |
| 135 |
parent_plant = base / 'parent_link' |
| 136 |
os.symlink(parent_real, parent_plant) |
| 137 |
os.lchown(parent_plant, NOBODY_UID, NOBODY_UID) |
| 138 |
list_with_early_input(parent_plant / 'victim_in_target') |
| 139 |
if captured_contains(LEAK_MARKER, secs=2): |
| 140 |
test_fail( |
| 141 |
f"--early-input followed a planted PARENT-component symlink (owned by " |
| 142 |
f"uid {NOBODY_UID}): {parent_plant} -> {parent_real} was traversed and " |
| 143 |
f"victim_in_target forwarded to the daemon ({LEAK_MARKER!r} in the " |
| 144 |
f"early-exec capture). This is the /tmp/somedir/-class parent-flip case. " |
| 145 |
f"Fix: per-component path walk refusing parent symlinks owned by " |
| 146 |
f"untrusted uids.") |
| 147 |
|
| 148 |
print("early-input-symlink: leaf + parent plants refused") |