| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import os |
| 19 |
import pwd |
| 20 |
import subprocess |
| 21 |
|
| 22 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail, test_skipped |
| 23 |
|
| 24 |
if os.geteuid() != 0: |
| 25 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 26 |
"(simulates 'root runs rsync -a src/ dest/' over an " |
| 27 |
"unprivileged user's directory)") |
| 28 |
|
| 29 |
ATT_UID = None |
| 30 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 31 |
try: |
| 32 |
u = pwd.getpwnam(name).pw_uid |
| 33 |
if u != 0 and u != os.geteuid(): |
| 34 |
ATT_UID = u |
| 35 |
break |
| 36 |
except KeyError: |
| 37 |
continue |
| 38 |
if ATT_UID is None: |
| 39 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 40 |
|
| 41 |
|
| 42 |
def run(cwd): |
| 43 |
subprocess.run(rsync_argv('-a', 'src/', 'dest/'), cwd=str(cwd), |
| 44 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
base = SCRATCHDIR / 'destchdir-rel' |
| 49 |
src = base / 'src' |
| 50 |
dest = base / 'dest' |
| 51 |
outside = base / 'outside' |
| 52 |
rmtree(base) |
| 53 |
(src / 'sub').mkdir(parents=True) |
| 54 |
for i in range(4): |
| 55 |
(src / 'sub' / f'f{i}').write_text("payload\n") |
| 56 |
outside.mkdir(parents=True) |
| 57 |
os.symlink(outside, dest) |
| 58 |
os.lchown(dest, ATT_UID, ATT_UID) |
| 59 |
|
| 60 |
run(base) |
| 61 |
|
| 62 |
escaped = sorted(p.name for p in outside.iterdir()) |
| 63 |
if escaped: |
| 64 |
test_fail( |
| 65 |
"relative destination chdir followed an ATTACKER-owned symlink: the " |
| 66 |
f"receiver wrote files OUTSIDE the tree ({escaped}). change_dir() must " |
| 67 |
"refuse a relative dest symlink not owned by uid 0 or the euid.") |
| 68 |
|
| 69 |
|
| 70 |
base2 = SCRATCHDIR / 'destchdir-rel-admin' |
| 71 |
src2 = base2 / 'src' |
| 72 |
real = base2 / 'realdest' |
| 73 |
rmtree(base2) |
| 74 |
(src2 / 'sub').mkdir(parents=True) |
| 75 |
(src2 / 'sub' / 'f0').write_text("payload\n") |
| 76 |
real.mkdir(parents=True) |
| 77 |
os.symlink(real, base2 / 'dest') |
| 78 |
|
| 79 |
run(base2) |
| 80 |
|
| 81 |
if not (real / 'sub' / 'f0').is_file(): |
| 82 |
test_fail( |
| 83 |
"a root-owned symlinked destination (the /backup->/mnt/disk admin " |
| 84 |
"pattern) was NOT followed -- the cross-uid defense must still follow " |
| 85 |
"the operator's own symlinked dest.") |