gh-149816: Fix UAF on concurrent dict resize in free-threaded builds#153712
Closed
sourabhligade wants to merge 1 commit into
Closed
gh-149816: Fix UAF on concurrent dict resize in free-threaded builds#153712sourabhligade wants to merge 1 commit into
sourabhligade wants to merge 1 commit into
Conversation
…uilds When a shared dict is resized, the old keys/values tables are retained via QSBR so concurrent lock-free readers can still touch the memory. Those tables previously kept live me_key/me_value pointers after ownership was transferred or released, so a later update could free the objects while a reader (including LOAD_ATTR_WITH_HINT) still held a stale table — a use-after-free. Clear entry slots before QSBR-delayed free, null before decref in dictkeys_decref, re-validate ma_keys after acquiring a value in the hint fast path and threadsafe unicode lookup, and treat a NULL me_key under an active index as KEY_CHANGED so poisoned tables force a locked retry rather than a false miss. This addresses finding (124) from the free-threading race audit.
|
The following commit authors need to sign the Contributor License Agreement: |
24 tasks
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.
Summary
Fixes finding (124) from #149816: a use-after-free when a free-threaded dict is resized while another thread does lock-free lookups (including specialized
LOAD_ATTR_WITH_HINT).Root cause
On resize of a shared dict, the old
PyDictKeysObject/ values array is retained via QSBR so concurrent lock-free readers can still touch the table memory. Those tables previously kept liveme_key/me_valuepointers after ownership was transferred to the new table (or released on clear). A later update of the dict can free those objects while a reader still holds a stale table pointer and tries to incref them — UAF / ABA.Fix
Structural hardening of the reclaim path, not a one-line band-aid:
dictkeys_clear_entries/free_keys_object/free_values: clear entry slots before QSBR-delayed free so stale readers cannot revive transferred objects.dictkeys_decref: snapshot then null slots beforeXDECREF, so concurrent readers cannot follow a pointer we are about to free.NULLme_keyunder an active index is treated asDKIX_KEY_CHANGED(force locked retry) rather than a false miss — necessary so poisoning does not surface as spuriousKeyError/AttributeError.lookup_threadsafe_unicodeand_LOAD_ATTR_WITH_HINT: re-validatema_keys(and the slot value) after acquiring a reference, matching_Py_dict_lookup_threadsafe. Covers the deferred-refcount try-incref path that skips slot re-validation.Tests
test_racing_load_attr_with_hint_and_resizetest_racing_dict_resize_and_lookupBoth stress concurrent resize + lock-free / specialized reads under free-threading (and TSAN in local builds).
Test plan
./python.exe -m unittest test.test_free_threading.test_dict.TestDict test.test_free_threading.test_races.TestRaces(×5, free-threaded + TSAN)