Skip to content

Commit 0fba211

Browse files
picnixzgpshead
andauthored
gh-159786: Convert HACL* update() errors in _hmac after releasing the lock (GH-159805)
* gh-159786: Convert HACL* update() errors in `_hmac` after releasing the lock `_hacl_hmac_state_update_once()` converted the HACL* exit code into an exception while the thread state was detached and, for HMAC.update(), while the object's mutex was held. On debug builds, this re-attached the thread state under the mutex and then called PyErr_Occurred() with the thread state detached in the chunking loop of `_hacl_hmac_state_update()`. Return the raw HACL* exit code instead and convert it in `_hmac.new()` and `_hmac.HMAC.update()` once the thread state is re-attached and the mutex is released, as `hmac_digest_compute_locked()` already does. Release builds keep ignoring the update() exit code. Since `_hacl_convert_errno()` is now always called with an attached thread state, remove the `PyGILState_Ensure()` added by gh-135561 and assert that a thread state is attached instead. * simplify ifdefs: always check for the hacl error --------- Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 4c356f7 commit 0fba211

2 files changed

Lines changed: 27 additions & 29 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fix the error handling of the HACL* routine used by :func:`hmac.new` and
2+
:meth:`hmac.HMAC.update`. If the maximum input length of the underlying hash
3+
function is exceeded, an :exc:`OverflowError` is now raised on all builds,
4+
instead of crashing on DEBUG builds and being silently ignored otherwise.
5+
Code correctness. Realistically this can only happen at implausible sizes.
6+
Patch by Bénédikt Tran.

‎Modules/hmacmodule.c‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,17 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
379379
*
380380
* If 'code' represents a successful operation, this returns 0.
381381
* Otherwise, this sets an appropriate exception and returns -1.
382+
*
383+
* The caller must have an attached thread state.
382384
*/
383385
static int
384386
_hacl_convert_errno(hacl_errno_t code)
385387
{
386-
assert(PyGILState_GetThisThreadState() != NULL);
388+
assert(PyThreadState_GetUnchecked() != NULL);
387389
if (code == Hacl_Streaming_Types_Success) {
388390
return 0;
389391
}
390392

391-
PyGILState_STATE gstate = PyGILState_Ensure();
392393
switch (code) {
393394
case Hacl_Streaming_Types_InvalidAlgorithm: {
394395
PyErr_SetString(PyExc_ValueError, "invalid HACL* algorithm");
@@ -413,7 +414,6 @@ _hacl_convert_errno(hacl_errno_t code)
413414
break;
414415
}
415416
}
416-
PyGILState_Release(gstate);
417417
return -1;
418418
}
419419

@@ -449,39 +449,33 @@ _hacl_hmac_state_free(HACL_HMAC_state *state)
449449
/*
450450
* Call the HACL* HMAC-HASH update function on the given data.
451451
*
452-
* On DEBUG builds, the update() call is verified.
453-
*
454-
* Return 0 on success; otherwise, set an exception and return -1 on failure.
452+
* Return the HACL* exit code. No exception is set, so that this
453+
* can be called without an attached thread state or with a lock held.
455454
*/
456-
static int
455+
static hacl_errno_t
457456
_hacl_hmac_state_update_once(HACL_HMAC_state *state,
458457
uint8_t *buf, uint32_t len)
459458
{
460-
#ifndef NDEBUG
461-
hacl_errno_t code = Hacl_Streaming_HMAC_update(state, buf, len);
462-
return _hacl_convert_errno(code);
463-
#else
464-
(void)Hacl_Streaming_HMAC_update(state, buf, len);
465-
return 0;
466-
#endif
459+
return Hacl_Streaming_HMAC_update(state, buf, len);
467460
}
468461

469462
/*
470463
* Perform the HMAC-HASH update() operation in a streaming fashion.
471464
*
472-
* On DEBUG builds, each update() call is verified.
473-
*
474-
* Return 0 on success; otherwise, set an exception and return -1 on failure.
465+
* Return the HACL* exit code. No exception is set, so that this
466+
* can be called without an attached thread state or with a lock held.
467+
* Use _hacl_convert_errno() afterwards to set an appropriate exception.
475468
*/
476-
static int
469+
static hacl_errno_t
477470
_hacl_hmac_state_update(HACL_HMAC_state *state, uint8_t *buf, Py_ssize_t len)
478471
{
479472
assert(len >= 0);
480473
#ifdef Py_HMAC_SSIZE_LARGER_THAN_UINT32
481474
while (len > UINT32_MAX_AS_SSIZE_T) {
482-
if (_hacl_hmac_state_update_once(state, buf, UINT32_MAX) < 0) {
483-
assert(PyErr_Occurred());
484-
return -1;
475+
hacl_errno_t code = _hacl_hmac_state_update_once(state, buf,
476+
UINT32_MAX);
477+
if (code != Hacl_Streaming_Types_Success) {
478+
return code;
485479
}
486480
buf += UINT32_MAX;
487481
len -= UINT32_MAX;
@@ -786,18 +780,16 @@ _hmac_new_impl(PyObject *module, PyObject *keyobj, PyObject *msgobj,
786780
GET_BUFFER_VIEW_OR_ERROR(msgobj, &msg, goto error);
787781
/* Do not use self->mutex here as this is the constructor
788782
* where it is not yet possible to have concurrent access. */
783+
hacl_errno_t code;
789784
HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(
790785
msg.len,
791-
rc = _hacl_hmac_state_update(self->state, msg.buf, msg.len)
786+
code = _hacl_hmac_state_update(self->state, msg.buf, msg.len)
792787
);
793788
PyBuffer_Release(&msg);
794-
#ifndef NDEBUG
789+
rc = _hacl_convert_errno(code);
795790
if (rc < 0) {
796791
goto error;
797792
}
798-
#else
799-
(void)rc;
800-
#endif
801793
}
802794
assert(rc == 0);
803795
return (PyObject *)self;
@@ -889,15 +881,15 @@ static PyObject *
889881
_hmac_HMAC_update_impl(HMACObject *self, PyObject *msgobj)
890882
/*[clinic end generated code: output=962134ada5e55985 input=7c0ea830efb03367]*/
891883
{
892-
int rc = 0;
884+
hacl_errno_t code;
893885
Py_buffer msg;
894886
GET_BUFFER_VIEW_OR_ERROUT(msgobj, &msg);
895887
HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
896888
self, msg.len,
897-
rc = _hacl_hmac_state_update(self->state, msg.buf, msg.len)
889+
code = _hacl_hmac_state_update(self->state, msg.buf, msg.len)
898890
);
899891
PyBuffer_Release(&msg);
900-
return rc < 0 ? NULL : Py_None;
892+
return _hacl_convert_errno(code) < 0 ? NULL : Py_None;
901893
}
902894

903895
/*

0 commit comments

Comments
 (0)