| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
an |
| 9 |
ed |
| 10 |
|
| 11 |
|
| 12 |
en |
| 13 |
a |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
import os |
| 28 |
import pwd |
| 29 |
import subprocess |
| 30 |
|
| 31 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped |
| 32 |
|
| 33 |
|
| 34 |
if os.geteuid() != 0: |
| 35 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 36 |
"(the attacker simulation)") |
| 37 |
|
| 38 |
NOBODY_UID = None |
| 39 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 40 |
try: |
| 41 |
u = pwd.getpwnam(name).pw_uid |
| 42 |
if u != 0 and u != os.geteuid(): |
| 43 |
NOBODY_UID = u |
| 44 |
break |
| 45 |
except KeyError: |
| 46 |
continue |
| 47 |
if NOBODY_UID is None: |
| 48 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 49 |
|
| 50 |
|
| 51 |
base = SCRATCHDIR / 'batch' |
| 52 |
src = base / 'src' |
| 53 |
dest = base / 'dest' |
| 54 |
plants = base / 'plants' |
| 55 |
|
| 56 |
rmtree(base) |
| 57 |
src.mkdir(parents=True) |
| 58 |
(src / 'f').write_text("payload\n") |
| 59 |
dest.mkdir(parents=True) |
| 60 |
plants.mkdir(parents=True) |
| 61 |
|
| 62 |
|
| 63 |
def run_write_batch(batch_plant_path): |
| 64 |
"""Run rsync with --write-batch=batch_plant_path. The actual transfer |
| 65 |
succeeds; we only care whether the batch open follows the planted symlink.""" |
| 66 |
|
| 67 |
rmtree(dest); dest.mkdir(parents=True) |
| 68 |
return subprocess.run( |
| 69 |
rsync_argv('-a', '--write-batch=' + str(batch_plant_path), |
| 70 |
f'{src}/', f'{dest}/'), |
| 71 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
he |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
victim = plants / 'sentinel_leaf' |
| 80 |
INITIAL_BYTES = b"VICTIM_INITIAL_CONTENTS\n" |
| 81 |
victim.write_bytes(INITIAL_BYTES) |
| 82 |
os.chmod(victim, 0o600) |
| 83 |
|
| 84 |
leaf_plant = plants / 'write_batch_leaf' |
| 85 |
os.symlink(victim, leaf_plant) |
| 86 |
os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID) |
| 87 |
|
| 88 |
run_write_batch(leaf_plant) |
| 89 |
|
| 90 |
t. |
| 91 |
leaf_after = victim.read_bytes() |
| 92 |
if leaf_after != INITIAL_BYTES: |
| 93 |
test_fail( |
| 94 |
f"--write-batch followed a planted LEAF symlink (owned by uid " |
| 95 |
f"{NOBODY_UID}): victim {victim} was truncated/overwritten by the " |
| 96 |
f"O_TRUNC open. {len(INITIAL_BYTES)} initial bytes replaced by " |
| 97 |
f"{len(leaf_after)} bytes of attacker-influenceable batch data. " |
| 98 |
f"Fix: refuse planted symlinks at --write-batch.") |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
a |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
real_target = plants / 'parent_real_target' |
| 108 |
real_target.mkdir() |
| 109 |
|
| 110 |
parent_plant = plants / 'parent_link' |
| 111 |
os.symlink(real_target, parent_plant) |
| 112 |
os.lchown(parent_plant, NOBODY_UID, NOBODY_UID) |
| 113 |
|
| 114 |
run_write_batch(parent_plant / 'batchfile') |
| 115 |
|
| 116 |
|
| 117 |
created = sorted(p.name for p in real_target.iterdir()) |
| 118 |
if created: |
| 119 |
test_fail( |
| 120 |
f"--write-batch followed a planted PARENT-component symlink (owned " |
| 121 |
f"by uid {NOBODY_UID}): {parent_plant} -> {real_target} was " |
| 122 |
f"traversed and rsync wrote batch artefact(s) into the victim dir: " |
| 123 |
f"{created}. /tmp/somedir/-class parent-flip; plain O_NOFOLLOW does " |
| 124 |
"NOT defend. Fix: per-component path walk with parent-symlink " |
| 125 |
"ownership check.") |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
ch |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
legit_src = base / 'rb_src' |
| 142 |
legit_src.mkdir() |
| 143 |
(legit_src / 'rb_canary').write_bytes(b'AUDIT_READ_BATCH_LEAK_PAYLOAD') |
| 144 |
|
| 145 |
|
| 146 |
legit_batch_dir = base / 'rb_legit_batch' |
| 147 |
legit_batch_dir.mkdir() |
| 148 |
legit_batch = legit_batch_dir / 'batch' |
| 149 |
subprocess.run( |
| 150 |
rsync_argv('-a', f'--write-batch={legit_batch}', |
| 151 |
f'{legit_src}/', f'{dest}/rb_setup_unused/'), |
| 152 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 153 |
|
| 154 |
|
| 155 |
rb_dest = base / 'rb_dest' |
| 156 |
rmtree(rb_dest); rb_dest.mkdir() |
| 157 |
rb_leaf_plant = plants / 'read_batch_leaf' |
| 158 |
os.symlink(legit_batch, rb_leaf_plant) |
| 159 |
os.lchown(rb_leaf_plant, NOBODY_UID, NOBODY_UID) |
| 160 |
|
| 161 |
subprocess.run( |
| 162 |
rsync_argv('-a', f'--read-batch={rb_leaf_plant}', str(rb_dest) + '/'), |
| 163 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 164 |
|
| 165 |
if (rb_dest / 'rb_canary').exists(): |
| 166 |
test_fail( |
| 167 |
f"--read-batch followed a planted LEAF symlink (owned by uid " |
| 168 |
f"{NOBODY_UID}): rsync read the symlink's target as protocol data " |
| 169 |
f"and replayed the legitimate batch from it, creating " |
| 170 |
f"{rb_dest}/rb_canary in the destination. The leaked batch could " |
| 171 |
"have written anywhere the receiver can write (higher-power " |
| 172 |
"primitive than --write-batch). Fix: refuse planted symlinks at " |
| 173 |
"--read-batch.") |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
d, |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
import stat as _stat |
| 187 |
|
| 188 |
rb_dev = plants / 'read_batch_dev' |
| 189 |
try: |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
os.mknod(rb_dev, 0o600 | _stat.S_IFCHR, os.stat('/dev/null').st_rdev) |
| 196 |
except OSError as e: |
| 197 |
test_skipped( |
| 198 |
f"cannot mknod a char device for S_ISREG sub-test ({e}); " |
| 199 |
"likely no CAP_MKNOD or the filesystem disallows devnodes") |
| 200 |
|
| 201 |
rb_dev_dest = base / 'rb_dev_dest' |
| 202 |
rmtree(rb_dev_dest); rb_dev_dest.mkdir() |
| 203 |
proc = subprocess.run( |
| 204 |
rsync_argv('-a', f'--read-batch={rb_dev}', str(rb_dev_dest) + '/'), |
| 205 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
refused = ('is not a regular file' in proc.stderr |
| 213 |
or ('open error' in proc.stderr and proc.returncode != 0)) |
| 214 |
if not refused: |
| 215 |
test_fail( |
| 216 |
f"--read-batch did not refuse a non-regular batch path (char device at " |
| 217 |
f"{rb_dev}). rsync returncode {proc.returncode}, stderr: " |
| 218 |
f"{proc.stderr!r}. Fix: after the safe open, fstat() the fd and " |
| 219 |
"refuse !S_ISREG.") |