Created
August 3, 2026 20:29
-
-
Save mpage/2ed6b002a76edd0ede6ca43c97c33079 to your computer and use it in GitHub Desktop.
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
| ## Vulnerability #108 | |
| **Title:** Borrowed dict used after lock release | |
| **Category:** Memory Safety Violations | |
| **Tags:** write,race,dos | |
| **CWEs:** CWE-416 | |
| **CVSS:** CVSS:4.0/AV:L/AC:L/AT:P/PR:L/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N | |
| **Severity:** High (7.3) | |
| **Location:** `cpython/Objects/dictobject.c:7409:7415` in function `_PyObject_StoreInstanceAttribute` | |
| ### Description | |
| `_PyObject_StoreInstanceAttribute` keeps a borrowed `PyDictObject *dict` across lock boundaries and then passes it to `store_instance_attr_dict` without taking ownership (`cpython/Objects/dictobject.c:7404-7415`). The callee dereferences/locks that pointer (`cpython/Objects/dictobject.c:7360-7366`). A concurrent `__dict__` replacement path can atomically swap the managed dict pointer (`cpython/Objects/dictobject.c:7673-7674`) and later drop the old dict reference (`cpython/Objects/dictobject.c:7735-7737`), so the stale pointer can be reclaimed before subsequent use. | |
| ### Trigger Conditions | |
| Pre-conditions: | |
| - Interpreter is built with free-threading (`Py_GIL_DISABLED`); this branch is not used in the regular GIL build. | |
| - Target object type uses managed dict + inline values (heap types commonly do this), with writable `__dict__`. | |
| - At least two threads can operate on the same object concurrently. | |
| Data flow: | |
| 1. Thread A repeatedly performs `setattr(obj, "a", 1)` so execution reaches `_PyObject_StoreInstanceAttribute` and obtains `dict` from `_PyObject_GetManagedDict` (`cpython/Objects/dictobject.c:7400-7405`), then leaves object critical section (`cpython/Objects/dictobject.c:7409`). | |
| 2. Thread B concurrently performs `obj.__dict__ = {}` (or another dict), reaching `_PyObject_SetManagedDict`, which swaps managed dict pointer (`cpython/Objects/dictobject.c:7673-7674`) and schedules decref of the replaced dict (`cpython/Objects/dictobject.c:7735-7737`). | |
| 3. Thread A calls `store_instance_attr_dict(obj, dict, ...)` with the stale pointer (`cpython/Objects/dictobject.c:7415`). Under contention, mutex wait may detach thread state (`cpython/Python/lock.c:653-657`, `cpython/Python/parking_lot.c:329-335`, `cpython/Python/pystate.c:2286`). | |
| 4. While A is detached/offline, delayed reclamation can free the old dict; A then resumes and accesses/locks freed memory via `dict` in `store_instance_attr_dict` (`cpython/Objects/dictobject.c:7360-7366`). | |
| ### Impact | |
| This is a concurrent use-after-free in interpreter core dict/object machinery. The most severe consequence is **memory corruption** in the Python process (potentially including arbitrary code execution in favorable heap/race conditions), with more likely outcomes being hard crashes/aborts (denial of service). Exploitability depends on race timing and free-threaded configuration, but the lifetime violation is in a security-critical memory-safety path. | |
| ### Remediation | |
| - In `_PyObject_StoreInstanceAttribute()` (`cpython/Objects/dictobject.c`), ensure the managed dict pointer is stabilized before use across lock boundaries: either take a strong reference to `dict` while still under synchronization, or re-fetch `dict` only under the dict lock path and validate it is still current before calling `store_instance_attr_dict()`. | |
| - In the same function, remove the unlocked gap between `Py_END_CRITICAL_SECTION()` and `store_instance_attr_dict(...)` for the `dict != NULL` race case; restructure so that any borrowed pointer read under object lock is not used after that lock is released unless ownership is held. | |
| - Add/align with the existing free-threading pattern used in `_PyObject_SetManagedDict()` (`try_set_dict_inline_only_or_other_dict` taking `Py_INCREF` before lock transitions): use the same “incref before unlock / decref after use” lifetime discipline for managed dict handoff paths. | |
| - Add a focused free-threaded regression test (threaded setattr + concurrent `__dict__` replacement on the same object) that stress-loops this interleaving and asserts no crash/UAF under debug allocators/ASAN builds. | |
| ### Reproduction | |
| 1. Use a **free-threaded** CPython build (the variant with GIL disabled support), preferably a debug/sanitized build (ASAN/UBSAN or debug assertions) to make failures easier to observe. | |
| 2. Create a test object from a normal heap type (so it has a managed `__dict__`/inline-values behavior), then run two concurrent workloads on the **same object**: | |
| - one workload continuously sets/deletes normal attributes (e.g., `obj.x = ...`, `del obj.x`, many distinct names), | |
| - another workload continuously replaces `obj.__dict__` with new dictionaries. | |
| 3. Keep both workloads running at high iteration counts and high contention (multiple threads repeatedly touching the same object rather than separate objects), and let it run long enough to exercise race windows. | |
| 4. If no signal appears quickly, increase thread count/contention and retry; this race is timing-sensitive. | |
| 5. Confirm the issue by observing any of the following under stress: | |
| - interpreter crash/segfault, | |
| - sanitizer report indicating use-after-free or invalid access in dict/object attribute paths, | |
| - debug assertion failures in critical-section/dict consistency code during attribute store and dict replacement races. | |
| ### Code Context | |
| ``` | |
| Py_END_CRITICAL_SECTION(); | |
| if (dict == NULL) { | |
| return res; | |
| } | |
| } | |
| return store_instance_attr_dict(obj, dict, name, value); | |
| ``` | |
| --- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment