Title: Use-after-free in `asyncio` Task deallocation via re-registering task in `call_exception_handler` · Issue #142556 · python/cpython · GitHub
Open Graph Title: Use-after-free in `asyncio` Task deallocation via re-registering task in `call_exception_handler` · Issue #142556 · python/cpython
X Title: Use-after-free in `asyncio` Task deallocation via re-registering task in `call_exception_handler` · Issue #142556 · python/cpython
Description: What happened? During Task teardown TaskObj_dealloc() calls the finalizer (TaskObj_finalize) which invokes the loop's call_exception_handler (user code). If that handler re-registers the same Task (e.g. calls _asyncio._register_task(cont...
Open Graph Description: What happened? During Task teardown TaskObj_dealloc() calls the finalizer (TaskObj_finalize) which invokes the loop's call_exception_handler (user code). If that handler re-registers the same Task ...
X Description: What happened? During Task teardown TaskObj_dealloc() calls the finalizer (TaskObj_finalize) which invokes the loop's call_exception_handler (user code). If that handler re-registers the same T...
Opengraph URL: https://github.com/python/cpython/issues/142556
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use-after-free in `asyncio` Task deallocation via re-registering task in `call_exception_handler`","articleBody":"### What happened?\n\nDuring `Task` teardown `TaskObj_dealloc()` calls the finalizer (`TaskObj_finalize`) which invokes the loop's `call_exception_handler` (user code). If that handler re-registers the same `Task` (e.g. calls `_asyncio._register_task(context[\"task\"])`), the runtime may add the freed task pointer into a registry while `tp_free` is still running, causing a heap use-after-free when the registry later touches the pointer.\n\n**Proof of Concept:**\n```python\nimport _asyncio\n\nclass EvilLoop:\n def get_debug(self):\n return False\n\n def call_exception_handler(self, context):\n _asyncio._register_task(context[\"task\"])\n\nasync def coro():\n pass\n\nloop = EvilLoop()\ntask = _asyncio.Task(coro(), loop=loop)\n```\n\n\n**Affected Versions:**\n| Python Version | Status | Exit Code |\n|---|---|---|\n| `Python 3.9.24+ (heads/3.9:9c4638d, Oct 17 2025, 11:19:30) ` | Exception | 1 |\n| `Python 3.10.19+ (heads/3.10:0142619, Oct 17 2025, 11:20:05) [GCC 13.3.0]` | Exception | 1 |\n| `Python 3.11.14+ (heads/3.11:88f3f5b, Oct 17 2025, 11:20:44) [GCC 13.3.0]` | Exception | 1 |\n| `Python 3.12.12+ (heads/3.12:8cb2092, Oct 17 2025, 11:21:35) [GCC 13.3.0]` | Exception | 1 |\n| `Python 3.13.9+ (heads/3.13:0760a57, Oct 17 2025, 11:22:25) [GCC 13.3.0]` | Exception | 1 |\n| `Python 3.14.0+ (heads/3.14:889e918, Oct 17 2025, 11:23:02) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.15.0a1+ (heads/main:fbf0843, Oct 17 2025, 11:23:37) [GCC 13.3.0]` | ASAN | 1 |\n\n**Vulnerable Code Snippet**\n\n```\nstatic void\nTaskObj_dealloc(PyObject *self)\n{\n _PyObject_ResurrectStart(self);\n // Unregister the task here so that even if any subclass of Task\n // which doesn't end up calling TaskObj_finalize not crashes.\n unregister_task((TaskObj *)self);\n\n PyObject_CallFinalizer(self); // Call TaskObj_finalize\n\n if (_PyObject_ResurrectEnd(self)) {\n return;\n }\n\n PyTypeObject *tp = Py_TYPE(self);\n PyObject_GC_UnTrack(self);\n\n PyObject_ClearWeakRefs(self);\n\n (void)TaskObj_clear(self);\n tp->tp_free(self);\n Py_DECREF(tp);\n}\n\nstatic void\nTaskObj_finalize(PyObject *op)\n{\n TaskObj *task = (TaskObj*)op;\n PyObject *context;\n PyObject *message = NULL;\n PyObject *func;\n\n if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) {\n goto done;\n }\n\n /* Save the current exception, if any. */\n PyObject *exc = PyErr_GetRaisedException();\n\n context = PyDict_New();\n if (context == NULL) {\n goto finally;\n }\n\n message = PyUnicode_FromString(\"Task was destroyed but it is pending!\");\n if (message == NULL) {\n goto finally;\n }\n\n if (PyDict_SetItem(context, &_Py_ID(message), message) \u003c 0 ||\n PyDict_SetItem(context, &_Py_ID(task), (PyObject*)task) \u003c 0)\n {\n goto finally;\n }\n\n if (task->task_source_tb != NULL) {\n if (PyDict_SetItem(context, &_Py_ID(source_traceback),\n task->task_source_tb) \u003c 0)\n {\n goto finally;\n }\n }\n\t \n\t// Bug: In the call_exception_handler, we re-register the task so that the registry holds its pointer\n\t// While later, the tp->tp_free(self); in TaskObj_dealloc, the task will be freed and any access to the pointer will cause UAF.\n\t// When task has ended, the call_exception_handler method will be invoked.\n func = PyObject_GetAttr(task->task_loop, &_Py_ID(call_exception_handler));\n if (func != NULL) {\n PyObject *res = PyObject_CallOneArg(func, context);\n if (res == NULL) {\n PyErr_FormatUnraisable(\"Exception ignored while calling asyncio \"\n \"function %R\", func);\n }\n else {\n Py_DECREF(res);\n }\n Py_DECREF(func);\n }\n\nfinally:\n Py_XDECREF(context);\n Py_XDECREF(message);\n\n /* Restore the saved exception. */\n PyErr_SetRaisedException(exc);\n\ndone:\n FutureObj_finalize((PyObject*)task);\n}\n```\n\n**Sanitizer**\n\n```python\n=================================================================\n==1485200==ERROR: AddressSanitizer: heap-use-after-free on address 0x5150000657d0 at pc 0x62a676285a06 bp 0x7ffe931f2c30 sp 0x7ffe931f2c20\nWRITE of size 8 at 0x5150000657d0 thread T0\n #0 0x62a676285a05 in llist_concat Include/internal/pycore_llist.h:95\n #1 0x62a676285a05 in PyThreadState_Clear Python/pystate.c:1687\n #2 0x62a676285bb1 in interpreter_clear Python/pystate.c:746\n #3 0x62a676286636 in _PyInterpreterState_Clear Python/pystate.c:904\n #4 0x62a67627cb73 in finalize_interp_clear Python/pylifecycle.c:1920\n #5 0x62a67627d669 in _Py_Finalize Python/pylifecycle.c:2337\n #6 0x62a67627d6f2 in Py_FinalizeEx Python/pylifecycle.c:2378\n #7 0x62a6762df847 in Py_RunMain Modules/main.c:774\n #8 0x62a6762dfa2e in pymain_main Modules/main.c:802\n #9 0x62a6762dfdb3 in Py_BytesMain Modules/main.c:826\n #10 0x62a675d63645 in main Programs/python.c:15\n #11 0x7405bae2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #12 0x7405bae2a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #13 0x62a675d63574 in _start (/home/jackfromeast/Desktop/entropy/tasks/grammar-afl++-latest/targets/cpython/python+0x2dd574) (BuildId: ff3dc40ea460bd4beb2c3a72283cca525b319bf0)\n\n0x5150000657d0 is located 208 bytes inside of 504-byte region [0x515000065700,0x5150000658f8)\nfreed by thread T0 here:\n #0 0x7405bb2fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52\n #1 0x62a675f9596d in _PyMem_RawFree Objects/obmalloc.c:91\n #2 0x62a675f97cd9 in _PyMem_DebugRawFree Objects/obmalloc.c:2955\n #3 0x62a675f97d1a in _PyMem_DebugFree Objects/obmalloc.c:3100\n #4 0x62a675fc006c in PyObject_Free Objects/obmalloc.c:1522\n #5 0x62a6761fecf7 in PyObject_GC_Del Python/gc.c:2435\n #6 0x62a6762eef87 in TaskObj_dealloc Modules/_asynciomodule.c:3010\n #7 0x62a675f8c481 in _Py_Dealloc Objects/object.c:3200\n #8 0x62a675fdd81c in Py_DECREF Include/refcount.h:401\n #9 0x62a675ff5646 in type_call Objects/typeobject.c:2463\n #10 0x62a675ecec71 in _PyObject_MakeTpCall Objects/call.c:242\n #11 0x62a675ecef19 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:167\n #12 0x62a675ecef72 in PyObject_Vectorcall Objects/call.c:327\n #13 0x62a676154c60 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:2920\n #14 0x62a676190e54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #15 0x62a676191148 in _PyEval_Vector Python/ceval.c:2001\n #16 0x62a6761913f8 in PyEval_EvalCode Python/ceval.c:884\n #17 0x62a676288507 in run_eval_code_obj Python/pythonrun.c:1365\n #18 0x62a676288723 in run_mod Python/pythonrun.c:1459\n #19 0x62a67628957a in pyrun_file Python/pythonrun.c:1293\n #20 0x62a67628c220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #21 0x62a67628c4f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #22 0x62a6762dd74d in pymain_run_file_obj Modules/main.c:410\n #23 0x62a6762dd9b4 in pymain_run_file Modules/main.c:429\n #24 0x62a6762df1b2 in pymain_run_python Modules/main.c:691\n #25 0x62a6762df842 in Py_RunMain Modules/main.c:772\n #26 0x62a6762dfa2e in pymain_main Modules/main.c:802\n #27 0x62a6762dfdb3 in Py_BytesMain Modules/main.c:826\n #28 0x62a675d63645 in main Programs/python.c:15\n #29 0x7405bae2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n\npreviously allocated by thread T0 here:\n #0 0x7405bb2fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69\n #1 0x62a675f96284 in _PyMem_RawMalloc Objects/obmalloc.c:63\n #2 0x62a675f95655 in _PyMem_DebugRawAlloc Objects/obmalloc.c:2887\n #3 0x62a675f956bd in _PyMem_DebugRawMalloc Objects/obmalloc.c:2920\n #4 0x62a675f96f3b in _PyMem_DebugMalloc Objects/obmalloc.c:3085\n #5 0x62a675fbff28 in PyObject_Malloc Objects/obmalloc.c:1493\n #6 0x62a675ff203b in _PyObject_MallocWithType Include/internal/pycore_object_alloc.h:46\n #7 0x62a675ff203b in _PyType_AllocNoTrack Objects/typeobject.c:2504\n #8 0x62a675ff21c7 in PyType_GenericAlloc Objects/typeobject.c:2535\n #9 0x62a675fd9dd9 in PyType_GenericNew Objects/typeobject.c:2549\n #10 0x62a675ff5346 in type_call Objects/typeobject.c:2448\n #11 0x62a675ecec71 in _PyObject_MakeTpCall Objects/call.c:242\n #12 0x62a675ecef19 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:167\n #13 0x62a675ecef72 in PyObject_Vectorcall Objects/call.c:327\n #14 0x62a676154c60 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:2920\n #15 0x62a676190e54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #16 0x62a676191148 in _PyEval_Vector Python/ceval.c:2001\n #17 0x62a6761913f8 in PyEval_EvalCode Python/ceval.c:884\n #18 0x62a676288507 in run_eval_code_obj Python/pythonrun.c:1365\n #19 0x62a676288723 in run_mod Python/pythonrun.c:1459\n #20 0x62a67628957a in pyrun_file Python/pythonrun.c:1293\n #21 0x62a67628c220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #22 0x62a67628c4f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #23 0x62a6762dd74d in pymain_run_file_obj Modules/main.c:410\n #24 0x62a6762dd9b4 in pymain_run_file Modules/main.c:429\n #25 0x62a6762df1b2 in pymain_run_python Modules/main.c:691\n #26 0x62a6762df842 in Py_RunMain Modules/main.c:772\n #27 0x62a6762dfa2e in pymain_main Modules/main.c:802\n #28 0x62a6762dfdb3 in Py_BytesMain Modules/main.c:826\n #29 0x62a675d63645 in main Programs/python.c:15\n #30 0x7405bae2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n\nSUMMARY: AddressSanitizer: heap-use-after-free Include/internal/pycore_llist.h:95 in llist_concat\nShadow bytes around the buggy address:\n 0x515000065500: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\n 0x515000065580: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\n 0x515000065600: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa fa\n 0x515000065680: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\n 0x515000065700: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\n=>0x515000065780: fd fd fd fd fd fd fd fd fd fd[fd]fd fd fd fd fd\n 0x515000065800: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\n 0x515000065880: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa\n 0x515000065900: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa\n 0x515000065980: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\n 0x515000065a00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd\nShadow byte legend (one shadow byte represents 8 application bytes):\n Addressable: 00\n Partially addressable: 01 02 03 04 05 06 07 \n Heap left redzone: fa\n Freed heap region: fd\n Stack left redzone: f1\n Stack mid redzone: f2\n Stack right redzone: f3\n Stack after return: f5\n Stack use after scope: f8\n Global redzone: f9\n Global init order: f6\n Poisoned by user: f7\n Container overflow: fc\n Array cookie: ac\n Intra object redzone: bb\n ASan internal: fe\n Left alloca redzone: ca\n Right alloca redzone: cb\n==1485200==ABORTING\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-142565\n* gh-142566\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-11T03:54:15Z","url":"https://github.com/python/cpython/issues/142556"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:daafad50-47a3-dddc-44b5-bde67712e076 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AFD6:361639:3440CA4:45B4F20:6B0203A4 |
| html-safe-nonce | 255394af80e48eabb9436fb8c0272459c4209f0dc846c728a31232839e0cebfe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkQ2OjM2MTYzOTozNDQwQ0E0OjQ1QjRGMjA6NkIwMjAzQTQiLCJ2aXNpdG9yX2lkIjoiOTA2MzMwNTI5MDQ5NzI2MjUwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 440cefa654e7b9d0d9351af0dcaed9b1f6308076cc53fe80462d2fa4a0a30537 |
| hovercard-subject-tag | issue:3717628623 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/142556/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d8139da4a6320ace9f5c6cc4e906e364fd424ebd5bc5a6d1874699122d02eca/python/cpython/issues/142556 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d8139da4a6320ace9f5c6cc4e906e364fd424ebd5bc5a6d1874699122d02eca/python/cpython/issues/142556 |
| og:image:alt | What happened? During Task teardown TaskObj_dealloc() calls the finalizer (TaskObj_finalize) which invokes the loop's call_exception_handler (user code). If that handler re-registers the same Task ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jackfromeast |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width