Skip to content
Memory corruption Disclosed report Advanced

Range constructor type confusion DoS

The mruby interpreter's range literal constructor looks up the Range class at runtime, so redefining the Range constant to another class causes a type confusion and a segfault. An attacker who can run Ruby code could crash the interpreter, and the reporter suggests RCE may be possible but was not demonstrated.

h72 Follow

Originally disclosed on HackerOne

Bounty$10,000 High View original
  • Triaged in 35 hours
  • Fixed in 5 days
  • Paid in 33 days
  • Disclosed after 34 days

It's possible to crash mruby by redefining the Range class and then using the range literal syntax:

Range = Array
(1..2).inspect

The mrb_range_new function allocates and initializes a range object backed by the RRange struct, however it uses runtime constant lookup to find the Range class object. Redefining the Range constant to point to a different class and calling an instance method causes a segfault, as the RRange::edges field is confused for the iv field on other structs.

It may be possible to achieve RCE through this vulnerability, but there are significant complicating factors and I have not spent the time trying to develop an RCE PoC.

I have attached a patch which fixes this bug. My patch adds a range_class field to mrb_state, following the pattern other core classes use to avoid runtime constant lookups.

The original report as published on HackerOne, by h72. 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. 5 min read.

What the report shows

The target is mruby, the lightweight Ruby implementation. The researcher found that the interpreter can be crashed by redefining the Range constant and then using the range literal syntax. The whole proof of concept is two lines:

Ruby
Range = Array
(1..2).inspect

The first line rebinds the Range constant to Array. The second line builds a range literal and immediately calls an instance method on it. Instead of a normal result, the process segfaults.

The report explains the mechanism directly: mrb_range_new allocates and initialises a range object backed by the RRange struct, but it finds the Range class object through a runtime constant lookup. When the constant points at a different class, the object is still treated as an RRange while the method being called belongs to the other class. The RRange::edges field is then interpreted as the iv field of a different struct layout, and the mismatch ends in a segfault.

The researcher notes that remote code execution may be reachable through the same flaw, but that there are significant complicating factors and that no RCE proof of concept was developed. A patch was attached with the report. It adds a range_class field to mrb_state, following the pattern other core classes use to avoid runtime constant lookups.

The report was rated high severity under the weakness Uncontrolled Resource Consumption and paid a bounty of 10,000 USD. It was disclosed on 17 December 2016.

Root cause

The likely root cause is a type confusion caused by late binding of a core class. The range constructor trusts a name lookup to return the class it expects, but the name is mutable from script code. Nothing checks that the object found under Range is actually the built-in range class before the constructor writes an RRange into memory and hands it back as an instance of whatever class the constant now names. Method dispatch then follows the new class's expectations about object layout, while the memory still holds an RRange. Fields at the same offset mean different things in the two layouts, so a read or write lands on the wrong data.

Impact

The immediate effect is denial of service: running these two lines takes the interpreter process down with a segfault. The researcher considered RCE possible but did not demonstrate it and described the path as complicated. The report does not establish what an attacker could reach beyond the crash.

How this kind of bug works

Many language runtimes keep a table of core classes and resolve them by name at the point of use. That is convenient, because the same constructor code serves interpreted code, native extensions and the standard library. It becomes dangerous when the name is also writable from the language itself. A script can rebind the constant to an attacker-chosen class, and the constructor, which was written assuming the canonical class, still allocates its own internal struct. The resulting object is a lie: its header or class pointer says one thing, its memory layout says another.

Type confusion of this shape is a memory safety problem. If the two layouts disagree about which offset holds a pointer, a method call on the confused object can dereference attacker-influenced data, write through a pointer, or simply read out of bounds. Even when exploitation is hard, a crash in a shared interpreter is a denial of service.

The general defences are the same ones the patch here follows. Resolve core classes once at start-up and store them in the interpreter state, so no runtime lookup can be redirected. Validate that a looked-up class is the expected one before allocating. Where the language allows constants to be reassigned, either freeze the core ones or make the constructor independent of the constant table entirely.

