Skip to content

Commit dc3c075

Browse files
[3.12] gh-126080: fix UAF on task->task_context in task_call_step_soon due to an evil loop.__getattribute__ (GH-126120) (#126251)
gh-126080: fix UAF on `task->task_context` in `task_call_step_soon` due to an evil `loop.__getattribute__` (GH-126120) (cherry picked from commit 0e86655) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 8450b24 commit dc3c075

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
@@ -2751,7 +2751,11 @@ task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg)
27512751
return -1;
27522752
}
27532753

2754-
int ret = call_soon(state, task->task_loop, cb, NULL, task->task_context);
2754+
// Beware: An evil call_soon could alter task_context.
2755+
// See: https://github.com/python/cpython/issues/126080.
2756+
PyObject *task_context = Py_NewRef(task->task_context);
2757+
int ret = call_soon(state, task->task_loop, cb, NULL, task_context);
2758+
Py_DECREF(task_context);
27552759
Py_DECREF(cb);
27562760
return ret;
27572761
}

0 commit comments

Comments
 (0)