Title: Heap out-of-bound read in `socket.sendmsg` ancillary parser after re-entrant `__index__` clears the control list · Issue #143637 · python/cpython · GitHub
Open Graph Title: Heap out-of-bound read in `socket.sendmsg` ancillary parser after re-entrant `__index__` clears the control list · Issue #143637 · python/cpython
X Title: Heap out-of-bound read in `socket.sendmsg` ancillary parser after re-entrant `__index__` clears the control list · Issue #143637 · python/cpython
Description: What happened? _socket_socket_sendmsg_impl fast-paths ancillary data with PySequence_Fast, assuming the original list stays stable. Parsing each (level, type, data) tuple calls PyLong_AsLongAndOverflow, which runs __index__; a malicious ...
Open Graph Description: What happened? _socket_socket_sendmsg_impl fast-paths ancillary data with PySequence_Fast, assuming the original list stays stable. Parsing each (level, type, data) tuple calls PyLong_AsLongAndOver...
X Description: What happened? _socket_socket_sendmsg_impl fast-paths ancillary data with PySequence_Fast, assuming the original list stays stable. Parsing each (level, type, data) tuple calls PyLong_AsLongAndOver...
Opengraph URL: https://github.com/python/cpython/issues/143637
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Heap out-of-bound read in `socket.sendmsg` ancillary parser after re-entrant `__index__` clears the control list","articleBody":"### What happened?\n\n`_socket_socket_sendmsg_impl` fast-paths ancillary data with `PySequence_Fast`, assuming the original list stays stable. Parsing each `(level, type, data)` tuple calls `PyLong_AsLongAndOverflow`, which runs `__index__`; a malicious `__index__` can clear the shared list mid-loop, leaving `ncmsgs` and cached element pointers stale. The next `PySequence_Fast_GET_ITEM` then indexes past the shortened list and dereferences freed slots, crashing with a heap OOB read at Modules/socketmodule.c:5005.\n\n**Proof of Concept:**\n\n```python\nimport socket\n\nseq = []\n\nclass Mut:\n def __init__(self):\n self.tripped = False\n def __index__(self):\n if not self.tripped:\n self.tripped = True\n seq.clear()\n return 0\n\nseq[:] = [\n (socket.SOL_SOCKET, Mut(), b'x'),\n (socket.SOL_SOCKET, 0, b'x'),\n]\nleft, right = socket.socketpair()\nleft.sendmsg([b'x'], seq)\n```\n\n**Vulnerable Code Snippet:**\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n```c\n/* Buggy Re-entrant Path */\nlong\nPyLong_AsLongAndOverflow(PyObject *vv, int *overflow)\n{\n /* ... */\n if (PyLong_Check(vv)) {\n v = (PyLongObject *)vv;\n }\n else {\n v = (PyLongObject *)_PyNumber_Index(vv); /* Reentrant call site */\n if (v == NULL)\n return -1;\n do_decref = 1;\n }\n /* ... */\n}\n\nif ((cmsg_fast = PySequence_Fast(cmsg_arg,\n \"sendmsg() argument 2 must be an \"\n \"iterable\")) == NULL)\n goto finally;\nncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);\n/* ... */\nwhile (ncmsgbufs \u003c ncmsgs) {\n if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs), /* crashing pointer derived */\n \"(iiy*):[sendmsg() ancillary data items]\",\n \u0026cmsgs[ncmsgbufs].level,\n \u0026cmsgs[ncmsgbufs].type,\n \u0026cmsgs[ncmsgbufs].data)) /* Crash site */\n goto finally;\n /* ... */\n}\n\n/* Clobbering Path */\nstatic void\nlist_clear_impl(PyListObject *a, bool is_resize)\n{\n PyObject **items = a-\u003eob_item;\n /* ... */\n\n Py_ssize_t i = Py_SIZE(a);\n Py_SET_SIZE(a, 0);\n FT_ATOMIC_STORE_PTR_RELEASE(a-\u003eob_item, NULL);\n a-\u003eallocated = 0;\n while (--i \u003e= 0) {\n Py_XDECREF(items[i]); /* state mutate site */\n }\n free_list_items(items, use_qsbr);\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==423360==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000008 (pc 0x73089bf5b108 bp 0x7ffe70547070 sp 0x7ffe70546db0 T0)\n==423360==The signal is caused by a READ memory access.\n==423360==Hint: address points to the zero page.\n #0 0x73089bf5b108 in _socket_socket_sendmsg_impl Modules/socketmodule.c:5005\n #1 0x73089bf5b108 in _socket_socket_sendmsg Modules/clinic/socketmodule.c.h:188\n #2 0x605448f333e7 in _PyObject_VectorcallTstate Include/internal/pycore_call.h:169\n #3 0x605448f333e7 in PyObject_Vectorcall Objects/call.c:327\n #4 0x605448de75a2 in _PyEval_EvalFrameDefault Python/generated_cases.c.h:1620\n #5 0x6054492b1ad6 in _PyEval_EvalFrame Include/internal/pycore_ceval.h:121\n #6 0x6054492b1ad6 in _PyEval_Vector Python/ceval.c:2001\n #7 0x6054492b1ad6 in PyEval_EvalCode Python/ceval.c:884\n #8 0x6054493f716e in run_eval_code_obj Python/pythonrun.c:1365\n #9 0x6054493f716e in run_mod Python/pythonrun.c:1459\n #10 0x6054493fbe17 in pyrun_file Python/pythonrun.c:1293\n #11 0x6054493fbe17 in _PyRun_SimpleFileObject Python/pythonrun.c:521\n #12 0x6054493fc93c in _PyRun_AnyFileObject Python/pythonrun.c:81\n #13 0x60544946fe3c in pymain_run_file_obj Modules/main.c:410\n #14 0x60544946fe3c in pymain_run_file Modules/main.c:429\n #15 0x60544946fe3c in pymain_run_python Modules/main.c:691\n #16 0x60544947171e in Py_RunMain Modules/main.c:772\n #17 0x60544947171e in pymain_main Modules/main.c:802\n #18 0x60544947171e in Py_BytesMain Modules/main.c:826\n #19 0x73089c42a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58\n #20 0x73089c42a28a in __libc_start_main_impl ../csu/libc-start.c:360\n #21 0x605448e0b634 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/socketmodule.c:5005 in _socket_socket_sendmsg_impl\n==423360==ABORTING\n```\n\u003c/details\u003e\n\n### CPython versions tested on:\n\n\u003cdetails\u003e\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\u003e\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-143892\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/jackfromeast","@type":"Person","name":"jackfromeast"},"datePublished":"2026-01-10T05:09:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/143637/cpython/issues/143637"}
| 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:8949fbbd-aab0-bce5-568f-9d663c89ce6e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93E8:1E0146:181D01D:2058981:6969AC9D |
| html-safe-nonce | 1f71a1e6b235aaa30c4ac58b9ff68f77082dad596b9c83e384e8ba56862a12f5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0U4OjFFMDE0NjoxODFEMDFEOjIwNTg5ODE6Njk2OUFDOUQiLCJ2aXNpdG9yX2lkIjoiNzE2NjIyMTUwNDI5NzQxMzc4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ce7dd97f00476cdbae30b2bee6ed1d49ed95b3055032bb94bb017797dbd5268f |
| hovercard-subject-tag | issue:3799131372 |
| 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/143637/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9ff0060fbc0b456c0287c8c3c40ca3b81d9fd4898b85fabc1696212625061699/python/cpython/issues/143637 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9ff0060fbc0b456c0287c8c3c40ca3b81d9fd4898b85fabc1696212625061699/python/cpython/issues/143637 |
| og:image:alt | What happened? _socket_socket_sendmsg_impl fast-paths ancillary data with PySequence_Fast, assuming the original list stays stable. Parsing each (level, type, data) tuple calls PyLong_AsLongAndOver... |
| 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 | 24c4c97a2d520cb286b35e1a4c22d7a4df3c26a2fa28dd7cdf0e65db327b4de7 |
| 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 | 124667f43168afb6c9c03b7c02eb5b1d2e1be3d9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width