Repository navigation
Enforce ownership check on shmctl(IPC_RMID) - #14401
Conversation
|
Please add a syscall test. The issue gave a nice reproducer. Make sure that the newly added syscall test covers the reproducer, fails without your fix and passes with your fix. |
|
Added the syscall test The test creates a 0600 SysV shared memory segment as the creator, drops Verification:
|
ayushr2
left a comment
There was a problem hiding this comment.
Please squash your commits
173de2f to
5b7178b
Compare
|
Thanks @ayushr2, that makes complete sense. Updated the implementation:
Verified that |
ayushr2
left a comment
There was a problem hiding this comment.
Thanks for the fix — the bug is real and the scope is right. ipcctl_obtain_check() is only reached from shmctl_down() in Linux (ipc/shm.c), which handles exactly IPC_RMID and IPC_SET, so IPC_RMID was the only missing case: IPC_SET already goes through ipc.Object.Set, IPC_STAT/SHM_STAT correctly check read permission in Shm.IPCStat instead, and IPC_INFO/SHM_INFO have no check in Linux either. (SHM_LOCK/SHM_UNLOCK does skip its CAP_IPC_LOCK-or-euid check, but that's a different predicate on a no-op path — separate issue, not this PR.)
Moving the check into the shm package under s.mu is the right call. A few things left, one substantive — see inline comments.
On the commit message: please use a plain imperative title per repo style, e.g. "Enforce ownership check on shmctl(IPC_RMID)" without the fix: prefix, and drop the Assisted-by: muse spark 1.2 trailer. The PR description is also stale — it still describes the first approach ("In pkg/sentry/syscalls/linux/sys_shm.go:147, obtain creds via auth.CredentialsFromContext(t)...") along with build-process narration that doesn't belong in permanent history.
|
AI usage disclaimer: the above review is from Claude |
5b7178b to
e0edfc4
Compare
|
Thanks @ayushr2, all feedback addressed:
Both |
|
[🤖] Invalidating approval due to rebase as a means to notify @konstantin-s-bogom to re-review. Current diff looks different from pre-rebase diff. |
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 gvisor#14191
80e7c73 to
f502e92
Compare
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check f502e92 PiperOrigin-RevId: 1003171679
[🤖] Invalidating approval due to rebase as a means to notify @konstantin-s-bogom to re-review. Current diff looks different from pre-rebase diff.
|
[🤖] Invalidating approval due to rebase as a means to notify @konstantin-s-bogom to re-review. Current diff looks different from pre-rebase diff. |
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check e679272 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check e679272 PiperOrigin-RevId: 1003171679
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 - `make test TARGETS="//test/syscalls:shm_test_runsc_ptrace"`: PASS - `make test TARGETS="//test/syscalls:shm_test_native"`: PASS Auto-fixed-by: [🤖] @EtiennePerot FUTURE_COPYBARA_INTEGRATE_REVIEW=#14401 from adi-IL:fix/shm-ipcrmid-own-check e679272 PiperOrigin-RevId: 1005154563
Fixes #14191
What was wrong
Any uid could destroy any SysV shared memory segment via
shmctl(IPC_RMID).Shmctlinpkg/sentry/syscalls/linux/sys_shm.godirectly invokedsegment.MarkDestroyed(t)without checking that the caller is the creator, the current owner, or holdsCAP_SYS_ADMINin the owning user namespace. In Linux (ipc/shm.c:shmctl_down),IPC_RMIDandIPC_SETboth requireipcctl_obtain_check. In gVisor,IPC_SETalready enforces this vias.obj.Set, and other SysV mechanisms (semaphore,msgqueue) enforce it viaRegistry.Remove, butshmctl(IPC_RMID)was unauthenticated.What changed
Authorization in
Shm.MarkDestroyed(pkg/sentry/kernel/shm/shm.go):Shm.MarkDestroyed(ctx context.Context) errornow extracts credentials viaauth.CredentialsFromContext(ctx)and executess.obj.CheckOwnership(creds)unders.mu.Lock()within the same critical section that setspendingDestruction = true.linuxerr.EPERMif unauthorized.shmctl(IPC_SET)calls while keeping the ownership policy centralized in theshmpackage.s.registry.dissociateKey(s)ands.DecRef(ctx)outsides.muto preserve theRegistry.mu -> Shm.mulock hierarchy.Syscall Handler (
pkg/sentry/syscalls/linux/sys_shm.go):Shmctl,linux.IPC_RMIDdirectly delegates toreturn 0, nil, segment.MarkDestroyed(t).Syscall Regression Test (
test/syscalls/linux/shm.cc):ShmTest.RmidOwnershipPermissionDenied.CAP_SYS_ADMINdropped, scratch UID/GID) receivesEPERMwhen callingshmctl(IPC_RMID).SHM_DESTis not set onIPC_STAT.IPC_SET,shmctl(IPC_RMID)succeeds.Verification
make test TARGETS="//test/syscalls:shm_test_runsc_ptrace": PASSmake test TARGETS="//test/syscalls:shm_test_native": PASS