Repository navigation
Description
Title: [BUG] Array replacement from callbacks causes out-of-bounds reads in case splats and IO#puts
Version
Tested on current master at 77eca771af72b8ff018369b8aead3ad86e5d0195
on x86-64 Linux with Clang 18.1.3. The ASan build used the repository's
build_config/asan.rb configuration.
Summary
Two Array iteration loops save RARRAY_LEN() before invoking Ruby code but
reload RARRAY_PTR() on each iteration. If the callback replaces the same
Array with a shorter Array, later iterations use the new pointer with the old
length and read beyond the replacement allocation.
The two independently reachable sites are:
mrb_obj_ceqq()insrc/kernel.c, reached by ordinary
case value; when *patternssyntax. A pattern's===method is the
invalidating callback.io_puts_ary()inmrbgems/mruby-io/src/io.c, reached by
IO#puts(array). An element'sto_smethod is the invalidating callback.
Neither reproducer needs a C extension, malformed bytecode, allocation
failure, or a custom allocator.
Reproduction
Build the full-core sanitizer configuration:
rake MRUBY_CONFIG=build_config/asan.rbThen run the two Ruby files provided in the follow-up comment:
build/asan/bin/mruby poc/case-splat-array-replacement.rb
build/asan/bin/mruby poc/io-puts-array-replacement.rbThe case reproducer reports an eight-byte heap-buffer-overflow in
mrb_obj_ceqq() at src/kernel.c:635. The IO reproducer reports an eight-byte
heap-buffer-overflow while io_puts_ary() loads the next element at
mrbgems/mruby-io/src/io.c:1027.
In an unsanitized full-core build, each reproducer terminated with SIGSEGV in
three out of three fresh processes. Replacing the Array with another Array of
the original length is clean in both sanitizer and unsanitized builds.
Root cause
Both loops have the same lifetime pattern:
mrb_int len = RARRAY_LEN(ary);
for (mrb_int i = 0; i < len; i++) {
/* load current RARRAY_PTR(ary)[i], then invoke Ruby code */
}Array#replace can change both the pointer and length during the callback.
The next condition still admits indices below the old len, even when those
indices are outside the current owner.
Proposed fix
Keep the saved length as the upper bound so growth does not extend an
in-progress traversal, and also check the current length before each load:
i < len && i < RARRAY_LEN(ary)The two-commit series provided in the follow-up comments keeps separate source changes and regression
tests for the core and mruby-io sites.
Validation of the patched tree:
- full-core ASan/UBSan mrbtest: 2230 passed, 0 failed, 0 crashed, 2 skipped;
- full-core unsanitized mrbtest: 2222 passed, 0 failed, 0 crashed, 10 skipped;
- both positive reproducers: 3/3 clean in ASan/UBSan and 3/3 clean in the
unsanitized build; - unchanged-length callback control: clean in all tested builds.
Related reports and impact
PR #6876 is not the same issue: it caps recursion depth for cyclic Arrays in
IO#puts; it does not cover Array replacement from to_s or a stale extent.
Issue #6854 concerns case ... in pattern-matching semantics, not
case ... when *patterns callback mutation.
The demonstrated consequences are native invalid reads and repeatable process
crashes from valid Ruby code. The IO#puts case can print a value interpreted
from beyond the replacement Array before crashing, but testing has not shown a
controlled value, stable address disclosure, arbitrary read/write, or code
execution.
Activity
kimaiden1984-boop commentedon Aug 11, 2026
Complete Ruby reproducers and unchanged-length control:
case ... when *patternsIO#puts(Array)unchanged-length control
kimaiden1984-boop commentedon Aug 11, 2026
Proposed patch 1/2, including the core regression test:
kimaiden1984-boop commentedon Aug 11, 2026
Proposed patch 2/2, including the
mruby-ioregression test:mruby-io: bound the IO#puts array walk by the array's current length