Skip to content
Memory corruption Disclosed report Advanced

Memory disclosure in mruby String#lines method

The String#lines method cached a pointer to the string's buffer before yielding to a caller-supplied block. If that block modified or freed the string, the cached pointer became dangling and later iterations read freed heap memory, leaking pointers and object data back to the caller.

isra17 Follow

Originally disclosed on HackerOne

  • Triaged in 4 days
  • Fixed in 5 days
  • Paid in 36 days
  • Disclosed after 36 days

This bug was found with jmlb337.

Hey again, while reviewing mruby for vulnerabilities, I stumble onto a case that allow an attacker to leak heap content including pointer that can be used along another vulnerability to craft a complete exploit.

Reproduction Step

  1. Allocate a string with a few lines.
  2. Call String#lines and free or reallocate the string.
  3. Allocate a few objects.
  4. The next lines will now contains the value of the newly allocated data, including pointer used by mrb_values.

PoC

Ruby
@a = []
$a = ("a"*0xf + "\n") * 1000
$a.lines do |l|
  $a.clear
  foo = "UUUUUUUU" * 1000
  @a << l
end

Look at @a to get the "UUUU..." mrb_value object and strings.

Explaination

The bug is triggered due to the caching of p at string.c:310:

C
  char *p = RSTRING_PTR(self), *t;
  char *e = p + RSTRING_LEN(self);

However, while iterating on each line, the function allow the caller to provide a block to be called for each line string.c:324:

C
      mrb_yield_argv(mrb, blk, 1, &arg);

This block let the attacker to update the self string, in which case p will now be a dangling pointer pointing to free memory. Allocating new objects will end up in this free location and let the next iteration read this data before giving it back to the block.

Exploitability

The vulnerability is exploitable as long as the attacker can run arbitrary ruby code in the mruby interpreter. It should cover mruby-engine case as used by Shopify.

Impact

This vulnerability comes handy to locate object address in the heap, by allowing reliable, cheap and simple memory disclosure. We would use this bug to build a complete RCE along with another reported bug in the following 1 or 2 week (Will add a comment with the other report ID). I spoke with François Chagnon and we preferred to report the bugs as soon as possible while working on provable RCE afterward so it can get patched earlier.

Proposed Fix

See patch in attachment.

The original report as published on HackerOne, by isra17. 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

The method cached the string's data pointer before invoking a user block that could mutate or free the string, leaving a dangling pointer used in later iterations.

Impact

An attacker who can run Ruby code in the interpreter could reliably and cheaply disclose heap contents, including object addresses, which could be combined with another bug to build a full exploit.

What the attacker needed

Ability to run Ruby code in mruby

The fix

The method should not cache the string's data pointer across the block call, or should re-read it after the block runs; a patch was attached but the text does not confirm it was applied.

How it was found

  1. Allocate a string containing several lines.
  2. Call String#lines with a block that clears or reallocates the string.
  3. Allocate new objects so they reuse the freed memory.
  4. Observe that later lines contain the newly allocated data, including pointers.

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 can hand a piece of its own memory to a callback function it runs. Here the method kept using an old address for the string even after the callback changed or freed that string. The old address then pointed at memory that had been reused, so the method handed back whatever new data now lived there, which can include secret addresses an attacker needs.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by isra17

View all

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

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.