Skip to content

Raise StackError instead of crashing on deeply nested recursive extensions#397

Merged
byroot merged 3 commits into
msgpack:masterfrom
Watson1978:fix-recursive-ext-stack-overflow
Jul 6, 2026
Merged

Raise StackError instead of crashing on deeply nested recursive extensions#397
byroot merged 3 commits into
msgpack:masterfrom
Watson1978:fix-recursive-ext-stack-overflow

Conversation

@Watson1978

Copy link
Copy Markdown
Contributor

Summary

A deeply nested payload that uses a recursive extension type (a type registered with recursive: true) can crash the Ruby VM with a SIGSEGV instead of raising MessagePack::StackError.

read_raw_body_begin() ignored the return value of _msgpack_unpacker_stack_push() on the recursive-extension path:

/* ext/msgpack/unpacker.c */
_msgpack_unpacker_stack_push(uk, STACK_TYPE_RECURSIVE, 1, Qnil);
int raised;
obj = protected_proc_call(proc, 1, &uk->self, &raised);

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 at MSGPACK_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 propagates PRIMITIVE_STACK_TOO_DEEP and raises StackError.

Impact

This is reachable from MessagePack.unpack / Factory#load on attacker-controlled bytes whenever a recursive extension type is registered. msgpack is 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

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem "msgpack"
end

recursive_type = Struct.new(:payload)
factory = MessagePack::Factory.new
factory.register_type(0x01, recursive_type,
  packer:   ->(obj, packer)  { packer.write(obj.payload) },
  unpacker: ->(unpacker)     { recursive_type.new(unpacker.read) },
  recursive: true,
)

obj = 42
1000.times { obj = recursive_type.new(obj) }
payload = factory.dump(obj)

factory.load(payload)   # => SIGSEGV before the fix; MessagePack::StackError after

🤖 Generated with Claude Code

Watson1978 and others added 3 commits July 6, 2026 10:41
…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 byroot merged commit b136b7f into msgpack:master Jul 6, 2026
19 checks passed
@Watson1978

Copy link
Copy Markdown
Contributor Author

Thanks

@Watson1978 Watson1978 deleted the fix-recursive-ext-stack-overflow branch July 6, 2026 06:43
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