sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

Sandbox startup fails when kernel.shm_rmid_forced=1 (control registry SysV segment is destroyed on detach)

Open ricardobranco777 opened this issue 1 week ago • 0 comments

On a host with kernel.shm_rmid_forced=1, every sandlock run fails. The first run after a clean state fails with EINVAL, and every later run fails with ENOENT. The sandbox never starts, even for sandlock run -r /usr ... -- true.

Reproducer

  $ cat /proc/sys/kernel/shm_rmid_forced
  1
  $ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /proc -- true
  Error: process error: io error: Invalid argument (os error 22)
  $ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc -r /proc -- true
  Error: process error: io error: No such file or directory (os error 2)

Setting sudo sysctl kernel.shm_rmid_forced=0 makes it work. On the affected machine, 17 of 29 test_cow integration tests also failed until I made that change.

Cause

control/ownership.rs keeps the control registry in SysV shared memory, keyed by 0x534c40ed. It attaches the segment, detaches it, then attaches it again later. With shm_rmid_forced=1, the kernel marks a segment for removal as soon as its attach count reaches zero, so the segment disappears after the first shmdt.

strace of the first run:

  semget(0x534c40ed, 4, IPC_CREAT|0600) = 7
  shmget(0x534c40ed, 524328, IPC_CREAT|0600) = 32794
  shmat(32794, NULL, 0) = 0x70fd5eeb9000
  shmdt(0x70fd5eeb9000) = 0
  shmat(32794, NULL, 0) = -1 EINVAL      <- segment already destroyed

The semaphore array outlives the segment. The next run sees an initialized semaphore and calls shmget(key, size, 0600) without IPC_CREAT, which fails with ENOENT, and it keeps failing until someone removes the semaphore with ipcrm -S 0x534c40ed. A second segment, key 0xc457ffc5, shows the same pattern.

Impact

  • Sandlock is unusable on hosts with this sysctl set. It is not the kernel default (0), but some distributions and hardening guides enable it.
  • Stale semaphores are left behind and must be removed by hand.
  • A failure to register in the control registry takes down sandbox startup. The best-effort fallback already exists for socket bind errors (EPERM, EACCES, ENOSYS), but not for this path.

Suggested fix

Either (or both):

  1. Don't rely on a zero-attach segment staying alive. Keep one attachment open for the lifetime of the process, or for as long as the registry is in use.
  2. Treat registry setup errors as best-effort. Skip registration, so the sandbox still runs and only sandlock ps loses visibility, the way the failed-bind fallback does.

Also, #317 and #333 added registry::disable(), which skips registration entirely. That would avoid this bug, but only for embedders that call it. The CLI and SDK users still hit the bug.

ricardobranco777 avatar Nov 24 '26 05:11 ricardobranco777