Skip to content

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

Merged
copybara-service[bot] merged 3 commits into
masterfrom
test/cl1013863765
Nov 4, 2026
Merged

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

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

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

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

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]>
@copybara-service copybara-service Bot added the exported Issue was exported automatically label Nov 4, 2026
@copybara-service
copybara-service Bot merged commit 2ac0f43 into master Nov 4, 2026
8 of 11 checks passed
@copybara-service
copybara-service Bot deleted the test/cl1013863765 branch November 4, 2026 20:06
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.

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

2 participants