Title: fix: use-after-free in unpackb() ExtraData path for non-contiguous input by ThomasWaldmann · Pull Request #722 · msgpack/msgpack-python · GitHub
Open Graph Title: fix: use-after-free in unpackb() ExtraData path for non-contiguous input by ThomasWaldmann · Pull Request #722 · msgpack/msgpack-python
X Title: fix: use-after-free in unpackb() ExtraData path for non-contiguous input by ThomasWaldmann · Pull Request #722 · msgpack/msgpack-python
Description: Fixes #720. Problem For non-contiguous input, get_data_from_buffer() releases the original view and makes a temporary contiguous copy, so buf points into memory owned by view: https://github.com/msgpack/msgpack-python/blob/main/msgpack/_unpacker.pyx#L129-L134 unpackb() then releases view in its finally block — freeing that copy — and only afterwards reads buf+off to build the ExtraData payload. ExtraData.extra is therefore filled from freed memory. This is the same class of bug as #677, on a code path that fix did not cover. Reproducer (from #720) import msgpack buf = bytearray(b"\x00\xffa\xffb\xffc\xff") view = memoryview(buf)[::2] assert bytes(view) == b"\x00abc" try: msgpack.unpackb(view) except msgpack.ExtraData as e: print(e.unpacked) # 0 (correct) print(e.extra) # b'ab\x00' (expected b'abc') Reproduced on macOS 15 / arm64 / CPython 3.14 with 1.2.1 and with current main. Running the same reproducer under the macOS allocator's free-poisoning makes the use-after-free unambiguous: PYTHONMALLOC=malloc MallocScribble=1 python repro.py extra=b'\xaa\xaa\x00' # expected b'abc' so arbitrary heap contents can end up in ExtraData.extra, not merely a shifted copy of the input. The reporter's ASan output (memcpy-param-overlap) is the same event seen from the other side: PyBytes_FromStringAndSize re-allocates the just-freed block and copies it onto itself. Unpacker.feed() uses the same helper but copies via append_buffer() before releasing, so it is unaffected. fallback.py is also unaffected. Fix Copy the extra data out before releasing the view. Kept minimal: unpack_data() still runs outside the try, so only the PyBytes_FromStringAndSize call moved. Test test_unpack_noncontiguous_memoryview_extra_data in test/test_memoryview.py, next to the #677 test. Verified it fails on main (At index 4 diff: b'\x00' != b'a') and passes with the fix. Full suite passes for both implementations — 135 passed (Cython), 133 passed / 2 skipped (MSGPACK_PUREPYTHON=1) — and also with MallocScribble=1. Note that whether the corruption is observable depends on allocator reuse, so the test uses a payload/extra size combination that reliably triggers it on CPython's pymalloc; the assertion itself is unconditionally correct.
Open Graph Description: Fixes #720. Problem For non-contiguous input, get_data_from_buffer() releases the original view and makes a temporary contiguous copy, so buf points into memory owned by view: https://github.com/ms...
X Description: Fixes #720. Problem For non-contiguous input, get_data_from_buffer() releases the original view and makes a temporary contiguous copy, so buf points into memory owned by view: https://github.com/ms...
Opengraph URL: https://github.com/msgpack/msgpack-python/pull/722
X: @github
Domain: redirect.github.com
IPv4 address: 140.82.114.4
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4d15bcb9-310c-0503-c4e3-64c314dc1960 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 87A6:72507:458A08:60168A:6B074CB0 |
| html-safe-nonce | 381e63e6c8626bc365bbacbccd10f999e8878ed08a4946100d1e47d2c58b1b37 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0E2OjcyNTA3OjQ1OEEwODo2MDE2OEE6NkIwNzRDQjAiLCJ2aXNpdG9yX2lkIjoiOTA3MDk4MDA3OTIyNzk4MDk3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0e1ddf1fddeb0b548f18cfdb5a57859f9fad4d59c4142fafa703ab3e9afe8125 |
| hovercard-subject-tag | pull_request:4196823339 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/msgpack/msgpack-python/pull/722/files |
| twitter:image | https://avatars.githubusercontent.com/u/356103?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/356103?s=400&v=4 |
| og:image:alt | Fixes #720. Problem For non-contiguous input, get_data_from_buffer() releases the original view and makes a temporary contiguous copy, so buf points into memory owned by view: https://github.com/ms... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 26f151990bcb5b67b87df8b3f96ed340f849939d44e36f348402de5e39cca989 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width