Skip to content

Check exported buffers after the default callback returns - #735

Draft
AbhinavMir wants to merge 1 commit into
msgpack:mainfrom
AbhinavMir:packer-default-callback-exports
Draft

AbhinavMir wants to merge 1 commit into
msgpack:mainfrom
AbhinavMir:packer-default-callback-exports

Conversation

@AbhinavMir

@AbhinavMir AbhinavMir commented Aug 30, 2026 •

Copy link
Copy Markdown

The default callback can export the internal buffer with getbuffer().
The packer then continues and can reallocate that buffer.
The export points at freed memory after the reallocation.

Call _check_exports() after the callback returns. The packer now
raises BufferError. The pure Python packer already raises BufferError
in this case.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Other supported user-code hooks can still export the buffer before a reallocating write.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Prevents buffer invalidation after default exports the packer’s internal buffer.

Changes:

  • Checks active exports after default returns.
  • Adds regression coverage for the unsafe reallocation scenario.
File summaries
File Description
msgpack/_packer.pyx Adds post-callback export validation.
test/test_buffer.py Tests exported-buffer safety during callbacks.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread msgpack/_packer.pyx
Comment on lines +267 to +269
# The callback may have exported the internal buffer.
# Packing on would reallocate it and invalidate the export.
self._check_exports()
@oakkaya

oakkaya commented Nov 23, 2026

Copy link
Copy Markdown

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

This patch only calls _check_exports() after the default callback returns. The other code paths that run arbitrary Python in the middle of a pack can still export the buffer immediately before a realloc:

  1. A dict subclass overriding items() that calls packer.getbuffer() before delegating to super().items().
  2. A list subclass overriding __iter__ that exports the buffer, followed by a large element.
  3. pack_map_pairs() with an iterable whose __iter__ calls getbuffer().

On both unpatched HEAD and this branch the stored memoryview reads stale bytes (e.g. b'' where the current buffer starts with b'\x81'). A 20-seed x 200-sequence stateful fuzz produced ASan heap-use-after-free in PyBuffer_ToContiguous on 10/20 seeds. PRs #770/#774/#775, which guard __getbuffer__ with a packing-depth counter, showed 0 crashes / 0 stale-view mismatches over 4,000 sequences each.

So this approach looks incomplete; the __getbuffer__ guard appears to cover the remaining paths. Happy to share the minimal reproducer or the ASan 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.

3 participants