Repository navigation
Block semop with sem_op -32768 instead of wrapping the semaphore negative - #15760
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
manninglucas
left a comment
There was a problem hiding this comment.
Can we also update wakeWaiters in pkg/sentry/kernel/semaphore/semaphore.go:747 to use if int32(s.value)+int32(w.value) < 0 { (and remove func abs(val int16) int16)? Leaving abs(-32768) in wakeWaiters causes any subsequent ExecuteOps/SetVal on the set to unconditionally wake -32768 waiters, and since semTimedOp (pkg/sentry/syscalls/linux/sys_sem.go:125) restarts BlockWithTimeout with the full relative timeout on each wakeup, repeated spurious wakeups can prevent semtimedop from timing out.
|
Thanks, you're right. Pushed 5fd28b0. New test |
|
Hi @adi-IL, Thank you for implementing the fix and adding regression tests. I'm the original reporter of #15729. I first disclosed this finding to the gVisor security team on September 28, 2026, and subsequently published the issue with my original research and evidence archive. The attached report already documented the I appreciate your work implementing and testing the corrections. Would you mind including the following discovery acknowledgment in the PR description or final commit message?
This would preserve the original research attribution alongside your implementation contribution. Thank you! |
|
Thank you @elnadym165. Your report in #15729 found the bug and pointed at both fixes, including the |
A negative sem_op is a wait: Linux adds it to semval and blocks (or returns EAGAIN with IPC_NOWAIT) when the result is negative. It never returns ERANGE for a wait. Negating SemOp in int16 also overflowed for -32768. Compute the result in int32 and drop the ERANGE check. Reported-by: Mohamed Elnady <[email protected]>
abs(int16) wraps for -32768, so a waiter for -32768 was woken by every change to the semaphore. semtimedop restarts its full timeout on each wakeup, so such a waiter could never time out. Check the result of the op in int32 instead, as executeOps does, and drop abs. Reported-by: Mohamed Elnady <[email protected]>
5fd28b0 to
0d8ae8a
Compare
…tive Fixes #15729 `(*Set).executeOps` negates `op.SemOp` as an `int16`. For `SemOp == -32768` the negation wraps back to -32768, so both the wait check and the ERANGE check are skipped and the semaphore is left at -32768. This does the wait check in `int32` (`semval + sem_op < 0`), which sends the op down the existing block or `EAGAIN` path, and drops the ERANGE branch for negative ops. That matches Linux `ipc/sem.c` `perform_atomic_semop_slow`, where a negative op goes to `would_block` when the result is below zero and can never exceed SEMVMX, so it never returns ERANGE. 5fd28b0 fixes the same wraparound in `wakeWaiters`. `abs(int16)` also wrapped at -32768, so unrelated ops could wake such a waiter. Since `semTimedOp` restarts the full timeout after each wakeup, a semtimedop might never time out. `wakeWaiters` now checks `int32(s.value)+int32(w.value) < 0`, the same form `executeOps` uses, and `abs` is removed. Testing. `TestWaitMinInt16` sets the value to 32767 and checks that -32768 with IPC_NOWAIT returns `ErrWouldBlock` and leaves the value unchanged, and that it blocks without IPC_NOWAIT. It fails without the `executeOps` change. `TestWaitMinInt16NotWoken` blocks a -32768 waiter and checks that an unrelated +1 op doesn't wake it. It fails without the `wakeWaiters` change. `bazel test //pkg/sentry/kernel/semaphore:semaphore_test` passes all 6 tests at 5fd28b0. Reported-by: Mohamed Elnady (@elnadym165), who found and reported the bug in #15729. FUTURE_COPYBARA_INTEGRATE_REVIEW=#15760 from adi-IL:fix-semop-int16-overflow 0d8ae8a PiperOrigin-RevId: 1013863765
Fixes #15729
(*Set).executeOpsnegatesop.SemOpas anint16. ForSemOp == -32768the negation wraps back to -32768, so both the wait check and the ERANGE check are skipped and the semaphore is left at -32768.This does the wait check in
int32(semval + sem_op < 0), which sends the op down the existing block orEAGAINpath, and drops the ERANGE branch for negative ops. That matches Linuxipc/sem.cperform_atomic_semop_slow, where a negative op goes towould_blockwhen the result is below zero and can never exceed SEMVMX, so it never returns ERANGE.5fd28b0 fixes the same wraparound in
wakeWaiters.abs(int16)also wrapped at -32768, so unrelated ops could wake such a waiter. SincesemTimedOprestarts the full timeout after each wakeup, a semtimedop might never time out.wakeWaitersnow checksint32(s.value)+int32(w.value) < 0, the same formexecuteOpsuses, andabsis removed.Testing.
TestWaitMinInt16sets the value to 32767 and checks that -32768 with IPC_NOWAIT returnsErrWouldBlockand leaves the value unchanged, and that it blocks without IPC_NOWAIT. It fails without theexecuteOpschange.TestWaitMinInt16NotWokenblocks a -32768 waiter and checks that an unrelated +1 op doesn't wake it. It fails without thewakeWaiterschange.bazel test //pkg/sentry/kernel/semaphore:semaphore_testpasses all 6 tests at 5fd28b0.Reported-by: Mohamed Elnady (@elnadym165), who found and reported the bug in #15729.