Skip to main content

ensure_ipc_socket_dir

Function ensure_ipc_socket_dir 

Source
pub fn ensure_ipc_socket_dir() -> PathBuf
Expand description

Directory under which all launcher Unix sockets live. Created with 0700 perms so cross-user squatting can’t happen.

Resolution order (A1.1 of SPEC_LAUNCHER_LINUX_PACKAGED_AND_SPLASH_ 2026_06_05):

  1. $XDG_RUNTIME_DIR/agentmux/ — preferred; tmpfs, per-user, automatically cleaned up by systemd-logind on session end.
  2. /tmp/agentmux-{uid}/ — fallback for environments without a systemd user manager. UID in the path so users can’t collide.

Security (codex P1 + reagent P1 on #1288). The /tmp fallback path is reachable by every local user. The resolver must close every TOCTOU window:

  • If the dir doesn’t exist, create it NON-RECURSIVELY (so a race between our stat and our create fails with AlreadyExists, not silently succeeds the way create_dir_all would).
  • Immediately after a successful create, RE-STAT and verify ownership + mode. A create_dir(0700) syscall under a 0022 umask still produces a 0700 dir; this re-stat is belt-and- suspenders against the unlikely case where the dir we just created has been replaced by an attacker between mkdir and re-stat.
  • If the dir already exists, symlink_metadata (NOT metadata — we must not follow symlinks) and refuse to proceed unless it’s a real directory owned by our uid with mode masking 0700.
  • Refusal = std::process::exit(2) with a clear error. We do NOT try to recover by picking a different path; that would enlarge the trust boundary. Ensure the IPC socket directory exists with safe ownership + mode.

This is the SIDE-EFFECTING half of the path/ensure split — callers that need the directory to actually exist (only the launcher’s startup path) call this BEFORE binding/connecting. Read-only inspectors (e.g. future --diag tools) use ipc_socket_dir_path directly and never reach this code.

Returns the validated directory path on success. Calls std::process::exit(2) on any verification failure — we do NOT recover by picking a different path; that would enlarge the trust boundary.

(Reagent P2 on PR #1288: previously this logic was inside the ipc_socket_dir function which pipe_name called, making pipe_name a filesystem-mutating + process-exiting function on Unix while it’s a pure string formatter on Windows. Split into a pure ipc_socket_dir_path + this ensure-step.)