@@ -2907,8 +2907,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
2907
2907
if (task -> task_must_cancel ) {
2908
2908
PyObject * r ;
2909
2909
int is_true ;
2910
+
2911
+ // Beware: An evil `__getattribute__` could
2912
+ // prematurely delete task->task_cancel_msg before the
2913
+ // task is cancelled, thereby causing a UAF crash.
2914
+ //
2915
+ // See https://github.com/python/cpython/issues/126138
2916
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
2910
2917
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
2911
- task -> task_cancel_msg );
2918
+ task_cancel_msg );
2919
+ Py_DECREF (task_cancel_msg );
2920
+
2912
2921
if (r == NULL ) {
2913
2922
return NULL ;
2914
2923
}
@@ -3000,8 +3009,17 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
3000
3009
if (task -> task_must_cancel ) {
3001
3010
PyObject * r ;
3002
3011
int is_true ;
3012
+
3013
+ // Beware: An evil `__getattribute__` could
3014
+ // prematurely delete task->task_cancel_msg before the
3015
+ // task is cancelled, thereby causing a UAF crash.
3016
+ //
3017
+ // See https://github.com/python/cpython/issues/126138
3018
+ PyObject * task_cancel_msg = Py_NewRef (task -> task_cancel_msg );
3003
3019
r = PyObject_CallMethodOneArg (result , & _Py_ID (cancel ),
3004
- task -> task_cancel_msg );
3020
+ task_cancel_msg );
3021
+ Py_DECREF (task_cancel_msg );
3022
+
3005
3023
if (r == NULL ) {
3006
3024
return NULL ;
3007
3025
}
0 commit comments