Repository navigation
UAF in GC visit_generation() #160663
Copy link
Copy link
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Description
Bug report
Similar to #112564 we call GC_NEXT(gc) which is vulnerable to UAF in
Lines 2165 to 2179 in 036d309
| visit_generation(gcvisitobjects_t callback, void *arg, struct gc_generation *gen) | |
| { | |
| PyGC_Head *gc_list, *gc; | |
| gc_list = &gen->head; | |
| for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) { | |
| PyObject *op = FROM_GC(gc); | |
| Py_INCREF(op); | |
| int res = callback(op, arg); | |
| Py_DECREF(op); | |
| if (!res) { | |
| return -1; | |
| } | |
| } | |
| return 0; | |
| } |
Fix could be similar to proposal in #160662
Reproduction, similar to the above bug we are looping through each entity but rely on the entity being alive when calling NEXT in the entity loop
#include <Python.h>
static PyObject *victim; /* the only reference to the victim */
static int
drop_current(PyObject *op, void *arg)
{
if (op == victim) {
Py_CLEAR(victim); /* the visitor's +1 is now the last ref */
}
return 1;
}
static PyObject *
run(PyObject *module, PyObject *unused)
{
victim = PySet_New(NULL); /* GC-tracked, and sets have no freelist */
if (victim == NULL) {
return NULL;
}
PyUnstable_GC_VisitObjects(drop_current, NULL); /* segfaults */
Py_RETURN_NONE;
}
static PyMethodDef methods[] = {
{"run", run, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT, "visitmin", NULL, -1, methods,
};
PyMODINIT_FUNC
PyInit_visitmin(void)
{
return PyModule_Create(&module);
}
import visitmin
visitmin.run()
print("no crash")CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dumpA hard crash of the interpreter, possibly with a core dump
Activity
sergey-miryanov commentedon Oct 25, 2026
@johng Would you like to open a PR?