[3.14] gh-135561: ensure that the GIL is held when handling an HACL* error in _hmac (GH-135562) (#135725)

gh-135561: ensure that the GIL is held when handling an HACL* error in `_hmac` (GH-135562)
(cherry picked from commit c765683398)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-06-19 21:48:29 +02:00 committed by GitHub
parent 8caf3fdacb
commit 83e0ab17f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 8 deletions

View file

@ -493,36 +493,41 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
static int
_hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
{
assert(PyGILState_GetThisThreadState() != NULL);
if (code == Hacl_Streaming_Types_Success) {
return 0;
}
PyGILState_STATE gstate = PyGILState_Ensure();
switch (code) {
case Hacl_Streaming_Types_Success: {
return 0;
}
case Hacl_Streaming_Types_InvalidAlgorithm: {
// only makes sense if an algorithm is known at call time
assert(algorithm != NULL);
assert(PyUnicode_CheckExact(algorithm));
PyErr_Format(PyExc_ValueError, "invalid algorithm: %U", algorithm);
return -1;
break;
}
case Hacl_Streaming_Types_InvalidLength: {
PyErr_SetString(PyExc_ValueError, "invalid length");
return -1;
break;
}
case Hacl_Streaming_Types_MaximumLengthExceeded: {
PyErr_SetString(PyExc_OverflowError, "maximum length exceeded");
return -1;
break;
}
case Hacl_Streaming_Types_OutOfMemory: {
PyErr_NoMemory();
return -1;
break;
}
default: {
PyErr_Format(PyExc_RuntimeError,
"HACL* internal routine failed with error code: %d",
code);
return -1;
break;
}
}
PyGILState_Release(gstate);
return -1;
}
/*