8000 gh-92112: Fix crash triggered by an evil custom `mro()` by izbyshev · Pull Request #92113 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-92112: Fix crash triggered by an evil custom mro() #92113

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
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5784,6 +5784,23 @@ def mro(cls):
class A(metaclass=M):
pass

def test_disappearing_custom_mro(self):
"""
gh-92112: A custom mro() returning a result conflicting with
__bases__ and deleting itself caused a double free.
"""
class B:
pass

class M(DebugHelperMeta):
def mro(cls):
del M.mro
return (B,)

with self.assertRaises(TypeError):
class A(metaclass=M):
pass


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash triggered by an evil custom ``mro()`` on a metaclass.
20 changes: 11 additions & 9 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,26 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
Py_ssize_t i, n;
int custom = !Py_IS_TYPE(type, &PyType_Type);
int unbound;
PyObject *mro_meth = NULL;
PyObject *type_mro_meth = NULL;

if (custom) {
PyObject *mro_meth, *type_mro_meth;
mro_meth = lookup_maybe_method(
(PyObject *)type, &_Py_ID(mro), &unbound);
if (mro_meth == NULL)
if (mro_meth == NULL) {
goto clear;
}
type_mro_meth = lookup_maybe_method(
(PyObject *)&PyType_Type, &_Py_ID(mro), &unbound);
if (type_mro_meth == NULL)
if (type_mro_meth == NULL) {
Py_DECREF(mro_meth);
goto clear;
if (mro_meth != type_mro_meth)
}
int custom_mro = (mro_meth != type_mro_meth);
Py_DECREF(mro_meth);
Py_DECREF(type_mro_meth);
if (custom_mro) {
goto clear;
Py_XDECREF(mro_meth);
Py_XDECREF(type_mro_meth);
}
}
n = PyTuple_GET_SIZE(bases);
for (i = 0; i < n; i++) {
Expand All @@ -373,8 +377,6 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
}
return;
clear:
Py_XDECREF(mro_meth);
Py_XDECREF(type_mro_meth);
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
type->tp_version_tag = 0; /* 0 is not a valid version tag */
}
Expand Down
0