8000 gh-130151: Fix reference leaks in `_hashlib.hmac_{new,digest}` (GH-13… · python/cpython@0718201 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0718201

Browse files
authored
gh-130151: Fix reference leaks in _hashlib.hmac_{new,digest} (GH-130152)
* fix leak in `_hashlib.hmac_new` * fix leak in `hmac_digest` * fix exception type in `_hashlib.HMAC.copy`
1 parent 72ea3c0 commit 0718201

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix reference leaks in :func:`!_hashlib.hmac_new` and
2+
:func:`!_hashlib.hmac_digest`. Patch by Bénédikt Tran.

Modules/_hashopenssl.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15561556
PyObject *digestmod)
15571557
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
15581558
{
1559-
PyTypeObject *type = get_hashlib_state(module)->HMACtype;
15601559
PY_EVP_MD *digest;
15611560
HMAC_CTX *ctx = NULL;
15621561
HMACobject *self = NULL;
@@ -1569,8 +1568,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15691568
}
15701569

15711570
if (digestmod == NULL) {
1572-
PyErr_SetString(
1573-
PyExc_TypeError, "Missing required parameter 'digestmod'.");
1571+
PyErr_SetString(PyExc_TypeError,
1572+
"Missing required parameter 'digestmod'.");
15741573
return NULL;
15751574
}
15761575

@@ -1581,40 +1580,37 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15811580

15821581
ctx = HMAC_CTX_new();
15831582
if (ctx == NULL) {
1584-
_setException(PyExc_ValueError, NULL);
1583+
PyErr_NoMemory();
15851584
goto error;
15861585
}
15871586

1588-
r = HMAC_Init_ex(
1589-
ctx,
1590-
(const char*)key->buf,
1591-
(int)key->len,
1592-
digest,
1593-
NULL /*impl*/);
1587+
r = HMAC_Init_ex(ctx, key->buf, (int)key->len, digest, NULL /* impl */);
15941588
PY_EVP_MD_free(digest);
15951589
if (r == 0) {
15961590
_setException(PyExc_ValueError, NULL);
15971591
goto error;
15981592
}
15991593

1600-
self = (HMACobject *)PyObject_New(HMACobject, type);
1594+
_hashlibstate *state = get_hashlib_state(module);
1595+
self = PyObject_New(HMACobject, state->HMACtype);
16011596
if (self == NULL) {
16021597
goto error;
16031598
}
16041599

16051600
self->ctx = ctx;
1601+
ctx = NULL; // 'ctx' is now owned by 'self'
16061602
HASHLIB_INIT_MUTEX(self);
16071603

16081604
if ((msg_obj != NULL) && (msg_obj != Py_None)) {
1609-
if (!_hmac_update(self, msg_obj))
1605+
if (!_hmac_update(self, msg_obj)) {
16101606
goto error;
1607+
}
16111608
}
1612-
1613-
return (PyObject*)self;
1609+
return (PyObject *)self;
16141610

16151611
error:
16161612
if (ctx) HMAC_CTX_free(ctx);
1617-
if (self) PyObject_Free(self);
1613+
Py_XDECREF(self);
16181614
return NULL;
16191615
}
16201616

@@ -1681,14 +1677,14 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
16811677

16821678
HMAC_CTX *ctx = HMAC_CTX_new();
16831679
if (ctx == NULL) {
1684-
return _setException(PyExc_ValueError, NULL);
1680+
return PyErr_NoMemory();
16851681
}
16861682
if (!locked_HMAC_CTX_copy(ctx, self)) {
16871683
HMAC_CTX_free(ctx);
16881684
return _setException(PyExc_ValueError, NULL);
16891685
}
16901686

1691-
retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
1687+
retval = PyObject_New(HMACobject, Py_TYPE(self));
16921688
if (retval == NULL) {
16931689
HMAC_CTX_free(ctx);
16941690
return NULL;
@@ -1703,7 +1699,10 @@ static void
17031699
_hmac_dealloc(HMACobject *self)
17041700
{
17051701
PyTypeObject *tp = Py_TYPE(self);
1706-
HMAC_CTX_free(self->ctx);
1702+
if (self->ctx != NULL) {
1703+
HMAC_CTX_free(self->ctx);
1704+
self->ctx = NULL;
1705+
}
17071706
PyObject_Free(self);
17081707
Py_DECREF(tp);
17091708
}
@@ -1748,6 +1747,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
17481747
return 0;
17491748
}
17501749
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
1750+
HMAC_CTX_free(temp_ctx);
17511751
_setException(PyExc_ValueError, NULL);
17521752
return 0;
17531753
}

0 commit comments

Comments
 (0)
0