Skip to content
DoS Disclosed report Beginner

Null pointer dereference in mrb_str_prepend

A null pointer dereference occurs in mruby's String#prepend method when called on an empty string with an empty argument, causing a segmentation fault. An attacker could crash the interpreter by executing this simple Ruby expression.

tunz Follow

Originally disclosed on HackerOne

Bounty$800 View original
  • Triaged in 20 days
  • Fixed in 33 days
  • Paid in 47 days
  • Disclosed after 48 days

PoC

Ruby
String.new.prepend("")

Cause

This crash is caused by null dereference in https://github.com/mruby/mruby/blob/master/mrbgems/mruby-string-ext/src/string.c#L474

Test

$ gdb -q --args ./bin/mruby test4.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test4.rb

Program received signal SIGSEGV, Segmentation fault.
0x0000000000464553 in mrb_str_prepend (mrb=0x1538010, self=...) at /home/tunz/working/mruby/mruby/mrbgems/mruby-string-ext/src/string.c:474
474       RSTR_PTR(s1)[len] = '\0';

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

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 4 min read.

What the report shows

The target is mruby, the embeddable Ruby implementation, and specifically the string extension gem mruby-string-ext. The researcher found that a single call on an empty string crashes the interpreter with a segmentation fault.

The proof of concept is one line of Ruby:

Ruby
String.new.prepend("")

Calling prepend with an empty string on a freshly created, empty String is enough to bring the process down. The report attributes the crash to a null dereference in the mrb_str_prepend function, pointing at line 474 of mrbgems/mruby-string-ext/src/string.c in the mruby repository.

To confirm the location, the researcher ran the interpreter under gdb:

$ gdb -q --args ./bin/mruby test4.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test4.rb

Program received signal SIGSEGV, Segmentation fault.
0x0000000000464553 in mrb_str_prepend (mrb=0x1538010, self=...) at /home/tunz/working/mruby/mruby/mrbgems/mruby-string-ext/src/string.c:474
474       RSTR_PTR(s1)[len] = '\0';

The backtrace lands exactly on the line the report names. The faulting statement writes a terminating null byte through RSTR_PTR(s1) at offset len. The report does not include the surrounding function body, so the exact path that leaves s1 without a valid buffer is not shown; the line itself is the evidence.

The report was filed against the shopify-scripts program on HackerOne and paid a bounty of 800 USD. It was disclosed on 7 February 2017. The platform classifies the weakness as uncontrolled resource consumption, though the crash described is a null pointer dereference. No CVSS score or severity rating is given.

Root cause

The report attributes the crash to a null dereference in mrb_str_prepend at line 474. When the string being prepended has length zero, the code still reaches the write of the null terminator at RSTR_PTR(s1)[len]. If the buffer pointer has not been allocated, or the string object is in a state where RSTR_PTR yields a null pointer, that write dereferences null and the process dies. The report states the null dereference directly but does not walk through the branch that produces it.

Impact

What the report demonstrates is a crash: calling prepend with an empty string on an empty String terminates the interpreter. The report does not discuss memory corruption beyond the crash, nor whether the write could be steered to an attacker-chosen address, so treat remote code execution as unproven here.

How this kind of bug works

A null pointer dereference happens when code follows a pointer that was never set, or was set to null, and the program tries to read or write through it. In memory-safe languages this is caught, but interpreters and native extensions written in C routinely manipulate raw buffers, and a missed edge case in that manipulation turns into a fault.

Empty and zero-length inputs are a classic trigger. Code that computes an offset from a length, allocates a buffer of that size, and then writes a terminator at buffer[length] can behave differently when the length is zero: the allocation may be skipped, return null, or the terminator write may land outside the intended region. The same class of mistake appears whenever a function assumes at least one byte of input.

The defences are straightforward: validate lengths before touching buffers, handle the zero-length case explicitly (often as a no-op), check allocation results, and run the interpreter under sanitizers and fuzzing so these paths are exercised. Memory-safe rewrites of the hot paths also remove the class entirely.

How to test for it

  1. Identify interpreter or extension functions that take string arguments and manipulate their buffers, especially concatenation, prepend, slicing and formatting routines.
  2. Feed them degenerate inputs: empty strings, strings of length zero after encoding conversion, null bytes, and combinations where one operand is empty and the other is not.
  3. Run the interpreter under a debugger or with sanitizers so a fault gives you a stack trace and a source line rather than a silent exit.
  4. A positive result is a reproducible crash whose trace points into the suspect function, ideally at a specific line you can quote.
  5. Common false positives: crashes caused by your own harness or by invalid byte sequences the API documents as unsupported, and faults that only occur with debug builds. Confirm the input is valid for the API before reporting.

How it is fixed

Hunting notes

This class hides in native extensions and interpreter internals: string, array and encoding libraries, parsers, and anything that computes offsets from lengths. Recon signals include projects with C extensions, recent refactors of buffer handling, and APIs that accept user-controlled strings. To get a report accepted, show a minimal reproduction and the exact faulting line from a debugger.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 6 min read.

What happened, in plain words

mruby is a small, embeddable version of the Ruby programming language. "Embeddable" means other programs can include it inside themselves to run Ruby code, much like a calculator app can be built into a larger piece of software. One of its optional add-ons, called a gem, is mruby-string-ext, which adds extra things you can do with text (strings).

A security researcher found that a single, very short line of Ruby code makes the interpreter crash. The interpreter is the program that reads and runs Ruby code.

The proof of concept (PoC), meaning the smallest piece of code that demonstrates the problem, is this:

Ruby
String.new.prepend("")

