| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
me |
| 7 |
ia |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
import os |
| 29 |
import pwd |
| 30 |
import subprocess |
| 31 |
|
| 32 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped |
| 33 |
|
| 34 |
|
| 35 |
if os.geteuid() != 0: |
| 36 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 37 |
"(the attacker simulation)") |
| 38 |
|
| 39 |
NOBODY_UID = None |
| 40 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 41 |
try: |
| 42 |
u = pwd.getpwnam(name).pw_uid |
| 43 |
if u != 0 and u != os.geteuid(): |
| 44 |
NOBODY_UID = u |
| 45 |
break |
| 46 |
except KeyError: |
| 47 |
continue |
| 48 |
if NOBODY_UID is None: |
| 49 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 50 |
|
| 51 |
|
| 52 |
base = SCRATCHDIR / 'excludefrom' |
| 53 |
src = base / 'src' |
| 54 |
dest = base / 'dest' |
| 55 |
outside = base / 'outside' |
| 56 |
|
| 57 |
rmtree(base) |
| 58 |
src.mkdir(parents=True) |
| 59 |
(src / 'canary_file').write_text("would-be-transferred\n") |
| 60 |
(src / 'other_file').write_text("noise\n") |
| 61 |
dest.mkdir(parents=True) |
| 62 |
outside.mkdir(parents=True) |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
(outside / 'exclude_list').write_text("canary_file\n") |
| 68 |
|
| 69 |
|
| 70 |
def run_excludefrom(plant_path): |
| 71 |
rmtree(dest) |
| 72 |
dest.mkdir(parents=True) |
| 73 |
return subprocess.run( |
| 74 |
rsync_argv('-a', '--exclude-from=' + str(plant_path), |
| 75 |
f'{src}/', f'{dest}/'), |
| 76 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 77 |
|
| 78 |
|
| 79 |
plants = base / 'plants' |
| 80 |
plants.mkdir() |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
leaf_plant = plants / 'exclude_leaf' |
| 87 |
os.symlink(outside / 'exclude_list', leaf_plant) |
| 88 |
os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID) |
| 89 |
|
| 90 |
leaf = run_excludefrom(leaf_plant) |
| 91 |
canary = dest / 'canary_file' |
| 92 |
if leaf.returncode == 0 and not canary.exists(): |
| 93 |
test_fail( |
| 94 |
f"--exclude-from followed a planted LEAF symlink (owned by uid " |
| 95 |
f"{NOBODY_UID}): rsync read the symlink's target as filter rules and " |
| 96 |
f"applied the leaked pattern, dropping src/canary_file from the " |
| 97 |
f"transfer. Fix: refuse planted symlinks at --exclude-from.") |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
real_target = plants / 'parent_real_target' |
| 104 |
real_target.mkdir() |
| 105 |
(real_target / 'exclude_list').write_text("canary_file\n") |
| 106 |
|
| 107 |
parent_plant = plants / 'parent_link' |
| 108 |
os.symlink(real_target, parent_plant) |
| 109 |
os.lchown(parent_plant, NOBODY_UID, NOBODY_UID) |
| 110 |
|
| 111 |
parent = run_excludefrom(parent_plant / 'exclude_list') |
| 112 |
canary = dest / 'canary_file' |
| 113 |
if parent.returncode == 0 and not canary.exists(): |
| 114 |
test_fail( |
| 115 |
f"--exclude-from followed a planted PARENT-component symlink (owned " |
| 116 |
f"by uid {NOBODY_UID}): {parent_plant} -> {real_target} was " |
| 117 |
f"traversed and the leaked exclude_list was applied, dropping " |
| 118 |
f"canary_file from the transfer. /tmp/somedir/-class parent-flip " |
| 119 |
"that plain O_NOFOLLOW does NOT defend. Fix: per-component path walk " |
| 120 |
"with parent-symlink ownership check.") |