Skip to content

Block semop with sem_op -32768 instead of wrapping the semaphore negative - #15760

Merged
copybara-service[bot] merged 2 commits into
gvisor:masterfrom
adi-IL:fix-semop-int16-overflow
Nov 4, 2026
Merged

copybara-service[bot] merged 2 commits into
gvisor:masterfrom
adi-IL:fix-semop-int16-overflow

Conversation

@adi-IL

@adi-IL adi-IL commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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.

@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@manninglucas manninglucas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adi-IL

adi-IL commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, you're right. abs(-32768) wraps, so a waiter with that value could be woken by unrelated ops, and since semTimedOp restarts the full timeout after each wakeup, a semtimedop might never time out.

Pushed 5fd28b0. wakeWaiters now checks int32(s.value)+int32(w.value) < 0, the same form executeOps uses, and abs is removed. Waiters for zero still wake exactly as before.

New test TestWaitMinInt16NotWoken blocks a -32768 waiter and checks that an unrelated +1 op doesn't wake it. It fails without the semaphore.go change and passes with it. bazel test //pkg/sentry/kernel/semaphore:semaphore_test passes, all 6 tests.

@elnadym165

elnadym165 commented Oct 15, 2026 •

Copy link
Copy Markdown

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 sem_op=-32768 overflow, Linux/runc/runsc differential reproductions, the proposed int32 arithmetic fix, removal of the incompatible ERANGE path, and the related abs(int16) issue in wakeWaiters.

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?

Reported-by: Mohamed Elnady (@elnadym165)

This would preserve the original research attribution alongside your implementation contribution.

Thank you!

@adi-IL

adi-IL commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @elnadym165. Your report in #15729 found the bug and pointed at both fixes, including the abs(int16) problem in wakeWaiters. I added "Reported-by: Mohamed Elnady (@elnadym165)" to the PR description. Both commits will carry a Reported-by trailer too.

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]>
@adi-IL
adi-IL force-pushed the fix-semop-int16-overflow branch from 5fd28b0 to 0d8ae8a Compare October 15, 2026 07:03
@manninglucas manninglucas added the ready to pull Causes the PR to be imported into Google. Will be removed soon. label Oct 19, 2026
@EtiennePerot EtiennePerot added ready to pull Causes the PR to be imported into Google. Will be removed soon. and removed ready to pull Causes the PR to be imported into Google. Will be removed soon. labels Nov 4, 2026
copybara-service Bot pushed a commit that referenced this pull request Nov 4, 2026
…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
@copybara-service
copybara-service Bot merged commit 2ac0f43 into gvisor:master Nov 4, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull Causes the PR to be imported into Google. Will be removed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

semaphore: semop(-32768) incorrectly succeeds and produces negative semaphore values

4 participants