Skip to content

Commit 0e86655

Browse files
authored
gh-126080: fix UAF on task->task_context in task_call_step_soon due to an evil loop.__getattribute__ (#126120)
1 parent 3275cb1 commit 0e86655

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a use-after-free crash on :class:`asyncio.Task` objects for which the
2+
underlying event loop implements an evil :meth:`~object.__getattribute__`.
3+
Reported by Nico-Posada. Patch by Bénédikt Tran.

Modules/_asynciomodule.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,11 @@ task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg)
27382738
return -1;
27392739
}
27402740

2741-
int ret = call_soon(state, task->task_loop, cb, NULL, task->task_context);
2741+
// Beware: An evil call_soon could alter task_context.
2742+
// See: https://github.com/python/cpython/issues/126080.
2743+
PyObject *task_context = Py_NewRef(task->task_context);
2744+
int ret = call_soon(state, task->task_loop, cb, NULL, task_context);
2745+
Py_DECREF(task_context);
27422746
Py_DECREF(cb);
27432747
return ret;
27442748
}

0 commit comments

Comments
 (0)