[MEDIUM] Validate Struct backing memory during initialization - #1201
Open
OskarEichler wants to merge 1 commit into
Open
OskarEichler wants to merge 1 commit into
OskarEichler wants to merge 1 commit into
Conversation
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
FFI::Structduring construction.Struct#pointer=and remove its narrowing cast.Issue
Struct#pointer=checks that the supplied memory covers the layout, butStruct#initialize(pointer)did not. A shortMemoryPointer,Buffer, or bounded pointer could therefore back a larger struct.Most field accessors perform their own bounds checks. Some whole-struct paths do not:
Struct#initialize_copycopies the full layout size, and a struct returned by value from a callback is also copied at the declared native size. A short input can therefore cause an out-of-bounds read and copy adjacent process memory.I classify this as medium urgency when an FFI wrapper maps untrusted, variable-length input into a fixed struct. Code that controls both the layout and pointer remains the common case, which limits exposure.
Reproduction
On ffi 1.17.4 and current
master, construction accepts the undersized allocation:Duplicating that object subsequently reaches an eight-byte
memcpyfrom the one-byte allocation instruct_initialize_copy.After this change, construction raises:
Relationship to #1193
#1193 adds checks to
AbstractMemory#__copy_from__, including the nested-struct assignment caller. This change complements that sink check: it enforces the existing struct backing-memory invariant at construction and covers other whole-struct consumers, including duplication and callback returns.JRuby already rejects an undersized struct at construction, as documented in #1193.
Verification
rbenv exec bundle exec rspec spec/ffi/struct_spec.rb: 148 examples, 0 failures.rbenv exec bundle exec rspec: 5,063 examples, 0 failures.git diff --checkpasses.Limitations
LONG_MAXsize and remain intentionally unchecked because FFI cannot infer their allocation extent.Breaking changes
Code that intentionally constructs a struct over a bounded pointer shorter than its declared layout now receives
ArgumentError. Such an object could not safely support all struct operations; this aligns construction with the existingpointer=behavior and JRuby.