| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
import os |
| 24 |
import subprocess |
| 25 |
|
| 26 |
from rsyncfns import ( |
| 27 |
SCRATCHDIR, find_attacker_uid, rmtree, rsync_argv, rsync_supports, |
| 28 |
start_test_daemon, test_fail, test_skipped, write_daemon_conf, |
| 29 |
) |
| 30 |
|
| 31 |
DAEMON_PORT = 12906 |
| 32 |
MARKER = "OUT-OF-MODULE-SECRET\n" |
| 33 |
|
| 34 |
if os.geteuid() != 0: |
| 35 |
test_skipped("requires root to plant a foreign-owned symlink and serve as root") |
| 36 |
ATT = find_attacker_uid() |
| 37 |
if ATT is None: |
| 38 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 39 |
if not rsync_supports('--copy-dirlinks'): |
| 40 |
test_skipped("rsync lacks --copy-dirlinks") |
| 41 |
|
| 42 |
base = SCRATCHDIR / 'insecure-admin' |
| 43 |
rmtree(base) |
| 44 |
mod = base / 'mod' |
| 45 |
outside = base / 'outside' |
| 46 |
mod.mkdir(parents=True) |
| 47 |
outside.mkdir(parents=True) |
| 48 |
|
| 49 |
|
| 50 |
(mod / 'intree.txt').write_text("in-module\n") |
| 51 |
(outside / 'secret.txt').write_text(MARKER) |
| 52 |
os.symlink('../outside', mod / 'sub') |
| 53 |
os.lchown(mod / 'sub', ATT, ATT) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
conf = write_daemon_conf([ |
| 59 |
('mod_secure', {'path': str(mod), 'use chroot': 'no', 'read only': 'yes', |
| 60 |
'uid': '0', 'gid': '0'}), |
| 61 |
('mod_insecure', {'path': str(mod), 'use chroot': 'no', 'read only': 'yes', |
| 62 |
'uid': '0', 'gid': '0', 'insecure links': 'yes'}), |
| 63 |
]) |
| 64 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 65 |
|
| 66 |
|
| 67 |
def pull(modname): |
| 68 |
out = base / f'dest_{modname}' |
| 69 |
rmtree(out) |
| 70 |
out.mkdir() |
| 71 |
subprocess.run( |
| 72 |
rsync_argv('-r', '--copy-dirlinks', f'{url}{modname}/', f'{out}/'), |
| 73 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 74 |
served = out / 'sub' / 'secret.txt' |
| 75 |
return served.is_file() and served.read_text() == MARKER |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if pull('mod_secure'): |
| 80 |
test_fail("mod_secure served the out-of-module file through the planted " |
| 81 |
"directory symlink: the daemon followed it without `insecure " |
| 82 |
"links = yes` -- confinement failed.") |
| 83 |
|
| 84 |
|
| 85 |
if not pull('mod_insecure'): |
| 86 |
test_fail("mod_insecure did NOT serve the out-of-module file even with " |
| 87 |
"`insecure links = yes`: the per-module admin opt-out was not " |
| 88 |
"honored by the sender enumeration/content gates.") |
| 89 |
|
| 90 |
print("insecure links = yes: refused by default, honored as an admin opt-out") |