-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-145301: Fix double-free in hashlib and hmac module initialization #145321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
157f397
799cd48
6b2f0d3
dc3e19c
d791cd9
154eda5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| import tempfile | ||
| import threading | ||
| import unittest | ||
| import subprocess | ||
| import textwrap | ||
| from test import support | ||
| from test.support import _4G, bigmemtest | ||
| from test.support import hashlib_helper | ||
|
|
@@ -1201,6 +1203,43 @@ def test_readonly_types(self): | |
| with self.assertRaisesRegex(TypeError, "immutable type"): | ||
| hash_type.value = False | ||
|
|
||
| @unittest.skipUnless(HASH is not None, 'need _hashlib') | ||
| def test_hashlib_init_memory_error_no_df(self): | ||
| """gh-145301 regression test.""" | ||
|
|
||
| try: | ||
| import _testcapi | ||
| if not hasattr(_testcapi, 'set_nomemory'): | ||
| self.skipTest('requires _testcapi.set_nomemory') | ||
| except ImportError: | ||
| self.skipTest('requires _testcapi') | ||
|
||
|
|
||
| code = textwrap.dedent(""" | ||
| import sys | ||
|
||
| import _testcapi | ||
|
|
||
| if '_hashlib' in sys.modules: | ||
| del sys.modules['_hashlib'] | ||
|
||
|
|
||
| _testcapi.set_nomemory(40, 41) | ||
| try: | ||
| import _hashlib | ||
| except (MemoryError, ImportError): | ||
| pass | ||
| finally: | ||
| _testcapi.remove_mem_hooks() | ||
| """) | ||
|
|
||
| rc = subprocess.call( | ||
|
||
| [sys.executable, '-c', code], | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| ) | ||
| # rc < 0 means crash (signal on Unix), which indicates double-free | ||
| # rc >= 0 means normal exit (even with MemoryError), which is expected | ||
| self.assertGreaterEqual(rc, 0, | ||
| "Process crashed - Loss double-free in _hashlib") | ||
|
|
||
|
|
||
| class KDFTests(unittest.TestCase): | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix a crash when :mod:`hashlib` or :mod:`hmac` C extension module initialization fails. | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1457,7 +1457,11 @@ py_hmac_hinfo_ht_new(void) | |
| do { \ | ||
| int rc = py_hmac_hinfo_ht_add(table, KEY, value); \ | ||
| if (rc < 0) { \ | ||
| PyMem_Free(value); \ | ||
| /* entry may already be in ht, will be freed by \ | ||
| _Py_hashtable_destroy() */ \ | ||
|
||
| if (value->refcnt == 0) { \ | ||
| PyMem_Free(value); \ | ||
| } \ | ||
| goto error; \ | ||
| } \ | ||
| else if (rc == 1) { \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the imports alphabetically sorted.