Raise StackError instead of crashing on deeply nested recursive extensions#397
Merged
byroot merged 3 commits intoJul 6, 2026
Merged
Conversation
…sions read_raw_body_begin() ignored the return value of _msgpack_unpacker_stack_push() on the recursive-extension path. Recursive extensions re-enter msgpack_unpacker_read() through the user proc, consuming a full C stack frame per nesting level. Once the unpacker stack was already at MSGPACK_UNPACKER_STACK_CAPACITY, the failed push was silently discarded and the recursion continued unbounded, exhausting the C stack and crashing the VM with SIGSEGV instead of raising StackError like every other container type. Return PRIMITIVE_STACK_TOO_DEEP when the push fails so a maliciously deep recursive-extension payload raises MessagePack::StackError. This is reachable from MessagePack.unpack / Factory#load on attacker-controlled bytes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The MessagePack::StackError behavior verified by this spec is specific to the C extension, which caps nesting at MSGPACK_UNPACKER_STACK_CAPACITY. The Java (JRuby) decoder has no equivalent guard and recurses until the JVM stack overflows, so the spec cannot assert MessagePack::StackError there. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Packing a 200+ level nested recursive extension recurses through the packer proc once per level, which overflows the C stack on platforms with a small default stack (Windows CI raised SystemStackError during Factory#dump before the assertion could run). Construct the nested type-0x01 extension payload iteratively from the inside out instead, so only the unpack path under test exercises deep nesting. The loaded payload still nests past MSGPACK_UNPACKER_STACK_CAPACITY (200 levels) and raises MessagePack::StackError on the C extension. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
byroot
approved these changes
Jul 6, 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.
Summary
A deeply nested payload that uses a recursive extension type (a type registered with
recursive: true) can crash the Ruby VM with aSIGSEGVinstead of raisingMessagePack::StackError.read_raw_body_begin()ignored the return value of_msgpack_unpacker_stack_push()on the recursive-extension path:Recursive extensions re-enter
msgpack_unpacker_read()through the user proc, consuming a full C stack frame per nesting level. Once the unpacker stack is already atMSGPACK_UNPACKER_STACK_CAPACITY, the failed push is silently discarded and the recursion continues unbounded, exhausting the C stack and crashing the process — whereas every other container type (array/map/raw) correctly propagatesPRIMITIVE_STACK_TOO_DEEPand raisesStackError.Impact
This is reachable from
MessagePack.unpack/Factory#loadon attacker-controlled bytes whenever a recursive extension type is registered.msgpackis a core dependency of Fluentd and similar log/data pipelines, where the unpacker routinely consumes untrusted input, so an unbounded-recursion crash is a denial-of-service risk rather than a clean, rescuable error.Reproduction
🤖 Generated with Claude Code