Skip to content

wayland

wayland

Supervise stock wl-mitm with an immutable policy and no credential access.

The Python service keeps the supervisor's process identity while the proxy executes separately. Parent-death signalling covers abrupt wrapper death; normal shutdown terminates and reaps the proxy. Filesystem confinement does not restrict pathname Unix sockets or network access on every host kernel. The optional SELinux domain supplies additional IPC restrictions.

run_proxy(cfg, paths, stop) async

Run the task's optional proxy until it exits or the supervisor stops it.

Source code in src/terok_sandbox/supervisor/wayland.py
async def run_proxy(cfg: SidecarConfig, paths: SupervisorPaths, stop: asyncio.Event) -> None:
    """Run the task's optional proxy until it exits or the supervisor stops it."""
    session = cfg.wayland
    if not session.proxied:
        raise RuntimeError("Wayland child launched without proxy wiring")
    assert session.binary is not None and session.upstream is not None  # nosec B101 - narrowed by proxied
    require_wayland_exec_context()
    confine = partial(_confine_proxy, os.getpid(), session.binary, paths.wayland_socket.parent)
    with _policy_fd(proxy_config(session.upstream, paths.wayland_socket)) as config_fd:
        with explained_preexec(confine) as preexec_fn:
            process = await asyncio.create_subprocess_exec(
                str(session.binary),
                str(_PROC_FDS / str(config_fd)),
                pass_fds=(config_fd,),
                close_fds=True,
                stdin=asyncio.subprocess.DEVNULL,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.STDOUT,
                env={"LANG": "C.UTF-8", "TOKIO_WORKER_THREADS": "2"},
                cwd=paths.wayland_socket.parent,
                preexec_fn=preexec_fn,
            )
        exited = asyncio.create_task(_wait_proxy_exit(process))
        stopping = asyncio.create_task(stop.wait())
        output = asyncio.create_task(relay_output(process, "wl-mitm"))
        try:
            done, _ = await asyncio.wait((exited, stopping), return_when=asyncio.FIRST_COMPLETED)
            if exited in done:
                raise RuntimeError(f"wl-mitm exited with status {exited.result()}")
        finally:
            stopping.cancel()
            try:
                if process.returncode is None:
                    with contextlib.suppress(ProcessLookupError):
                        process.terminate()
                    try:
                        await asyncio.wait_for(asyncio.shield(exited), _PROXY_TERM_GRACE)
                    except TimeoutError:
                        with contextlib.suppress(ProcessLookupError):
                            process.kill()
                await asyncio.wait_for(asyncio.shield(exited), _PROXY_TERM_GRACE)
            finally:
                exited.cancel()
                await finish_output(output)
                await asyncio.gather(stopping, exited, return_exceptions=True)