@@ -2967,8 +2967,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
2967
2967
if (task -> task_must_cancel ) {
2968
2968
PyObject * r ;
2969
2969
int is_true ;
2970
+
2971
+ // Beware: An evil `__getattribute__` could
2972
+ // prematurely delete task->task_cancel_msg before the
2973
+ // task is cancelled, thereby causing a UAF crash.
2974
+ //
2975
+ // See https://github.com/python/cpython/issues/126138
2976
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
2970
2977
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
2971
- task -> task_cancel_msg );
2978
+ task_cancel_msg );
2979
+ Py_DECREF (task_cancel_msg );
2980
+
2972
2981
if (r == NULL ) {
2973
2982
return NULL ;
2974
2983
}
@@ -3060,8 +3069,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
3060
3069
if (task -> task_must_cancel ) {
3061
3070
PyObject * r ;
3062
3071
int is_true ;
3072
+
3073
+ // Beware: An evil `__getattribute__` could
3074
+ // prematurely delete task->task_cancel_msg before the
3075
+ // task is cancelled, thereby causing a UAF crash.
3076
+ //
3077
+ // See https://github.com/python/cpython/issues/126138
3078
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
3063
3079
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
3064
- task -> task_cancel_msg );
3080
+ task_cancel_msg );
3081
+ Py_DECREF (task_cancel_msg );
3082
+
3065
3083
if (r == NULL ) {
3066
3084
return NULL ;
3067
3085
}
0 commit comments