Spaces:
Running
Running
fix: prevent FIFO descriptors from blocking discovery
Browse filesFiles changed (2)
- src/lumi_eggcracker/procfd.py+10-1
- tests/test_procfd.pyAdded+59
305 unmodified lines306307308309310311312305 unmodified lines"""if isinstance(number, bool) or not isinstance(number, int) or number < 0:raise JsonInputError("target descriptor number is invalid")flags = os.O_RDONLY | getattr(os, "O_CLOEXEC", 0)path = proc / str(value.pid) / "fd" / str(number)try:descriptor = os.open(path, flags)305 unmodified lines306307308309310311312313314315316317318319320321305 unmodified lines"""if isinstance(number, bool) or not isinstance(number, int) or number < 0:raise JsonInputError("target descriptor number is invalid")# A target can deliberately expose a FIFO, device, or another descriptor# whose open would wait for external activity. Discovery must never let# an untrusted workload block the supervisor before the descriptor has# been validated as a regular file. O_NONBLOCK is inert for ordinary# files and lets the caller reject non-regular descriptors after fstat.flags = (os.O_RDONLY| getattr(os, "O_CLOEXEC", 0)| getattr(os, "O_NONBLOCK", 0))path = proc / str(value.pid) / "fd" / str(number)try:descriptor = os.open(path, flags)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859from __future__ import annotations
import osimport tempfileimport threadingimport unittestfrom pathlib import Path
from lumi_eggcracker.discovery import identityfrom lumi_eggcracker.jsonio import JsonInputErrorfrom lumi_eggcracker.procfd import open_process_fd
@unittest.skipUnless(os.name == "posix" and hasattr(os, "mkfifo"), "requires POSIX FIFOs")class ProcessDescriptorTests(unittest.TestCase): def test_fifo_descriptor_reopen_cannot_block_discovery(self) -> None: """A hostile FIFO descriptor must not stop autonomous scan progress.""" with tempfile.TemporaryDirectory() as raw: fifo = Path(raw) / "hostile-fd" os.mkfifo(fifo, mode=0o600) keeper = os.open(fifo, os.O_RDWR | os.O_NONBLOCK | os.O_CLOEXEC) target_fd = os.open(fifo, os.O_RDONLY | os.O_NONBLOCK | os.O_CLOEXEC) os.close(keeper) process = identity(os.getpid()) self.assertIsNotNone(process) assert process is not None
outcome: dict[str, object] = {}
def probe() -> None: try: outcome["descriptor"] = open_process_fd(process, target_fd)[0] except (JsonInputError, OSError) as error: outcome["error"] = error
worker = threading.Thread(target=probe, daemon=True) writer = -1 worker.start() worker.join(timeout=0.5) blocked = worker.is_alive() if blocked: # Unblock an unfixed implementation so this regression cannot # strand the test process after reporting the failure. writer = os.open(fifo, os.O_WRONLY | os.O_NONBLOCK | os.O_CLOEXEC) worker.join(timeout=1.0) try: self.assertFalse(blocked, "reopening a FIFO descriptor blocked discovery") self.assertNotIn("error", outcome) finally: descriptor = outcome.get("descriptor") if isinstance(descriptor, int): os.close(descriptor) if writer >= 0: os.close(writer) os.close(target_fd)
if __name__ == "__main__": unittest.main()src/lumi_eggcracker/procfd.py+10-1
tests/test_procfd.pyAdded+59