Stop write_skippable_frame reading past the end of its input - #144
Open
jeremy wants to merge 1 commit into
Open
Conversation
dst_size is input_size + ZSTD_SKIPPABLEHEADERSIZE + skip_size, but input_data holds only input_size bytes, so rb_str_new(input_data, dst_size) reads (8 + skip_size) bytes past the end of the argument's buffer. The copied bytes never survive: ZSTD_writeSkippableFrame overwrites the frame at offset 0 and rb_str_resize then truncates the result to output_size. So the buffer only ever needed to be allocated, not initialised -- which is what rb_read_skippable_frame does a few lines below.
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.
ext/zstdruby/skippable_frame.c:dst_sizeis the destination size, butinput_dataholds onlyinput_sizebytes — so this copies8 + skip_sizebytes past the end of the first argument's String buffer.Why removing the copy is safe
The copied bytes never survive.
ZSTD_writeSkippableFramewrites the frame at offset 0, andrb_str_resize(output, output_size)then truncates the result to8 + skip_size. Soinput_value's contents are discarded either way — only its length participates, viadst_size.rb_str_new(NULL, dst_size)allocates the same buffer without reading anything, which is exactly whatrb_read_skippable_framedoes a few lines below.Reproducing
Public API only, no GC involved — a plain out-of-bounds read:
Graded by over-read size, 3/3 each, both builds made in the same step that ran the test:
The threshold is the point at which the read leaves the mapped region — smaller over-reads silently return adjacent heap, which is why this hasn't shown up as a crash before. Ruby 4.0.6, in a container.
Unrelated observation, not changed here
While writing the test I noticed the return value doesn't contain the compressed data, despite the README naming it
compressed_data_with_skippable_frame:So the frame replaces the input rather than being prepended to it. That may well be intended — I've left it alone, since changing it would be a behaviour change rather than a memory-safety fix. Flagging it in case the README is what's wrong.
Found while sweeping native gems for dangling pointers and out-of-bounds reads.