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.

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()

1 participant