Skip to content

Refuse to export the Packer buffer during pack - #770

Open
CAOShurong wants to merge 1 commit into
msgpack:mainfrom
CAOShurong:fix-packer-export-during-pack
Open

CAOShurong wants to merge 1 commit into
msgpack:mainfrom
CAOShurong:fix-packer-export-during-pack

Conversation

@CAOShurong

Copy link
Copy Markdown

The C Packer accepts getbuffer() while pack() is still writing. That happens from the default callback, from a dict subclass''s items(), and from pack_map_pairs(). _check_exports() runs only at the start of a public method, so the export is invisible to it. If packing then grows the buffer, PyMem_Realloc frees the block the memoryview still references.

pack(), pack_map_pairs(), and pack_ext_type() now count nested calls, and the buffer protocol raises BufferError until they return. An export taken between calls is unchanged, and �ytes() still copies. #735 checks exports after the default callback only, so the items() and pack_map_pairs() paths remain open there.

On Windows with Python 3.13, the extension was built with cython msgpack/_cmsgpack.pyx and installed with pip install -e .. The new tests failed on the unmodified extension and pass after this change. pytest -v test: 181 passed, 1 skipped. MSGPACK_PUREPYTHON=1 pytest -v test: 178 passed, 4 skipped.

The pure Python Packer is unchanged. Its buffer is a BytesIO, so growth does not free an existing view. pack_map_pairs() still keeps a partial header when iteration raises. This was not run under AddressSanitizer.

Fixes #769.

The C Packer accepted getbuffer() from a default callback, from
dict.items(), and from pack_map_pairs() while it was still writing.
Growing the buffer then freed the exported memory. Those methods now
reject the export until they return.

pytest -v test: 181 passed, 1 skipped.
MSGPACK_PUREPYTHON=1 pytest -v test: 178 passed, 4 skipped.
The pure Python buffer is not freed when it grows.
@oakkaya

oakkaya commented Nov 23, 2026

Copy link
Copy Markdown

Independent ASan verification of this approach (HEAD e3a7160, C extension rebuilt with -fsanitize=address,undefined):

I applied this patch (__getbuffer__ / packing-depth guard) and ran a stateful fuzz where default, a dict subclass's items(), a list subclass's __iter__, and a pack_map_pairs() iterable each randomly call getbuffer(), reentrant pack(), pack_ext_type(), bytes(), or raise. 20 seeds x 200 sequences, ASan+UBSan, on this branch family: 0 crashes / 0 stale-view mismatches / 0 ASan errors over 4,000 sequences. A targeted matrix confirmed the export is rejected on all four re-entrant paths, and that the depth counter is cleaned up correctly after exceptions in default / pack_map_pairs / pack_ext_type (post-exception getbuffer() succeeds, and the export guard still holds).

For comparison, unpatched HEAD and PR #735 both produced ASan heap-use-after-free in PyBuffer_ToContiguous on 10/20 seeds with the same harness. This __getbuffer__ guard looks like the correct fix. Happy to share the reproducer or traces if useful.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Packer use-after-free via re-entrant getbuffer() during pack()

2 participants