Skip to content

Commit aa6da32

Browse files
tiranalexhenrie
andauthored
bpo-43362: Fix invalid free and return check in _sha3 module (GH-25463)
Commit 93d50a6 / GH-21855 changed the order of variable definitions, which introduced a potential invalid free bug. Py_buffer object is now initialized earlier and the result of Keccak initialize is verified. Co-authored-by: Alex Henrie <[email protected]> Signed-off-by: Christian Heimes <[email protected]> Co-authored-by: Alex Henrie <[email protected]>
1 parent f5c5c0c commit aa6da32

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix invalid free in _sha3 module. The issue was introduced in 3.10.0a1.
2+
Python 3.9 and earlier are not affected.

Modules/_sha3/sha3module.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,16 @@ static PyObject *
193193
py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity)
194194
/*[clinic end generated code: output=90409addc5d5e8b0 input=bcfcdf2e4368347a]*/
195195
{
196+
HashReturn res;
197+
Py_buffer buf = {NULL, NULL};
198+
SHA3State *state = PyType_GetModuleState(type);
196199
SHA3object *self = newSHA3object(type);
197200
if (self == NULL) {
198201
goto error;
199202
}
200203

201-
SHA3State *state = PyType_GetModuleState(type);
202204
assert(state != NULL);
203205

204-
HashReturn res;
205206
if (type == state->sha3_224_type) {
206207
res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
207208
} else if (type == state->sha3_256_type) {
@@ -229,7 +230,12 @@ py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity)
229230
goto error;
230231
}
231232

232-
Py_buffer buf = {NULL, NULL};
233+
if (res != SUCCESS) {
234+
PyErr_SetString(PyExc_RuntimeError,
235+
"internal error in SHA3 initialize()");
236+
goto error;
237+
}
238+
233239
if (data) {
234240
GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
235241
if (buf.len >= HASHLIB_GIL_MINSIZE) {

0 commit comments

Comments
 (0)