Repository navigation
Fix excessive memory allocation from crafted container headers - #409
Merged
byroot merged 1 commit intoOct 11, 2026
Merged
Conversation
A container header carries an element count that the unpacker used to pre-allocate the Array or Hash. The count is capped at SHRT_MAX per container, but nested containers each pre-allocate up to that cap, so a small crafted message of deeply nested headers could still force a large allocation, and a streaming Unpacker holds it until the connection ends. An element needs at least one byte, so a container cannot hold more elements than there are bytes left to read. Cap the initial size by the buffered size as well; the container still grows as more data arrives. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
byroot
approved these changes
Oct 11, 2026
Contributor
Author
|
Thanks |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A container header carries an element count that the unpacker uses to pre-allocate the Array or Hash. The count is capped at
SHRT_MAXper container, but nested containers each pre-allocate up to that cap, so a small crafted message of deeply nested headers can still force a large allocation. A streamingUnpacker(feed_each/each) keeps the stack on EOF, so it holds that memory until the connection ends, which makes it reachable for anything decoding untrusted data off the wire.An element needs at least one byte (a map pair at least two), so a container cannot hold more elements than there are bytes left to read. This caps the initial size by the buffered size as well; the container still grows as more data arrives, so fully-buffered decodes are unaffected.
Reproduction
On my environment the 384-byte message grows RSS by about 16.9 MB before this change and about 0.4 MB after it.
🤖 Generated with Claude Code