Skip to content

gh-149816: Fix UAF on concurrent dict resize in free-threaded builds#153712

Closed
sourabhligade wants to merge 1 commit into
python:mainfrom
sourabhligade:fix/gh-149816-dict-resize-uaf
Closed

gh-149816: Fix UAF on concurrent dict resize in free-threaded builds#153712
sourabhligade wants to merge 1 commit into
python:mainfrom
sourabhligade:fix/gh-149816-dict-resize-uaf

Conversation

@sourabhligade

@sourabhligade sourabhligade commented Jul 14, 2026

Copy link
Copy Markdown

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 live me_key / me_value pointers 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:

  1. dictkeys_clear_entries / free_keys_object / free_values: clear entry slots before QSBR-delayed free so stale readers cannot revive transferred objects.
  2. dictkeys_decref: snapshot then null slots before XDECREF, so concurrent readers cannot follow a pointer we are about to free.
  3. Thread-safe key compares: a NULL me_key under an active index is treated as DKIX_KEY_CHANGED (force locked retry) rather than a false miss — necessary so poisoning does not surface as spurious KeyError / AttributeError.
  4. lookup_threadsafe_unicode and _LOAD_ATTR_WITH_HINT: re-validate ma_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_resize
  • test_racing_dict_resize_and_lookup

Both 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)
  • Stress repro for attribute load vs resize (50 trials, 0 failures)
  • CI free-threading / TSAN jobs on this PR

…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.
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants