Skip to content

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

Open
kitkatt7 wants to merge 2 commits into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard
Open

kitkatt7 wants to merge 2 commits into
msgpack:mainfrom
kitkatt7:packer-reentrant-guard

Conversation

@kitkatt7

@kitkatt7 kitkatt7 commented Nov 16, 2026 •

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, pack_array_header and pack_map_header append to pk.buf too, so they check the flag as well. calling any of them from default under an outer pack() used to let autoreset drain pk.length and silently truncate the outer output: packer.pack([1, object(), 2]) with a default that calls packer.pack_ext_type(1, b"E" * 16) returned c4026f6b02 instead of 9301c4026f6b02. they now raise RuntimeError like pack() does. (an earlier version of this description claimed pack_ext_type was covered by #765 removing its len() call. that was wrong on two counts: #765 is still open, and dropping the len() call wouldn't help here anyway, since the problem is the write into the outer buffer rather than the python that len() runs.)

the fallback writes through BytesIO, which already refuses resizing while exported, so the new tests are C-only. they fail on main and pass 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.

pack_ext_type, pack_array_header and pack_map_header also append to
pk.buf, so calling any of them from default under an outer pack() let
autoreset drain pk.length and silently truncate the outer output
instead of raising. check _packing in those three as well.
@kitkatt7

Copy link
Copy Markdown
Author

good catch on both counts, and you were right that I had the #765 reasoning backwards. #765 is indeed still open, and more to the point, removing the len() call wouldn't have helped here anyway: the problem isn't the python that len() runs, it's the write into the outer pack's buffer, so pack_ext_type needed the flag regardless.

checking it I found pack_array_header and pack_map_header have the same hole, since they append to pk.buf too. all three were silently truncating instead of raising, e.g. packer.pack([1, object(), 2]) with a default that calls packer.pack_ext_type(1, b"E" * 16) came back as c4026f6b02 rather than 9301c4026f6b02, because the inner autoreset drains pk.length and the outer pack carries on writing from zero. pushed a commit that checks _packing in those three as well, plus a parametrised test over all five writers. both builds still pass.

one thing I did look at and deliberately left alone: a top-level pack_ext_type can still hand out a memoryview from inside an overridden len, but the view comes back length 0 since nothing has been written yet, so there's nothing stale to read. that one is really #765's repeated-len() territory and I think it belongs there rather than here.

and yes, please do post the harness if you don't mind. the reentrant pack_ext_type case is one my own test didn't reach, so it would be useful to run the stateful version against this.

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