Skip to content

2.18.3 still ships the CVE-2026-54696 heap overflow (Ruby 4.0.10 bundles it) — is a 2.18.4 backport planned? #1151

Done

Description

@imoshkinlocal

Summary

CVE-2026-54696 (heap buffer overflow in the JSON generator when streaming to
an IO) is fixed only in 2.19.9, but 2.18.3 still contains the vulnerable
code
— and 2.18.3 was released on 2026-11-13, i.e. after 2.19.9
(2026-06-11).

Consequence: Ruby 4.0.10 ships json 2.18.3 (ext/json/fbuffer/fbuffer.h in
ruby_4_0 is byte-identical to the 2.18.3 gem, sha256
21441869ea8e...a7d6), so the current stable Ruby is vulnerable to a
1-byte heap overflow that aborts the process.

Is 2.18.x still maintained? If yes, could a 2.18.4 be cut with the fix?
If not, that's fine — Ruby just needs to sync ext/json to 2.19.9+ for 4.0.x,
but I'd like to know which way to push.

Reproducer

# json_54696_poc.rb
require "json"
require "stringio"

io = StringIO.new
JSON.dump([1] * 7 + ["a" * 16_380], io)  # 1 byte past the 16 383-byte buffer
puts "survived: #{io.string.bytesize} bytes"
$ gem install json -v 2.18.3
$ ruby json_54696_poc.rb
malloc(): corrupted top size
Aborted (core dumped)          # exit 134

$ gem install json -v 2.19.9
$ ruby json_54696_poc.rb
survived: 16398 bytes

No sanitizer needed; n=7 (the smallest overflow, 1 byte) is already enough to
corrupt the malloc chunk header and abort. JSON::State#generate(obj, io) is
affected the same way; JSON.generate(obj) without an IO is not.

On Ruby 4.0.10 with a full ASan build of the tree:

==378931==ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 16368 at 0x5290000221ff thread T0
    #1 ruby_nonempty_memcpy
    #2 fbuffer_append_reserved      ext/json/fbuffer/fbuffer.h:136
    #5 search_flush                 ext/json/generator/generator.c:142
    #6 copy_remaining_bytes         ext/json/generator/generator.c:294
    #7 search_escape_basic_sse2     ext/json/generator/generator.c:486
    #8 convert_UTF8_to_JSON         ext/json/generator/generator.c:234
    #9 raw_generate_json_string     ext/json/generator/generator.c:1141
   #10 generate_json_array          ext/json/generator/generator.c:1319
0x5290000221ff is located 0 bytes to the right of 16383-byte region

Cause

In 2.18.3 ext/json/ext/fbuffer/fbuffer.h:

