| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import os |
| 19 |
import platform |
| 20 |
import shutil |
| 21 |
import subprocess |
| 22 |
import time |
| 23 |
|
| 24 |
from rsyncfns import ( |
| 25 |
SCRATCHDIR, race_budget, |
| 26 |
forced_protocol, get_rootgid, get_rootuid, get_testuid, |
| 27 |
rmtree, run_rsync, rsync_argv, start_path_flipper, stop_flipper, |
| 28 |
start_test_daemon, test_fail, test_skipped, |
| 29 |
) |
| 30 |
|
| 31 |
DAEMON_PORT = 12896 |
| 32 |
MARKER_UID = 60001 |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if platform.system() != 'Linux': |
| 37 |
test_skipped("POSIX ACL race test is Linux-only (setfacl/getfacl semantics)") |
| 38 |
if not shutil.which('setfacl') or not shutil.which('getfacl'): |
| 39 |
test_skipped("setfacl/getfacl not available") |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
proto = forced_protocol() |
| 47 |
if proto is not None and proto < 30: |
| 48 |
test_skipped(f"ACL transfer requires protocol 30+ (negotiated {proto})") |
| 49 |
vv = run_rsync('-VV', check=True, capture_output=True) |
| 50 |
if '"ACLs": true' not in vv.stdout: |
| 51 |
test_skipped("rsync built without ACL support") |
| 52 |
if '"ACL_at": true' not in vv.stdout: |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
print("acl-symlink-race: no race-safe ACL primitive on this platform; " |
| 60 |
"--acls is functional-but-racy here by policy (race not asserted)") |
| 61 |
raise SystemExit(0) |
| 62 |
|
| 63 |
|
| 64 |
def setfacl(spec, path): |
| 65 |
if subprocess.run(['setfacl', '-m', spec, str(path)]).returncode != 0: |
| 66 |
test_skipped("filesystem has ACLs disabled (setfacl failed)") |
| 67 |
|
| 68 |
|
| 69 |
def getfacl(path) -> str: |
| 70 |
|
| 71 |
|
| 72 |
return subprocess.run(['getfacl', str(path)], |
| 73 |
capture_output=True, text=True).stdout |
| 74 |
|
| 75 |
|
| 76 |
def facl_has_marker(path) -> bool: |
| 77 |
return f'user:{MARKER_UID}:' in getfacl(path) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
mod = SCRATCHDIR / 'module' |
| 82 |
outside = SCRATCHDIR / 'outside' |
| 83 |
src = SCRATCHDIR / 'src_files' |
| 84 |
conf = SCRATCHDIR / 'test-rsyncd.conf' |
| 85 |
|
| 86 |
for d in (mod, outside, src): |
| 87 |
rmtree(d) |
| 88 |
d.mkdir(parents=True) |
| 89 |
|
| 90 |
|
| 91 |
priv = outside / 'privileged' |
| 92 |
priv.write_text("ROOT_OWNED_SECRET\n") |
| 93 |
os.chmod(priv, 0o600) |
| 94 |
if facl_has_marker(priv): |
| 95 |
test_fail("baseline outside file already carries the marker ACL") |
| 96 |
|
| 97 |
|
| 98 |
victim_src = src / 'victim' |
| 99 |
os.mkfifo(victim_src, 0o600) |
| 100 |
setfacl(f'user:{MARKER_UID}:rwx', victim_src) |
| 101 |
|
| 102 |
|
| 103 |
os.symlink(str(priv), mod / '.evil') |
| 104 |
|
| 105 |
my_uid = get_testuid() |
| 106 |
root_uid = get_rootuid() |
| 107 |
root_gid = get_rootgid() |
| 108 |
uid_line = f"uid = {root_uid}" |
| 109 |
gid_line = f"gid = {root_gid}" |
| 110 |
if my_uid != root_uid: |
| 111 |
uid_line = '#' + uid_line |
| 112 |
gid_line = '#' + gid_line |
| 113 |
|
| 114 |
conf.write_text(f"""\ |
| 115 |
use chroot = no |
| 116 |
{uid_line} |
| 117 |
{gid_line} |
| 118 |
log file = {SCRATCHDIR}/rsyncd.log |
| 119 |
[upload] |
| 120 |
path = {mod} |
| 121 |
use chroot = no |
| 122 |
read only = no |
| 123 |
""") |
| 124 |
|
| 125 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 126 |
|
| 127 |
|
| 128 |
def push(): |
| 129 |
subprocess.run( |
| 130 |
rsync_argv('-rtpA', '--specials', f'{src}/', f'{url}upload/'), |
| 131 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
push() |
| 138 |
landed = mod / 'victim' |
| 139 |
if not facl_has_marker(landed): |
| 140 |
test_fail("positive control: a normal push did not apply the FIFO's ACL in " |
| 141 |
"the module -- the daemon isn't preserving special-file ACLs, so " |
| 142 |
"the race scenario would be vacuous") |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
landed.unlink() |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
flip = start_path_flipper(mod / 'victim', mod / '.evil') |
| 155 |
deadline = time.monotonic() + race_budget(10.0) |
| 156 |
try: |
| 157 |
while time.monotonic() < deadline: |
| 158 |
push() |
| 159 |
if facl_has_marker(priv): |
| 160 |
test_fail( |
| 161 |
f"ACL-set symlink race: the FIFO's user:{MARKER_UID}:rwx ACL was " |
| 162 |
"applied to the outside privileged file -- the receiver's " |
| 163 |
"set_acl() followed the leaf symlink (LPE). " |
| 164 |
f"getfacl {priv}:\n" + getfacl(priv) |
| 165 |
) |
| 166 |
finally: |
| 167 |
stop_flipper(flip) |
| 168 |
|
| 169 |
|