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.
- 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
- Allocate a string with a few lines.
- Call String#lines and free or reallocate the string.
- Allocate a few objects.
- The next lines will now contains the value of the newly allocated data, including pointer used by
mrb_values.
PoC
@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:
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:
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.
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
- Allocate a string containing several lines.
- Call String#lines with a block that clears or reallocates the string.
- Allocate new objects so they reuse the freed memory.
- 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.