Skip to content

Commit b4b81e1

Browse files
authored
gh-159811: Raise OpenSSL errors in _hashlib.HMAC after re-attaching the thread state (#159818)
`hashlib_openssl_HMAC_update_once()` set an exception when the OpenSSL HMAC update function failed, but it was called inside a GIL-released region and the object's mutex held by `_hashlib.HMAC.update()` and `_hashlib.hmac_new()` when the data exceeds `HASHLIB_GIL_MINSIZE` bytes. Instead, the HMAC update function is called directly in `_hmac_update()`, and the exception is set once the thread state is re-attached and the mutex is released (restores the behavior prior to gh-134531). Since the OpenSSL error queue is per thread, the exception type and message are unaffected.
1 parent 6f439ee commit b4b81e1

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in :mod:`hmac` when the OpenSSL routine used by
2+
:meth:`hmac.HMAC.update` or by :func:`hmac.new` fails on data larger
3+
than 2048 bytes. Patch by Bénédikt Tran.

‎Modules/_hashopenssl.c‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,16 +1976,6 @@ hashlib_HMAC_get_hashlib_digest_name(HMACobject *self)
19761976
#endif
19771977
}
19781978

1979-
static int
1980-
hashlib_openssl_HMAC_update_once(PY_HMAC_CTX_TYPE *ctx, const Py_buffer *v)
1981-
{
1982-
if (!PY_HMAC_update(ctx, (const unsigned char *)v->buf, (size_t)v->len)) {
1983-
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(PY_HMAC_update));
1984-
return -1;
1985-
}
1986-
return 0;
1987-
}
1988-
19891979
/* Thin wrapper around PY_HMAC_CTX_free that allows to pass a NULL 'ctx'. */
19901980
static inline void
19911981
hashlib_openssl_HMAC_CTX_free(PY_HMAC_CTX_TYPE *ctx)
@@ -2240,10 +2230,15 @@ _hmac_update(HMACobject *self, PyObject *obj)
22402230
GET_BUFFER_VIEW_OR_ERROR(obj, &view, return -1);
22412231
HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
22422232
self, view.len,
2243-
r = hashlib_openssl_HMAC_update_once(self->ctx, &view)
2233+
r = PY_HMAC_update(self->ctx, (const unsigned char *)view.buf,
2234+
(size_t)view.len)
22442235
);
22452236
PyBuffer_Release(&view);
2246-
return r;
2237+
if (r == 0) {
2238+
notify_smart_ssl_error_occurred_in(Py_STRINGIFY(PY_HMAC_update));
2239+
return -1;
2240+
}
2241+
return 0;
22472242
}
22482243

22492244
/*[clinic input]

0 commit comments

Comments
 (0)