| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
→ |
| 9 |
|
| 10 |
). |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
P. |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
import os |
| 34 |
import pwd |
| 35 |
import subprocess |
| 36 |
|
| 37 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped |
| 38 |
|
| 39 |
|
| 40 |
if os.geteuid() != 0: |
| 41 |
test_skipped("requires root to plant a symlink owned by a non-root, " |
| 42 |
"non-self uid (the attacker simulation)") |
| 43 |
|
| 44 |
NOBODY_UID = None |
| 45 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 46 |
try: |
| 47 |
NOBODY_UID = pwd.getpwnam(name).pw_uid |
| 48 |
if NOBODY_UID != 0 and NOBODY_UID != os.geteuid(): |
| 49 |
break |
| 50 |
except KeyError: |
| 51 |
continue |
| 52 |
if NOBODY_UID is None or NOBODY_UID == 0 or NOBODY_UID == os.geteuid(): |
| 53 |
test_skipped("no untrusted-uid user available for cross-uid plant " |
| 54 |
"(tried nobody, nfsnobody, daemon)") |
| 55 |
|
| 56 |
|
| 57 |
base = SCRATCHDIR / 'logfile' |
| 58 |
src = base / 'src' |
| 59 |
dest = base / 'dest' |
| 60 |
plants = base / 'plants' |
| 61 |
|
| 62 |
rmtree(base) |
| 63 |
src.mkdir(parents=True) |
| 64 |
(src / 'f').write_text("payload\n") |
| 65 |
dest.mkdir(parents=True) |
| 66 |
plants.mkdir(parents=True) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
sentinel = plants / 'sentinel_leaf' |
| 75 |
sentinel.write_text("LEAF_SENTINEL_INITIAL\n") |
| 76 |
os.chmod(sentinel, 0o600) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
leaf_path = plants / 'log_leaf' |
| 81 |
os.symlink(sentinel, leaf_path) |
| 82 |
os.lchown(leaf_path, NOBODY_UID, NOBODY_UID) |
| 83 |
|
| 84 |
subprocess.run( |
| 85 |
rsync_argv('-a', '--log-file=' + str(leaf_path), f'{src}/', f'{dest}/'), |
| 86 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 87 |
|
| 88 |
leaf_after = sentinel.read_text() |
| 89 |
if leaf_after != "LEAF_SENTINEL_INITIAL\n": |
| 90 |
test_fail( |
| 91 |
f"--log-file followed a planted LEAF symlink (owned by uid " |
| 92 |
f"{NOBODY_UID}): {sentinel} changed from 'LEAF_SENTINEL_INITIAL' to " |
| 93 |
f"{leaf_after!r}. The privileged rsync appended its log into the " |
| 94 |
f"attacker-chosen file. Fix: refuse planted symlinks at --log-file.") |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
real_target = plants / 'parent_real_target' |
| 103 |
real_target.mkdir() |
| 104 |
|
| 105 |
|
| 106 |
→ |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
parent_plant = plants / 'parent_link' |
| 112 |
os.symlink(real_target, parent_plant) |
| 113 |
os.lchown(parent_plant, NOBODY_UID, NOBODY_UID) |
| 114 |
|
| 115 |
log_via_parent = parent_plant / 'rsync.log' |
| 116 |
subprocess.run( |
| 117 |
rsync_argv('-a', '--log-file=' + str(log_via_parent), f'{src}/', f'{dest}/'), |
| 118 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 119 |
|
| 120 |
|
| 121 |
outside_log = real_target / 'rsync.log' |
| 122 |
if outside_log.exists(): |
| 123 |
test_fail( |
| 124 |
f"--log-file followed a planted PARENT-component symlink (owned by " |
| 125 |
f"uid {NOBODY_UID}): {parent_plant} -> {real_target} was traversed " |
| 126 |
f"and rsync's log was created at {outside_log}. This is the " |
| 127 |
"/tmp/somedir/-class parent-flip case that plain O_NOFOLLOW does NOT " |
| 128 |
"defend (O_NOFOLLOW is leaf-only). Fix: per-component path walk that " |
| 129 |
"refuses parent symlinks owned by an untrusted uid.") |