RUBY-3893 Fix heap use-after-free in put_array#371
Merged
Conversation
RARRAY_PTR is unsafe to hold across any call that may invoke the Ruby runtime or GC. The loop body called rb_funcall twice per element (for bson_type and to_bson), so a callback that mutated the array and triggered GC compaction could free the backing store while the cached pointer was still in use. Replace the raw pointer walk with rb_ary_entry, which re-derives the element from the live array object on every access. Also snapshot the array length before the loop and raise RuntimeError if it changes, rather than silently producing corrupted BSON output.
There was a problem hiding this comment.
Pull request overview
This PR hardens BSON array serialization in the native extension by eliminating an unsafe cached RARRAY_PTR traversal that could become dangling across Ruby callbacks/GC, and adds a spec to verify the new failure mode when an array is mutated during serialization.
Changes:
- Rework
rb_bson_byte_buffer_put_arrayto fetch each element withrb_ary_entryrather than walking a cached backing pointer. - Snapshot the initial array length and raise if the array size changes mid-serialization.
- Add an RSpec example that mutates the array size during element serialization and asserts an exception is raised.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
ext/bson/write.c |
Makes C-level array serialization resilient to Ruby callbacks/GC by avoiding cached backing pointers and detecting size mutations. |
spec/bson/array_spec.rb |
Adds regression coverage for array-size mutation during serialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Keep original_len as long and raise RangeError for arrays exceeding INT32_MAX, preventing truncation from silently corrupting the modification check. Mark element as volatile VALUE so the conservative GC can see it on the C stack during ALLOC_N calls in pvt_put_array_index.
comandeo-mongo
approved these changes
Jun 8, 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.
Description
rb_bson_byte_buffer_put_arraycached the array's raw backing pointer viaRARRAY_PTRbefore the loop, then calledrb_funcalltwice per element (bson_typeandto_bson). Either callback could execute arbitrary Ruby, trigger GC compaction, and free the old backing store — leaving a dangling pointer that subsequent iterations dereferenced. AddressSanitizer confirms heap-use-after-free atwrite.c:665.rb_ary_entry, which re-derives each element from the live array object and is safe across Ruby callbacks.RuntimeErrorif it changes mid-iteration, rather than silently producing corrupted BSON output.