Skip to content

refuse buffer export and re-entry while Packer is packing - #775

Open
kitkatt7 wants to merge 1 commit into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard
Open

kitkatt7 wants to merge 1 commit into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard

Conversation

@kitkatt7

Copy link
Copy Markdown

the C Packer only checks for exported buffers when pack() starts, but default (and the iteration in pack_map_pairs) runs arbitrary python while packing. if that code calls memoryview(packer), the outer pack keeps writing and msgpack_pack_write reallocs pk.buf, so the view is left pointing at freed memory. with PYTHONMALLOC=debug, a default that grabs the view and returns b"x" * (1 << 20) on Packer(default=default, buf_size=1024) gives a view that reads back 0xdd bytes. calling packer.pack() from default has a related problem: autoreset zeroes pk.length under the outer call and its output is silently corrupted. this adds a _packing flag set by pack() and pack_map_pairs(), the same way #704 guarded Unpacker. getbuffer raises BufferError and re-entry raises RuntimeError while the flag is set. pack_ext_type is left alone because #765 already removes the len() call, which was the only python it ran. the fallback writes through BytesIO, which already refuses resizing while exported, so the new test is C-only. the test fails on main and passes with the change; pytest passes for both the C and pure-python builds.

@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 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: 0 crashes / 0 stale-view mismatches / 0 ASan errors over 4,000 sequences. The export guard and depth counter are cleaned up correctly after exceptions. This is the only patch in the family that also rejects reentrant pack() (raising RuntimeError) and fixes the autoreset=True outer-output loss — a nice improvement over #770/#774.

Two notes:

  1. The PR description says pack_ext_type is left out because fix out-of-bounds read when packing ext data with overridden __len__ #765 removes its len() call. On current HEAD e3a7160, fix out-of-bounds read when packing ext data with overridden __len__ #765 is still open and pack_ext_type still calls len(data). More importantly, even if len() is removed, a reentrant pack_ext_type() from inside default writes into the outer pack's buffer and corrupts the output (same behavior in the pure-Python fallback, so it is not C-specific memory unsafety).
  2. Unpatched HEAD and PR Check exported buffers after the default callback returns #735 both crash with ASan heap-use-after-free in PyBuffer_ToContiguous on 10/20 seeds with the same harness; this patch does not.

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.

2 participants