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