From e0fbcfabd4f8c61f7babdc724c687c61894a8c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 16 Jun 2025 15:02:27 +0200 Subject: [PATCH 1/4] fix GIL issue --- Modules/hmacmodule.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Modules/hmacmodule.c b/Modules/hmacmodule.c index b404d5732ec857..b0d3f0aca02183 100644 --- a/Modules/hmacmodule.c +++ b/Modules/hmacmodule.c @@ -498,28 +498,38 @@ _hacl_convert_errno(hacl_errno_t code, PyObject *algorithm) return 0; } case Hacl_Streaming_Types_InvalidAlgorithm: { + PyGILState_STATE gstate = PyGILState_Ensure(); // 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); + PyGILState_Release(gstate); return -1; } case Hacl_Streaming_Types_InvalidLength: { + PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_SetString(PyExc_ValueError, "invalid length"); + PyGILState_Release(gstate); return -1; } case Hacl_Streaming_Types_MaximumLengthExceeded: { + PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_SetString(PyExc_OverflowError, "maximum length exceeded"); + PyGILState_Release(gstate); return -1; } case Hacl_Streaming_Types_OutOfMemory: { + PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_NoMemory(); + PyGILState_Release(gstate); return -1; } default: { + PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_Format(PyExc_RuntimeError, "HACL* internal routine failed with error code: %d", code); + PyGILState_Release(gstate); return -1; } } From 0b2f4e4b08c3b5e36d031f4b39631667bd2b3809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 16 Jun 2025 15:04:18 +0200 Subject: [PATCH 2/4] blurb --- .../next/Library/2025-06-16-15-03-03.gh-issue-135561.mJCN8D.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-16-15-03-03.gh-issue-135561.mJCN8D.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-16-15-03-03.gh-issue-135561.mJCN8D.rst b/Misc/NEWS.d/next/Library/2025-06-16-15-03-03.gh-issue-135561.mJCN8D.rst new file mode 100644 index 00000000000000..ee743f161138e6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-16-15-03-03.gh-issue-135561.mJCN8D.rst @@ -0,0 +1,2 @@ +Fix a crash on DEBUG builds when an HACL* HMAC routine fails. Patch by +Bénédikt Tran. From aea6888c53dc926937de00dc8502ffca85be56c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 16 Jun 2025 17:45:25 +0200 Subject: [PATCH 3/4] simplify `_hacl_convert_errno` --- Modules/hmacmodule.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/Modules/hmacmodule.c b/Modules/hmacmodule.c index b0d3f0aca02183..dc5d48f6f70e96 100644 --- a/Modules/hmacmodule.c +++ b/Modules/hmacmodule.c @@ -493,46 +493,40 @@ 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: { - PyGILState_STATE gstate = PyGILState_Ensure(); // 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); - PyGILState_Release(gstate); - return -1; + break; } case Hacl_Streaming_Types_InvalidLength: { - PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_SetString(PyExc_ValueError, "invalid length"); - PyGILState_Release(gstate); - return -1; + break; } case Hacl_Streaming_Types_MaximumLengthExceeded: { - PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_SetString(PyExc_OverflowError, "maximum length exceeded"); - PyGILState_Release(gstate); - return -1; + break; } case Hacl_Streaming_Types_OutOfMemory: { - PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_NoMemory(); - PyGILState_Release(gstate); - return -1; + break; } default: { - PyGILState_STATE gstate = PyGILState_Ensure(); PyErr_Format(PyExc_RuntimeError, "HACL* internal routine failed with error code: %d", code); - PyGILState_Release(gstate); - return -1; + break; } } + PyGILState_Release(gstate); + return -1; } /* From 718e6208a75a4198155aef3b293150c497f77ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 19 Jun 2025 19:00:58 +0200 Subject: [PATCH 4/4] Update Modules/hmacmodule.c Co-authored-by: Victor Stinner --- Modules/hmacmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/hmacmodule.c b/Modules/hmacmodule.c index dc5d48f6f70e96..9c92d9d145f5a3 100644 --- a/Modules/hmacmodule.c +++ b/Modules/hmacmodule.c @@ -497,6 +497,7 @@ _hacl_convert_errno(hacl_errno_t code, PyObject *algorithm) if (code == Hacl_Streaming_Types_Success) { return 0; } + PyGILState_STATE gstate = PyGILState_Ensure(); switch (code) { case Hacl_Streaming_Types_InvalidAlgorithm: {