gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup#148847
Closed
colesbury wants to merge 1 commit intopython:mainfrom
Closed
gh-148820: Fix _PyRawMutex use-after-free on spurious semaphore wakeup#148847colesbury wants to merge 1 commit intopython:mainfrom
_PyRawMutex use-after-free on spurious semaphore wakeup#148847colesbury wants to merge 1 commit intopython:mainfrom
Conversation
d659c6b to
da9e042
Compare
… wakeup _PyRawMutex_UnlockSlow CAS-removes the waiter from the list and then calls _PySemaphore_Wakeup, with no handshake. If _PySemaphore_Wait returns Py_PARK_INTR, the waiter can destroy its stack-allocated semaphore before the unlocker's Wakeup runs, causing a fatal error from ReleaseSemaphore / sem_post. Loop in _PyRawMutex_LockSlow until _PySemaphore_Wait returns Py_PARK_OK, which is only signalled when a matching Wakeup has been observed. Also include GetLastError() and the handle in the Windows fatal messages in _PySemaphore_Init, _PySemaphore_Wait, and _PySemaphore_Wakeup to make similar races easier to diagnose in the future.
da9e042 to
52bf353
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.
_PyRawMutex_UnlockSlowCAS-removes the waiter from the list and then calls_PySemaphore_Wakeup, with no handshake (unlike parking_lot'swait_entry.is_unparkingprotocol). If the waiter's_PySemaphore_Waitreturns for any reason other than the matching Wakeup — e.g.Py_PARK_INTRfromsigint_eventon the main thread on Windows, orEINTRon POSIX — the waiter can exit the wait, re-acquire the mutex (already CAS-unlocked by the unlocker), break out of the loop, and call_PySemaphore_Destroyon the stack-allocated semaphore before the unlocker's_PySemaphore_Wakeupruns. That Wakeup then hits a closed handle, producingFatal Python error: _PySemaphore_Wakeup: parking_lot: ReleaseSemaphore failed(Win32ERROR_INVALID_HANDLE).Observed on free-threaded 3.14 on Windows with
coverage run+ trio +signal.raise_signal(SIGINT)from a non-main thread. Parking-lot's_PyParkingLot_Parkis not affected because its bucket-lockedis_unparkinghandshake already ensures the parker waits for the unparker's Wakeup before destroying the semaphore.Fix
Loop in
_PyRawMutex_LockSlowuntil_PySemaphore_WaitreturnsPy_PARK_OK.Py_PARK_OKis only returned when a matching post was observed on all backends (WAIT_OBJECT_0/sem_waitsuccess /sema->counter > 0), so looping until OK guarantees the unlocker's pending Wakeup has fired before we destroy the semaphore. Deferring SIGINT until the unlock completes is acceptable in practice:_PyRawMutexprotects only short critical sections (principally parking-lot bucket locks), so ctrl-C is delivered near-immediately.Also include
GetLastError()and theHANDLEvalue in the Windows fatal messages in_PySemaphore_Init,_PySemaphore_Wait, and_PySemaphore_Wakeup— these were key in identifying the bug as a use-after-free (error 6 =ERROR_INVALID_HANDLE) rather than a double-release.The
_PySemaphore_WakeupAPI signature is intentionally left unchanged to preserve the ability to backport this fix.Based on analysis and patch by @colesbury in https://gist.github.com/colesbury/3e4c6180e3eb4b3b9fd07b26f3196e12 / https://gist.github.com/colesbury/f9f0c5cf1a00f2c946e80795fcc245d7.
Verification
Stress harness (16 workers × 100 iterations ×
coverage runof the trio SIGINT-from-thread reproducer) showed ~0.19% crash rate on the unpatched baseline and 0% with the loop.