Skip to content

Fix SEGV on stack depth underflow in recursive extension unpacking - #405

Merged
byroot merged 1 commit into
msgpack:masterfrom
Watson1978:fix/recursive-ext-stack-underflow
Sep 3, 2026
Merged

Fix SEGV on stack depth underflow in recursive extension unpacking#405
byroot merged 1 commit into
msgpack:masterfrom
Watson1978:fix/recursive-ext-stack-underflow

Conversation

@Watson1978

Copy link
Copy Markdown
Contributor

A recursive extension proc that rescues an inner read error, or that calls Unpacker#skip, can leave stack.depth at 0 before read_raw_body_begin pops the barrier it pushed. The unconditional pop then underflowed the depth to SIZE_MAX and read/wrote out-of-bounds stack entries, crashing the VM (SIGSEGV). This restores the depth to just below the pushed barrier instead of decrementing unconditionally.

Reproduction

require "msgpack"

klass = Class.new
factory = MessagePack::Factory.new
factory.register_type(0x01, klass,
  packer: ->(_obj, packer) { packer.write(nil) },
  # A recursive proc that skips its extension body.
  unpacker: ->(u) { u.skip },
  recursive: true)

# fixext1 (type = 0x01), then fixarray(1) and an integer as the body.
factory.unpack("\xd4\x01\x91\x2a".b)
puts "no crash"

Before this change the script crashes with a SIGSEGV; after it, it prints no crash.

🤖 Generated with Claude Code

A recursive extension proc that rescues an inner read error, or that
calls Unpacker#skip, drives stack.depth down to 0 before
read_raw_body_begin pops the barrier it pushed. The unconditional pop
then underflowed depth to SIZE_MAX and read/wrote out-of-bounds stack
entries, crashing the VM. Restore the depth to just below the pushed
barrier instead of decrementing unconditionally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Watson1978
Watson1978 force-pushed the fix/recursive-ext-stack-underflow branch from e7e544e to 7ff80ad Compare September 3, 2026 01:30
@byroot
byroot merged commit 4e2a755 into msgpack:master Sep 3, 2026
19 checks passed
@Watson1978
Watson1978 deleted the fix/recursive-ext-stack-underflow branch September 3, 2026 07:11
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