sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

sandbox: background descendants outlive a normal sandbox exit

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

When the main child exits, the supervisor returns and releases the seccomp listener, but nothing sweeps the sandbox's process group. Any descendant still running is reparented and keeps going after sandlock run has returned 0:

$ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /dev/null -w /dev/null -- sh -c 'sleep 73 & sleep 1; exit 0'
$ echo $?
0
$ ps -o pid,ppid,pgid,sid,args -C sleep
    PID    PPID    PGID     SID COMMAND
1033674    5179 1033673 1033667 sleep 73      # still there 6s later, NoNewPrivs=1, Seccomp=2

Drop for Sandbox only calls killpg while the state is Created, Running or Paused, so once the main child has been reaped the group is left alone.

This is the same end state as #238 without anyone killing the supervisor. The survivors stay confined by Landlock and the seccomp filter, but every notified syscall returns ENOSYS, so they limp instead of working, and they still hold the caller's stdio. A survivor that wakes up later writes its failures into the caller's terminal after the sandbox is supposedly gone:

$ sandlock run ... -- sh -c 'setsid sh -c "sleep 2; cat /etc/hostname; echo rc=\$?" & sleep 1; exit 0'
$ # prompt is back, then:
sleep: write error: Function not implemented
Segmentation fault (core dumped)
sh: 1: Cannot fork

It is also racy from the sandboxed program's point of view: with sh -c 'sleep 73 & exit 0' the background job sometimes starts and survives, and sometimes loses the race with teardown and dies with cannot open /dev/null: Function not implemented. setsid cmd as the sandbox command fails the same way (cannot open shared object file: Error 38), because setsid(1) forks and the original leader exits.

Proposed fix: when the main child exits, kill the whole process group unconditionally before releasing the listener, so the sandbox's lifetime is the lifetime of its main process. The group id stays valid while any member is alive, so killpg after reaping the leader still reaches them. If waiting for background jobs is ever wanted, that should be an explicit option, not the accidental half-alive state we have today.

Processes that left the group are not covered by this; that is tracked separately.

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