static void fbuffer_do_inc_capa(FBuffer *fb, unsigned long requested)
{
    if (RB_UNLIKELY(fb->io)) {
        if (fb->capa < FBUFFER_IO_BUFFER_SIZE) {
            fbuffer_realloc(fb, FBUFFER_IO_BUFFER_SIZE);   /* 512 -> 16383, len stays 16 */
        } else {
            fbuffer_flush(fb);
        }

        if (RB_LIKELY(requested < fb->capa)) {             /* ignores fb->len */
            return;
        }
    }
    ...

The buffer starts with the 512-byte stack buffer; the first growth reallocates
to 16 383 bytes without flushing, and the early return compares requested
with the total capacity instead of the remaining room. The SIMD tail of a
long string is passed as one 16 368-byte chunk, so with 16 bytes already in
the buffer 16 + 16368 > 16383.

Fixes tested

Minimal backport for 2.18.x (one line, keeps the existing structure):

-        if (RB_LIKELY(requested < fb->capa)) {
+        if (RB_LIKELY(requested <= fb->capa - fb->len)) {
             return;
         }

Upstream design (what 2.19.9 has, and ruby/ruby master):
fb->capa != 0 → flush, then compare; grow capacity from requested + fb->len
(json commit 996bac6 / ruby/ruby 9ac37cd626).

I tested both against Ruby 4.0.10 sources (i.e. the 2.18.3 code) on a full
ASan build:

  • PoCs above + a sweep of 32 (length 16 350…16 400 × padding 7…240)
    combinations: pre-fix ASan WRITE of size 16336 on the first one; both
    fixes clean, sweep ok: 32 combinations.
  • 3.96M fuzz iterations on the one-line fix, 7 005 IO-generator iterations on
    the upstream-style backport (strings near 16 KB, escapes/UTF-8, StringIO
    and File targets): no ASan reports.
  • test/json on the patched tree: 224 tests, 3 147 assertions, 0 failures.

I can send either variant as a PR to the 2.18.3 tag (or a patch file) if you
prefer — just say which one.

Changelog nit

CHANGES.md in 2.18.3 lists the format-string fix (GHSA-3m6g-2423-7cp3)
under the 2026-11-02 (2.18.2) heading, but the 2.18.2 tarball does not
contain it — JSON.parse('{"%s%s%s%s":1,"%s%s%s%s":2}', allow_duplicate_key: false)
still segfaults on 2.18.2 and only 2.18.3 raises JSON::ParserError
(filed rubysec/ruby-advisory-db#1344 to add 2.18.3 as a patched version).
Probably worth moving that entry to the 2.18.3 section.

Activity

  1. byroot commented on Nov 27, 2026

    @byroot
    Member

    Consequence: Ruby 4.0.10 ships json 2.18.3

    Gems like json have been extracted so people can upgrade independently. 1.8.2 and 1.8.3 backport have been done mostly for convenience but weren't strictly required by Ruby's security policy.

  2. imoshkinlocal commented on Nov 27, 2026

    @imoshkinlocal
    Author

    Thanks — that's a fair point about extraction, and gem install json -v 2.19.9 is indeed the first-line fix for a stock Ruby 4.0.10.

    Two things I'd like to settle, because they decide what a distribution package has to do:

    1. Is a 2.18.4 backport actually excluded, or just "not required"? 2.18.3 already carries two security backports — CVE-2026-33210 in 2.18.2 and GHSA-cjpr-jw2g-fw99 in 2.18.3 — so the branch is being maintained for security fixes. That's why I assumed the fbuffer fix would land there too. (Side note: because the advisory's fixed event is only 2.19.2, 2.18.3 is still flagged for CVE-2026-33210; PR json CVE-2026-33210: add 2.18.3 to patched_versions rubysec/ruby-advisory-db#1344 adds ~> 2.18.3 to patched_versions so that backport is visible to scanners.)

    2. Would you consider bumping the bundled ext/json on ruby_4_0 instead of doing a 2.18.4? master already carries 3.2.0, and 2.19.9 has required_ruby_version >= 2.7, so nothing in the gemspec blocks it. ruby_4_0 currently pins 2.18.3 in ext/json/lib/json/version.rb, so every 4.0.x tarball ships the vulnerable generator — and 4.0 is a supported stable branch, so this seems worth fixing in the branch rather than asking every consumer to override the default gem.

    Overriding is not free for a distribution: json is a default gem installed by make install, so we either patch ext/json inside the ruby source package or build a newer json as a separate package and add a dependency. Both are doable — I already have a 111-line fbuffer backport that makes 4.0.10 survive the reproducer (it takes the 2.19.9 sizing, needed_capa = requested + fb->len, while keeping the capa != 0 IO buffer path) — but they are distro-specific, and gem install at build time is not an option.

    If the answer to (1) is "excluded by policy" and to (2) is "no", just say so explicitly and I'll close this, carry the patch downstream and cite the thread. If either is open, I can send the fbuffer patch as a PR against 2.x-stable (for a 2.18.4) or against ruby_4_0 (for the bundled copy) — whichever you consider useful.

  3. byroot commented on Nov 27, 2026

    @byroot
    Member
    1. Is a 2.18.4 backport actually excluded, or just "not required"?

    Just "not required", I did the .2 and .3 backport because someone asked nicely.

    Would you consider bumping the bundled ext/json on ruby_4_0 instead of doing a 2.18.4?

    I'd rather avoid it, it's really confusing when the embedded version of a gem claims a version but is actually a weird mis-match. If I have to backport this I'd rather do it well.

    So if it's causing you annoyance, I can release a 2.18.4 and update ruby 4.0, but the base isn't 2.x-stable.

  4. imoshkinlocal commented on Nov 27, 2026

    @imoshkinlocal
    Author

    Yes, please — a 2.18.4 would genuinely help, and your point about the embedded version is right: a tree that claims 2.19.9 but is a patched 2.18 is exactly the mismatch you describe. Let's drop that suggestion.

    Why it matters here: 4.0.x tarballs carry 2.18.3, distributions build from the tarball (gem install at build time is not an option), and scanners keep flagging 2.18.x because the advisory's fixed version is 2.19.9. A real 2.18.4 fixes both the bundled copy and the branch.

    I put a fix on top of the v2.18.3 tag (not 2.x-stable, as you said — that is 2.21.2 now) to make sure the base works:

    --- a/ext/json/ext/fbuffer/fbuffer.h
    +++ b/ext/json/ext/fbuffer/fbuffer.h
    @@
    -        if (RB_LIKELY(requested < fb->capa)) {
    +        if (RB_LIKELY(requested <= fb->capa - fb->len)) {
                 return;
             }

    The IO path grows capa to FBUFFER_IO_BUFFER_SIZE without flushing, then returns if requested fits in capa alone — fb->len is not subtracted, so the next write goes past the buffer. On the v2.18.3 base, JSON.dump([1]*7 + ["a"*16380], StringIO.new) aborts with malloc(): invalid size (unsorted); with the one-liner it survives, and the full suite is 234 tests / 2742 assertions / 0 failures (Ruby 3.1.2).

    I also have the fuller 2.19.9-style backport (fbuffer_init + doubling realloc, 111 lines) that passes the same suite, but for a point release I'd rather send the one-liner: it fixes the invariant instead of redesigning the buffer.

    How would you like it delivered?

    • a PR against a branch cut from v2.18.3 — tell me the name you want (2-18-sec?), or
    • the commit pasted here, for you to apply the way you did for .2 and .3.

    If you'd rather not cut 2.18.4, say so and I'll close this and carry the patch downstream — but then every 4.0.x consumer inherits the same problem. If you do cut it, I'm happy to validate the release candidate on Ruby 4.0.10 and 3.1.2 before you publish.

  5. byroot commented on Nov 27, 2026

    @byroot
    Member

    2.18.4 released, Ruby backport request opened: ruby/ruby#20257

  6. imoshkinlocal commented on Nov 27, 2026

    @imoshkinlocal
    Author

    Sorry to reopen this, but 2.18.4 does not actually fix CVE-2026-54696.

    The change at ext/json/ext/fbuffer/fbuffer.h:106 only turns < into <=, so it covers requested == fb->capa but still does not subtract fb->len. In the IO path, fbuffer_do_inc_capa grows capa to FBUFFER_IO_BUFFER_SIZE (16383) without flushing and then returns if requested <= capa — any requested in [capa - len, capa] still writes past the buffer. The original PoC sits exactly in that range: search_flush hands over a 16368-byte chunk while the buffer already holds at least 16 bytes of JSON, so the < → <= change does not affect it at all.

    Reproduced on the released gem, not a local build:

    $ gem install json -v 2.18.4
    $ ruby -e 'gem "json","2.18.4"; require "json"; puts JSON::VERSION; require "stringio"; JSON.dump([1]*7 + ["a"*16380], StringIO.new)'
    2.18.4
    before
    malloc(): invalid size (unsorted)     # rc=134

    And with ASan, built from the v2.18.4 tag:

    ERROR: AddressSanitizer: heap-buffer-overflow
    WRITE of size 16368
      #3 fbuffer_append_reserved ../fbuffer/fbuffer.h:136
      #5 search_flush generator.c:142
      #9 raw_generate_json_string generator.c:1141
    

    The correct early-return is the negation of the check that fbuffer_inc_capa already does at line 129:

    /* fbuffer_inc_capa, line 129 */
    if (RB_UNLIKELY(requested > fb->capa - fb->len)) { ... }
    
    /* so the IO branch should be */
    if (RB_LIKELY(requested <= fb->capa - fb->len)) {
        return;
    }

    With only that expression changed, the same ASan build survives five iterations of the PoC, and the suite on v2.18.4 is 234 tests / 2742 assertions / 0 failures.

    This also affects ruby/ruby#20257, which carries the same one-line diff — if it merges as-is, 4.0.x still ships the overflow. Could you amend #20257 and cut 2.18.5? I can send the corrected diff as a PR against v2.18.4 (or whatever branch you use for the 2.18 line) right away, and I'm happy to run the release candidate through the PoC and the suite before you publish.

  7. byroot commented on Nov 27, 2026

    @byroot
    Member

    Urgh.. alright I've cut a 2.18.5 with the proper fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions