`zoneinfo`: Heap use-after-free and double-free in `clear_strong_cache()` via re-entrant key `__del__` · Issue #163188 · python/cpython
Repository navigation
zoneinfo: Heap use-after-free and double-free in clear_strong_cache() via re-entrant key __del__ #163188
New issue
New issue
Backlog
Backlog
zoneinfo: Heap use-after-free and double-free in clear_strong_cache() via re-entrant key __del__ #163188
Labels
extension-modules
type-bug
Description
@nevercodecorrect's profile nevercodecorrect opened on Dec 5, 2026Bug report
Description
In Modules/_zoneinfo.c, clear_strong_cache() (L2581-2589 ) passes state->ZONEINFO_STRONG_CACHE to strong_cache_free() (L2587) before clearing state->ZONEINFO_STRONG_CACHE = NULL (L2588).
When a cached key is a str subclass whose __del__ calls ZoneInfo.clear_cache(), Py_XDECREF(node->key) inside strong_cache_node_free() (L2380 ) re-entrantly invokes clear_strong_cache() on the still-attached linked list, freeing all StrongCacheNode structs. Upon return, the outer strong_cache_node_free() / strong_cache_free() frame dereferences the freed node and next_node (L2381, L2383, L2398), causing a heap use-after-free, double-Py_XDECREF, and double-free under both standard GIL and free-threaded CPython .
Reproducer
import zoneinfo
class EvilKey(str):
def __del__(self):
zoneinfo.ZoneInfo.clear_cache()
zoneinfo.ZoneInfo.clear_cache()
for name in ("UTC", "GMT", "Etc/UTC"):
try:
k = EvilKey(name)
_ = zoneinfo.ZoneInfo(k)
del k
except Exception:
pass
zoneinfo.ZoneInfo.clear_cache()
./configure --with-address-sanitizer --with-pydebug make -j PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python -X faulthandler reproduce.py
=================================================================
==63==ERROR: AddressSanitizer: heap-use-after-free on address 0x503000027970
READ of size 8 at 0x503000027970 thread T0
#0 strong_cache_free Modules/_zoneinfo.c:2398:27
#1 clear_strong_cache Modules/_zoneinfo.c:2587:5
#2 zoneinfo_ZoneInfo_clear_cache_impl Modules/_zoneinfo.c:524:9
Causes
clear_strong_cachefrees before nulling the global root pointer:Lines 2581 to 2589 in 47a261d
clear_strong_cache ( zoneinfo_state * state , const PyTypeObject * const type ) { if ( type != state -> ZoneInfoType ) { return ; } strong_cache_free ( state -> ZONEINFO_STRONG_CACHE ); state -> ZONEINFO_STRONG_CACHE = NULL ; } strong_cache_node_free/strong_cache_freetraverse cachednext_nodeacrossPy_XDECREF(node->key):Lines 2378 to 2403 in 47a261d
strong_cache_node_free ( StrongCacheNode * node ) { Py_XDECREF ( node -> key ); Py_XDECREF ( node -> zone ); PyMem_Free ( node ); } /* Frees all nodes at or after a specified root in the strong cache. * * This can be used on the root node to free the entire cache or it can be used * to clear all nodes that have been expired (which, if everything is going * right, will actually only be 1 node at a time). */ void strong_cache_free ( StrongCacheNode * root ) { StrongCacheNode * node = root ; StrongCacheNode * next_node ; while ( node != NULL ) { next_node = node -> next ; strong_cache_node_free ( node ); node = next_node ; } }
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
No one assignedLabels
extension-modules
type-bug