Repository navigation
Block semop with sem_op -32768 instead of wrapping the semaphore negative - #16974
Merged
Merged
Conversation
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
Bot
force-pushed
the
test/cl1013863765
branch
from
November 4, 2026 19:07
1b53f26 to
0fe4adf
Compare
PiperOrigin-RevId: 1014018775
copybara-service
Bot
force-pushed
the
test/cl1013863765
branch
from
November 4, 2026 20:05
0fe4adf to
2ac0f43
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Block semop with sem_op -32768 instead of wrapping the semaphore negative
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.
FUTURE_COPYBARA_INTEGRATE_REVIEW=#15760 from adi-IL:fix-semop-int16-overflow 0d8ae8a