Title: Null pointer dereference in `TextIOWrapper.close()` via re-entrant `closed` property that detaches `buffer` · Issue #142594 · python/cpython · GitHub
Open Graph Title: Null pointer dereference in `TextIOWrapper.close()` via re-entrant `closed` property that detaches `buffer` · Issue #142594 · python/cpython
X Title: Null pointer dereference in `TextIOWrapper.close()` via re-entrant `closed` property that detaches `buffer` · Issue #142594 · python/cpython
Description: What happened? TextIOWrapper.close() queries the stream’s closed property. A user-defined RawIOBase.closed can re-enter and call wrapper.detach(), clearing self->buffer. The function then continues and calls self->buffer.close() on a NUL...
Open Graph Description: What happened? TextIOWrapper.close() queries the stream’s closed property. A user-defined RawIOBase.closed can re-enter and call wrapper.detach(), clearing self->buffer. The function then continues...
X Description: What happened? TextIOWrapper.close() queries the stream’s closed property. A user-defined RawIOBase.closed can re-enter and call wrapper.detach(), clearing self->buffer. The function then contin...
Opengraph URL: https://github.com/python/cpython/issues/142594
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Null pointer dereference in `TextIOWrapper.close()` via re-entrant `closed` property that detaches `buffer`","articleBody":"### What happened?\n\n`TextIOWrapper.close()` queries the stream’s `closed` property. A user-defined `RawIOBase.closed` can re-enter and call `wrapper.detach()`, clearing `self->buffer`. The function then continues and calls `self->buffer.close()` on a NULL pointer, leading to a null-pointer dereference in `_PyObject_GetMethod`/`_Py_TYPE`.\n\n**Proof of Concept:**\n```python\nimport io\n\nclass EvilBuffer(io.RawIOBase):\n def __init__(self):\n super().__init__()\n self.wrapper = None\n self.detached = False\n\n @property\n def closed(self):\n if self.wrapper is not None and not self.detached:\n self.detached = True\n self.wrapper.detach()\n return False\n\nraw = EvilBuffer()\nwrapper = io.TextIOWrapper(raw)\nraw.wrapper = wrapper\nwrapper.close()\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) ` | ASAN | 1 |\n| `Python 3.10.19+ (heads/3.10:0142619, Oct 17 2025, 11:20:05) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.11.14+ (heads/3.11:88f3f5b, Oct 17 2025, 11:20:44) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.12.12+ (heads/3.12:8cb2092, Oct 17 2025, 11:21:35) [GCC 13.3.0]` | ASAN | 1 |\n| `Python 3.13.9+ (heads/3.13:0760a57, Oct 17 2025, 11:22:25) [GCC 13.3.0]` | ASAN | 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**Related Code Snippet**\n```python\n/*[clinic input]\n@critical_section\n_io.TextIOWrapper.close\n[clinic start generated code]*/\n\nstatic PyObject *\n_io_TextIOWrapper_close_impl(textio *self)\n/*[clinic end generated code: output=056ccf8b4876e4f4 input=8e12d7079d5ac5c1]*/\n{\n PyObject *res;\n int r;\n CHECK_ATTACHED(self);\n\t\t\n // Bug: Re-enter the closed property method which detached the textio buffer \n res = _io_TextIOWrapper_closed_get_impl(self);\n if (res == NULL)\n return NULL;\n r = PyObject_IsTrue(res);\n Py_DECREF(res);\n if (r \u003c 0)\n return NULL;\n\n if (r > 0) {\n Py_RETURN_NONE; /* stream already closed */\n }\n else {\n PyObject *exc = NULL;\n if (self->finalizing) {\n res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn),\n (PyObject *)self);\n if (res) {\n Py_DECREF(res);\n }\n else {\n PyErr_Clear();\n }\n }\n if (_PyFile_Flush((PyObject *)self) \u003c 0) {\n exc = PyErr_GetRaisedException();\n }\n\t\t\t\t\n\t\t// Crash: self->buffer=0x0 after detached.\n res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));\n if (exc != NULL) {\n _PyErr_ChainExceptions1(exc);\n Py_CLEAR(res);\n }\n return res;\n }\n}\n```\n\n**Sanitizer Output:**\n```\n=================================================================\n==1558952==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000008 (pc 0x64c4d0fea39a bp 0x7ffe22fcb5f0 sp 0x7ffe22fcb530 T0)\n==1558952==The signal is caused by a READ memory access.\n==1558952==Hint: address points to the zero page.\n #0 0x64c4d0fea39a in _Py_TYPE Include/object.h:277\n #1 0x64c4d0fea39a in _PyObject_GetMethodStackRef Objects/object.c:1698\n #2 0x64c4d0f2a796 in PyObject_VectorcallMethod Objects/call.c:840\n #3 0x64c4d13e717d in PyObject_CallMethodNoArgs Include/cpython/abstract.h:65\n #4 0x64c4d13e717d in _io_TextIOWrapper_close_impl Modules/_io/textio.c:3169\n #5 0x64c4d13e7277 in _io_TextIOWrapper_close Modules/_io/clinic/textio.c.h:1160\n #6 0x64c4d0f48571 in method_vectorcall_NOARGS Objects/descrobject.c:448\n #7 0x64c4d0f28e7f in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #8 0x64c4d0f28f72 in PyObject_Vectorcall Objects/call.c:327\n #9 0x64c4d11a7056 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #10 0x64c4d11eae54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #11 0x64c4d11eb148 in _PyEval_Vector Python/ceval.c:2001\n #12 0x64c4d11eb3f8 in PyEval_EvalCode Python/ceval.c:884\n #13 0x64c4d12e2507 in run_eval_code_obj Python/pythonrun.c:1365\n #14 0x64c4d12e2723 in run_mod Python/pythonrun.c:1459\n #15 0x64c4d12e357a in pyrun_file Python/pythonrun.c:1293\n #16 0x64c4d12e6220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #17 0x64c4d12e64f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #18 0x64c4d133774d in pymain_run_file_obj Modules/main.c:410\n #19 0x64c4d13379b4 in pymain_run_file Modules/main.c:429\n #20 0x64c4d13391b2 in pymain_run_python Modules/main.c:691\n #21 0x64c4d1339842 in Py_RunMain Modules/main.c:772\n #22 0x64c4d1339a2e in pymain_main Modules/main.c:802\n #23 0x64c4d1339db3 in Py_BytesMain Modules/main.c:826\n #24 0x64c4d0dbd645 in main Programs/python.c:15\n #25 0x7cb5cbe2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #26 0x7cb5cbe2a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #27 0x64c4d0dbd574 in _start (/home/jackfromeast/Desktop/entropy/tasks/grammar-afl++-latest/targets/cpython/python+0x2dd574) (BuildId: ff3dc40ea460bd4beb2c3a72283cca525b319bf0)\n\nAddressSanitizer can not provide additional info.\nSUMMARY: AddressSanitizer: SEGV Include/object.h:277 in _Py_TYPE\n==1558952==ABORTING\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-142706\n* gh-142755\n* gh-142757\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-11T18:57:03Z","url":"https://github.com/python/cpython/issues/142594"}
| 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:ae775db7-f10e-59dc-0b7f-ba06585e8121 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94C6:34CAB:36DCC72:462AFC5:6B01F948 |
| html-safe-nonce | c9835bbc51c7ceb1f026f53597b3b4dccc92d71ec7627504e7f3816c015f0781 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEM2OjM0Q0FCOjM2RENDNzI6NDYyQUZDNTo2QjAxRjk0OCIsInZpc2l0b3JfaWQiOiIzOTgyMTEwNDQ2NzUxNzA5NTEzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1560360a59be7b39d8c31309483fa471384d8ffb61881672d6d5c5dd16033e2a |
| hovercard-subject-tag | issue:3720595161 |
| 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/142594/issue_layout |
| twitter:image | https://opengraph.githubassets.com/aae3fe60448a0916663fb7cceec4b439d2e9a1c995be629d10ffe7a13eea29d0/python/cpython/issues/142594 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/aae3fe60448a0916663fb7cceec4b439d2e9a1c995be629d10ffe7a13eea29d0/python/cpython/issues/142594 |
| og:image:alt | What happened? TextIOWrapper.close() queries the stream’s closed property. A user-defined RawIOBase.closed can re-enter and call wrapper.detach(), clearing self->buffer. The function then continues... |
| 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