Skip to content
Memory corruption Disclosed report Advanced

Type confusion in mrb_exc_set leading to memory corruption

The exception class macros in mruby are resolved at runtime rather than being fixed constants, so a script can redefine an exception name such as NotImplementedError to an ordinary class like String. When internal code then raises that 'exception', the engine treats a non-exception object as one, causing a native crash and potentially memory corruption and code execution.

raydot Follow

Originally disclosed on HackerOne

Critical View original
  • Triaged in 7 hours
  • Fixed in 12 hours
  • Paid in 21 days
  • Disclosed after 21 days

Similar to #181871, but the bug is more general. The E_*_ERROR macros are not constants, so the exception types can be redefined to not be exceptions:

#define E_NOTIMP_ERROR              (mrb_class_get(mrb, "NotImplementedError"))

This means that any code calling mrb_raise on an exception macro can instead get a non-exception object, leading to memory corruption and arbitrary code execution. This snippet causes a native crash in mruby-engine:

NotImplementedError = String
Module.constants # mrb_raise(mrb, E_NOTIMP_ERROR, "Module.constants not implemented");

This should be fixed by making mrb_exc_set check that it is an exception type. Attached is a patch to mruby to fix this problem.

The original report as published on HackerOne, by raydot. 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 the exception macros resolve class objects at runtime via mrb_class_get, and mrb_exc_set does not verify that the object it is given is actually an exception type.

Impact

An attacker who can run mruby code could trigger memory corruption and potentially achieve arbitrary code execution.

What the attacker needed

Ability to run mruby code

The fix

It should be fixed by making mrb_exc_set verify that the object it receives is an exception type; the report also attaches a patch to mruby.

How it was found

  1. Redefine the NotImplementedError constant to point at a non-exception class such as String.
  2. Call a method whose implementation raises that exception, for example Module.constants.
  3. Observe the native crash in mruby-engine.

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.

In mruby, some built-in error names are looked up when they are used, not locked in when the program is built. A script can therefore point an error name at a normal class, and when the engine later tries to raise that 'error' it handles the wrong kind of object. That mismatch crashes the interpreter and can corrupt memory.

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 raydot

View all

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