Skip to content

bpo-40071: Fix potential crash in _functoolsmodule.c #19273

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 1 commit into from
Apr 1, 2020

Conversation

phsilva
Copy link
Contributor

@phsilva phsilva commented Apr 1, 2020

Changes on 7dd549e made _functools compatible with
PEP-489 and we could have multiple modules instances loaded.

But, right now there is no way to make kwd_mark global into
a per module instance variable. kwd_mark is used on lru_cache_new
which does not have a reference to a PyModule*, necessary to use
PyModule_GetState.

PEP-573 will solve this problem and will allow us to move the global
state to per-module data and properly clear the state when unloading
a module instance.

This change temporarily disable cleaning of kwd_mark to avoid NULL
pointer dereference if we clear kwd_mark and other module instances
still alive use it.

https://bugs.python.org/issue40071

@@ -1413,7 +1413,9 @@ static PyMethodDef _functools_methods[] = {
static void
_functools_free(void *m)
{
Py_CLEAR(kwd_mark);
// FIXME: Avoid double free when using multi-phase initialization (PEP-489)
// Will fix when PEP-573 gets in.
Copy link
Member

Choose a reason for hiding this comment

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

The comment is not correct. The bug is not "double free": Py_CLEAR(NULL) does NULL, that's fine. The problem is if one module instance is deallocated, kwd_mark is cleared to NULL. Other instances will use NULL and so crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rephrased the comment and updated commit message. Thanks for getting it.

Changes on 7dd549e made _functools compatible with
PEP-489 and we could have multiple modules instances loaded.

But, right now there is no way to make `kwd_mark` global into
a per module instance variable. kwd_mark is used on lru_cache_new
which does not have a reference to a PyModule*, necessary to use
PyModule_GetState.

PEP-573 will solve this problem and will allow us to move the global
state to per-module data and properly clear the state when unloading
a module instance.

This change temporarily disable cleaning of kwd_mark to avoid NULL
pointer dereference if we clear kwd_mark and other module instances
still alive use it.

wip
@phsilva phsilva force-pushed the bpo-40071-fix-crash branch from a6ab03d to eb807ad Compare April 1, 2020 14:44
@vstinner vstinner merged commit eacc074 into python:master Apr 1, 2020
@phsilva phsilva deleted the bpo-40071-fix-crash branch April 3, 2020 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants