Title: Heap buffer overflow in `Packer(buf_size=…)` with an unstable `__index__` · Issue #723 · msgpack/msgpack-python · GitHub
Open Graph Title: Heap buffer overflow in `Packer(buf_size=…)` with an unstable `__index__` · Issue #723 · msgpack/msgpack-python
X Title: Heap buffer overflow in `Packer(buf_size=…)` with an unstable `__index__` · Issue #723 · msgpack/msgpack-python
Description: I found a heap buffer overflow fuzzing the C extension. The input is contrived (it needs an object whose __index__ answers differently each call) but sharing it in case it's useful. Versions msgpack 1.2.1 (pip install msgpack), CPython 3...
Open Graph Description: I found a heap buffer overflow fuzzing the C extension. The input is contrived (it needs an object whose __index__ answers differently each call) but sharing it in case it's useful. Versions msgpac...
X Description: I found a heap buffer overflow fuzzing the C extension. The input is contrived (it needs an object whose __index__ answers differently each call) but sharing it in case it's useful. Versions ms...
Opengraph URL: https://github.com/msgpack/msgpack-python/issues/723
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Heap buffer overflow in `Packer(buf_size=…)` with an unstable `__index__`","articleBody":"I found a heap buffer overflow fuzzing the C extension. The input is contrived (it needs an object whose `__index__` answers differently each call) but sharing it in case it's useful.\n\n### Versions\n\nmsgpack 1.2.1 (`pip install msgpack`), CPython 3.12, Linux x86_64. The same\ncode is on `main`.\n\n### Reproducer\n\n```python\nimport msgpack\n\nclass Unstable:\n \"\"\"__index__ answers with a small size first and a large one afterwards.\"\"\"\n def __init__(self, first, rest):\n self._v = [first, rest]\n self._n = 0\n def __index__(self):\n v = self._v[min(self._n, 1)]\n self._n += 1\n return v\n __int__ = __index__\n\nmsgpack.Packer(buf_size=Unstable(600, 1 \u003c\u003c 20)).pack(b\"A\" * 100000) # SIGSEGV\n```\n\n**Output**\n```python\n[1] 123498 segmentation fault (core dumped) python\n```\n\n### Sanitizer build\n\n```\nAddressSanitizer: heap-buffer-overflow\n #1 msgpack_pack_write msgpack/pack.h:53:5\n #2 msgpack_pack_raw_body msgpack/pack_template.h:488:16\n #3 Packer._pack_inner _cmsgpack.c:7137:18\n #4 Packer._pack _cmsgpack.c:8391:15\n allocated by:\n #1 Packer.__cinit__ _cmsgpack.c:5931\n```\n\n### What happens\n\n`__cinit__` uses `buf_size` at two lines, and Cython converts it separately at each, so the object is asked for its value once per line:\n\n```cython\ndef __cinit__(self, buf_size=256*1024, **_kwargs):\n self.pk.buf = \u003cchar*> PyMem_Malloc(buf_size) # :114 -> 600\n if self.pk.buf == NULL:\n raise MemoryError(\"Unable to allocate internal buffer.\")\n self.pk.buf_size = buf_size # :117 -> 1048576\n```\n\n([`_packer.pyx:114`](https://github.com/msgpack/msgpack-python/blob/v1.2.1/msgpack/_packer.pyx#L114), [`:117`](https://github.com/msgpack/msgpack-python/blob/v1.2.1/msgpack/_packer.pyx#L117))\n\nWith `Unstable(600, 1 \u003c\u003c 20)` the packer holds 600 bytes and records 1048576 as its capacity, and everything afterwards works from the recorded number:\n\n```c\nif (len + l > bs) { /* pack.h:45 — bs = pk.buf_size = 1048576 */\n ...grow...\n}\nmemcpy(buf + len, data, l);\n```\n\n100000 is under 1048576, so the buffer never grows and the `memcpy` writes 100000 bytes into a 600-byte block.\n([`pack.h:45`](https://github.com/msgpack/msgpack-python/blob/v1.2.1/msgpack/pack.h#L45))\n\n---\n\nIt reads like a bug to me, but the input is not a natural one, so I'm leaving it here. \nEntirely reasonable if this is out of scope.\n","author":{"url":"https://github.com/marinelay","@type":"Person","name":"marinelay"},"datePublished":"2026-08-07T18:05:57Z","url":"https://github.com/msgpack/msgpack-python/issues/723"}
| 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:982ecf9a-fb29-3203-5bcd-c69a8a67d36a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C5E8:15F301:425BF0:5B91C6:6B074D3B |
| html-safe-nonce | 12d1115326c52396d10a13bd0781ab4515ce6c209353cb2cc95284dfc8f27fff |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNUU4OjE1RjMwMTo0MjVCRjA6NUI5MUM2OjZCMDc0RDNCIiwidmlzaXRvcl9pZCI6IjQ4NDE5NTMwMzM4NzEzNzk3NzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2ef2ec2e19115336f30dc0e21bfce69d01d2ee2eae4e17d16395bf67ab1c8417 |
| hovercard-subject-tag | issue:5092784096 |
| 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/msgpack/msgpack-python/723/issue_layout |
| twitter:image | https://opengraph.githubassets.com/127fd38f04e11ee361110b3730d8971f21a5a71e6eb7e71e4b8a44b1833eacff/msgpack/msgpack-python/issues/723 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/127fd38f04e11ee361110b3730d8971f21a5a71e6eb7e71e4b8a44b1833eacff/msgpack/msgpack-python/issues/723 |
| og:image:alt | I found a heap buffer overflow fuzzing the C extension. The input is contrived (it needs an object whose __index__ answers differently each call) but sharing it in case it's useful. Versions msgpac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | marinelay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 26f151990bcb5b67b87df8b3f96ed340f849939d44e36f348402de5e39cca989 |
| turbo-cache-control | no-preview |
| go-import | github.com/msgpack/msgpack-python git https://github.com/msgpack/msgpack-python.git |
| octolytics-dimension-user_id | 198264 |
| octolytics-dimension-user_login | msgpack |
| octolytics-dimension-repository_id | 2242705 |
| octolytics-dimension-repository_nwo | msgpack/msgpack-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 2242705 |
| octolytics-dimension-repository_network_root_nwo | msgpack/msgpack-python |
| 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 | cd00d8452bcb4e56a60ba7c0e61a697b46513fe9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width