8000 gh-106092: Fix use-after-free crash in frame_dealloc (#106875) · python/cpython@557b05c · GitHub
[go: up one dir, main page]

Skip to content

Commit 557b05c

Browse files
authored
gh-106092: Fix use-after-free crash in frame_dealloc (#106875)
It was possible for the trashcan to delay the deallocation of a PyFrameObject until after its corresponding _PyInterpreterFrame has already been freed. So frame_dealloc needs to avoid dereferencing the f_frame pointer unless it first checks that the pointer still points to the interpreter frame within the frame object. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 052a0d1 commit 557b05c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a segmentation fault caused by a use-after-free bug in ``frame_dealloc``
2+
when the trashcan delays the deallocation of a ``PyFrameObject``.

Objects/frameobject.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -879,20 +879,21 @@ frame_dealloc(PyFrameObject *f)
879879
/* It is the responsibility of the owning generator/coroutine
880880
* to have cleared the generator pointer */
881881

882-
assert(f->f_frame->owner != FRAME_OWNED_BY_GENERATOR ||
883-
_PyFrame_GetGenerator(f->f_frame)->gi_frame_state == FRAME_CLEARED);
884-
885882
if (_PyObject_GC_IS_TRACKED(f)) {
886883
_PyObject_GC_UNTRACK(f);
887884
}
888885

889886
Py_TRASHCAN_BEGIN(f, frame_dealloc);
890887
PyObject *co = NULL;
891888

889+
/* GH-106092: If f->f_frame was on the stack and we reached the maximum
890+
* nesting depth for deallocations, the trashcan may have delayed this
891+
* deallocation until after f->f_frame is freed. Avoid dereferencing
892+
* f->f_frame unless we know it still points to valid memory. */
893+
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
894+
892895
/* Kill all local variables including specials, if we own them */
893-
if (f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
894-
assert(f->f_frame == (_PyInterpreterFrame *)f->_f_frame_data);
895-
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
896+
if (f->f_frame == frame && frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
896897
/* Don't clear code object until the end */
897898
co = frame->f_executable;
898899
frame->f_executable = NULL;

0 commit comments

Comments
 (0)
0