Title: socket.sendmsg() and socket.recvmsg_into() crash via __buffer__ re-entrancy (related to gh-143637) · Issue #143988 · python/cpython · GitHub
Open Graph Title: socket.sendmsg() and socket.recvmsg_into() crash via __buffer__ re-entrancy (related to gh-143637) · Issue #143988 · python/cpython
X Title: socket.sendmsg() and socket.recvmsg_into() crash via __buffer__ re-entrancy (related to gh-143637) · Issue #143988 · python/cpython
Description: Crash report What happened? Bug description Similar to gh-143637, socket.sendmsg() and socket.recvmsg_into() can crash due to re-entrant mutation during argument parsing, but via the __buffer__ protocol instead of __index__. Affected fun...
Open Graph Description: Crash report What happened? Bug description Similar to gh-143637, socket.sendmsg() and socket.recvmsg_into() can crash due to re-entrant mutation during argument parsing, but via the __buffer__ pro...
X Description: Crash report What happened? Bug description Similar to gh-143637, socket.sendmsg() and socket.recvmsg_into() can crash due to re-entrant mutation during argument parsing, but via the __buffer__ pro...
Opengraph URL: https://github.com/python/cpython/issues/143988
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"socket.sendmsg() and socket.recvmsg_into() crash via __buffer__ re-entrancy (related to gh-143637)","articleBody":"# Crash report\n\n### What happened?\n\n\n## Bug description\n\nSimilar to gh-143637, `socket.sendmsg()` and `socket.recvmsg_into()` can crash due to re-entrant mutation during argument parsing, but via the `__buffer__` protocol instead of `__index__`.\n\n## Affected functions\n\n| Function | Argument | Trigger |\n|----------|----------|---------|\n| `sendmsg()` | arg 1 (data buffers) | `__buffer__` |\n| `recvmsg_into()` | arg 1 (buffers) | `__buffer__` |\n\n## Root cause\n\nSame as gh-143637: `PySequence_Fast()` returns the original list when input is already a list. During iteration, `PyObject_GetBuffer()` triggers `__buffer__` callbacks which can clear the list, causing subsequent accesses to read invalid memory.\n\n## PoC - sendmsg() crash\n\n```python\nimport socket\n\nseq = []\n\nclass MutBuffer:\n def __init__(self, data):\n self._data = bytes(data)\n self.tripped = False\n \n def __buffer__(self, flags):\n if not self.tripped:\n self.tripped = True\n seq.clear()\n return memoryview(self._data)\n\nseq[:] = [MutBuffer(b'Hello'), b'World', b'Test']\nleft, right = socket.socketpair()\nleft.sendmsg(seq) # CRASH: Exit code 139 (SIGSEGV)\n```\n\n## PoC - recvmsg_into() crash\n\n```python\nimport socket\n\nseq = []\n\nclass MutBuffer:\n def __init__(self, data):\n self._data = bytearray(data)\n self.tripped = False\n \n def __buffer__(self, flags):\n if not self.tripped:\n self.tripped = True\n seq.clear()\n return memoryview(self._data)\n\nseq[:] = [MutBuffer(b'x' * 100), bytearray(100), bytearray(100)]\nleft, right = socket.socketpair()\nleft.send(b'Hello World!')\nright.recvmsg_into(seq) # CRASH: Exit code 139 (SIGSEGV)\n```\n\n## Fix\n\nReplace `PySequence_Fast()` with `PySequence_Tuple()` in `sock_sendmsg_iovec()` and `sock_recvmsg_into()`.\n\nI have a PR ready.\n\n## CPython versions tested on:\n\nmain branch (3.15.0a5+)\n\n## Operating system and architecture:\n\nmacOS 15.0 arm64\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-143987\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-01-18T06:56:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/143988/cpython/issues/143988"}
| 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:6782a676-bef5-5593-aaf0-ef7a7f8db4fc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D7D2:3891C0:AF06DB:EB829F:697017AD |
| html-safe-nonce | 2b22b46730994af7c68a6e1c137edde6c4c070fd69bf881018b460a2741b6735 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEN0QyOjM4OTFDMDpBRjA2REI6RUI4MjlGOjY5NzAxN0FEIiwidmlzaXRvcl9pZCI6IjYzMjQxNjA4ODQ5NDI4Mzc2NzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f84fd41aafff3ae8c71b8a5d9194d0eae1b32be962d39446b7919e3323e84633 |
| hovercard-subject-tag | issue:3826434776 |
| 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/143988/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5d63aa3689a43084fa7bbeae814b0657eeebc25487b0a5f52afab56812a765fb/python/cpython/issues/143988 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5d63aa3689a43084fa7bbeae814b0657eeebc25487b0a5f52afab56812a765fb/python/cpython/issues/143988 |
| og:image:alt | Crash report What happened? Bug description Similar to gh-143637, socket.sendmsg() and socket.recvmsg_into() can crash due to re-entrant mutation during argument parsing, but via the __buffer__ pro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043a09521e81f00c45482046b3f38390b8b292cf9a86ad665a632633a072c05c |
| 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 | 98881eadf8ae73000970c42161c094b0809b1330 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width