Skip to content

Commit 1d76644

Browse files
picnixzmiss-islington
authored andcommitted
pythongh-130151: Fix reference leaks in _hashlib.hmac_{new,digest} (pythonGH-130152)
* fix leak in `_hashlib.hmac_new` * fix leak in `hmac_digest` * fix exception type in `_hashlib.HMAC.copy` (cherry picked from commit 0718201) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 6abec03 commit 1d76644

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
@@ -1557,7 +1557,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15571557
PyObject *digestmod)
15581558
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
15591559
{
1560-
PyTypeObject *type = get_hashlib_state(module)->HMACtype;
15611560
PY_EVP_MD *digest;
15621561
HMAC_CTX *ctx = NULL;
15631562
HMACobject *self = NULL;
@@ -1570,8 +1569,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15701569
}
15711570

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

@@ -1582,40 +1581,37 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
15821581

15831582
ctx = HMAC_CTX_new();
15841583
if (ctx == NULL) {
1585-
_setException(PyExc_ValueError, NULL);
1584+
PyErr_NoMemory();
15861585
goto error;
15871586
}
15881587

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

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

16061601
self->ctx = ctx;
1602+
ctx = NULL; // 'ctx' is now owned by 'self'
16071603
HASHLIB_INIT_MUTEX(self);
16081604

16091605
if ((msg_obj != NULL) && (msg_obj != Py_None)) {
1610-
if (!_hmac_update(self, msg_obj))
1606+
if (!_hmac_update(self, msg_obj)) {
16111607
goto error;
1608+
}
16121609
}
1613-
1614-
return (PyObject*)self;
1610+
return (PyObject *)self;
16151611

16161612
error:
16171613
if (ctx) HMAC_CTX_free(ctx);
1618-
if (self) PyObject_Free(self);
1614+
Py_XDECREF(self);
16191615
return NULL;
16201616
}
16211617

@@ -1682,14 +1678,14 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
16821678

16831679
HMAC_CTX *ctx = HMAC_CTX_new();
16841680
if (ctx == NULL) {
1685-
return _setException(PyExc_ValueError, NULL);
1681+
return PyErr_NoMemory();
16861682
}
16871683
if (!locked_HMAC_CTX_copy(ctx, self)) {
16881684
HMAC_CTX_free(ctx);
16891685
return _setException(PyExc_ValueError, NULL);
16901686
}
16911687

1692-
retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
1688+
retval = PyObject_New(HMACobject, Py_TYPE(self));
16931689
if (retval == NULL) {
16941690
HMAC_CTX_free(ctx);
16951691
return NULL;
@@ -1704,7 +1700,10 @@ static void
17041700
_hmac_dealloc(HMACobject *self)
17051701
{
17061702
PyTypeObject *tp = Py_TYPE(self);
1707-
HMAC_CTX_free(self->ctx);
1703+
if (self->ctx != NULL) {
1704+
HMAC_CTX_free(self->ctx);
1705+
self->ctx = NULL;
1706+
}
17081707
PyObject_Free(self);
17091708
Py_DECREF(tp);
17101709
}
@@ -1749,6 +1748,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
17491748
return 0;
17501749
}
17511750
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
1751+
HMAC_CTX_free(temp_ctx);
17521752
_setException(PyExc_ValueError, NULL);
17531753
return 0;
17541754
}

0 commit comments

Comments
 (0)