You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refuse buffer export and re-entry while Packer is packing - #775
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.
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.
Happy to share the reproducer or traces if useful.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.