| 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, rmtree, rsync_argv, start_test_daemon, |
| 32 |
test_fail, test_skipped, write_daemon_conf, |
| 33 |
) |
| 34 |
|
| 35 |
DAEMON_PORT = 12901 |
| 36 |
MARKER = "AUDIT_MOTD_LEAK_MARKER_DO_NOT_DISCLOSE" |
| 37 |
|
| 38 |
|
| 39 |
if os.geteuid() != 0: |
| 40 |
test_skipped("requires root to plant a symlink owned by a non-self uid " |
| 41 |
"(the attacker simulation)") |
| 42 |
|
| 43 |
NOBODY_UID = None |
| 44 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 45 |
try: |
| 46 |
u = pwd.getpwnam(name).pw_uid |
| 47 |
if u != 0 and u != os.geteuid(): |
| 48 |
NOBODY_UID = u |
| 49 |
break |
| 50 |
except KeyError: |
| 51 |
continue |
| 52 |
if NOBODY_UID is None: |
| 53 |
test_skipped("no untrusted-uid user available for cross-uid plant") |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
src = FROMDIR |
| 58 |
rmtree(src) |
| 59 |
make_tree(src, depth=1) |
| 60 |
|
| 61 |
base = SCRATCHDIR / 'daemoncfg' |
| 62 |
rmtree(base) |
| 63 |
base.mkdir() |
| 64 |
plants = base / 'plants' |
| 65 |
plants.mkdir() |
| 66 |
modpath = base / 'mod' |
| 67 |
modpath.mkdir() |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
victim = plants / 'motd_victim' |
| 73 |
victim.write_text(MARKER + "\n") |
| 74 |
os.chmod(victim, 0o600) |
| 75 |
|
| 76 |
motd_plant = plants / 'motd_plant' |
| 77 |
os.symlink(victim, motd_plant) |
| 78 |
os.lchown(motd_plant, NOBODY_UID, NOBODY_UID) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
lock_victim_dir = base / 'lock_victim' |
| 88 |
lock_victim_dir.mkdir() |
| 89 |
lock_plant = plants / 'lock_plant' |
| 90 |
os.symlink(lock_victim_dir / 'leaked_lockfile', lock_plant) |
| 91 |
os.lchown(lock_plant, NOBODY_UID, NOBODY_UID) |
| 92 |
|
| 93 |
conf = write_daemon_conf( |
| 94 |
[('mod', {'path': modpath, 'read only': 'yes', |
| 95 |
'max connections': '5'})], |
| 96 |
globals={'motd file': str(motd_plant), |
| 97 |
'lock file': str(lock_plant)}, |
| 98 |
) |
| 99 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
proc = subprocess.run( |
| 106 |
rsync_argv(url + 'mod/'), |
| 107 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 108 |
|
| 109 |
if MARKER in proc.stdout or MARKER in proc.stderr: |
| 110 |
test_fail( |
| 111 |
f"daemon followed a planted motd symlink (owned by uid {NOBODY_UID}): " |
| 112 |
f"the marker {MARKER!r} from {victim} was read via the symlink and " |
| 113 |
"sent to the connecting client as the message of the day. Fix: refuse " |
| 114 |
"planted symlinks at the daemon motd open (and analogously for " |
| 115 |
"early-input / lock / --config).") |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
leaked_lockfile = lock_victim_dir / 'leaked_lockfile' |
| 120 |
if leaked_lockfile.exists(): |
| 121 |
test_fail( |
| 122 |
f"daemon followed a planted lock-file symlink (owned by uid " |
| 123 |
f"{NOBODY_UID}): {lock_plant} -> {leaked_lockfile} was traversed by " |
| 124 |
f"claim_connection()'s open(O_CREAT) at connection.c:33, creating a " |
| 125 |
f"root-owned file at the attacker-chosen path. Fix: refuse planted " |
| 126 |
"symlinks at the daemon lock-file open.") |