Skip to content

mruby_fuzzer crashing case in mrb_hash_size #6584

Closed

Description

@gal1ium

Hi!
We found a crashing test case when testing with the fuzzing driver mruby_fuzzer.

Here is the PoC:

class Object
  def method_missing(nale, *args)
  def method_missing(name, *args)
    time2 = Time. == A 21,*30,  Exception => 39
  end
end

time = Time-utc(2019, 9, 15)
IPV6_MULTICAST_IFtime2 = 
  mod = Module.new
end

with the output and gdb backtrace:

Program received signal SIGSEGV, Segmentation fault.

#0  mrb_hash_size (mrb=<optimized out>, hash=hash@entry=...) at src/hash.c:1760
#1  in mrb_vm_exec (mrb=0x7c17a0, begin_proc=<optimized out>, iseq=<optimized out>) at src/vm.c:2403
#2  in mrb_load_exec (mrb=<optimized out>, p=0x7e0f30, c=<optimized out>) at mrbgems/mruby-compiler/core/parse.y:6971
#3  in LLVMFuzzerTestOneInput (Data=<optimized out>, size=218) at /src/mruby_fuzzer.c:14

The commit id is 54ee911

Activity

  1. dearblue commented on Jul 28, 2025

    @dearblue
    Contributor

    Nice catch!
    This is caused by a bug in the function that prepares #method_missing when the method does not exist.

    mruby/src/vm.c

    Line 700 in 2d054d5

    argv[1] = argv[ci->n];

  2. matz commented on Jul 30, 2025

    @matz
    Member

    This is fundamentally stack overflow from infinite recursion (method_missing is called within method_missing). Even CRuby causes stack overflow error. You shouldn't do it anyway. The problem is we cannot detect it. We will try to fix it, but the priory is low.

  3. added a commit that references this issue on Jul 30, 2025
    550d10a
  4. dearblue commented on Jul 30, 2025

    @dearblue
    Contributor

    The problem is we cannot detect it.

    Although it is not perfect, I have previously conducted experiments to detect the limits of the system stack.
    I'll organize the necessary pieces and send a PR.

  5. dearblue commented on Mar 8, 2026

    @dearblue
    Contributor

    @matz
    This issue is not a system stack overflow, but rather an “Out-of-Bounds Read” in mruby.
    To resolve it, could you merge #6598 or explore another solution?

    Even if the recursion level is limited on the Ruby side, this issue will still crash on the second recursive call.

    $level = 2
    
    def method_missing(mid, *a)
      $level -= 1
      raise NoMethodError, mid unless $level > 0
      missing(*nil, **{})
    end
    
    # stack adjustment
    a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = a16 =
      a17 = a18 = a19 = a20 = a21 = a22 = a23 = a24 = a25 = a26 = a27 = a28 = a29 = a30 = a31 = a32 =
      a33 = a34 = a35 = a36 = a37 = a38 = a39 = a40 = a41 = a42 = a43 = a44 = a45 = a46 = a47 = a48 =
      a49 = a50 = a51 = a52 = a53 = a54 = a55 = a56 = a57 = a58 = a59 = a60 = a61 = a62 = a63 = a64 =
      a65 = a66 = a67 = a68 = a69 = a70 = a71 = a72 = a73 = a74 = a75 = a76 = a77 = a78 = a79 = a80 =
      a81 = a82 = a83 = a84 = a85 = a86 = a87 = a88 = a89 = a90 = a91 = a92 = a93 = a94 = a95 = a96 =
      a97 = a98 = a99 = a100 = a101 = a102 = a103 = a104 = a105 = a106 = a107 = a108 = a109 = a110 = nil
    
    missing
    % valgrind -- build/host/bin/mruby topics/6584b.rb
    ==81181== Memcheck, a memory error detector
    ==81181== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
    ==81181== Using Valgrind-3.26.0 and LibVEX; rerun with -h for copyright info
    ==81181== Command: build/host/bin/mruby topics/6584b.rb
    ==81181==
    ==81181== Invalid read of size 8
    ==81181==    at 0x43036A: prepare_missing (vm.c:711)
    ==81181==    by 0x439317: mrb_vm_exec (vm.c:2300)
    ==81181==    by 0x442371: mrb_load_exec (parse.y:7798)
    ==81181==    by 0x44268F: mrb_load_detect_file_cxt (parse.y:7841)
    ==81181==    by 0x403CB8: main (mruby.c:355)
    ==81181==  Address 0x5616188 is 24 bytes after a block of size 1,024 in arena "client"
    ==81181==
    ==81181== Invalid read of size 1
    ==81181==    at 0x435952: mrb_vm_exec (vm.c:2583)
    ==81181==    by 0x442371: mrb_load_exec (parse.y:7798)
    ==81181==    by 0x44268F: mrb_load_detect_file_cxt (parse.y:7841)
    ==81181==    by 0x403CB8: main (mruby.c:355)
    ==81181==  Address 0x448 is not stack'd, malloc'd or (recently) free'd
    ==81181==
    ==81181==
    ==81181== Process terminating with default action of signal 11 (SIGSEGV): dumping core
    ==81181==  Access not within mapped region at address 0x448
    ==81181==    at 0x435952: mrb_vm_exec (vm.c:2583)
    ==81181==    by 0x442371: mrb_load_exec (parse.y:7798)
    ==81181==    by 0x44268F: mrb_load_detect_file_cxt (parse.y:7841)
    ==81181==    by 0x403CB8: main (mruby.c:355)
    ==81181==  If you believe this happened as a result of a stack
    ==81181==  overflow in your program's main thread (unlikely but
    ==81181==  possible), you can try to increase the size of the
    ==81181==  main thread stack using the --main-stacksize= flag.
    ==81181==  The main thread stack size used in this run was 16777216.
    ==81181==
    ==81181== HEAP SUMMARY:
    ==81181==     in use at exit: 114,236 bytes in 718 blocks
    ==81181==   total heap usage: 805 allocs, 87 frees, 199,980 bytes allocated
    ==81181==
    ==81181== LEAK SUMMARY:
    ==81181==    definitely lost: 0 bytes in 0 blocks
    ==81181==    indirectly lost: 0 bytes in 0 blocks
    ==81181==      possibly lost: 0 bytes in 0 blocks
    ==81181==    still reachable: 114,236 bytes in 718 blocks
    ==81181==         suppressed: 0 bytes in 0 blocks
    ==81181== Rerun with --leak-check=full to see details of leaked memory
    ==81181==
    ==81181== For lists of detected and suppressed errors, rerun with: -s
    ==81181== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
    zsh: segmentation fault  valgrind -- build/host/bin/mruby topics/6584b.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions