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: github.com
IPv4 address: 140.82.113.3
| 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:637e1aa9-a73f-6248-5524-0a7d27c81936 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A72A:197AF:55343A:74F996:6B075108 |
| html-safe-nonce | 831c49c5a5019be80dbd56b7ac657e65a50a025faa4454154f8ba8d5c74d82ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzJBOjE5N0FGOjU1MzQzQTo3NEY5OTY6NkIwNzUxMDgiLCJ2aXNpdG9yX2lkIjoiODE0NTk2NTQzMDcyNTg5MDMxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ee9f02c77670bb94e4459e4a35de3271460ba60605822d9344c6d54da7b7f75f |
| 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 | 663b1a69a41309856ffd9403fa5b764f6c0f78a39a0a8f75fb2c205c96a947be |
| 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 | effa2c0a160a75bd95d9ffcc6320197d7aea0d83 |
| 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