Skip to content

Commit c1d7768

Browse files
authored
gh-145376: Fix crashes in md5module.c and hmacmodule.c (#145422)
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.
1 parent 1d091a3 commit c1d7768

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix double free and null pointer dereference in unusual error scenarios
2+
in :mod:`hashlib` and :mod:`hmac` modules.

Modules/hmacmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,6 @@ static void
13781378
py_hmac_hinfo_ht_free(void *hinfo)
13791379
{
13801380
py_hmac_hinfo *entry = (py_hmac_hinfo *)hinfo;
1381-
assert(entry->display_name != NULL);
13821381
if (--(entry->refcnt) == 0) {
13831382
Py_CLEAR(entry->display_name);
13841383
PyMem_Free(hinfo);
@@ -1477,7 +1476,8 @@ py_hmac_hinfo_ht_new(void)
14771476
e->hashlib_name == NULL ? e->name : e->hashlib_name
14781477
);
14791478
if (value->display_name == NULL) {
1480-
PyMem_Free(value);
1479+
/* 'value' is owned by the table (refcnt > 0),
1480+
so _Py_hashtable_destroy() will free it. */
14811481
goto error;
14821482
}
14831483
}

Modules/md5module.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ static void
8787
MD5_dealloc(PyObject *op)
8888
{
8989
MD5object *ptr = _MD5object_CAST(op);
90-
Hacl_Hash_MD5_free(ptr->hash_state);
90+
if (ptr->hash_state != NULL) {
91+
Hacl_Hash_MD5_free(ptr->hash_state);
92+
ptr->hash_state = NULL;
93+
}
9194
PyTypeObject *tp = Py_TYPE(op);
9295
PyObject_GC_UnTrack(ptr);
9396
PyObject_GC_Del(ptr);

0 commit comments

Comments
 (0)