@@ -2994,8 +2994,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
2994
2994
if (task -> task_must_cancel ) {
2995
2995
PyObject * r ;
2996
2996
int is_true ;
2997
+
2998
+ // Beware: An evil `__getattribute__` could
2999
+ // prematurely delete task->task_cancel_msg before the
3000
+ // task is cancelled, thereby causing a UAF crash.
3001
+ //
3002
+ // See https://github.com/python/cpython/issues/126138
3003
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
2997
3004
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
2998
- task -> task_cancel_msg );
3005
+ task_cancel_msg );
3006
+ Py_DECREF (task_cancel_msg );
3007
+
2999
3008
if (r == NULL ) {
3000
3009
return NULL ;
3001
3010
}
@@ -3087,8 +3096,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
3087
3096
if (task -> task_must_cancel ) {
3088
3097
PyObject * r ;
3089
3098
int is_true ;
3099
+
3100
+ // Beware: An evil `__getattribute__` could
3101
+ // prematurely delete task->task_cancel_msg before the
3102
+ // task is cancelled, thereby causing a UAF crash.
3103
+ //
3104
+ // See https://github.com/python/cpython/issues/126138
3105
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
3090
3106
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
3091
- task -> task_cancel_msg );
3107
+ task_cancel_msg );
3108
+ Py_DECREF (task_cancel_msg );
3109
+
3092
3110
if (r == NULL ) {
3093
3111
return NULL ;
3094
3112
}
0 commit comments