Repository navigation
Keep the inflated member alive in GzipReader.zcat - #191
Open
vytautaskubilius wants to merge 1 commit into
Open
vytautaskubilius wants to merge 1 commit into
vytautaskubilius wants to merge 1 commit into
Conversation
Zlib::GzipReader.zcat copied each inflated member into the result with rb_str_cat, but nothing referenced the member string after its pointer and length were read, so a GC triggered while growing the result could free it in the middle of the copy. With members of tens of MB this crashed in memcpy, or returned data of the right length that was all NUL bytes or otherwise corrupted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mame
approved these changes
Nov 17, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Zlib::GzipReader.zcatcopies each inflated member into the result withrb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf)), but nothing referencestmpbufafter its pointer and length are read. Growingbufcan trigger a GC, which can then freetmpbufin the middle of the copy. Depending on what happens to the freed memory, this either crashes inmemcpy(viastr_buf_cat4), or returns data of the right length that is all NUL bytes or otherwise corrupted.We ran into this in production after
faraday-gzip3.2.0 switched fromGzipReader#readtoGzipReader.zcat- a ~23MB gzip HTTP response decompressed into ~31MB of NUL bytes, whileGzipReader#read,Zlib::InflateandZlib.gunzipreturned the correct data for the same bytes in the same process. Something along these lines reproduces it inruby:3.4.12-slim-trixie(aarch64, zlib 1.3.1), though not on every run:Running 12 cases like this (1-31MB, random and text payloads, written in one go and with periodic sync flushes) three times, the current code crashed or returned corrupted data in every round, and with this change it didn't once. I couldn't turn it into a deterministic test - I tried
EnvUtil.under_gc_stresswith a 1MB member and it passes without the fix too, I think because the freed buffer has to be large enough to be unmapped or reused before the bad copy becomes visible.I reported this to security@ruby-lang.org first, and they're treating it as a regular bug.
🤖 Generated with Claude Code