| 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 |
import os |
| 27 |
import pwd |
| 28 |
import subprocess |
| 29 |
|
| 30 |
from rsyncfns import ( |
| 31 |
FROMDIR, SCRATCHDIR, make_tree, makepath, rmtree, rsync_argv, |
| 32 |
start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 33 |
) |
| 34 |
|
| 35 |
DAEMON_PORT = 12902 |
| 36 |
USER = 'tuser' |
| 37 |
CORRECT_PASSWORD = 'correctpass' |
| 38 |
|
| 39 |
|
| 40 |
if os.geteuid() != 0: |
| 41 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 42 |
"(the attacker simulation)") |
| 43 |
|
| 44 |
NOBODY_UID = None |
| 45 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 46 |
try: |
| 47 |
u = pwd.getpwnam(name).pw_uid |
| 48 |
if u != 0 and u != os.geteuid(): |
| 49 |
NOBODY_UID = u |
| 50 |
break |
| 51 |
except KeyError: |
| 52 |
continue |
| 53 |
if NOBODY_UID is None: |
| 54 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
os.environ['RSYNC_PASSWORD'] = 'env-fallback-wrong' |
| 59 |
|
| 60 |
|
| 61 |
src = FROMDIR |
| 62 |
rmtree(src) |
| 63 |
make_tree(src, depth=1) |
| 64 |
|
| 65 |
authdir = SCRATCHDIR / 'authdest' |
| 66 |
secrets = SCRATCHDIR / 'rsyncd.secrets' |
| 67 |
secrets.write_text(f'{USER}:{CORRECT_PASSWORD}\n') |
| 68 |
secrets.chmod(0o600) |
| 69 |
|
| 70 |
conf = write_daemon_conf([ |
| 71 |
('auth', {'path': authdir, 'read only': 'no', |
| 72 |
'auth users': USER, 'secrets file': secrets}), |
| 73 |
]) |
| 74 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 75 |
userurl = url.replace('rsync://', f'rsync://{USER}@', 1) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
victim = SCRATCHDIR / 'victim_password' |
| 81 |
victim.write_text(CORRECT_PASSWORD + '\n') |
| 82 |
victim.chmod(0o600) |
| 83 |
|
| 84 |
|
| 85 |
def push_with_pwfile(pwfile_path): |
| 86 |
"""Run an rsync push via the daemon with --password-file=pwfile_path.""" |
| 87 |
rmtree(authdir) |
| 88 |
makepath(authdir) |
| 89 |
return subprocess.run( |
| 90 |
rsync_argv('-a', f'--password-file={pwfile_path}', |
| 91 |
f'{src}/', f'{userurl}auth/'), |
| 92 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
real_pwfile = SCRATCHDIR / 'real_pwfile' |
| 101 |
real_pwfile.write_text(CORRECT_PASSWORD + '\n') |
| 102 |
real_pwfile.chmod(0o600) |
| 103 |
|
| 104 |
sanity = push_with_pwfile(real_pwfile) |
| 105 |
if sanity.returncode != 0: |
| 106 |
test_fail( |
| 107 |
"sanity check failed: a non-symlinked password file with the correct " |
| 108 |
f"password could not authenticate. stderr: {sanity.stderr!r}") |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
plants = SCRATCHDIR / 'plants' |
| 115 |
rmtree(plants); plants.mkdir() |
| 116 |
leaf_plant = plants / 'pwfile_leaf' |
| 117 |
os.symlink(victim, leaf_plant) |
| 118 |
os.lchown(leaf_plant, NOBODY_UID, NOBODY_UID) |
| 119 |
|
| 120 |
leaf = push_with_pwfile(leaf_plant) |
| 121 |
if leaf.returncode == 0: |
| 122 |
test_fail( |
| 123 |
f"--password-file followed a planted LEAF symlink (owned by uid " |
| 124 |
f"{NOBODY_UID}): rsync read {victim} via the symlink and sent its " |
| 125 |
f"content as the auth password; the daemon accepted -> transfer " |
| 126 |
f"succeeded. This is a content-disclosure primitive (the leaked " |
| 127 |
f"bytes are sent on the wire). Fix: refuse planted symlinks at " |
| 128 |
f"--password-file.") |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
real_target = plants / 'parent_real_target' |
| 135 |
real_target.mkdir() |
| 136 |
(real_target / 'victim_in_target').write_text(CORRECT_PASSWORD + '\n') |
| 137 |
os.chmod(real_target / 'victim_in_target', 0o600) |
| 138 |
|
| 139 |
parent_plant = plants / 'parent_link' |
| 140 |
os.symlink(real_target, parent_plant) |
| 141 |
os.lchown(parent_plant, NOBODY_UID, NOBODY_UID) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
parent = push_with_pwfile(parent_plant / 'victim_in_target') |
| 148 |
if parent.returncode == 0: |
| 149 |
test_fail( |
| 150 |
f"--password-file followed a planted PARENT-component symlink (owned " |
| 151 |
f"by uid {NOBODY_UID}): {parent_plant} -> {real_target} was traversed " |
| 152 |
f"and rsync read victim_in_target via the planted parent; the daemon " |
| 153 |
f"accepted the leaked password -> transfer succeeded. This is the " |
| 154 |
"/tmp/somedir/-class parent-flip case. Fix: per-component path walk " |
| 155 |
"refusing parent symlinks owned by untrusted uids.") |