fix(tools): survive unreadable parent mount-ns in the launcher namespace guard (#613)

[T] Observed live on physical silicon (Tesla P100 SXM2, host-native
nvidia_rs lanes, kernel 6.12.105, driver 8f5bf596):

do_namespace() reads /proc/<getppid()>/ns/mnt unconditionally. When the
launcher is started through a container-runtime or supervisor chain
(incus exec, docker exec, systemd wrappers) the spawning process can
already be gone or invisible in the PID namespace, getppid() reports
pid 0, and the readlink aborts the launcher with:

    FileNotFoundError: [Errno 2] No such file or directory: '/proc/0/ns/mnt'

before any mount step runs. This reproduced on every lane start in that
setup until the guard was made tolerant.

Fix: fall back to the namespace init's mount ns (/proc/1/ns/mnt), which
is the original mount namespace the app process must diverge from, and
if even that link is unreadable (hidepid / ptrace checks under a
non-root test run) degrade to the self-comparison alone. The guard
stays fail-closed in all three paths: if unshare() did not create a
private namespace, now == before still aborts.

Verification (both on the patch, python 3.12):
- non-root, getppid() -> 0: launcher now passes the guard and stops at
  unshare(CLONE_NEWNS) EPERM (was: FileNotFoundError crash at the guard)
- root, getppid() -> 0: do_namespace() completes, in_private_namespace
  True, ns id differs from before (guard + unshare + check all work)
- unchanged behavior when getppid()'s ns is readable (first path taken)

Signed-off-by: Patrick Hamid <phamid@users.noreply.github.com>
Co-authored-by: Patrick Hamid <phamid@users.noreply.github.com>
This commit is contained in:
Patrick Hamid
2026-12-06 02:20:40 +11:00
committed by GitHub
parent 59190a5dc1
commit 02cf1e18cc

View File

@ -542,7 +542,23 @@ class Executor:
def do_namespace(self, _detail: Dict[str, object]) -> None:
before = os.readlink("/proc/self/ns/mnt")
parent = os.readlink("/proc/%d/ns/mnt" % os.getppid())
try:
parent = os.readlink("/proc/%d/ns/mnt" % os.getppid())
except OSError:
# The spawning process can already be gone or invisible when the
# launcher is started through a container-runtime or supervisor
# chain (e.g. `incus exec`/`docker exec`, systemd wrappers):
# getppid() may report pid 0 or a pid without a /proc entry.
# This comparison only proves that unshare() below created a new
# namespace, so the namespace's init is a safe stand-in for the
# original mount namespace.
try:
parent = os.readlink("/proc/1/ns/mnt")
except OSError:
# Another user's namespace links can also be unreadable
# (hidepid / ptrace checks): degrade to the self-comparison,
# which alone still proves that unshare() took effect.
parent = before
if self.libc.unshare(CLONE_NEWNS) != 0:
raise LauncherError("unshare(CLONE_NEWNS): " + os.strerror(ctypes.get_errno()))
now = os.readlink("/proc/self/ns/mnt")