Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-1635741: Fix potential refleaks in binascii module #18613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Changes from 1 commit
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
46 changes: 43 additions & 3 deletions 46 Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ typedef struct binascii_state {
PyObject *Incomplete;
} binascii_state;

static binascii_state *
get_binascii_state(PyObject *module)
{
return (binascii_state *)PyModule_GetState(module);
}

/*
** hqx lookup table, ascii->binary.
*/
Expand Down Expand Up @@ -1617,17 +1623,21 @@ binascii_exec(PyObject *m) {
if (state->Error == NULL) {
return -1;
}
Py_INCREF(state->Error);
result = PyModule_AddObject(m, "Error", state->Error);
if (result == -1) {
Py_DECREF(state->Error);
return -1;
}

state->Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
if (state->Incomplete == NULL) {
return -1;
}
Py_INCREF(state->Incomplete);
result = PyModule_AddObject(m, "Incomplete", state->Incomplete);
if (result == -1) {
Py_DECREF(state->Incomplete);
return -1;
}

Expand All @@ -1639,16 +1649,46 @@ static PyModuleDef_Slot binascii_slots[] = {
{0, NULL}
};

static int
binascii_traverse(PyObject *m, visitproc visit, void *arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I would prefer to use longer variable name than just "m". Rename "m" to "mod" or "module". Same remark if following functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, no probleam, i will update it.

{
binascii_state *state = get_binascii_state(m);
if (state == NULL) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this behavior soon.

return -1;
}
Py_VISIT(state->Error);
Py_VISIT(state->Incomplete);
return 0;
}

static int
binascii_clear(PyObject *m)
{
binascii_state *state = get_binascii_state(m);
if (state == NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that state can be NULL. If it's NULL, it's a bug in Python. No? Same remark in binascii_traverse().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do some debug in my vm:

Breakpoint 1, binascii_traverse (module=0x7ffff7e803d0, visit=0x4630b6 <bad_traverse_test>, arg=0x0) at /temp/shihai/cpython/Modules/binascii.c:1654
1654    {
(gdb) n
1655        binascii_state *state = get_binascii_state(module);
(gdb) n
1656        if (state == NULL) {
(gdb) n
1657            return -1;
(gdb) p state
$5 = (binascii_state *) 0x0
(gdb) c
Continuing.

Breakpoint 3, binascii_exec (module=0x7ffff7e803d0) at /temp/shihai/cpython/Modules/binascii.c:1615
1615    binascii_exec(PyObject *module) {

What's the reason?
the malloc operation of md_state will be called after first calling binascii_traverse.
https://github.com/python/cpython/blob/master/Objects/moduleobject.c#L399.

return -1;
}
Py_CLEAR(state->Error);
Py_CLEAR(state->Incomplete);
return 0;
}

static void
binascii_free(void *m)
{
binascii_clear((PyObject *)m);
}

static struct PyModuleDef binasciimodule = {
PyModuleDef_HEAD_INIT,
"binascii",
doc_binascii,
sizeof(binascii_state),
binascii_module_methods,
binascii_slots,
NULL,
NULL,
NULL
binascii_traverse,
binascii_clear,
binascii_free
};

PyMODINIT_FUNC
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.