René's URL Explorer Experiment


Title: Use-after-free in `_io.BytesIO.writelines` via re-entrant `__buffer__` close · Issue #143378 · python/cpython · GitHub

Open Graph Title: Use-after-free in `_io.BytesIO.writelines` via re-entrant `__buffer__` close · Issue #143378 · python/cpython

X Title: Use-after-free in `_io.BytesIO.writelines` via re-entrant `__buffer__` close · Issue #143378 · python/cpython

Description: What happened? _io.BytesIO.writelines pulls buffers from each iterable element inside write_bytes_lock_held, but a crafted object can close the target in __buffer__, freeing self->buf while the write path still runs, which makes write_by...

Open Graph Description: What happened? _io.BytesIO.writelines pulls buffers from each iterable element inside write_bytes_lock_held, but a crafted object can close the target in __buffer__, freeing self->buf while the wri...

X Description: What happened? _io.BytesIO.writelines pulls buffers from each iterable element inside write_bytes_lock_held, but a crafted object can close the target in __buffer__, freeing self->buf while the ...

Opengraph URL: https://github.com/python/cpython/issues/143378

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use-after-free in `_io.BytesIO.writelines` via re-entrant `__buffer__` close","articleBody":"### What happened?\n\n`_io.BytesIO.writelines` pulls buffers from each iterable element inside `write_bytes_lock_held`, but a crafted object can close the target in `__buffer__`, freeing `self-\u003ebuf` while the write path still runs, which makes `write_bytes_lock_held` dereference freed memory.\n\n**Proof of Concept:**\n\n```python\nfrom _io import BytesIO\n\n\nclass Evil:\n    def __init__(self, target):\n        self.target = target\n\n    def __buffer__(self, flags):\n        self.target.close()\n        return memoryview(b\"A\")\n\n\nbio = BytesIO()\nbio.writelines([Evil(bio)])\n```\n\n**Vulnerable Code Snippet:**\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```c\nstatic PyObject *\n_io_BytesIO_writelines_impl(bytesio *self, PyObject *lines)\n/*[clinic end generated code: output=03a43a75773bc397 input=5d6a616ae39dc9ca]*/\n{\n    PyObject *it, *item;\n\n    CHECK_CLOSED(self);\n\n    it = PyObject_GetIter(lines);\n    if (it == NULL)\n        return NULL;\n\n    while ((item = PyIter_Next(it)) != NULL) {\n        Py_ssize_t ret = write_bytes_lock_held(self, item);\n        Py_DECREF(item);\n        if (ret \u003c 0) {\n            Py_DECREF(it);\n            return NULL;\n        }\n    }\n    Py_DECREF(it);\n\n    /* See if PyIter_Next failed */\n    if (PyErr_Occurred())\n        return NULL;\n\n    Py_RETURN_NONE;\n}\n\n/* Buggy Re-entrant Path */\nPy_NO_INLINE static Py_ssize_t\nwrite_bytes_lock_held(bytesio *self, PyObject *b)\n{\n    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);\n\n    Py_buffer buf;\n    if (PyObject_GetBuffer(b, \u0026buf, PyBUF_CONTIG_RO) \u003c 0) {  /* Reentrant call site */\n        return -1;\n    }\n    Py_ssize_t len = buf.len;\n    /* ... */\n\n    memcpy(PyBytes_AS_STRING(self-\u003ebuf) + self-\u003epos,  /* crashing pointer derived */ /* Crash site */\n           buf.buf, len);\n    /* ... */\n    return len;\n}\n\n/* Clobbering Path */\nstatic PyObject *\n_io_BytesIO_close_impl(bytesio *self)\n{\n    CHECK_EXPORTS(self);\n    Py_CLEAR(self-\u003ebuf);  /* state mutate site */\n    Py_RETURN_NONE;\n}\n```\n\u003c/details\u003e\n\n**Sanitizer Output:**\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```\nAddressSanitizer:DEADLYSIGNAL\n=================================================================\n==2192404==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000010 (pc 0x5d4bc5d1b62d bp 0x7fff6c6ec680 sp 0x7fff6c6ec560 T0)\n==2192404==The signal is caused by a READ memory access.\n==2192404==Hint: address points to the zero page.\n    #0 0x5d4bc5d1b62d in write_bytes_lock_held Modules/_io/bytesio.c:215\n    #1 0x5d4bc5d1c0cf in _io_BytesIO_writelines_impl Modules/_io/bytesio.c:802\n    #2 0x5d4bc5d1c0cf in _io_BytesIO_writelines Modules/_io/clinic/bytesio.c.h:547\n    #3 0x5d4bc57213e7 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n    #4 0x5d4bc57213e7 in PyObject_Vectorcall Objects/call.c:327\n    #5 0x5d4bc55d55a2 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n    #6 0x5d4bc5a9fad6 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n    #7 0x5d4bc5a9fad6 in _PyEval_Vector Python/ceval.c:2001\n    #8 0x5d4bc5a9fad6 in PyEval_EvalCode Python/ceval.c:884\n    #9 0x5d4bc5be516e in run_eval_code_obj Python/pythonrun.c:1365\n    #10 0x5d4bc5be516e in run_mod Python/pythonrun.c:1459\n    #11 0x5d4bc5be9e17 in pyrun_file Python/pythonrun.c:1293\n    #12 0x5d4bc5be9e17 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n    #13 0x5d4bc5bea93c in _PyRun_AnyFileObject Python/pythonrun.c:81\n    #14 0x5d4bc5c5de3c in pymain_run_file_obj Modules/main.c:410\n    #15 0x5d4bc5c5de3c in pymain_run_file Modules/main.c:429\n    #16 0x5d4bc5c5de3c in pymain_run_python Modules/main.c:691\n    #17 0x5d4bc5c5f71e in Py_RunMain Modules/main.c:772\n    #18 0x5d4bc5c5f71e in pymain_main Modules/main.c:802\n    #19 0x5d4bc5c5f71e in Py_BytesMain Modules/main.c:826\n    #20 0x7d5d7d22a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n    #21 0x7d5d7d22a28a in __libc_start_main_impl ../csu/libc-start.c:360\n    #22 0x5d4bc55f9634 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 Modules/_io/bytesio.c:215 in write_bytes_lock_held\n==2192404==ABORTING\n```\n\u003c/details\u003e\n\n### CPython versions tested on:\n\n3.12, 3.13, 3.14, 3.15\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 --\u003e\n### Linked PRs\n* gh-143408\n* gh-143599\n* gh-143600\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2026-01-03T04:10:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/143378/cpython/issues/143378"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:fbab727d-cfc6-e7e5-7f4d-26eb4f4b3e36
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC76A:2ECB0B:196940:23D371:6969FE32
html-safe-nonce219d5fd4b0de3cc9ef790d8ef5ac9886b694f084a25fafef90935f22bf62ddd2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzZBOjJFQ0IwQjoxOTY5NDA6MjNEMzcxOjY5NjlGRTMyIiwidmlzaXRvcl9pZCI6IjU0NDU4MDY2NDUxNzk5NzMxNzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacbafb351c1fe1c43c22fe7892fa074c371222c1dedaae2b8c9c3f8d14f6008e8e
hovercard-subject-tagissue:3777651984
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/143378/issue_layout
twitter:imagehttps://opengraph.githubassets.com/0afb6012dd07c86dfa8de99cb475fd1e6001d3461c8df1ed2adcf00b2f4cafd0/python/cpython/issues/143378
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/0afb6012dd07c86dfa8de99cb475fd1e6001d3461c8df1ed2adcf00b2f4cafd0/python/cpython/issues/143378
og:image:altWhat happened? _io.BytesIO.writelines pulls buffers from each iterable element inside write_bytes_lock_held, but a crafted object can close the target in __buffer__, freeing self->buf while the wri...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejackfromeast
hostnamegithub.com
expected-hostnamegithub.com
None7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasebdde15ad1b403e23b08bbd89b53fbe6bdf688cad
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/143378#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F143378
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F143378
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/143378
Reloadhttps://github.com/python/cpython/issues/143378
Reloadhttps://github.com/python/cpython/issues/143378
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/143378
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/143378
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/143378
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/143378
Use-after-free in _io.BytesIO.writelines via re-entrant __buffer__ closehttps://github.com/python/cpython/issues/143378#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-IOhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-IO%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com/jackfromeast
https://github.com/jackfromeast
jackfromeasthttps://github.com/jackfromeast
on Jan 3, 2026https://github.com/python/cpython/issues/143378#issue-3777651984
gh-143378: Fix UAF when BytesIO is concurrently mutated during write operations #143408https://github.com/python/cpython/pull/143408
[3.14] gh-143378: Fix use-after-free when BytesIO is concurrently mutated during write operations (GH-143408) #143599https://github.com/python/cpython/pull/143599
[3.13] gh-143378: Fix use-after-free when BytesIO is concurrently mutated during write operations (GH-143408) #143600https://github.com/python/cpython/pull/143600
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
topic-IOhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-IO%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.