]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145376: Fix crashes in `md5module.c` and `hmacmodule.c` (#145422)
authorPieter Eendebak <pieter.eendebak@gmail.com>
Fri, 6 Mar 2026 20:00:06 +0000 (21:00 +0100)
committerGitHub <noreply@github.com>
Fri, 6 Mar 2026 20:00:06 +0000 (20:00 +0000)
Fix a possible NULL pointer dereference in `md5module.c` and a double-free in `hmacmodule.c`.
Those crashes only occur in error paths taken when the interpreter fails to allocate memory.

Misc/NEWS.d/next/Library/2026-03-02-19-41-39.gh-issue-145376.OOzSOh.rst [new file with mode: 0644]
Modules/hmacmodule.c
Modules/md5module.c

diff --git a/Misc/NEWS.d/next/Library/2026-03-02-19-41-39.gh-issue-145376.OOzSOh.rst b/Misc/NEWS.d/next/Library/2026-03-02-19-41-39.gh-issue-145376.OOzSOh.rst
new file mode 100644 (file)
index 0000000..b6dbda0
--- /dev/null
@@ -0,0 +1,2 @@
+Fix double free and null pointer dereference in unusual error scenarios
+in :mod:`hashlib` and :mod:`hmac` modules.
index 7a040103bcb234863a3fd536333ef2e14950057e..1a212fa3d37e18dfa3abfd16511d70bc8c759463 100644 (file)
@@ -1378,7 +1378,6 @@ static void
 py_hmac_hinfo_ht_free(void *hinfo)
 {
     py_hmac_hinfo *entry = (py_hmac_hinfo *)hinfo;
-    assert(entry->display_name != NULL);
     if (--(entry->refcnt) == 0) {
         Py_CLEAR(entry->display_name);
         PyMem_Free(hinfo);
@@ -1477,7 +1476,8 @@ py_hmac_hinfo_ht_new(void)
             e->hashlib_name == NULL ? e->name : e->hashlib_name
         );
         if (value->display_name == NULL) {
-            PyMem_Free(value);
+            /* 'value' is owned by the table (refcnt > 0),
+               so _Py_hashtable_destroy() will free it. */
             goto error;
         }
     }
index 56e9faf4c62002899777975c53876ca6581c4212..e598b1fe67240dc70ee7c35eae0fc42c28ba7c81 100644 (file)
@@ -87,7 +87,10 @@ static void
 MD5_dealloc(PyObject *op)
 {
     MD5object *ptr = _MD5object_CAST(op);
-    Hacl_Hash_MD5_free(ptr->hash_state);
+    if (ptr->hash_state != NULL) {
+        Hacl_Hash_MD5_free(ptr->hash_state);
+        ptr->hash_state = NULL;
+    }
     PyTypeObject *tp = Py_TYPE(op);
     PyObject_GC_UnTrack(ptr);
     PyObject_GC_Del(ptr);