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