Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix reference leaks in :func:`!_hashlib.hmac_new` and
:func:`!_hashlib.hmac_digest`. Patch by Bénédikt Tran.
34 changes: 17 additions & 17 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
PyObject *digestmod)
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
{
PyTypeObject *type = get_hashlib_state(module)->HMACtype;
PY_EVP_MD *digest;
HMAC_CTX *ctx = NULL;
HMACobject *self = NULL;
Expand All @@ -1569,8 +1568,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
}

if (digestmod == NULL) {
PyErr_SetString(
PyExc_TypeError, "Missing required parameter 'digestmod'.");
PyErr_SetString(PyExc_TypeError,
"Missing required parameter 'digestmod'.");
return NULL;
}

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

ctx = HMAC_CTX_new();
if (ctx == NULL) {
_setException(PyExc_ValueError, NULL);
PyErr_NoMemory();
goto error;
}

r = HMAC_Init_ex(
ctx,
(const char*)key->buf,
(int)key->len,
digest,
NULL /*impl*/);
r = HMAC_Init_ex(ctx, key->buf, (int)key->len, digest, NULL /* impl */);
PY_EVP_MD_free(digest);
if (r == 0) {
_setException(PyExc_ValueError, NULL);
goto error;
}

self = (HMACobject *)PyObject_New(HMACobject, type);
_hashlibstate *state = get_hashlib_state(module);
self = PyObject_New(HMACobject, state->HMACtype);
if (self == NULL) {
goto error;
}

self->ctx = ctx;
ctx = NULL; // 'ctx' is now owned by 'self'
HASHLIB_INIT_MUTEX(self);

if ((msg_obj != NULL) && (msg_obj != Py_None)) {
if (!_hmac_update(self, msg_obj))
if (!_hmac_update(self, msg_obj)) {
goto error;
}
}

return (PyObject*)self;
return (PyObject *)self;

error:
if (ctx) HMAC_CTX_free(ctx);
if (self) PyObject_Free(self);
Py_XDECREF(self);
return NULL;
}

Expand Down Expand Up @@ -1681,7 +1677,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)

HMAC_CTX *ctx = HMAC_CTX_new();
if (ctx == NULL) {
return _setException(PyExc_ValueError, NULL);
return PyErr_NoMemory();
}
if (!locked_HMAC_CTX_copy(ctx, self)) {
HMAC_CTX_free(ctx);
Expand All @@ -1703,7 +1699,10 @@ static void
_hmac_dealloc(HMACobject *self)
{
PyTypeObject *tp = Py_TYPE(self);
HMAC_CTX_free(self->ctx);
if (self->ctx != NULL) {
HMAC_CTX_free(self->ctx);
self->ctx = NULL;
}
PyObject_Free(self);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1748,6 +1747,7 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
return 0;
}
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
HMAC_CTX_free(temp_ctx);
_setException(PyExc_ValueError, NULL);
return 0;
}
Expand Down
Loading