Title: gh-145301: Fix double-free in hashlib and hmac module initialization by krylosov-aa · Pull Request #145321 · python/cpython · GitHub
Open Graph Title: gh-145301: Fix double-free in hashlib and hmac module initialization by krylosov-aa · Pull Request #145321 · python/cpython
X Title: gh-145301: Fix double-free in hashlib and hmac module initialization by krylosov-aa · Pull Request #145321 · python/cpython
Description: Both hashlib and hmac modules have similar double-free bugs in their hashtable initialization code. Problem hashlib In py_hashentry_table_new(), when _Py_hashtable_set() fails while adding an entry by its py_alias key (after successfully adding it by py_name), the code calls PyMem_Free(entry) before goto error. The error handler then calls _Py_hashtable_destroy() which frees the same entry again via py_hashentry_t_destroy_value(). hmac In py_hmac_hinfo_ht_new(), the Py_HMAC_HINFO_LINK macro has the same issue. When py_hmac_hinfo_ht_add() fails for hashlib_name key (after successfully adding name), the code calls PyMem_Free(value) while the entry is already in the hashtable with refcnt=1. Solution hashlib Remove the manual PyMem_Free(entry) call since the entry is already tracked by the hashtable under the py_name key and will be properly freed by _Py_hashtable_destroy(). hmac Add a refcnt check before freeing: only call PyMem_Free(value) if value->refcnt == 0 (meaning it was never added to the table). If refcnt > 0, the entry is already in the table and will be freed by _Py_hashtable_destroy(). Tests No test added. I couldn't find a practical way to trigger this bug since it requires _Py_hashtable_set() to fail during module initialization. This type of bug is best caught by sanitizers rather than unit tests. The fix can be verified by running the reproducer provided by the issue author: Build with ASan: mkdir build-asan && cd build-asan ../configure --with-pydebug --with-address-sanitizer --without-pymalloc make -j$(nproc) Reproducer script (uaf_asan.py): import subprocess import sys code = ( "import sys, _testcapi\n" "if '_hashlib' in sys.modules:\n" " del sys.modules['_hashlib']\n" "_testcapi.set_nomemory(40, 41)\n" "try:\n" " import _hashlib\n" "except (MemoryError, ImportError):\n" " pass\n" "finally:\n" " _testcapi.remove_mem_hooks()\n" ) result = subprocess.run( [sys.executable, '-c', code], capture_output=True, text=True, timeout=10 ) if result.returncode != 0: print(f"[*] CRASH confirmed (rc={result.returncode})") print(f"[*] {result.stderr.strip().split(chr(10))[-1]}") else: print("[*] No crash (try different start values)") Before fix: $ ./build-asan/python uaf_asan.py [*] CRASH confirmed (rc=-6) [*] python: ../Include/internal/pycore_stackref.h:554: PyStackRef_FromPyObjectSteal: Assertion `obj != NULL' failed. After fix: No crash. Issue: gh-145301
Open Graph Description: Both hashlib and hmac modules have similar double-free bugs in their hashtable initialization code. Problem hashlib In py_hashentry_table_new(), when _Py_hashtable_set() fails while adding an entry...
X Description: Both hashlib and hmac modules have similar double-free bugs in their hashtable initialization code. Problem hashlib In py_hashentry_table_new(), when _Py_hashtable_set() fails while adding an entry...
Opengraph URL: https://github.com/python/cpython/pull/145321
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:3a044c03-6d1a-2a5b-8bd3-2c23ada0921d |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E39C:29FFA9:40C205E:51EB14E:6B0218DD |
| html-safe-nonce | 1fa6d116b99acfc444d4c2af332a20f5511b17136b25c974ca59e98115b4511f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzlDOjI5RkZBOTo0MEMyMDVFOjUxRUIxNEU6NkIwMjE4REQiLCJ2aXNpdG9yX2lkIjoiMTQ3MTUyMzU0MTkyNDA1OTM1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1aa03ffa03a0a79152c9eae60e05994753742fa5cddfe95d140bc9aaec8b50e0 |
| hovercard-subject-tag | pull_request:3336368625 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/python/cpython/pull/145321/files |
| twitter:image | https://avatars.githubusercontent.com/u/242901957?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/242901957?s=400&v=4 |
| og:image:alt | Both hashlib and hmac modules have similar double-free bugs in their hashtable initialization code. Problem hashlib In py_hashentry_table_new(), when _Py_hashtable_set() fails while adding an entry... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
Viewport: width=device-width