Line by line:

  • String.new creates a brand new, empty piece of text. Think of it as an empty sheet of paper.
  • .prepend("") asks Ruby to put another piece of text at the front of that sheet. The text being added is "", which is an empty string, so nothing is actually added.
  • The result should simply be an empty string. Instead, the interpreter crashes.

The report says the crash is a null dereference inside a function called mrb_str_prepend, at line 474 of the file mrbgems/mruby-string-ext/src/string.c in the mruby project on GitHub. A null dereference is explained in the next section.

To prove where the crash happens, the researcher ran the interpreter under a debugger. A debugger is a tool that lets you pause a program and inspect it while it runs. Here is the session exactly as recorded:

Text
$ gdb -q --args ./bin/mruby test4.rb
Reading symbols from ./bin/mruby...done.
(gdb) r
Starting program: /home/tunz/working/mruby/mruby/bin/mruby test4.rb

Program received signal SIGSEGV, Segmentation fault.
0x0000000000464553 in mrb_str_prepend (mrb=0x1538010, self=...) at /home/tunz/working/mruby/mruby/mrbgems/mruby-string-ext/src/string.c:474
474       RSTR_PTR(s1)[len] = '\0';

What each part means:

  • gdb -q --args ./bin/mruby test4.rb starts the debugger (gdb) quietly (-q) and tells it to run the mruby program with the file test4.rb as its input.
  • r is the debugger command to run the program.
  • Program received signal SIGSEGV, Segmentation fault means the operating system stopped the program because it tried to touch memory it is not allowed to touch. This is the crash.
  • The next line shows the function where it happened (mrb_str_prepend) and the exact source file and line number (474).
  • The last line is the source code at that line: RSTR_PTR(s1)[len] = '\0';. It writes a special end-of-text marker (the '\0', a null byte) into a memory buffer.

This matters because it confirms the crash is not random: it lands exactly on the line the report names. The report does not include the rest of the function, so it does not show the exact route that leaves the buffer unusable. The faulting line is the evidence given.

The report was submitted to the shopify-scripts program on HackerOne, a platform where researchers report bugs to companies. It earned a bounty of 800 USD and was disclosed on 7 February 2017. The platform labels the weakness as uncontrolled resource consumption, although the crash described is a null pointer dereference. No severity rating or CVSS score is given.

In general, how this kind of bug works

A null pointer dereference happens when a program tries to use a pointer that points to nothing. A pointer is a memory address, like a house number telling the program where some data lives. "Null" means the address is empty, like being told to deliver a parcel to no address at all. When the program tries to read or write there, the operating system stops it, and the program crashes.

Languages like Ruby normally protect programmers from this, but interpreters and their add-ons are often written in C, which works directly with raw memory. A missed edge case in that low-level code becomes a crash.

Empty inputs are a classic trigger. Code often works out a size from a length, reserves memory of that size, then writes an end marker just after the data. When the length is zero, the reservation may be skipped or come back empty, and the write goes to a place that does not exist. The same mistake appears whenever a function assumes there is at least one character of input.

The usual defences are simple in principle: check lengths before touching memory, handle the zero-length case explicitly (often by doing nothing and returning early), check that memory was actually reserved, and run the interpreter under tools that catch these faults, plus fuzzing. Fuzzing means feeding a program large numbers of strange inputs automatically to see what breaks.

How to test for it

  1. Find functions in the interpreter or its add-ons that take text and work on its memory, especially joining, prepending, slicing and formatting.
  2. Try degenerate inputs: empty strings, strings that become empty after a conversion, strings containing null bytes, and cases where one side is empty and the other is not.
  3. Run the interpreter under a debugger or with memory-checking tools, so a fault gives you a stack trace and a source line instead of a silent exit.
  4. A good result is a crash you can reproduce, whose trace points into the suspect function, ideally at a line you can quote.
  5. Watch for false positives: crashes caused by your own test setup, inputs the documentation says are unsupported, or faults that only appear in special builds. Confirm the input is valid before reporting.

How it is fixed

Hunting notes

This kind of bug hides in add-ons and interpreter internals: text, array and encoding libraries, parsers, and anything that computes positions from lengths. Useful signals include projects with C add-ons, recent changes to memory handling, and interfaces that accept text from users. To get a report accepted, show a minimal reproduction and the exact faulting line from a debugger.

Glossary

  • mruby: a small, embeddable implementation of the Ruby programming language.
  • Gem: an optional add-on that extends what a Ruby implementation can do.
  • Interpreter: a program that reads and runs code written in a language such as Ruby.
  • String: a piece of text in programming.
  • Proof of concept (PoC): the smallest piece of code that demonstrates a bug.
  • Null dereference: a crash caused by using a pointer that points to no valid memory address.
  • Pointer: a memory address that tells a program where data is stored.
  • Segmentation fault (SIGSEGV): the operating system stopping a program that touched memory it is not allowed to touch.
  • Debugger: a tool that lets you pause and inspect a running program.
  • gdb: a common debugger used on Linux and similar systems.
  • Stack trace / backtrace: the list of function calls that led to a point in a program, used to locate a crash.
  • Buffer: a block of memory used to hold data such as text.
  • Null byte: a byte with value zero, often used to mark the end of text in C.
  • Denial of service: making a program or service unavailable, for example by crashing it.
  • HackerOne: a platform where security researchers report bugs to companies.
  • Bounty: a payment made to a researcher for a valid bug report.
  • CVSS: a standard scoring system for rating the severity of a vulnerability.
  • Fuzzing: automatically feeding a program many strange inputs to find crashes.
  • Sanitizer: a tool that detects memory errors while a program runs.

More by tunz

View all

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