sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

sandbox: setsid/setpgid lets a process leave the sandbox's process group

Open congwang-mk opened this issue 2 months ago • 0 comments

Every teardown path reaches the sandbox through its process group: sandlock kill, Sandbox::kill/pause/resume, Drop for Sandbox, and the OCI supervisor all use killpg(child_pid, ...). Nothing filters setsid or setpgid, so a sandboxed process can step out of that group and none of those paths can see it any more:

$ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /dev/null -w /dev/null -- sh -c 'setsid sleep 72 & sleep 1; exit 0'
$ ps -o pid,ppid,pgid,sid,stat,args -C sleep
    PID    PPID    PGID     SID STAT COMMAND
1033046    5179 1033046 1033046 Ss   sleep 72

sleep 72 is its own session and group leader (pgid = sid = its pid), reparented, still running after the sandbox exited. While the sandbox is alive the same process is also invisible to sandlock kill and to pause/resume, which makes pause a partial freeze.

This also bounds the fixes for #238 and #251: both can be closed for well-behaved trees by killing the group, but a process that called setsid or setpgid escapes either one.

Options:

  1. Kill by pid set instead of by group. fork/clone are already notified, so the supervisor knows every pid in the sandbox; teardown, kill and pause can walk that set (pidfds avoid reuse races). This keeps job control working and does not depend on what the sandboxed code does with groups. The catch for #238 is that a watchdog outside the supervisor would need that set too.
  2. Trap setsid/setpgid and track the new group ids, so group-based teardown signals every group the sandbox has created. Cheaper than (1) and keeps interactive shells working, since bash calls setpgid for every job.
  3. Deny setsid/setpgid. Simplest, but breaks job control in interactive shells and anything that daemonizes, so probably not acceptable as a default.

(2) looks like the smallest change that keeps the existing killpg structure; (1) is the more robust end state.

congwang-mk avatar Sep 21 '26 22:09 congwang-mk