Repository navigation
Description
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 bytesNo 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 ASanWRITE of size 16336on 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
andFiletargets): no ASan reports. test/jsonon 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
byroot commentedon Nov 27, 2026
Gems like
jsonhave 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.imoshkinlocal commentedon Nov 27, 2026
Thanks — that's a fair point about extraction, and
gem install json -v 2.19.9is 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:
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
fbufferfix would land there too. (Side note: because the advisory'sfixedevent 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.3topatched_versionsso that backport is visible to scanners.)Would you consider bumping the bundled
ext/jsononruby_4_0instead of doing a 2.18.4?masteralready carries 3.2.0, and 2.19.9 hasrequired_ruby_version >= 2.7, so nothing in the gemspec blocks it.ruby_4_0currently pins 2.18.3 inext/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:
jsonis a default gem installed bymake install, so we either patchext/jsoninside the ruby source package or build a newerjsonas 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 thecapa != 0IO buffer path) — but they are distro-specific, andgem installat 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 againstruby_4_0(for the bundled copy) — whichever you consider useful.byroot commentedon Nov 27, 2026
Just "not required", I did the .2 and .3 backport because someone asked nicely.
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.4and update ruby 4.0, but the base isn't2.x-stable.imoshkinlocal commentedon Nov 27, 2026
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 installat 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.3tag (not2.x-stable, as you said — that is 2.21.2 now) to make sure the base works:The IO path grows
capatoFBUFFER_IO_BUFFER_SIZEwithout flushing, then returns ifrequestedfits incapaalone —fb->lenis not subtracted, so the next write goes past the buffer. On thev2.18.3base,JSON.dump([1]*7 + ["a"*16380], StringIO.new)aborts withmalloc(): 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?
v2.18.3— tell me the name you want (2-18-sec?), orIf 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.
byroot commentedon Nov 27, 2026
2.18.4 released, Ruby backport request opened: ruby/ruby#20257
imoshkinlocal commentedon Nov 27, 2026
Sorry to reopen this, but 2.18.4 does not actually fix CVE-2026-54696.
The change at
ext/json/ext/fbuffer/fbuffer.h:106only turns<into<=, so it coversrequested == fb->capabut still does not subtractfb->len. In the IO path,fbuffer_do_inc_capagrowscapatoFBUFFER_IO_BUFFER_SIZE(16383) without flushing and then returns ifrequested <= capa— anyrequestedin[capa - len, capa]still writes past the buffer. The original PoC sits exactly in that range:search_flushhands 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:
And with ASan, built from the
v2.18.4tag:The correct early-return is the negation of the check that
fbuffer_inc_capaalready does at line 129:With only that expression changed, the same ASan build survives five iterations of the PoC, and the suite on
v2.18.4is 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.byroot commentedon Nov 27, 2026
Urgh.. alright I've cut a 2.18.5 with the proper fix.