Title: `_hashlib`: Use-After-Free + Double Free in `_hashopenssl.c` `py_hashentry_table_new()` · Issue #145301 · python/cpython · GitHub
Open Graph Title: `_hashlib`: Use-After-Free + Double Free in `_hashopenssl.c` `py_hashentry_table_new()` · Issue #145301 · python/cpython
X Title: `_hashlib`: Use-After-Free + Double Free in `_hashopenssl.c` `py_hashentry_table_new()` · Issue #145301 · python/cpython
Description: Crash report What happened? In py_hashentry_table_new(), when _Py_hashtable_set() fails for an alias key (line 270), entry is explicitly freed via PyMem_Free(entry) at line 271. However, this same entry was already successfully inserted ...
Open Graph Description: Crash report What happened? In py_hashentry_table_new(), when _Py_hashtable_set() fails for an alias key (line 270), entry is explicitly freed via PyMem_Free(entry) at line 271. However, this same ...
X Description: Crash report What happened? In py_hashentry_table_new(), when _Py_hashtable_set() fails for an alias key (line 270), entry is explicitly freed via PyMem_Free(entry) at line 271. However, this same ...
Opengraph URL: https://github.com/python/cpython/issues/145301
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_hashlib`: Use-After-Free + Double Free in `_hashopenssl.c` `py_hashentry_table_new()`","articleBody":"# Crash report\n\n### What happened?\n\nIn `py_hashentry_table_new()`, when `_Py_hashtable_set()` fails for an alias key (line 270), `entry` is explicitly freed via `PyMem_Free(entry)` at line 271. However, this same `entry` was already successfully inserted into the hashtable under `py_name` at line 263. The `goto error` path then calls `_Py_hashtable_destroy(ht)` (line 280), which invokes the destroy callback `py_hashentry_t_destroy_value()` on the already-freed entry.\n\n\n```c\n// Modules/_hashopenssl.c - py_hashentry_table_new()\n\nfor (const py_hashentry_t *h = py_hashes; h->py_name != NULL; h++) {\n py_hashentry_t *entry = (py_hashentry_t *)PyMem_Malloc(sizeof(py_hashentry_t));\n if (entry == NULL) {\n goto error;\n }\n memcpy(entry, h, sizeof(py_hashentry_t));\n\n // [line 263] entry inserted into hashtable under py_name - hashtable now owns it\n if (_Py_hashtable_set(ht, (const void*)entry->py_name, (void*)entry) \u003c 0) {\n PyMem_Free(entry);\n goto error;\n }\n entry->refcnt = 1;\n\n if (h->py_alias != NULL) {\n // [line 270] second insert fails (e.g. OOM)\n if (_Py_hashtable_set(ht, (const void*)entry->py_alias, (void*)entry) \u003c 0) {\n PyMem_Free(entry); // [line 271] BUG: entry freed, but still in hashtable under py_name\n goto error; // [line 272] jumps to error path\n }\n entry->refcnt++;\n }\n}\n\nreturn ht;\nerror:\n _Py_hashtable_destroy(ht); // [line 280] destroy callback called on already-freed entry\n return NULL;\n```\n\n### Build\n\n```bash\nmkdir build-asan && cd build-asan\n../configure --with-pydebug --with-address-sanitizer --without-pymalloc\nmake -j$(nproc)\n```\n\n```python\nimport subprocess\nimport sys\n\ncode = (\n \"import sys, _testcapi\\n\"\n \"if '_hashlib' in sys.modules:\\n\"\n \" del sys.modules['_hashlib']\\n\"\n \"_testcapi.set_nomemory(40, 41)\\n\"\n \"try:\\n\"\n \" import _hashlib\\n\"\n \"except (MemoryError, ImportError):\\n\"\n \" pass\\n\"\n \"finally:\\n\"\n \" _testcapi.remove_mem_hooks()\\n\"\n)\n\nresult = subprocess.run(\n [sys.executable, '-c', code],\n capture_output=True, text=True, timeout=10\n)\n\nif result.returncode != 0:\n print(f\"[*] CRASH confirmed (rc={result.returncode})\")\n print(f\"[*] {result.stderr.strip().split(chr(10))[-1]}\")\nelse:\n print(\"[*] No crash (try different start values)\")\n```\n\n```bash\n$ ./build-asan/python uaf_asan.py\n[*] CRASH confirmed (rc=-6)\n[*] python: ../Include/internal/pycore_stackref.h:554: PyStackRef_FromPyObjectSteal: Assertion `obj != NULL' failed.\n```\n\n### GDB backtrace\n\n```bash\n$ gdb -batch -ex run -ex bt --args ./build-asan/python -c \"\nimport sys, _testcapi\nif '_hashlib' in sys.modules:\n del sys.modules['_hashlib']\n_testcapi.set_nomemory(40, 41)\ntry:\n import _hashlib\nexcept (MemoryError, ImportError):\n pass\nfinally:\n _testcapi.remove_mem_hooks()\n\"\n```\n\n```\npython: ../Include/internal/pycore_stackref.h:554: PyStackRef_FromPyObjectSteal: Assertion `obj != NULL' failed.\n\nProgram received signal SIGABRT, Aborted.\n\n#0 __pthread_kill_implementation at ./nptl/pthread_kill.c:44\n#1 __pthread_kill_internal at ./nptl/pthread_kill.c:78\n#2 __GI___pthread_kill at ./nptl/pthread_kill.c:89\n#3 __GI_raise at ../sysdeps/posix/raise.c:26\n#4 __GI_abort at ./stdlib/abort.c:79\n#5 __assert_fail_base — Assertion `obj != NULL' failed.\n#6 __assert_fail at ./assert/assert.c:105\n#7 PyStackRef_FromPyObjectSteal at ../Include/internal/pycore_stackref.h:554\n#8 _PyEval_EvalFrameDefault at ../Python/generated_cases.c.h:292\n...\n#15 import_find_and_load — importing _hashlib\n...\n#19 _PyEval_EvalFrameDefault at ../Python/generated_cases.c.h:6424\n```\n\n\nThe assertion fires because memory corruption during `_hashlib` module init (caused by the UAF/double-free) propagates a NULL into the eval loop.\n\n\n## Suggested Fix\n\nRemove `PyMem_Free(entry)` at line 271. The `entry` is already owned by the hashtable (under `py_name` with `refcnt=1`), so `_Py_hashtable_destroy()` in the error path will correctly clean it up via the destroy callback.\n\n```diff\n if (h->py_alias != NULL) {\n if (_Py_hashtable_set(ht, (const void*)entry->py_alias, (void*)entry) \u003c 0) {\n- PyMem_Free(entry);\n goto error;\n }\n entry->refcnt++;\n }\n```\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a6+\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-145321\n* gh-145523\n* gh-145532\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/raminfp","@type":"Person","name":"raminfp"},"datePublished":"2026-02-27T05:48:19Z","url":"https://github.com/python/cpython/issues/145301"}
| 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:da608e94-9887-5040-f566-6e0bf518e32e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D17A:AFBD9:1AB929C:233029E:6B021B62 |
| html-safe-nonce | 6212c3cdf72ab4c4283f40c7a022cb417f6539fc65b6ff250698781328faf322 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTdBOkFGQkQ5OjFBQjkyOUM6MjMzMDI5RTo2QjAyMUI2MiIsInZpc2l0b3JfaWQiOiI0MjYzODY1NjIyODMzNzk3OTg2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2c3cfc632474c4cc899613a518749a6e618c92fc685865d4aa98a011c8cbc1bc |
| hovercard-subject-tag | issue:3999236952 |
| 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/145301/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f6e4f259bb785d1535dba373fec0e36b4263aa8529762623dab25aa0b5ee6e85/python/cpython/issues/145301 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f6e4f259bb785d1535dba373fec0e36b4263aa8529762623dab25aa0b5ee6e85/python/cpython/issues/145301 |
| og:image:alt | Crash report What happened? In py_hashentry_table_new(), when _Py_hashtable_set() fails for an alias key (line 270), entry is explicitly freed via PyMem_Free(entry) at line 271. However, this same ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | raminfp |
| 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