How to test for it

Look for interpreters, template engines and embedded scripting layers that expose core classes as reassignable names. Good places to start are sandboxed Ruby, Python or JavaScript runtimes in SaaS products, plugin systems, formula and rule engines, and anything that documents a restricted subset of a language.

  1. Enumerate the core classes and constructors the sandbox leaves reachable. Try reading and reassigning the constants that name them.
  2. For each one, rebind the name to a different class, then exercise the literal or constructor syntax that uses it, and call a method on the result.
  3. Watch for a crash, a hang, a corrupted value or an unexpected object type coming back. A clean exception is a negative result; a process death or a value that does not match either class is a positive signal.
  4. Confirm reproducibility in a fresh session, and check whether the crash affects only your sandbox or the shared worker.

Common false positives are ordinary NameError or TypeError exceptions raised by the rebinding itself, which show the runtime rejected the change rather than confused a type. A crash caused by resource exhaustion from a huge literal is a different bug class and should not be reported as type confusion. Make sure the crash disappears when the rebinding line is removed.

How it is fixed

Store references to core classes in the interpreter state at initialisation and use those references in constructors, never a fresh name lookup.

Hunting notes

This class hides wherever a product embeds a general-purpose language and then tries to restrict it. Recon signals include documentation that mentions a sandboxed interpreter, user-supplied scripts or templates, custom filters and helpers, and version strings for embedded runtimes. Once you have a scripting surface, spend time mapping which constants and globals are writable; the interesting bugs are usually one rebinding away from a documented feature.

To get a report accepted, show the smallest reproducible input, state clearly whether the effect is a crash or something stronger, and separate what you proved from what you suspect. If you only have a denial of service, say so and describe the blast radius, such as whether other tenants share the process. Attaching a patch that follows the runtime's existing conventions, as the researcher did here, helps triage and fix.

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

Those programs do not run in normal Ruby. They run in mruby, a lightweight version of Ruby designed to be embedded inside another application. The idea is that the host application stays in control while the merchant's script does its work.

The researcher found that the interpreter could be crashed with just two lines. The whole proof of concept is:

Ruby
Range = Array
(1..2).inspect

To understand why this is surprising, you need two ideas from Ruby. A constant is a name that normally points at a fixed thing, such as a class. Range is the built-in class that represents a range of values, like the numbers 1 to 2. A range literal is the shorthand (1..2), which asks Ruby to build a range object for you. inspect is a method that returns a readable description of an object.

The first line reassigns the name Range so that it now points at Array, a completely different class. The second line builds a range literal and calls inspect on the result. Instead of returning a string, the process crashes with a segfault, which means the program tried to touch memory it was not allowed to touch and the operating system killed it.

The report explains the mechanism. Internally, a function called mrb_range_new creates the range object. It reserves memory laid out for a range, a structure called RRange. But to find out which class the object belongs to, it looks up the name Range at the moment the code runs, rather than remembering the real range class from start-up. After the first line, that name points at Array. So the object is still built as a range in memory, but the method being called belongs to the other class. The two kinds of object store different things at the same position in memory. A field the range uses for its endpoints, RRange::edges, gets read as if it were a field called iv belonging to the other layout. The mismatch ends in the segfault.

The researcher says remote code execution, meaning an attacker running their own commands on the server, may be possible through the same flaw. They also say there are significant complicating factors and that they did not spend time building a proof of concept for it. So the demonstrated effect is a crash, not code execution.

A patch came with the report. It adds a range_class field to mrb_state, the structure that holds the interpreter's overall state, following the pattern other core classes already use to avoid looking names up while the program runs.

The report was rated high severity under the weakness Uncontrolled Resource Consumption and paid a bounty of 10,000 USD. It was disclosed on 17 December 2016.

In general, this kind of bug

