Title: Null pointer dereference in `TextIOWrapper.seek` via re-entrant `__int__` · Issue #143007 · python/cpython · GitHub
Open Graph Title: Null pointer dereference in `TextIOWrapper.seek` via re-entrant `__int__` · Issue #143007 · python/cpython
X Title: Null pointer dereference in `TextIOWrapper.seek` via re-entrant `__int__` · Issue #143007 · python/cpython
Description: What happened? A crafted integer subclass overrides __int__ so when TextIOWrapper.seek converts the cookie with PyNumber_Long it detaches the wrapper and frees the buffer, yet the seek logic continues with the original self->buffer, lead...
Open Graph Description: What happened? A crafted integer subclass overrides __int__ so when TextIOWrapper.seek converts the cookie with PyNumber_Long it detaches the wrapper and frees the buffer, yet the seek logic contin...
X Description: What happened? A crafted integer subclass overrides __int__ so when TextIOWrapper.seek converts the cookie with PyNumber_Long it detaches the wrapper and frees the buffer, yet the seek logic contin...
Opengraph URL: https://github.com/python/cpython/issues/143007
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Null pointer dereference in `TextIOWrapper.seek` via re-entrant `__int__`","articleBody":"### What happened?\n\nA crafted integer subclass overrides `__int__` so when `TextIOWrapper.seek` converts the cookie with `PyNumber_Long` it detaches the wrapper and frees the buffer, yet the seek logic continues with the original `self->buffer`, leading `_io_TextIOWrapper_seek_impl` to call a method on a NULL pointer and crash the interpreter.\n\n**Proof of Concept:**\n```python\nimport io\n\nwrapper = io.TextIOWrapper(io.BytesIO(b\"x\"))\n\nclass Cookie(int):\n def __new__(cls, wrapper):\n obj = int.__new__(cls, 0)\n obj.wrapper = wrapper\n return obj\n def __int__(self):\n self.wrapper.detach()\n return 0\n\nwrapper.seek(Cookie(wrapper), io.SEEK_SET)\n```\n\n\n**Affected Versions:**\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, 16:57:16) [Clang 18.1.3 (1ubuntu1)]` | ASAN | 1 |\n\n\u003c/details>\n\n**Vulnerable Code:**\n\u003cdetails>\n\n```c\nstatic int\ntextiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)\n{\n unsigned char buffer[COOKIE_BUF_LEN];\n PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);\n if (cookieLong == NULL)\n return -1;\n\n if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),\n PY_LITTLE_ENDIAN, 0, 1) \u003c 0) {\n Py_DECREF(cookieLong);\n return -1;\n }\n Py_DECREF(cookieLong);\n\n memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos));\n memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags));\n memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed));\n memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip));\n memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof));\n\n return 0;\n}\n\nstatic PyObject *\n_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)\n/*[clinic end generated code: output=0a15679764e2d04d input=4bea78698be23d7e]*/\n{\n PyObject *posobj;\n cookie_type cookie;\n PyObject *res;\n int cmp;\n PyObject *snapshot;\n\n CHECK_ATTACHED(self);\n CHECK_CLOSED(self);\n\n Py_INCREF(cookieObj);\n\n if (!self->seekable) {\n _unsupported(self->state, \"underlying stream is not seekable\");\n goto fail;\n }\n\n /* ... */\n\n /* The strategy of seek() is to go back to the safe start point\n * and replay the effect of read(chars_to_skip) from there.\n */\n if (textiowrapper_parse_cookie(&cookie, cookieObj) \u003c 0)\n goto fail;\n\n /* Seek back to the safe start point. */\n posobj = PyLong_FromOff_t(cookie.start_pos);\n if (posobj == NULL)\n goto fail;\n\n // self->buf has been set to null\n res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj);\n```\n\n\u003c/details>\n\n**Sanitizer Output:**\n\u003cdetails>\n\n```\n=================================================================\n==1652913==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000008 (pc 0x5e52803ff39a bp 0x7fff1072a2c0 sp 0x7fff1072a200 T0)\n==1652913==The signal is caused by a READ memory access.\n==1652913==Hint: address points to the zero page.\n #0 0x5e52803ff39a in _Py_TYPE Include/object.h:277\n #1 0x5e52803ff39a in _PyObject_GetMethodStackRef Objects/object.c:1698\n #2 0x5e528033f796 in PyObject_VectorcallMethod Objects/call.c:840\n #3 0x5e52807f39a2 in PyObject_CallMethodOneArg Include/cpython/abstract.h:74\n #4 0x5e52807fb66b in _io_TextIOWrapper_seek_impl Modules/_io/textio.c:2647\n #5 0x5e52807fbe97 in _io_TextIOWrapper_seek Modules/_io/clinic/textio.c.h:938\n #6 0x5e528035d6ed in method_vectorcall_FASTCALL Objects/descrobject.c:402\n #7 0x5e528033de7f in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #8 0x5e528033df72 in PyObject_Vectorcall Objects/call.c:327\n #9 0x5e52805bc056 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #10 0x5e52805ffe54 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #11 0x5e5280600148 in _PyEval_Vector Python/ceval.c:2001\n #12 0x5e52806003f8 in PyEval_EvalCode Python/ceval.c:884\n #13 0x5e52806f7507 in run_eval_code_obj Python/pythonrun.c:1365\n #14 0x5e52806f7723 in run_mod Python/pythonrun.c:1459\n #15 0x5e52806f857a in pyrun_file Python/pythonrun.c:1293\n #16 0x5e52806fb220 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #17 0x5e52806fb4f6 in _PyRun_AnyFileObject Python/pythonrun.c:81\n #18 0x5e528074c74d in pymain_run_file_obj Modules/main.c:410\n #19 0x5e528074c9b4 in pymain_run_file Modules/main.c:429\n #20 0x5e528074e1b2 in pymain_run_python Modules/main.c:691\n #21 0x5e528074e842 in Py_RunMain Modules/main.c:772\n #22 0x5e528074ea2e in pymain_main Modules/main.c:802\n #23 0x5e528074edb3 in Py_BytesMain Modules/main.c:826\n #24 0x5e52801d2645 in main Programs/python.c:15\n #25 0x7642c742a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #26 0x7642c742a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #27 0x5e52801d2574 in _start (/home/jackfromeast/Desktop/entropy/targets/grammar-afl++-latest/targets/cpython/python+0x2dd574) (BuildId: 202d5dbb945f6d5f5a66ad50e2688d56affd6ecb)\n\nAddressSanitizer can not provide additional info.\nSUMMARY: AddressSanitizer: SEGV Include/object.h:277 in _Py_TYPE\n==1652913==ABORTING\n```\n\n\u003c/details>\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-143043\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2025-12-20T04:16:31Z","url":"https://github.com/python/cpython/issues/143007"}
| 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:f2170c4e-193b-a6f5-71b8-73f0cec3b9e8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ACE2:25CD63:3C0D4:4B73E:6B01F7E6 |
| html-safe-nonce | 05d4c8580bee2372cb47cf4bc756d3290df2d2480767fcd30881a48384c3c52c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0UyOjI1Q0Q2MzozQzBENDo0QjczRTo2QjAxRjdFNiIsInZpc2l0b3JfaWQiOiIyNjYxNDk4MzQ3ODY4NzgwNTE4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3ae6b7204c8889f9d7184df9098bbda32763a4d6d4255d369520ce8af2a7bdd9 |
| hovercard-subject-tag | issue:3749522864 |
| 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/143007/issue_layout |
| twitter:image | https://opengraph.githubassets.com/38dec5c0153449fd9acf7010fd8aef9afc7b18976de6ce24db83725b88ac5ee7/python/cpython/issues/143007 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/38dec5c0153449fd9acf7010fd8aef9afc7b18976de6ce24db83725b88ac5ee7/python/cpython/issues/143007 |
| og:image:alt | What happened? A crafted integer subclass overrides __int__ so when TextIOWrapper.seek converts the cookie with PyNumber_Long it detaches the wrapper and frees the buffer, yet the seek logic contin... |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width