mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
fix(tests): validate opt-in worker log directory; fix fd test
Review of e3ab98d: OBLITERATUS_TP_WORKER_LOG_DIR accepted existing
world-writable or symlinked directories. It must now be a private
(owner-only, no group/other write) non-symlink directory owned by the
current user, and log files are created relative to an O_NOFOLLOW
directory fd with O_EXCL and mode 0600; anything else fails closed. The
launch/wait fd test now tracks each launch's descriptors separately so it
no longer depends on fd number reuse.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e3ab98da38
commit
9eabacc911
2 files changed
+166
-27
No files matched your search
@@ -6,6 +6,7 @@ import ipaddress
|
||||
import os
|
||||
import signal
|
||||
import socket
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
@@ -147,21 +148,46 @@ def _worker_environment(
|
||||
return environment, sorted(optional)
|
||||
|
||||
|
||||
def _open_worker_log_directory(directory: Path) -> int:
|
||||
setting = f"OBLITERATUS_TP_WORKER_LOG_DIR={str(directory)!r}"
|
||||
try:
|
||||
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
before = directory.lstat()
|
||||
if not stat.S_ISDIR(before.st_mode):
|
||||
raise ValueError(f"{setting} must be a directory, not a symlink or other file")
|
||||
descriptor = os.open(directory, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
||||
except OSError as error:
|
||||
raise ValueError(f"{setting} could not be securely opened as a directory: {error}") from error
|
||||
try:
|
||||
opened = os.fstat(descriptor)
|
||||
if (opened.st_dev, opened.st_ino) != (before.st_dev, before.st_ino):
|
||||
raise ValueError(f"{setting} changed while opening the directory")
|
||||
if opened.st_uid != os.getuid():
|
||||
raise ValueError(f"{setting} must be owned by the current uid")
|
||||
if opened.st_mode & (stat.S_IWGRP | stat.S_IWOTH):
|
||||
raise ValueError(f"{setting} must have no group/other write bits")
|
||||
except BaseException:
|
||||
os.close(descriptor)
|
||||
raise
|
||||
return descriptor
|
||||
|
||||
|
||||
def _run_process_group(command: list[str], *, cwd: Path, environment: dict[str, str]) -> int:
|
||||
with ExitStack() as logs:
|
||||
stdout = stderr = subprocess.DEVNULL
|
||||
log_directory = os.environ.get("OBLITERATUS_TP_WORKER_LOG_DIR")
|
||||
if log_directory is not None:
|
||||
# Logs can contain env-derived paths; never point this at a shared directory.
|
||||
directory = Path(log_directory)
|
||||
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
# Logs can contain env-derived paths; pin a validated private directory.
|
||||
directory_descriptor = _open_worker_log_directory(Path(log_directory))
|
||||
logs.callback(os.close, directory_descriptor)
|
||||
log_id = uuid.uuid4().hex
|
||||
streams = []
|
||||
for name in ("stdout", "stderr"):
|
||||
descriptor = os.open(
|
||||
directory / f"llama-tp-{log_id}-{name}.log",
|
||||
os.O_CREAT | os.O_EXCL | os.O_WRONLY,
|
||||
f"llama-tp-{log_id}-{name}.log",
|
||||
os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW,
|
||||
0o600,
|
||||
dir_fd=directory_descriptor,
|
||||
)
|
||||
logs.callback(os.close, descriptor)
|
||||
streams.append(descriptor)
|
||||
|
||||
+135
-22
@@ -248,23 +248,38 @@ def test_process_group_discards_output_by_default(monkeypatch, tmp_path):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("outcome", ["success", "nonzero", "timeout", "launch_failure"])
|
||||
def test_process_group_captures_private_logs_and_closes_them(monkeypatch, tmp_path, outcome):
|
||||
@pytest.mark.parametrize("existing_directory", [False, True])
|
||||
def test_process_group_captures_private_logs_and_closes_them(
|
||||
monkeypatch, tmp_path, outcome, existing_directory,
|
||||
):
|
||||
directory = tmp_path / "private-logs"
|
||||
if existing_directory:
|
||||
directory.mkdir(mode=0o700)
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
||||
descriptors = []
|
||||
active_descriptors = []
|
||||
directory_descriptors = []
|
||||
opened = []
|
||||
real_open = os.open
|
||||
|
||||
def open_log(path, flags, mode):
|
||||
opened.append((path, flags, mode))
|
||||
return real_open(path, flags, mode)
|
||||
def open_log(path, flags, mode=0o777, *, dir_fd=None):
|
||||
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
||||
active_descriptors.append(descriptor)
|
||||
if dir_fd is None:
|
||||
assert path == directory
|
||||
assert flags == os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
||||
directory_descriptors.append(descriptor)
|
||||
else:
|
||||
assert dir_fd == directory_descriptors[-1]
|
||||
opened.append((path, flags, mode))
|
||||
return descriptor
|
||||
|
||||
monkeypatch.setattr(harness.os, "open", open_log)
|
||||
process = Mock(returncode=7 if outcome == "nonzero" else 0, pid=12345)
|
||||
|
||||
def wait(**_kwargs):
|
||||
# The parent's descriptors remain open until the child exits.
|
||||
for descriptor in descriptors:
|
||||
for descriptor in active_descriptors:
|
||||
os.fstat(descriptor)
|
||||
if outcome == "timeout" and process.wait.call_count == 1:
|
||||
raise subprocess.TimeoutExpired("worker", 600)
|
||||
@@ -288,32 +303,129 @@ def test_process_group_captures_private_logs_and_closes_them(monkeypatch, tmp_pa
|
||||
return process
|
||||
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", launch)
|
||||
|
||||
def run():
|
||||
active_descriptors.clear()
|
||||
try:
|
||||
return harness._run_process_group(
|
||||
["worker"], cwd=tmp_path, environment={"PATH": "/usr/bin"},
|
||||
)
|
||||
finally:
|
||||
# Check this launch before another launch can reuse its descriptor numbers.
|
||||
for descriptor in active_descriptors:
|
||||
with pytest.raises(OSError) as error:
|
||||
os.fstat(descriptor)
|
||||
assert error.value.errno == errno.EBADF
|
||||
|
||||
if outcome == "timeout":
|
||||
with pytest.raises(pytest.fail.Exception, match="process-hard deadline"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={"PATH": "/usr/bin"})
|
||||
run()
|
||||
killpg.assert_called_once_with(process.pid, harness.signal.SIGKILL)
|
||||
assert process.wait.call_args_list == [call(timeout=600), call(timeout=30)]
|
||||
elif outcome == "launch_failure":
|
||||
with pytest.raises(OSError, match="launch failed"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={"PATH": "/usr/bin"})
|
||||
run()
|
||||
process.wait.assert_not_called()
|
||||
else:
|
||||
# Reusing the directory creates a fresh pair and preserves the first pair.
|
||||
for _ in range(2):
|
||||
assert harness._run_process_group(
|
||||
["worker"], cwd=tmp_path, environment={"PATH": "/usr/bin"},
|
||||
) == process.returncode
|
||||
assert run() == process.returncode
|
||||
assert stat.S_IMODE(directory.stat().st_mode) == 0o700
|
||||
paths = list(directory.iterdir())
|
||||
assert len(paths) == len(descriptors) == len(opened)
|
||||
for path, flags, mode in opened:
|
||||
assert flags == os.O_CREAT | os.O_EXCL | os.O_WRONLY
|
||||
assert len(directory_descriptors) * 2 == len(descriptors)
|
||||
for name, flags, mode in opened:
|
||||
assert Path(name).name == name
|
||||
path = directory / name
|
||||
assert flags == os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW
|
||||
assert mode == 0o600
|
||||
assert stat.S_IMODE(path.stat().st_mode) == 0o600
|
||||
assert path.read_bytes() == (b"stdout" if path.name.endswith("stdout.log") else b"stderr")
|
||||
for descriptor in descriptors:
|
||||
with pytest.raises(OSError) as error:
|
||||
os.fstat(descriptor)
|
||||
assert error.value.errno == errno.EBADF
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", [0o777, 0o770, 0o702])
|
||||
def test_process_group_rejects_group_or_other_writable_log_directory(monkeypatch, tmp_path, mode):
|
||||
directory = tmp_path / "shared-logs"
|
||||
directory.mkdir()
|
||||
directory.chmod(mode)
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
||||
popen = Mock()
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
||||
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*group/other write bits"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert list(directory.iterdir()) == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("suffix", ["", "/"])
|
||||
def test_process_group_rejects_symlinked_log_directory(monkeypatch, tmp_path, suffix):
|
||||
target = tmp_path / "private-logs"
|
||||
target.mkdir(mode=0o700)
|
||||
directory = tmp_path / "linked-logs"
|
||||
directory.symlink_to(target, target_is_directory=True)
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", f"{directory}{suffix}")
|
||||
popen = Mock()
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
||||
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*not a symlink"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert list(target.iterdir()) == []
|
||||
|
||||
|
||||
def test_process_group_rejects_log_directory_owned_by_another_uid(monkeypatch, tmp_path):
|
||||
directory = tmp_path / "other-user-logs"
|
||||
directory.mkdir(mode=0o700)
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
||||
current_uid = directory.stat().st_uid + 1
|
||||
monkeypatch.setattr(harness.os, "getuid", lambda: current_uid)
|
||||
popen = Mock()
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
||||
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*owned by the current uid"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert list(directory.iterdir()) == []
|
||||
|
||||
|
||||
def test_process_group_rejects_a_file_as_log_directory(monkeypatch, tmp_path):
|
||||
path = tmp_path / "existing-file"
|
||||
path.write_bytes(b"preserve this file")
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(path))
|
||||
popen = Mock()
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
||||
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*directory"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert path.read_bytes() == b"preserve this file"
|
||||
|
||||
|
||||
def test_process_group_rejects_log_directory_replaced_during_open(monkeypatch, tmp_path):
|
||||
directory = tmp_path / "private-logs"
|
||||
directory.mkdir(mode=0o700)
|
||||
displaced = tmp_path / "displaced-logs"
|
||||
monkeypatch.setenv("OBLITERATUS_TP_WORKER_LOG_DIR", str(directory))
|
||||
real_open = os.open
|
||||
descriptors = []
|
||||
|
||||
def open_directory(path, flags, mode=0o777, *, dir_fd=None):
|
||||
assert path == directory
|
||||
assert dir_fd is None
|
||||
directory.rename(displaced)
|
||||
directory.mkdir(mode=0o700)
|
||||
descriptor = real_open(path, flags, mode)
|
||||
descriptors.append(descriptor)
|
||||
return descriptor
|
||||
|
||||
monkeypatch.setattr(harness.os, "open", open_directory)
|
||||
popen = Mock()
|
||||
monkeypatch.setattr(harness.subprocess, "Popen", popen)
|
||||
with pytest.raises(ValueError, match="OBLITERATUS_TP_WORKER_LOG_DIR=.*changed while opening"):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert list(directory.iterdir()) == list(displaced.iterdir()) == []
|
||||
assert len(descriptors) == 1
|
||||
with pytest.raises(OSError) as error:
|
||||
os.fstat(descriptors[0])
|
||||
assert error.value.errno == errno.EBADF
|
||||
|
||||
|
||||
def test_process_group_never_overwrites_an_existing_log(monkeypatch, tmp_path):
|
||||
@@ -324,8 +436,8 @@ def test_process_group_never_overwrites_an_existing_log(monkeypatch, tmp_path):
|
||||
descriptors = []
|
||||
real_open = os.open
|
||||
|
||||
def open_log(path, flags, mode):
|
||||
descriptor = real_open(path, flags, mode)
|
||||
def open_log(path, flags, mode=0o777, *, dir_fd=None):
|
||||
descriptor = real_open(path, flags, mode, dir_fd=dir_fd)
|
||||
descriptors.append(descriptor)
|
||||
return descriptor
|
||||
|
||||
@@ -336,7 +448,8 @@ def test_process_group_never_overwrites_an_existing_log(monkeypatch, tmp_path):
|
||||
harness._run_process_group(["worker"], cwd=tmp_path, environment={})
|
||||
popen.assert_not_called()
|
||||
assert existing.read_bytes() == b"preserve this log"
|
||||
assert len(descriptors) == 1
|
||||
with pytest.raises(OSError) as error:
|
||||
os.fstat(descriptors[0])
|
||||
assert error.value.errno == errno.EBADF
|
||||
assert len(descriptors) == 2
|
||||
for descriptor in descriptors:
|
||||
with pytest.raises(OSError) as error:
|
||||
os.fstat(descriptor)
|
||||
assert error.value.errno == errno.EBADF
|
||||
Reference in new issue
Block a user