Title: Use-after-free in `zone_from_strong_cache` via re-entrant `ZoneInfo.clear_cache()` from key `__eq__` · Issue #142782 · python/cpython · GitHub
Open Graph Title: Use-after-free in `zone_from_strong_cache` via re-entrant `ZoneInfo.clear_cache()` from key `__eq__` · Issue #142782 · python/cpython
X Title: Use-after-free in `zone_from_strong_cache` via re-entrant `ZoneInfo.clear_cache()` from key `__eq__` · Issue #142782 · python/cpython
Description: What happened? The zoneinfo_ZoneInfo_impl() looks up an entry via zone_from_strong_cache, which traverses the strong-cache list and compares keys using PyObject_RichCompareBool(key, node->key, Py_EQ). A crafted str subclass overrides __e...
Open Graph Description: What happened? The zoneinfo_ZoneInfo_impl() looks up an entry via zone_from_strong_cache, which traverses the strong-cache list and compares keys using PyObject_RichCompareBool(key, node->key, Py_E...
X Description: What happened? The zoneinfo_ZoneInfo_impl() looks up an entry via zone_from_strong_cache, which traverses the strong-cache list and compares keys using PyObject_RichCompareBool(key, node->key, P...
Opengraph URL: https://github.com/python/cpython/issues/142782
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use-after-free in `zone_from_strong_cache` via re-entrant `ZoneInfo.clear_cache()` from key `__eq__`","articleBody":"### What happened?\n\nThe `zoneinfo_ZoneInfo_impl()` looks up an entry via `zone_from_strong_cache`, which traverses the strong-cache list and compares keys using `PyObject_RichCompareBool(key, node->key, Py_EQ)`. A crafted `str` subclass overrides `__eq__` to call `ZoneInfo.clear_cache(only_keys=(self,))` during the comparison, freeing the currently visited `StrongCacheNode`. Traversal then proceeds and moves the (now freed) node to the LRU front, triggering a heap use-after-free (ASan crash in `remove_from_strong_cache`).\n\n**Proof of Concept:**\n```python\nfrom zoneinfo import ZoneInfo\n\nclass EvilStr(str):\n def __new__(cls, value: str):\n obj = super().__new__(cls, value)\n obj._triggered = False\n return obj\n\n def __eq__(self, other):\n if not self._triggered:\n self._triggered = True\n ZoneInfo.clear_cache(only_keys=(self,))\n return super().__eq__(other)\n\n def __hash__(self):\n return super().__hash__()\n\nkey1 = EvilStr(\"UTC\")\nkey2 = EvilStr(\"UTC\")\nZoneInfo(key1)\nZoneInfo(key2)\n```\n\n**Affected Versions:**\n\u003cdetails>\n\n| Python Version | Status | Exit Code |\n|---|---|---|\n| `Python 3.9.24+ (heads/3.9:9c4638d, Oct 17 2025, 11:19:30) ` | ASAN | 1 |\n| `Python 3.10.19+ (heads/3.10:0142619, Oct 17 2025, 11:20:05) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.11.14+ (heads/3.11:88f3f5b, Oct 17 2025, 11:20:44) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.12.12+ (heads/3.12:8cb2092, Oct 17 2025, 11:21:35) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.13.9+ (heads/3.13:0760a57, Oct 17 2025, 11:22:25) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.14.0+ (heads/3.14:889e918, Oct 17 2025, 11:23:02) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.15.0a1+ (heads/main:fbf0843, Oct 17 2025, 11:23:37) [GCC 13.3.0]` | ASAN | 1 |\n\n\u003c/details>\n\n**Related Code Snippet**\n\n\u003cdetails>\n\n```\nstatic PyObject *\nzoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key)\n/*[clinic end generated code: output=95e61dab86bb95c3 input=ef73d7a83bf8790e]*/\n{\n zoneinfo_state *state = zoneinfo_get_state_by_self(type);\n PyObject *instance = zone_from_strong_cache(state, type, key); // Trigged __eq__ method\n if (instance != NULL || PyErr_Occurred()) {\n return instance;\n }\n\n PyObject *weak_cache = get_weak_cache(state, type);\n instance = PyObject_CallMethod(weak_cache, \"get\", \"O\", key, Py_None);\n if (instance == NULL) {\n return NULL;\n }\n\n if (instance == Py_None) {\n Py_DECREF(instance);\n PyObject *tmp = zoneinfo_new_instance(state, type, key);\n if (tmp == NULL) {\n return NULL;\n }\n\n instance =\n PyObject_CallMethod(weak_cache, \"setdefault\", \"OO\", key, tmp);\n Py_DECREF(tmp);\n if (instance == NULL) {\n return NULL;\n }\n ((PyZoneInfo_ZoneInfo *)instance)->source = SOURCE_CACHE;\n }\n\n update_strong_cache(state, type, key, instance);\n return instance;\n}\n\n/* Retrieves a ZoneInfo from the strong cache if it's present.\n *\n * This function finds the ZoneInfo by key and if found will move the node to\n * the front of the LRU cache and return a new reference to it. It returns NULL\n * if the key is not in the cache.\n *\n * The strong cache is currently only implemented for the base class, so this\n * always returns a cache miss for subclasses.\n */\nstatic PyObject *\nzone_from_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,\n PyObject *const key)\n{\n if (type != state->ZoneInfoType) {\n return NULL; // Strong cache currently only implemented for base class\n }\n\n _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(state->ZoneInfoType);\n StrongCacheNode *cache = state->ZONEINFO_STRONG_CACHE;\n StrongCacheNode *node = find_in_strong_cache(cache, key);\n\n if (node != NULL) {\n StrongCacheNode **root = &(state->ZONEINFO_STRONG_CACHE);\n // Node has been accessed while it has been freed already\n move_strong_cache_node_to_front(state, root, node);\n return Py_NewRef(node->zone);\n }\n\n return NULL; // Cache miss\n}\n\n/* Retrieves the node associated with a key, if it exists.\n *\n * This traverses the strong cache until it finds a matching key and returns a\n * pointer to the relevant node if found. Returns NULL if no node is found.\n *\n * root may be NULL, indicating an empty cache.\n */\nstatic StrongCacheNode *\nfind_in_strong_cache(const StrongCacheNode *const root, PyObject *const key)\n{\n const StrongCacheNode *node = root;\n while (node != NULL) {\n\t\t// Trigged __eq__ method where the strong cache node has been freed due to the clear\n int rv = PyObject_RichCompareBool(key, node->key, Py_EQ); \n if (rv \u003c 0) {\n return NULL;\n }\n if (rv) {\n return (StrongCacheNode *)node;\n }\n\n node = node->next;\n }\n\n return NULL;\n}\n```\n\n\u003c/details>\n\n**Sanitizer Report**\n\n\u003cdetails>\n\n```python\n=================================================================\n==1443306==ERROR: AddressSanitizer: heap-use-after-free on address 0x506000079ef8 at pc 0x7108671893f8 bp 0x7fff61acc1b0 sp 0x7fff61acc1a0\nREAD of size 8 at 0x506000079ef8 thread T0\n #0 0x7108671893f7 in remove_from_strong_cache Modules/_zoneinfo.c:2379\n #1 0x710867189444 in move_strong_cache_node_to_front Modules/_zoneinfo.c:2457\n #2 0x71086718ce56 in zone_from_strong_cache Modules/_zoneinfo.c:2492\n #3 0x71086719149b in zoneinfo_ZoneInfo_impl Modules/_zoneinfo.c:323\n #4 0x710867191710 in zoneinfo_ZoneInfo Modules/clinic/_zoneinfo.c.h:64\n #5 0x5703b3b3f346 in type_call Objects/typeobject.c:2448\n #6 0x5703b3a18c71 in _PyObject_MakeTpCall Objects/call.c:242\n #7 0x5703b3a18f19 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:167\n #8 0x5703b3a18f72 in PyObject_Vectorcall Objects/call.c:327\n #9 0x5703b3c97056 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #10 0x5703b3cdae54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #11 0x5703b3cdb148 in _PyEval_Vector Python/ceval.c:2001\n #12 0x5703b3cdb3f8 in PyEval_EvalCode Python/ceval.c:884\n #13 0x5703b3dd2507 in run_eval_code_obj Python/pythonrun.c:1365\n #14 0x5703b3dd2723 in run_mod Python/pythonrun.c:1459\n #15 0x5703b3dd357a in pyrun_file Python/pythonrun.c:1293\n #16 0x5703b3dd6220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #17 0x5703b3dd64f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #18 0x5703b3e2774d in pymain_run_file_obj Modules/main.c:410\n #19 0x5703b3e279b4 in pymain_run_file Modules/main.c:429\n #20 0x5703b3e291b2 in pymain_run_python Modules/main.c:691\n #21 0x5703b3e29842 in Py_RunMain Modules/main.c:772\n #22 0x5703b3e29a2e in pymain_main Modules/main.c:802\n #23 0x5703b3e29db3 in Py_BytesMain Modules/main.c:826\n #24 0x5703b38ad645 in main Programs/python.c:15\n #25 0x710867c2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #26 0x710867c2a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #27 0x5703b38ad574 in _start (/home/jackfromeast/Desktop/entropy/tasks/grammar-afl++-latest/targets/cpython/python+0x2dd574) (BuildId: ff3dc40ea460bd4beb2c3a72283cca525b319bf0)\n\n0x506000079ef8 is located 24 bytes inside of 56-byte region [0x506000079ee0,0x506000079f18)\nfreed by thread T0 here:\n #0 0x7108680fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52\n #1 0x5703b3adf96d in _PyMem_RawFree Objects/obmalloc.c:91\n #2 0x5703b3ae1cd9 in _PyMem_DebugRawFree Objects/obmalloc.c:2955\n #3 0x5703b3ae1d1a in _PyMem_DebugFree Objects/obmalloc.c:3100\n #4 0x5703b3b09348 in PyMem_Free Objects/obmalloc.c:1070\n #5 0x710867190c1a in strong_cache_node_free Modules/_zoneinfo.c:2344\n #6 0x710867190c93 in eject_from_strong_cache Modules/_zoneinfo.c:2435\n #7 0x710867191079 in zoneinfo_ZoneInfo_clear_cache_impl Modules/_zoneinfo.c:524\n #8 0x7108671912f3 in zoneinfo_ZoneInfo_clear_cache Modules/clinic/_zoneinfo.c.h:255\n #9 0x5703b3acb0b6 in cfunction_vectorcall_FASTCALL_KEYWORDS_METHOD Objects/methodobject.c:481\n #10 0x5703b3a18e7f in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #11 0x5703b3a18f72 in PyObject_Vectorcall Objects/call.c:327\n #12 0x5703b3c9ec60 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:2920\n #13 0x5703b3cdae54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #14 0x5703b3cdb148 in _PyEval_Vector Python/ceval.c:2001\n #15 0x5703b3a189b8 in _PyFunction_Vectorcall Objects/call.c:413\n #16 0x5703b3b2b56b in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #17 0x5703b3b479a6 in vectorcall_unbound Objects/typeobject.c:3033\n #18 0x5703b3b479a6 in maybe_call_special_one_arg Objects/typeobject.c:3175\n #19 0x5703b3b47ad3 in _PyObject_MaybeCallSpecialOneArg Objects/typeobject.c:3190\n #20 0x5703b3b47b19 in slot_tp_richcompare Objects/typeobject.c:10729\n #21 0x5703b3ad8687 in do_richcompare Objects/object.c:1059\n #22 0x5703b3ad893d in PyObject_RichCompare Objects/object.c:1108\n #23 0x5703b3ad89ad in PyObject_RichCompareBool Objects/object.c:1130\n #24 0x71086718af2a in find_in_strong_cache Modules/_zoneinfo.c:2403\n #25 0x71086718ce3f in zone_from_strong_cache Modules/_zoneinfo.c:2488\n #26 0x71086719149b in zoneinfo_ZoneInfo_impl Modules/_zoneinfo.c:323\n #27 0x710867191710 in zoneinfo_ZoneInfo Modules/clinic/_zoneinfo.c.h:64\n #28 0x5703b3b3f346 in type_call Objects/typeobject.c:2448\n #29 0x5703b3a18c71 in _PyObject_MakeTpCall Objects/call.c:242\n #30 0x5703b3a18f19 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:167\n\npreviously allocated by thread T0 here:\n #0 0x7108680fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69\n #1 0x5703b3ae0284 in _PyMem_RawMalloc Objects/obmalloc.c:63\n #2 0x5703b3adf655 in _PyMem_DebugRawAlloc Objects/obmalloc.c:2887\n #3 0x5703b3adf6bd in _PyMem_DebugRawMalloc Objects/obmalloc.c:2920\n #4 0x5703b3ae0f3b in _PyMem_DebugMalloc Objects/obmalloc.c:3085\n #5 0x5703b3b09204 in PyMem_Malloc Objects/obmalloc.c:1041\n #6 0x71086718d3c9 in strong_cache_node_new Modules/_zoneinfo.c:2324\n #7 0x7108671913ba in update_strong_cache Modules/_zoneinfo.c:2514\n #8 0x710867191514 in zoneinfo_ZoneInfo_impl Modules/_zoneinfo.c:350\n #9 0x710867191710 in zoneinfo_ZoneInfo Modules/clinic/_zoneinfo.c.h:64\n #10 0x5703b3b3f346 in type_call Objects/typeobject.c:2448\n #11 0x5703b3a18c71 in _PyObject_MakeTpCall Objects/call.c:242\n #12 0x5703b3a18f19 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:167\n #13 0x5703b3a18f72 in PyObject_Vectorcall Objects/call.c:327\n #14 0x5703b3c97056 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #15 0x5703b3cdae54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #16 0x5703b3cdb148 in _PyEval_Vector Python/ceval.c:2001\n #17 0x5703b3cdb3f8 in PyEval_EvalCode Python/ceval.c:884\n #18 0x5703b3dd2507 in run_eval_code_obj Python/pythonrun.c:1365\n #19 0x5703b3dd2723 in run_mod Python/pythonrun.c:1459\n #20 0x5703b3dd357a in pyrun_file Python/pythonrun.c:1293\n #21 0x5703b3dd6220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #22 0x5703b3dd64f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #23 0x5703b3e2774d in pymain_run_file_obj Modules/main.c:410\n #24 0x5703b3e279b4 in pymain_run_file Modules/main.c:429\n #25 0x5703b3e291b2 in pymain_run_python Modules/main.c:691\n #26 0x5703b3e29842 in Py_RunMain Modules/main.c:772\n #27 0x5703b3e29a2e in pymain_main Modules/main.c:802\n #28 0x5703b3e29db3 in Py_BytesMain Modules/main.c:826\n #29 0x5703b38ad645 in main Programs/python.c:15\n\nSUMMARY: AddressSanitizer: heap-use-after-free Modules/_zoneinfo.c:2379 in remove_from_strong_cache\nShadow bytes around the buggy address:\n 0x506000079c00: fd fd fd fd fa fa fa fa fd fd fd fd fd fd fd fa\n 0x506000079c80: fa fa fa fa fd fd fd fd fd fd fd fd fa fa fa fa\n 0x506000079d00: fd fd fd fd fd fd fd fd fa fa fa fa fd fd fd fd\n 0x506000079d80: fd fd fd fd fa fa fa fa fd fd fd fd fd fd fd fa\n 0x506000079e00: fa fa fa fa fd fd fd fd fd fd fd fd fa fa fa fa\n=>0x506000079e80: fd fd fd fd fd fd fd fd fa fa fa fa fd fd fd[fd]\n 0x506000079f00: fd fd fd fa fa fa fa fa fd fd fd fd fd fd fd fd\n 0x506000079f80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\n 0x50600007a000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\n 0x50600007a080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\n 0x50600007a100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\nShadow byte legend (one shadow byte represents 8 application bytes):\n Addressable: 00\n Partially addressable: 01 02 03 04 05 06 07 \n Heap left redzone: fa\n Freed heap region: fd\n Stack left redzone: f1\n Stack mid redzone: f2\n Stack right redzone: f3\n Stack after return: f5\n Stack use after scope: f8\n Global redzone: f9\n Global init order: f6\n Poisoned by user: f7\n Container overflow: fc\n Array cookie: ac\n Intra object redzone: bb\n ASan internal: fe\n Left alloca redzone: ca\n Right alloca redzone: cb\n==1443306==ABORTING\n```\n\n\u003c/details>\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159441\n* gh-161463\n* gh-161505\n* gh-161506\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-16T02:45:26Z","url":"https://github.com/python/cpython/issues/142782"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e3a2790c-8d9f-a386-96fd-8a22c8941d2b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C782:33444D:352FEE7:46B3FA2:6B020381 |
| html-safe-nonce | 6aeb19a9ecf23dc508391a48619000e43f1000b987a2d66b129392458314c773 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzgyOjMzNDQ0RDozNTJGRUU3OjQ2QjNGQTI6NkIwMjAzODEiLCJ2aXNpdG9yX2lkIjoiNjEwNjQ2NzMyMzY1NTM1NzMxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3dbffa817cc9e6aa4c14fa91ceb268ac23eb9e8804a615f0356f4dee751401b9 |
| hovercard-subject-tag | issue:3732869165 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/142782/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eab12487caf50d4dc680031fc86854fb32000bc02f399b09761a7601859093fd/python/cpython/issues/142782 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eab12487caf50d4dc680031fc86854fb32000bc02f399b09761a7601859093fd/python/cpython/issues/142782 |
| og:image:alt | What happened? The zoneinfo_ZoneInfo_impl() looks up an entry via zone_from_strong_cache, which traverses the strong-cache list and compares keys using PyObject_RichCompareBool(key, node->key, Py_E... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jackfromeast |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width