This is a type confusion. The name comes from the object being treated as one type while its memory is laid out as another. A useful comparison is a form designed for one purpose being reused for another: if a box asks for a date of birth but someone writes a phone number in that slot, whoever reads the form later will misinterpret it. Here the interpreter reads a range's field as if it were a different class's field.

The underlying cause is late binding of a core class. Many language runtimes keep a table of built-in classes and look them up by name whenever they are needed. That is convenient, because the same code serves scripts, libraries and built-in features. It becomes dangerous when the script itself is allowed to change what a name points at. The constructor still assumes it is building the usual class, but the name now says something else.

Why does a crash matter? A crashing interpreter stops the service working. That is a denial of service: the attacker does not gain access, but they stop the service working. The report does not establish what an attacker could reach beyond the crash.

The defences are the ones the patch follows. Look up core classes once at start-up and store them in the interpreter state, so no later name change can redirect the lookup. Check that a looked-up class is the expected one before allocating memory for it. Where the language allows constants to be reassigned, either freeze the core ones or make the constructor independent of the name table.

If you were testing for this yourself, you would look for products that embed a general-purpose language and then try to restrict it: sandboxed Ruby, Python or JavaScript runtimes, plugin systems, rule engines, and anything documenting a limited subset of a language. The steps would be:

  1. List the core classes and constructors the sandbox leaves reachable, and try reading and reassigning the names that point at them.
  2. For each one, point the name at a different class, then use the literal or constructor syntax that relies on it, and call a method on the result.
  3. Watch for a crash, a hang, a corrupted value or an object of an unexpected type. A clean exception is a negative result; a process death or a value matching neither class is a positive signal.
  4. Confirm it reproduces in a fresh session, and check whether the crash affects only your sandbox or a shared worker.

Two common false positives are worth knowing. An ordinary NameError or TypeError raised by the reassignment itself means the runtime rejected the change, which is not a type confusion. A crash caused by a huge literal exhausting memory is a different bug class. Make sure the crash disappears when the reassignment line is removed.

To get a report accepted, show the smallest reproducible input, say clearly whether the effect is a crash or something stronger, and separate what you proved from what you suspect. If you only have a denial of service, say so clearly. Attaching a patch that follows the runtime's existing conventions, as the researcher did here, helps triage and fix.

Glossary

  • mruby: a lightweight implementation of the Ruby language designed to be embedded inside other applications.
  • Ruby: a programming language often used for scripting and web applications.
  • Constant: a named value in Ruby that is meant to stay fixed, such as a class name.
  • Class: a blueprint that defines what an object is and what it can do.
  • Range: the Ruby class representing a sequence of values between two endpoints, such as 1 to 2.
  • Range literal: the shorthand syntax (1..2) that builds a range object.
  • Method: a named action an object can perform, such as inspect.
  • inspect: a Ruby method that returns a readable description of an object.
  • Segfault: a crash caused when a program accesses memory it is not permitted to access.
  • Struct: a fixed layout of fields in memory that a program uses to store an object's data.
  • RRange: the internal structure that holds a range object's data in mruby.
  • mrb_range_new: the internal function that allocates and initialises a range object.
  • mrb_state: the internal structure holding an interpreter's overall state.
  • Runtime constant lookup: finding a class by its name while the program is running, rather than remembering it from start-up.
  • Type confusion: a bug where an object is treated as one type while its memory is laid out as another.
  • Late binding: resolving a name or class only when it is used, so a later change to the name affects the result.
  • Remote code execution (RCE): a flaw that lets an attacker run their own commands on a server.
  • Denial of service (DoS): making a service unavailable, for example by crashing it.
  • Uncontrolled Resource Consumption: a weakness category where a program uses more of a resource than it should, such as crashing a shared process.
  • Sandbox: a restricted environment that limits what a script is allowed to do.
  • Patch: a code change that fixes a bug.
  • Bounty: a payment made to a researcher for reporting a valid vulnerability.

More by h72

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.

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.