Repository navigation
Fix out-of-bounds read in string query suffix check - #4322
Merged
Merged
Conversation
pm_slice_type read *source to check for a trailing !, ?, or = even when the identifier had already consumed the whole slice, reading one byte past the input. Check source < end first. Co-authored-by: Stan Lo <stan.lo@shopify.com>
kddnewton
approved these changes
Oct 30, 2026
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.
Motivation
pm_string_query_local,pm_string_query_constant, andpm_string_query_method_nameall sharepm_slice_type. After it scans the identifier, it checks for a trailing!,?, or=. When the identifier runs to the end of the slice, that check reads*sourcewithsource == end, which is one byte past the input.Prism::StringQuerynever sees this, because Ruby strings always have a readable byte after their contents. Callers that pass a slice of a larger buffer don't have that guarantee, including C callers and the RustStringQueryAPI. It shows up in two ways:StringQuery::new(&b"a!"[..1]).is_local()returnsOk(false), because the byte after the slice happens to be!.Since
StringQueryis a safe Rust API, this is also a soundness bug. It's present in v2.1.0 and earlier.Implementation
Check
source < endbefore reading the suffix character.Tests
Both new tests query a slice whose next byte is
!,?, or=. They fail without the fix and pass with it.test_does_not_read_past_sliceintest/prism/ruby/string_query_test.rbcalls the C functions directly through FFI. It only runs with the FFI backend, since the C extension always passes whole Ruby strings.subslice_testinrust/ruby-prism/src/string_query.rscovers the RustStringQueryAPI, once per query type.I also checked by hand with a small C program that places a one-byte name right before a guard page. It crashes all three APIs before the fix and returns the correct answers after.