Skip to content

Enforce ownership check on shmctl(IPC_RMID) - #16263

Merged
copybara-service[bot] merged 3 commits into
masterfrom
test/cl1005154563
Oct 23, 2026
Merged

copybara-service[bot] merged 3 commits into
masterfrom
test/cl1005154563

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Enforce ownership check on shmctl(IPC_RMID)

Fixes #14191

What was wrong

Any uid could destroy any SysV shared memory segment via shmctl(IPC_RMID). Shmctl in pkg/sentry/syscalls/linux/sys_shm.go directly invoked segment.MarkDestroyed(t) without checking that the caller is the creator, the current owner, or holds CAP_SYS_ADMIN in the owning user namespace. In Linux (ipc/shm.c:shmctl_down), IPC_RMID and IPC_SET both require ipcctl_obtain_check. In gVisor, IPC_SET already enforces this via s.obj.Set, and other SysV mechanisms (semaphore, msgqueue) enforce it via Registry.Remove, but shmctl(IPC_RMID) was unauthenticated.

What changed

  1. Authorization in Shm.MarkDestroyed (pkg/sentry/kernel/shm/shm.go):

    • Shm.MarkDestroyed(ctx context.Context) error now extracts credentials via auth.CredentialsFromContext(ctx) and executes s.obj.CheckOwnership(creds) under s.mu.Lock() within the same critical section that sets pendingDestruction = true.
    • Returns linuxerr.EPERM if unauthorized.
    • Prevents data races and TOCTOU with concurrent shmctl(IPC_SET) calls while keeping the ownership policy centralized in the shm package.
    • Calls s.registry.dissociateKey(s) and s.DecRef(ctx) outside s.mu to preserve the Registry.mu -> Shm.mu lock hierarchy.
  2. Syscall Handler (pkg/sentry/syscalls/linux/sys_shm.go):

    • In Shmctl, linux.IPC_RMID directly delegates to return 0, nil, segment.MarkDestroyed(t).
  3. Syscall Regression Test (test/syscalls/linux/shm.cc):

    • Added ShmTest.RmidOwnershipPermissionDenied.
    • Verifies that a non-owner/non-creator process with dropped privileges (CAP_SYS_ADMIN dropped, scratch UID/GID) receives EPERM when calling shmctl(IPC_RMID).
    • Confirms the segment survives and SHM_DEST is not set on IPC_STAT.
    • Verifies the positive path: after transferring ownership to the scratch user via IPC_SET, shmctl(IPC_RMID) succeeds.

Verification

adi-IL and others added 2 commits October 20, 2026 16:30
Any uid could destroy any SysV shared memory segment via
shmctl(IPC_RMID) because Shmctl bypassed the ownership check that
Registry.Remove enforces for semaphore and msgqueue. Only the creator
or owner or a task with CAP_SYS_ADMIN in the owning user namespace
should be allowed to mark a segment for destruction, matching
Linux ipcctl_obtain_check and object CheckOwnership.

Enforce CheckOwnership inside Shm.MarkDestroyed under s.mu.Lock() in the
same critical section that transitions pendingDestruction to true,
returning linuxerr.EPERM if unauthorized. This ensures atomic authorization
and state transition without lock bouncing, and prevents TOCTOU against
concurrent shmctl(IPC_SET). Key dissociation occurs outside s.mu to
preserve the registry-to-shm lock hierarchy.

Add a syscall regression test RmidOwnershipPermissionDenied in
test/syscalls/linux/shm.cc covering permission denial on non-owners,
SHM_DEST survival check, and successful destruction following ownership
transfer via IPC_SET.

Fixes #14191
@copybara-service copybara-service Bot added the exported Issue was exported automatically label Oct 23, 2026
@copybara-service
copybara-service Bot merged commit 9f4c048 into master Oct 23, 2026
8 of 10 checks passed
@copybara-service
copybara-service Bot deleted the test/cl1005154563 branch October 23, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

exported Issue was exported automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Any uid can destroy any SysV shared memory segment

2 participants