Skip to content

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

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

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash on DEBUG builds when an HACL* HMAC routine fails. Patch by
Bénédikt Tran.
21 changes: 13 additions & 8 deletions Modules/hmacmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down
Loading