Validate Buffer#read_frame argument to prevent SEGV#97
Merged
Conversation
read_frame passed its data argument straight to buffer_read_frame(), which appends to it via rb_str_cat() without any type check. A non-String argument was dereferenced as a char* and crashed the VM instead of raising an error. Coerce data with StringValue() and reject frozen strings with rb_str_modify(), matching the validation already done by #append and #prepend. Add specs covering non-String, frozen String, and to_str coercion cases.
55f83ff to
8d1b207
Compare
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.
Summary
Buffer#read_framepassed itsdataargument straight to the internalbuffer_read_frame(), which appends to it viarb_str_cat()without any type check. Whendatais not a String, the immediate VALUE is dereferenced as achar*insiderb_str_cat, crashing the whole VM with a SEGV instead of raising a Ruby exception.Every other entry point (
#append,#prepend) already coerces its argument to a String, soread_framewas the only unguarded path.Fix
Coerce
datawithStringValue()and reject frozen strings withrb_str_modify()(sincedatais used as an output buffer that gets written to). This turns the crash into a properTypeError/FrozenError, matching the behavior of#appendand#prepend.No compatibility impact: existing callers that pass a valid mutable String are unaffected; only misuse is now raised as an exception.
Tests
Added specs to
spec/iobuffer_spec.rbcovering:TypeError(no more SEGV)FrozenError#to_strare coerced correctlyAll buffer specs pass locally.