| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import os |
| 19 |
import pwd |
| 20 |
import subprocess |
| 21 |
|
| 22 |
from rsyncfns import ( |
| 23 |
SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, |
| 24 |
test_fail, test_skipped, write_daemon_conf, |
| 25 |
) |
| 26 |
|
| 27 |
DAEMON_PORT = 12951 |
| 28 |
CONTENT = "served-from-under-a-private-parent\n" |
| 29 |
|
| 30 |
|
| 31 |
if os.geteuid() != 0: |
| 32 |
test_skipped("requires root (a 0700 parent the served uid cannot traverse, " |
| 33 |
"and a daemon that drops to an unprivileged uid)") |
| 34 |
|
| 35 |
UNPRIV = None |
| 36 |
for name in ('nobody', 'nfsnobody', 'daemon'): |
| 37 |
try: |
| 38 |
u = pwd.getpwnam(name) |
| 39 |
if u.pw_uid != 0 and u.pw_uid != os.geteuid(): |
| 40 |
UNPRIV = u |
| 41 |
break |
| 42 |
except KeyError: |
| 43 |
continue |
| 44 |
if UNPRIV is None: |
| 45 |
test_skipped("no unprivileged uid available to drop the daemon to") |
| 46 |
|
| 47 |
base = SCRATCHDIR / 'priv-parent' |
| 48 |
rmtree(base) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private = base / 'private' |
| 53 |
modpath = private / 'mod' |
| 54 |
sub = modpath / 'sub' |
| 55 |
makepath(sub) |
| 56 |
(sub / 'file').write_text(CONTENT) |
| 57 |
os.chmod(modpath, 0o755) |
| 58 |
os.chmod(sub, 0o755) |
| 59 |
os.chmod(sub / 'file', 0o644) |
| 60 |
os.chmod(private, 0o700) |
| 61 |
|
| 62 |
dest = base / 'dest' |
| 63 |
makepath(dest) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
conf = write_daemon_conf([ |
| 68 |
('m', {'path': modpath, 'read only': 'yes', |
| 69 |
'uid': str(UNPRIV.pw_uid), 'gid': str(UNPRIV.pw_gid)}), |
| 70 |
]) |
| 71 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 72 |
|
| 73 |
def pull(*opts): |
| 74 |
"""Pull the module to a fresh dest with the given options; assert it serves |
| 75 |
sub/file (the content open under the 0700 parent must not EACCES).""" |
| 76 |
rmtree(dest) |
| 77 |
makepath(dest) |
| 78 |
proc = subprocess.run( |
| 79 |
rsync_argv(*opts, f'{url}m/', f'{dest}/'), |
| 80 |
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True) |
| 81 |
if proc.returncode != 0: |
| 82 |
test_fail( |
| 83 |
f"daemon failed to serve a module under a 0700 parent as uid " |
| 84 |
f"{UNPRIV.pw_name} ({' '.join(opts)}): {proc.stderr.strip()!r}. The " |
| 85 |
"scan and the content open must resolve relative to the module root " |
| 86 |
"the daemon pinned while privileged, not re-walk the absolute module " |
| 87 |
"path (which EACCESes on the 0700 ancestor under the dropped uid).") |
| 88 |
got = dest / 'sub' / 'file' |
| 89 |
if not got.is_file() or got.read_text() != CONTENT: |
| 90 |
test_fail(f"the under-0700-parent module did not serve its file with " |
| 91 |
f"'{' '.join(opts)}' (dest/sub/file missing/wrong)") |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pull('-r') |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
pull('-rL') |
| 101 |
|
| 102 |
print("daemon-module-private-parent: served a module under a 0700 parent as " |
| 103 |
f"uid {UNPRIV.pw_name} (default + --copy-links content opens)") |