| 1 |
|
| 2 |
"""Lock down daemon in-module-symlink resolution for a writable, non-chroot |
| 3 |
module, across every combination of: |
| 4 |
|
| 5 |
insecure links {no (default secure), yes (admin opt-out)} |
| 6 |
x munge symlinks {no, yes} |
| 7 |
x link origin {pre-existing on disk, uploaded via rsync} |
| 8 |
x access VECTOR (how the client reaches the in-module link "evil"): |
| 9 |
read : --copy-dirlinks pull of the module (send_directory) |
| 10 |
write : --keep-dirlinks push into the module (send_directory) |
| 11 |
read-plain : plain pull of an explicit sub-path "evil/" (change_dir) |
| 12 |
write-plain : plain push into an explicit sub-path "evil/" (change_dir) |
| 13 |
compare-dest : push with --compare-dest=/evil (basis_link_stat) |
| 14 |
x symlink TYPE (the target the in-module link "evil" points at): |
| 15 |
rel-within : relative, stays inside the module (legit) |
| 16 |
rel-outside : relative, climbs OUT of the module (escape) |
| 17 |
rel-transits : relative, climbs above the module root with ".." |
| 18 |
then back IN -- net target inside, path transits out |
| 19 |
abs-outside : absolute, lands OUTSIDE the module (escape) |
| 20 |
abs-inside : absolute, lands INSIDE the module (legit) |
| 21 |
|
| 22 |
We measure whether the operation FOLLOWED the link to its target ("followed"). |
| 23 |
For an *outside* target, followed == an out-of-module escape. |
| 24 |
|
| 25 |
Contract pinned here for THIS branch: |
| 26 |
- insecure links = no (default): NO out-of-module escape via ANY vector -- |
| 27 |
every site (send_directory, change_dir, basis_link_stat, secure_basis_open) |
| 28 |
refuses a link whose target lands outside the module root. |
| 29 |
- insecure links = yes (admin opt-out): restores stock-3.2.7 follow behaviour |
| 30 |
UNIFORMLY at every site (the whole point of the option). We assert this |
| 31 |
against a REAL 3.2.7 daemon oracle when old_versions/rsync_3.2.7 is present: |
| 32 |
for every insecure=yes cell the current build must follow iff 3.2.7 follows. |
| 33 |
Without the oracle we fall back to a static contract that still catches a |
| 34 |
regression (the broken build returns follow=0 where 3.2.7/the contract |
| 35 |
want 1). |
| 36 |
|
| 37 |
Why the earlier version of this test missed the change_dir / basis_link_stat |
| 38 |
gap: its only vectors were `read`/`write` (--copy-dirlinks/--keep-dirlinks), |
| 39 |
which route 100%% through send_directory/secure_opendir -- the one daemon |
| 40 |
descent site that already honoured the opt-out -- so insecure=yes followed there |
| 41 |
and the test passed. The explicit-path (read-plain/write-plain -> change_dir) and |
| 42 |
alt-dest-basis (compare-dest -> basis_link_stat) vectors below exercise the sites |
| 43 |
that did NOT honour the opt-out. |
| 44 |
|
| 45 |
Needs root (foreign-owned plant; served as root) + a real TCP peer. |
| 46 |
""" |
| 47 |
|
| 48 |
import os |
| 49 |
import subprocess |
| 50 |
from pathlib import Path |
| 51 |
|
| 52 |
from rsyncfns import ( |
| 53 |
RSYNC, RSYNC_PEER, SCRATCHDIR, |
| 54 |
find_attacker_uid, require_tcp, rmtree, rsync_argv, rsync_supports, |
| 55 |
start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 56 |
) |
| 57 |
|
| 58 |
PORT_CUR = 12909 |
| 59 |
PORT_ORACLE = 12910 |
| 60 |
SECRET = "TARGET-CONTENT\n" |
| 61 |
PWNED = "WROTE-THROUGH-LINK\n" |
| 62 |
|
| 63 |
require_tcp("the daemon symlink-resolution flow needs a real TCP peer") |
| 64 |
if os.geteuid() != 0: |
| 65 |
test_skipped("requires root to plant a foreign-owned symlink and serve as root") |
| 66 |
if not rsync_supports('--copy-dirlinks'): |
| 67 |
test_skipped("rsync lacks --copy-dirlinks") |
| 68 |
ATT = find_attacker_uid() |
| 69 |
if ATT is None: |
| 70 |
test_skipped("no untrusted-uid user available for the cross-uid plant") |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
_repo = Path(__file__).resolve().parent.parent |
| 75 |
ORACLE_BIN = None |
| 76 |
if RSYNC_PEER != RSYNC: |
| 77 |
ORACLE_BIN = RSYNC_PEER |
| 78 |
elif (_repo / 'old_versions' / 'rsync_3.2.7').is_file(): |
| 79 |
ORACLE_BIN = str(_repo / 'old_versions' / 'rsync_3.2.7') |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ORACLE_BIN: |
| 88 |
try: |
| 89 |
_probe = subprocess.run([ORACLE_BIN, '--version'], |
| 90 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=15) |
| 91 |
if _probe.returncode != 0: |
| 92 |
ORACLE_BIN = None |
| 93 |
except (OSError, subprocess.TimeoutExpired): |
| 94 |
ORACLE_BIN = None |
| 95 |
|
| 96 |
TYPES = ('rel-within', 'rel-outside', 'rel-transits', 'abs-outside', 'abs-inside') |
| 97 |
OUTSIDE = {'rel-outside', 'abs-outside'} |
| 98 |
VECTORS = ('read', 'write', 'read-plain', 'write-plain', 'compare-dest') |
| 99 |
|
| 100 |
base = SCRATCHDIR / 'symlink-escape-matrix' |
| 101 |
rmtree(base) |
| 102 |
base.mkdir(parents=True) |
| 103 |
|
| 104 |
MODS = {} |
| 105 |
mod_conf = [] |
| 106 |
for insecure in (False, True): |
| 107 |
for munge in (False, True): |
| 108 |
name = f"m_{'ins' if insecure else 'safe'}_{'munge' if munge else 'nomunge'}" |
| 109 |
moddir = base / (name + '_root') |
| 110 |
outside = base / (name + '_outside') |
| 111 |
moddir.mkdir(); outside.mkdir() |
| 112 |
MODS[(insecure, munge)] = (name, moddir, outside) |
| 113 |
mod_conf.append((name, { |
| 114 |
'path': str(moddir), 'use chroot': 'no', 'read only': 'no', |
| 115 |
'uid': '0', 'gid': '0', |
| 116 |
'insecure links': 'yes' if insecure else 'no', |
| 117 |
'munge symlinks': 'yes' if munge else 'no', |
| 118 |
})) |
| 119 |
|
| 120 |
conf = write_daemon_conf(mod_conf, name='symlink-escape-matrix.conf') |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
url_cur = start_test_daemon(conf, PORT_CUR, rsync_cmd=RSYNC) |
| 126 |
url_oracle = None |
| 127 |
if ORACLE_BIN: |
| 128 |
conf_oracle = write_daemon_conf( |
| 129 |
mod_conf, |
| 130 |
{'pid file': str(SCRATCHDIR / 'rsyncd-oracle.pid'), |
| 131 |
'log file': str(SCRATCHDIR / 'rsyncd-oracle.log')}, |
| 132 |
name='symlink-escape-matrix-oracle.conf') |
| 133 |
url_oracle = start_test_daemon(conf_oracle, PORT_ORACLE, rsync_cmd=ORACLE_BIN) |
| 134 |
|
| 135 |
|
| 136 |
def link_target(moddir, outside, sltype): |
| 137 |
"""(symlink value, resolved target dir, target_is_outside) for a TYPE.""" |
| 138 |
realdir = moddir / 'realdir' |
| 139 |
if sltype == 'rel-within': |
| 140 |
return 'realdir', realdir, False |
| 141 |
if sltype == 'rel-outside': |
| 142 |
return '../' + outside.name, outside, True |
| 143 |
if sltype == 'rel-transits': |
| 144 |
return f'../{moddir.name}/realdir', realdir, False |
| 145 |
if sltype == 'abs-outside': |
| 146 |
return str(outside), outside, True |
| 147 |
if sltype == 'abs-inside': |
| 148 |
return str(realdir), realdir, False |
| 149 |
raise AssertionError(sltype) |
| 150 |
|
| 151 |
|
| 152 |
def attempt(url, insecure, munge, origin, vector, sltype): |
| 153 |
"""Build the cell fresh, run `vector` against daemon `url`, return |
| 154 |
(followed, target_outside).""" |
| 155 |
name, moddir, outside = MODS[(insecure, munge)] |
| 156 |
rmtree(moddir); moddir.mkdir() |
| 157 |
rmtree(outside); outside.mkdir() |
| 158 |
realdir = moddir / 'realdir' |
| 159 |
realdir.mkdir() |
| 160 |
(realdir / 'tgtfile').write_text(SECRET) |
| 161 |
(outside / 'tgtfile').write_text(SECRET) |
| 162 |
symval, resolved, t_out = link_target(moddir, outside, sltype) |
| 163 |
evil = moddir / 'evil' |
| 164 |
|
| 165 |
if origin == 'preexist': |
| 166 |
os.symlink(symval, evil) |
| 167 |
os.lchown(evil, ATT, ATT) |
| 168 |
else: |
| 169 |
up = base / 'srcup' |
| 170 |
rmtree(up); up.mkdir() |
| 171 |
os.symlink(symval, up / 'evil') |
| 172 |
subprocess.run(rsync_argv('-al', f'{up}/', f'{url}{name}/'), |
| 173 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 174 |
if not evil.is_symlink(): |
| 175 |
return False, t_out |
| 176 |
|
| 177 |
def run(*args): |
| 178 |
subprocess.run(rsync_argv(*args), |
| 179 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 180 |
|
| 181 |
if vector == 'read': |
| 182 |
dest = base / 'dest' |
| 183 |
rmtree(dest); dest.mkdir() |
| 184 |
run('-r', '--copy-dirlinks', f'{url}{name}/', f'{dest}/') |
| 185 |
through = dest / 'evil' / 'tgtfile' |
| 186 |
return (through.is_file() and through.read_text() == SECRET), t_out |
| 187 |
|
| 188 |
if vector == 'write': |
| 189 |
sw = base / 'srcw' |
| 190 |
rmtree(sw); sw.mkdir() |
| 191 |
(sw / 'evil').mkdir() |
| 192 |
(sw / 'evil' / 'pwned').write_text(PWNED) |
| 193 |
run('-r', '--keep-dirlinks', f'{sw}/', f'{url}{name}/') |
| 194 |
landed = resolved / 'pwned' |
| 195 |
return (landed.is_file() and landed.read_text() == PWNED), t_out |
| 196 |
|
| 197 |
if vector == 'read-plain': |
| 198 |
dest = base / 'dest' |
| 199 |
rmtree(dest); dest.mkdir() |
| 200 |
run('-r', f'{url}{name}/evil/', f'{dest}/') |
| 201 |
through = dest / 'tgtfile' |
| 202 |
return (through.is_file() and through.read_text() == SECRET), t_out |
| 203 |
|
| 204 |
if vector == 'write-plain': |
| 205 |
sw = base / 'srcw' |
| 206 |
rmtree(sw); sw.mkdir() |
| 207 |
(sw / 'pwned').write_text(PWNED) |
| 208 |
run('-r', f'{sw}/', f'{url}{name}/evil/') |
| 209 |
landed = resolved / 'pwned' |
| 210 |
return (landed.is_file() and landed.read_text() == PWNED), t_out |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
sc = base / 'srcc' |
| 216 |
rmtree(sc); sc.mkdir() |
| 217 |
(sc / 'tgtfile').write_text(SECRET) |
| 218 |
run('-r', '--checksum', '--compare-dest=/evil', f'{sc}/', f'{url}{name}/') |
| 219 |
pushed = moddir / 'tgtfile' |
| 220 |
return (not pushed.is_file()), t_out |
| 221 |
|
| 222 |
|
| 223 |
def static_followed(insecure, munge, origin, sltype): |
| 224 |
"""Fallback contract when no 3.2.7 oracle is present (insecure=yes only). |
| 225 |
The opt-out follows every pre-existing link; an uploaded link is sanitised |
| 226 |
(munge -> none follow; munge-off -> only a rel-within value survives).""" |
| 227 |
if origin == 'uploaded': |
| 228 |
return sltype == 'rel-within' and not munge |
| 229 |
return True |
| 230 |
|
| 231 |
|
| 232 |
_REPORT = os.environ.get('REPORT_MATRIX') |
| 233 |
grid, mismatches, escapes = [], [], [] |
| 234 |
for insecure in (False, True): |
| 235 |
for munge in (False, True): |
| 236 |
for origin in ('preexist', 'uploaded'): |
| 237 |
for vector in VECTORS: |
| 238 |
for sltype in TYPES: |
| 239 |
got, t_out = attempt(url_cur, insecure, munge, origin, vector, sltype) |
| 240 |
esc = got and t_out |
| 241 |
|
| 242 |
if not insecure: |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
line = (f"ins=0 munge={int(munge)} {origin:8} {vector:12} " |
| 247 |
f"{sltype:11}: followed={int(got)} " |
| 248 |
f"{'ESCAPE' if esc else ''}") |
| 249 |
if esc: |
| 250 |
escapes.append(f"DEFAULT ESCAPE: munge={int(munge)} " |
| 251 |
f"{origin}/{vector}/{sltype}") |
| 252 |
else: |
| 253 |
|
| 254 |
|
| 255 |
if url_oracle is not None: |
| 256 |
want, src = attempt(url_oracle, insecure, munge, |
| 257 |
origin, vector, sltype)[0], '327' |
| 258 |
else: |
| 259 |
want, src = static_followed(insecure, munge, origin, sltype), 'contract' |
| 260 |
line = (f"ins=1 munge={int(munge)} {origin:8} {vector:12} " |
| 261 |
f"{sltype:11}: followed={int(got)} want={int(want)}({src})") |
| 262 |
if got != want: |
| 263 |
mismatches.append( |
| 264 |
f"insecure=yes munge={'yes' if munge else 'no'} " |
| 265 |
f"{origin}/{vector}/{sltype}: followed={got}, " |
| 266 |
f"{src} expected={want}") |
| 267 |
grid.append(line) |
| 268 |
|
| 269 |
if _REPORT: |
| 270 |
test_fail("REPORT-MATRIX grid:\n " + "\n ".join(grid)) |
| 271 |
problems = [] |
| 272 |
if escapes: |
| 273 |
problems.append("secure-default confinement FAILED (out-of-module access):\n " |
| 274 |
+ "\n ".join(escapes)) |
| 275 |
if mismatches: |
| 276 |
problems.append("insecure-links opt-out did NOT match stock 3.2.7:\n " |
| 277 |
+ "\n ".join(mismatches)) |
| 278 |
if problems: |
| 279 |
test_fail("\n".join(problems)) |
| 280 |
|
| 281 |
oracle_note = ("vs a real 3.2.7 daemon oracle" if url_oracle |
| 282 |
else "vs the static fallback contract (no 3.2.7 binary present)") |
| 283 |
print("daemon-symlink-escape-matrix: 5 link types x preexist/uploaded x " |
| 284 |
f"5 vectors (read/write/read-plain/write-plain/compare-dest) x munge: " |
| 285 |
f"secure default never escapes the module, and insecure links=yes matches " |
| 286 |
f"stock-3.2.7 following uniformly ({oracle_note}).") |