Skip to content

Commit c765683

Browse files
authored
gh-135561: ensure that the GIL is held when handling an HACL* error in _hmac (#135562)
1 parent 9c3c020 commit c765683

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash on DEBUG builds when an HACL* HMAC routine fails. Patch by
2+
Bénédikt Tran.

Modules/hmacmodule.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,36 +493,41 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
493493
static int
494494
_hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
495495
{
496+
assert(PyGILState_GetThisThreadState() != NULL);
497+
if (code == Hacl_Streaming_Types_Success) {
498+
return 0;
499+
}
500+
501+
PyGILState_STATE gstate = PyGILState_Ensure();
496502
switch (code) {
497-
case Hacl_Streaming_Types_Success: {
498-
return 0;
499-
}
500503
case Hacl_Streaming_Types_InvalidAlgorithm: {
501504
// only makes sense if an algorithm is known at call time
502505
assert(algorithm != NULL);
503506
assert(PyUnicode_CheckExact(algorithm));
504507
PyErr_Format(PyExc_ValueError, "invalid algorithm: %U", algorithm);
505-
return -1;
508+
break;
506509
}
507510
case Hacl_Streaming_Types_InvalidLength: {
508511
PyErr_SetString(PyExc_ValueError, "invalid length");
509-
return -1;
512+
break;
510513
}
511514
case Hacl_Streaming_Types_MaximumLengthExceeded: {
512515
PyErr_SetString(PyExc_OverflowError, "maximum length exceeded");
513-
return -1;
516+
break;
514517
}
515518
case Hacl_Streaming_Types_OutOfMemory: {
516519
PyErr_NoMemory();
517-
return -1;
520+
break;
518521
}
519522
default: {
520523
PyErr_Format(PyExc_RuntimeError,
521524
"HACL* internal routine failed with error code: %d",
522525
code);
523-
return -1;
526+
break;
524527
}
525528
}
529+
PyGILState_Release(gstate);
530+
return -1;
526531
}
527532

528533
/*

0 commit comments

Comments
 (0)