Skip to content

Commit bd3aa0b

Browse files
gh-126703: Fix possible use after free in pycfunction freelist (GH-132319)
1 parent 3feac7a commit bd3aa0b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix possible use after free in cases where a method's definition has the same lifetime as its ``self``.

Objects/methodobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,16 @@ meth_dealloc(PyObject *self)
173173
if (m->m_weakreflist != NULL) {
174174
PyObject_ClearWeakRefs((PyObject*) m);
175175
}
176+
// We need to access ml_flags here rather than later.
177+
// `m->m_ml` might have the same lifetime
178+
// as `m_self` when it's dynamically allocated.
179+
int ml_flags = m->m_ml->ml_flags;
176180
// Dereference class before m_self: PyCFunction_GET_CLASS accesses
177181
// PyMethodDef m_ml, which could be kept alive by m_self
178182
Py_XDECREF(PyCFunction_GET_CLASS(m));
179183
Py_XDECREF(m->m_self);
180184
Py_XDECREF(m->m_module);
181-
if (m->m_ml->ml_flags & METH_METHOD) {
185+
if (ml_flags & METH_METHOD) {
182186
assert(Py_IS_TYPE(self, &PyCMethod_Type));
183187
_Py_FREELIST_FREE(pycmethodobject, m, PyObject_GC_Del);
184188
}

0 commit comments

Comments
 (0)