Protect getlist() walk with a critical section (free-threading, #9852) - #9854
Open
espressolee wants to merge 1 commit into
Open
Protect getlist() walk with a critical section (free-threading, #9852)#9854espressolee wants to merge 1 commit into
espressolee wants to merge 1 commit into
Conversation
getlist() (used by Image.point()) walks the caller's list via PySequence_Fast and the unchecked PySequence_Fast_GET_ITEM macro over a size captured earlier. On a free-threaded build a concurrent resize of that list drives an out-of-bounds read and a segfault (python-pillow#9852). Hold a critical section on the fast sequence during the walk and clamp the loop to its current length, mirroring the approach used for FontObject in python-pillow#9498. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
getlist()(used byImage.point()) walks the caller's list withPySequence_Fastand the uncheckedPySequence_Fast_GET_ITEMmacro over a size captured earlier in the function. On a free-threaded buildPySequence_Fastreturns the list itself for a list input, so the walk aliases the caller's list; another thread resizing it drives an out-of-bounds read and a segfault. This is #9852.This holds a critical section on the fast sequence for the walk and clamps the loop to its current length, so a resize that happened between the earlier size read and the walk cannot read out of bounds. It mirrors the critical-section approach already used for
FontObjectin #9498, and uses thePy_BEGIN_CRITICAL_SECTIONshim already vendored inthirdparty/pythoncapi_compat.h(a no-op block on < 3.13, where the GIL serialises the walk anyway).Verification
python3.14.0rc1t, built from source:img.im.point(shared, "L")while others resizeshared) goes from 10/10 SIGSEGV to 0/10 with this change; the no-mutator, decoy and GIL-on controls stay clean.Tests/test_image_point.pypasses (4/4), andpoint()output is unchanged for normal look-up tables (identity and offset LUTs verified).A concurrent resize now yields a truncated read rather than a crash; if you'd prefer it to raise instead, I'm happy to adjust.
Fixes #9852.
🤖 Generated with Claude Code