Title: Null pointer dereference in `BufferedWriter.seek` during re-entrant close · Issue #143375 · python/cpython · GitHub
Open Graph Title: Null pointer dereference in `BufferedWriter.seek` during re-entrant close · Issue #143375 · python/cpython
X Title: Null pointer dereference in `BufferedWriter.seek` during re-entrant close · Issue #143375 · python/cpython
Description: What happened? BufferedWriter.seek asks the offset object for __index__ before it flushes pending bytes, so a crafted type can close the writer during that conversion, leaving the buffered write state freed while _bufferedwriter_raw_writ...
Open Graph Description: What happened? BufferedWriter.seek asks the offset object for __index__ before it flushes pending bytes, so a crafted type can close the writer during that conversion, leaving the buffered write st...
X Description: What happened? BufferedWriter.seek asks the offset object for __index__ before it flushes pending bytes, so a crafted type can close the writer during that conversion, leaving the buffered write st...
Opengraph URL: https://github.com/python/cpython/issues/143375
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Null pointer dereference in `BufferedWriter.seek` during re-entrant close","articleBody":"### What happened?\n\n`BufferedWriter.seek` asks the offset object for `__index__` before it flushes pending bytes, so a crafted type can close the writer during that conversion, leaving the buffered write state freed while `_bufferedwriter_raw_write` retries the partial write and dereferences the stale pointer.\n\n**Proof of Concept:**\n\n```python\nimport io\n\nclass R:\n def __init__(self):\n self.calls = 0\n self.closed = False\n def writable(self): return True\n def seekable(self): return True\n def seek(self, off, whence=0): return 0\n def write(self, mv):\n self.calls += 1\n if self.calls == 1: return len(mv) - 1\n if self.calls == 2: raise BlockingIOError\n mv.tobytes()\n return len(mv)\n\nbw = io.BufferedWriter(R())\nbw.write(b\"ABCD\")\n\nclass T:\n def __index__(self):\n try: bw.close()\n except Exception: pass\n return 0\n\nbw.seek(T())\n```\n\n**Vulnerable Code Snippet:**\n\n\u003cdetails>\n\u003csummary>Click to expand\u003c/summary>\n\n```c\n/* Buggy Re-entrant Path */\nstatic PyObject *\n_io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)\n{\n Py_off_t target;\n /* ... */\n target = PyNumber_AsOff_t(targetobj, PyExc_ValueError); /* Reentrant call site */\n if (target == -1 && PyErr_Occurred())\n return NULL;\n /* ... */\n if (self->writable) {\n res = _bufferedwriter_flush_unlocked(self);\n /* ... */\n }\n /* ... */\n}\n\nstatic PyObject *\n_bufferedwriter_flush_unlocked(buffered *self)\n{\n /* ... */\n while (self->write_pos \u003c self->write_end) {\n n = _bufferedwriter_raw_write(self,\n self->buffer + self->write_pos, /* crashing pointer derived */\n Py_SAFE_DOWNCAST(self->write_end - self->write_pos,\n Py_off_t, Py_ssize_t));\n /* ... */\n }\n /* ... */\n}\n\nstatic Py_ssize_t\n_bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)\n{\n Py_buffer buf;\n PyObject *memobj, *res;\n /* NOTE: the buffer needn't be released as its object is NULL. */\n if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)\n return -1;\n memobj = PyMemoryView_FromBuffer(&buf);\n /* ... raw.write(memobj) ... */\n return n;\n}\n\nint\nPyBuffer_ToContiguous(void *buf, const Py_buffer *src, Py_ssize_t len, char order)\n{\n if (PyBuffer_IsContiguous(src, order)) {\n memcpy((char *)buf, src->buf, len); /* Crash site */\n return 0;\n }\n /* ... */\n return 0;\n}\n\n/* Clobbering Path */\nstatic PyObject *\n_io__Buffered_close_impl(buffered *self)\n{\n /* ... */\n if (self->buffer) {\n PyMem_Free(self->buffer); /* state mutate site */\n self->buffer = NULL;\n }\n /* ... */\n return res;\n}\n```\n\u003c/details>\n\n**Sanitizer Output:**\n\n\u003cdetails>\n\u003csummary>Click to expand\u003c/summary>\n\n```\nAddressSanitizer:DEADLYSIGNAL\n=================================================================\n==2188767==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000003 (pc 0x79e00cf88a84 bp 0x7ffd2d3db980 sp 0x7ffd2d3db928 T0)\n==2188767==The signal is caused by a READ memory access.\n==2188767==Hint: address points to the zero page.\n #0 0x79e00cf88a84 in __memmove_avx_unaligned_erms ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:318\n #1 0x654cee9496ed in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:29\n #2 0x654cee9496ed in PyBuffer_ToContiguous Objects/memoryobject.c:1063\n #3 0x654cee949fd9 in memoryview_tobytes_impl Objects/memoryobject.c:2310\n #4 0x654cee949fd9 in memoryview_tobytes Objects/clinic/memoryobject.c.h:335\n #5 0x654cee83b3e7 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #6 0x654cee83b3e7 in PyObject_Vectorcall Objects/call.c:327\n #7 0x654cee6ef5a2 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #8 0x654ceebba2a5 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #9 0x654ceebba2a5 in _PyEval_Vector Python/ceval.c:2001\n #10 0x654cee83d863 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #11 0x654cee83d863 in PyObject_VectorcallMethod Objects/call.c:859\n #12 0x654ceee3bfd5 in PyObject_CallMethodOneArg Include/cpython/abstract.h:74\n #13 0x654ceee3bfd5 in _bufferedwriter_raw_write Modules/_io/bufferedio.c:1985\n #14 0x654ceee3d3b6 in _bufferedwriter_flush_unlocked Modules/_io/bufferedio.c:2029\n #15 0x654ceee4613e in _io__Buffered_seek_impl Modules/_io/bufferedio.c:1425\n #16 0x654ceee4613e in _io__Buffered_seek Modules/_io/clinic/bufferedio.c.h:885\n #17 0x654cee83b3e7 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #18 0x654cee83b3e7 in PyObject_Vectorcall Objects/call.c:327\n #19 0x654cee6ef5a2 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #20 0x654ceebb9ad6 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #21 0x654ceebb9ad6 in _PyEval_Vector Python/ceval.c:2001\n #22 0x654ceebb9ad6 in PyEval_EvalCode Python/ceval.c:884\n #23 0x654ceecff16e in run_eval_code_obj Python/pythonrun.c:1365\n #24 0x654ceecff16e in run_mod Python/pythonrun.c:1459\n #25 0x654ceed03e17 in pyrun_file Python/pythonrun.c:1293\n #26 0x654ceed03e17 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #27 0x654ceed0493c in _PyRun_AnyFileObject Python/pythonrun.c:81\n #28 0x654ceed77e3c in pymain_run_file_obj Modules/main.c:410\n #29 0x654ceed77e3c in pymain_run_file Modules/main.c:429\n #30 0x654ceed77e3c in pymain_run_python Modules/main.c:691\n #31 0x654ceed7971e in Py_RunMain Modules/main.c:772\n #32 0x654ceed7971e in pymain_main Modules/main.c:802\n #33 0x654ceed7971e in Py_BytesMain Modules/main.c:826\n #34 0x79e00ce2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #35 0x79e00ce2a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #36 0x654cee713634 in _start (/home/jackfromeast/Desktop/entropy/targets/grammar-afl++-latest/targets/cpython/python+0x206634) (BuildId: 4d105290d0ad566a4d6f4f7b2f05fbc9e317b533)\n\nAddressSanitizer can not provide additional info.\nSUMMARY: AddressSanitizer: SEGV ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:318 in __memmove_avx_unaligned_erms\n==2188767==ABORTING\n```\n\u003c/details>\n\n### CPython versions tested on:\n\n\u003cdetails>\n\n| Python Version | Status | Exit Code |\n|---|---|---|\n| `Python 3.9.24+ (heads/3.9:111bbc15b26, Oct 28 2025, 16:51:20) ` | ASAN | 1 |\n| `Python 3.10.19+ (heads/3.10:014261980b1, Oct 28 2025, 16:52:08) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.11.14+ (heads/3.11:88f3f5b5f11, Oct 28 2025, 16:53:08) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.12.12+ (heads/3.12:8cb2092bd8c, Oct 28 2025, 16:54:14) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.13.9+ (heads/3.13:9c8eade20c6, Oct 28 2025, 16:55:18) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.14.0+ (heads/3.14:2e216728038, Oct 28 2025, 16:56:16) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n| `Python 3.15.0a1+ (heads/main:f5394c257ce, Oct 28 2025, 19:29:54) [GCC 13.3.0]` | ASAN | 1 |\n\n\u003c/details>\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a1+ (heads/main:f5394c257ce, Oct 28 2025, 19:29:54) [GCC 13.3.0]\n\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-143577\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2026-01-03T04:05:54Z","url":"https://github.com/python/cpython/issues/143375"}
| 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:83ec1c2d-eee7-76d1-5cdf-b01ba6efdfac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A43C:2679E9:30C7040:411CD0D:6B01EEF8 |
| html-safe-nonce | 18d145e7ed200f65580ab77993ead4c17d6874b9e48b7b31da325c74dcd03d41 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDNDOjI2NzlFOTozMEM3MDQwOjQxMUNEMEQ6NkIwMUVFRjgiLCJ2aXNpdG9yX2lkIjoiMjI5Mjc3ODg4NzcwMzM4NTUyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cd9fa4ecfa3076c71b47f035449001fc6b2d20e97f2fe0a35aca9fb14812b305 |
| hovercard-subject-tag | issue:3777649483 |
| 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/143375/issue_layout |
| twitter:image | https://opengraph.githubassets.com/be4cf7e48379ed0a51e5517064fde2c2a02d5baddeae44bd2b6d2191e35792a3/python/cpython/issues/143375 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/be4cf7e48379ed0a51e5517064fde2c2a02d5baddeae44bd2b6d2191e35792a3/python/cpython/issues/143375 |
| og:image:alt | What happened? BufferedWriter.seek asks the offset object for __index__ before it flushes pending bytes, so a crafted type can close the writer during that conversion, leaving the buffered write st... |
| 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