Title: NULL pointer dereference in BufferedIO methods after re-entrant detach() · Issue #154997 · python/cpython · GitHub
Open Graph Title: NULL pointer dereference in BufferedIO methods after re-entrant detach() · Issue #154997 · python/cpython
X Title: NULL pointer dereference in BufferedIO methods after re-entrant detach() · Issue #154997 · python/cpython
Description: Crash report What happened? Modules/_io/bufferedio.c does not re-validate self->raw after executing arbitrary Python code. This reproduces on released CPython 3.14.4 as well as the current main branch. The trigger is ordinary Python: a B...
Open Graph Description: Crash report What happened? Modules/_io/bufferedio.c does not re-validate self->raw after executing arbitrary Python code. This reproduces on released CPython 3.14.4 as well as the current main bra...
X Description: Crash report What happened? Modules/_io/bufferedio.c does not re-validate self->raw after executing arbitrary Python code. This reproduces on released CPython 3.14.4 as well as the current main ...
Opengraph URL: https://github.com/python/cpython/issues/154997
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NULL pointer dereference in BufferedIO methods after re-entrant detach()","articleBody":"# Crash report\n\n### What happened?\n\n`Modules/_io/bufferedio.c` does not re-validate `self->raw` after executing arbitrary Python code.\n\nThis reproduces on **released CPython 3.14.4** as well as the current main branch.\n\nThe trigger is ordinary Python: a `BufferedReader`, `BufferedWriter`, or `BufferedRandom` subclass that calls `detach()` from inside a callback (`flush()`, `read()`, `readinto()`, `write()`, etc.) while the original C method is still executing.\n\n`_io__Buffered_detach_impl()` sets\n\n```c\nself->raw = NULL;\nself->detached = 1;\nself->ok = 0;\n```\n\nSeveral functions in `Modules/_io/bufferedio.c` then continue and dispatch through `self->raw` without re-validating it.\n\nThe six reproduced NULL-receiver dispatch sites are:\n\n| Line | Function |\n|------|----------|\n| 591 | `_io__Buffered_close_impl` |\n| 788 | `_buffered_raw_tell` |\n| 1485 | `_io__Buffered_truncate_impl` |\n| 1640 | `_bufferedreader_raw_read` |\n| 1748 | `_bufferedreader_read_all` |\n| 1996 | `_bufferedwriter_raw_write` |\n\nA related site exists at:\n\n| Line | Function | Result |\n|------|----------|--------|\n| 818 | `_buffered_raw_seek` | `SystemError` (not a crash) |\n\n`_buffered_raw_seek()` uses `PyObject_CallMethodObjArgs()`, which NULL-checks its receiver and therefore reports a contract violation (`SystemError: null argument to internal routine`). The six crashing sites instead use `PyObject_CallMethodNoArgs()` or `PyObject_CallMethodOneArg()`, which proceed directly into `_PyObject_GetMethodStackRef()` and dereference `Py_TYPE(NULL)`.\n\nRepresentative AddressSanitizer output:\n\n```text\nAddressSanitizer: SEGV on unknown address 0x000000000008\n\n#0 _PyObject_GetMethodStackRef Objects/object.c\n#1 PyObject_VectorcallMethod Objects/call.c\n#2 PyObject_CallMethodNoArgs\n#3 _io__Buffered_close_impl\n```\n\n`0x8` is the `Py_TYPE` offset from a NULL receiver.\n\nThe pure-Python implementation (`_pyio`) does not crash on any of these inputs. Depending on the code path it raises an exception or completes normally, demonstrating that the operation is survivable and that the crash is specific to the C implementation.\n\n---\n\n### Prior art\n\nThis appears to be the same bug shape that was fixed in `Modules/_io/textio.c` by **gh-143008** (commit `db4b1948bc4`, PR #145957), which introduced `buffer_access_safe()` with the comment:\n\n> self->buffer can be detached (set to NULL) by any user code that is called leading to NULL pointer dereferences\n\nThat change modified `textio.c`, its clinic header, tests, and NEWS entries, but did **not** touch `bufferedio.c`.\n\nWithin `bufferedio.c` itself, `_io__Buffered__dealloc_warn_impl()` (around line 489) is already the only `self->raw` dispatch in the file that re-checks `self->raw` before using it (`if (self->ok && self->raw)`).\n\nThe corresponding fix in `bufferedio.c` appears to be mechanical: introduce a `raw_access_safe(buffered *self)` mirroring `buffer_access_safe()` and use it for every `self->raw` dispatch that follows code capable of executing arbitrary Python.\n\nThis report is **not** the same bug as gh-143375 / PR #143577. That change reordered checks in `_io__Buffered_seek_impl()` around `PyNumber_AsOff_t()` and `CHECK_CLOSED()`. The crashes reported here occur after re-entrant callbacks (`flush()`, `read()`, `readinto()`, `write()`, etc.) have detached the raw stream.\n\n---\n### Minimal reproducer\n\nThe following is sufficient to reproduce one representative crash by simply copying and pasting it into a CPython interpreter:\n\n```python\nimport io\n\nclass B(io.BufferedWriter):\n armed = True\n\n def flush(self):\n if self.armed:\n self.armed = False\n super().detach()\n\nB(io.BytesIO()).close()\n```\n\nUnder an AddressSanitizer build this crashes with:\n\n```text\nAddressSanitizer: SEGV on unknown address 0x000000000008\n\n#0 _PyObject_GetMethodStackRef Objects/object.c\n#1 PyObject_VectorcallMethod Objects/call.c\n#2 PyObject_CallMethodNoArgs\n#3 _io__Buffered_close_impl\n```\n\nThe comprehensive reproducer below exercises every investigated call site.\n\n---\n\n### Reproducer\n\nSave the following as `repro_bufferedio_stale_raw.py`.\n\nTo reproduce one representative crash:\n\n```bash\n./python -c \"$(./python repro_bufferedio_stale_raw.py close:591 io)\"\n```\n\nTo exercise every investigated site:\n\n```bash\n./python repro_bufferedio_stale_raw.py --all\n```\n\nRepresentative output:\n\n```text\nsite C source C function C (io) twin (_pyio)\nclose:591 bufferedio.c:591 _io__Buffered_close_impl SIGSEGV 3/3 exc 3/3\nraw_read:1640 bufferedio.c:1640 _bufferedreader_raw_read SIGSEGV 3/3 exc 3/3\nread_all:1748 bufferedio.c:1748 _bufferedreader_read_all SIGSEGV 3/3 exc 3/3\nraw_write:1996 bufferedio.c:1996 _bufferedwriter_raw_write SIGSEGV 3/3 exc 3/3\ntruncate:1485 bufferedio.c:1485 _io__Buffered_truncate_impl SIGSEGV 3/3 exc 3/3\nraw_tell:788 bufferedio.c:788 _buffered_raw_tell SIGSEGV 3/3 ok 3/3\nraw_seek:818 bufferedio.c:818 _buffered_raw_seek exc 3/3 ok 3/3\n```\n\n`raw_seek:818` intentionally soft-fails with `SystemError` because it dispatches through `PyObject_CallMethodObjArgs()`. It should not be interpreted as evidence that the stale-`self->raw` shape is already handled.\n\n```python\nimport argparse\nimport subprocess\nimport sys\n\n# site -> (C file:line, C function, driver source)\nSITES = {\n \"close:591\": (\"bufferedio.c:591\", \"_io__Buffered_close_impl\", \"\"\"\n class B(io.BufferedWriter):\n armed = True\n def flush(self):\n if self.armed:\n self.armed = False\n super().detach()\n B(io.BytesIO()).close()\n \"\"\"),\n \"raw_read:1640\": (\"bufferedio.c:1640\", \"_bufferedreader_raw_read\", \"\"\"\n class Raw(io.RawIOBase):\n def readable(self): return True\n def readinto(self, b):\n fire()\n b[0:1] = b\"a\"\n return 1\n b = mk(io.BufferedReader, Raw(), 4)\n b.read(64)\n \"\"\"),\n \"read_all:1748\": (\"bufferedio.c:1748\", \"_bufferedreader_read_all\", \"\"\"\n class Duck: # duck-typed: no RawIOBase.readall\n closed = False\n n = 0\n def readable(self): return True\n def writable(self): return False\n def seekable(self): return False\n def close(self): pass\n def flush(self): pass\n def read(self, *a):\n Duck.n += 1\n if Duck.n == 1:\n fire()\n return b\"abc\" if Duck.n \u003c 3 else b\"\"\n def readinto(self, buf):\n d = self.read()\n buf[0:len(d)] = d\n return len(d)\n b = mk(io.BufferedReader, Duck(), 4)\n b.read()\n \"\"\"),\n \"raw_write:1996\": (\"bufferedio.c:1996\", \"_bufferedwriter_raw_write\", \"\"\"\n class Raw(io.RawIOBase):\n def writable(self): return True\n def write(self, b):\n fire()\n return 1 # PARTIAL -> the flush loop iterates\n b = mk(io.BufferedWriter, Raw(), 4)\n b.write(b\"0123456789abcdef\")\n \"\"\"),\n \"truncate:1485\": (\"bufferedio.c:1485\", \"_io__Buffered_truncate_impl\", \"\"\"\n class Raw(io.RawIOBase):\n def readable(self): return False # skip _buffered_raw_seek\n def writable(self): return True\n def seekable(self): return True\n def tell(self): return 0\n def seek(self, p, w=0): return 0\n def truncate(self, p=None): return 0\n def write(self, b):\n fire()\n return len(b)\n b = mk(io.BufferedWriter, Raw(), 64)\n b.write(b\"012\")\n b.truncate(1)\n \"\"\"),\n \"raw_tell:788\": (\"bufferedio.c:788\", \"_buffered_raw_tell\", \"\"\"\n class Raw(io.RawIOBase):\n def readable(self): return False\n def writable(self): return True\n def seekable(self): return True\n def tell(self): return 0\n def seek(self, p, w=0): return 0\n def write(self, b): return len(b)\n def truncate(self, p=None):\n fire() # detach from raw.truncate, AFTER :1485\n return 0\n b = mk(io.BufferedWriter, Raw(), 64)\n b.write(b\"012\")\n b.truncate(1) # crashes at :1489 -> _buffered_raw_tell:788\n \"\"\"),\n # not a crash -- the contract-violation sibling, kept for completeness\n \"raw_seek:818\": (\"bufferedio.c:818\", \"_buffered_raw_seek\", \"\"\"\n class Raw(io.RawIOBase):\n def readable(self): return True\n def writable(self): return True\n def seekable(self): return True\n def tell(self): return 0\n def seek(self, p, w=0): return 0\n def readinto(self, b): b[0:1] = b\"a\"; return 1\n def write(self, b):\n fire()\n return len(b)\n b = mk(io.BufferedRandom, Raw(), 64)\n b.write(b\"012\")\n b.seek(0)\n \"\"\"),\n}\n\nHEADER = \"\"\"\\\nimport sys\n{importline}\n_state = {{}}\ndef mk(cls, raw, bufsize):\n class B(cls):\n def flush(self): # keeps detach()'s _PyFile_Flush off the lock\n return None\n b = B(raw, buffer_size=bufsize)\n _state[\"b\"] = b\n return b\ndef fire():\n if _state.get(\"fired\") or \"b\" not in _state:\n return\n _state[\"fired\"] = True\n try:\n _state[\"b\"].detach()\n print(\"[detached]\", file=sys.stderr)\n except BaseException as exc:\n print(\"[detach raised %s]\" % type(exc).__name__, file=sys.stderr)\n\"\"\"\n\n\ndef build(site, backend):\n importline = \"import io\" if backend == \"io\" else \"import _pyio as io\"\n import textwrap\n return HEADER.format(importline=importline) + textwrap.dedent(SITES[site][2])\n\n\ndef main():\n ap = argparse.ArgumentParser()\n ap.add_argument(\"site\", nargs=\"?\")\n ap.add_argument(\"backend\", nargs=\"?\", default=\"io\")\n ap.add_argument(\"--all\", action=\"store_true\")\n ap.add_argument(\"--python\", default=sys.executable)\n ap.add_argument(\"-n\", type=int, default=3)\n args = ap.parse_args()\n\n if not args.all:\n if not args.site:\n for s, (loc, fn, _) in SITES.items():\n print(\"%-16s %-22s %s\" % (s, loc, fn))\n return 0\n sys.stdout.write(build(args.site, args.backend))\n return 0\n\n print(\"%-16s %-22s %-34s %-14s %s\"\n % (\"site\", \"C source\", \"C function\", \"C (io)\", \"twin (_pyio)\"))\n worst = 0\n for site, (loc, fn, _) in SITES.items():\n cells = []\n for backend in (\"io\", \"_pyio\"):\n rcs = []\n for _ in range(args.n):\n p = subprocess.run([args.python, \"-c\", build(site, backend)],\n capture_output=True, text=True, timeout=90)\n rcs.append(p.returncode)\n uniq = sorted(set(rcs))\n tag = {-11: \"SIGSEGV\", -6: \"SIGABRT\", 0: \"ok\"}.get(uniq[0], \"exc\")\n if backend == \"io\" and uniq[0] \u003c 0:\n worst = 1\n cells.append(\"%s %d/%d\" % (tag, rcs.count(uniq[0]), len(rcs)))\n print(\"%-16s %-22s %-34s %-14s %s\" % (site, loc, fn, cells[0], cells[1]))\n return worst\n\n\nif __name__ == \"__main__\":\n raise SystemExit(main())\n```\n\n---\n\n### CPython versions tested on\n\n- CPython **3.14.4** (released)\n- CPython main branch\n\n---\n\n### Operating systems tested on\n\n- Linux\n\n---\n\n### Output from running `python -VV`\n\n```text\nPython 3.16.0a0 (heads/investigate-bufferedio-raw-detach:6a139d6e548, Jul 31 2026, 14:34:37) [GCC 13.3.0]\n```\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-155017\n* gh-162370\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/BHUVANSH855","@type":"Person","name":"BHUVANSH855"},"datePublished":"2026-07-31T14:59:57Z","url":"https://github.com/python/cpython/issues/154997"}
| 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:ceaf4fac-347f-ac40-fc27-792da92345e0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E164:1050BD:125C8E7:184EFB2:6B01EC18 |
| html-safe-nonce | 50b0ce2eecb7fe5824ac6d9928fdf9ecf0f749da8d1e59a2da2bfdaff55130ee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTY0OjEwNTBCRDoxMjVDOEU3OjE4NEVGQjI6NkIwMUVDMTgiLCJ2aXNpdG9yX2lkIjoiNTY1OTM2MDU1MzgzMTg4NTg0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 108a60fef6646727fc36c181967a14053ce216e36e7ed74aa3ddce1363ea38fe |
| hovercard-subject-tag | issue:5030780914 |
| 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/154997/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3894c207be43dfd29fc2363c93f50a34bdc4515584d0fe305a3b1ed7322add35/python/cpython/issues/154997 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3894c207be43dfd29fc2363c93f50a34bdc4515584d0fe305a3b1ed7322add35/python/cpython/issues/154997 |
| og:image:alt | Crash report What happened? Modules/_io/bufferedio.c does not re-validate self->raw after executing arbitrary Python code. This reproduces on released CPython 3.14.4 as well as the current main bra... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BHUVANSH855 |
| 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