Controlled address leak due to type confusion - ASLR bypass
Several functions in the mruby string library treat a returned value as an integer without checking its type, so a user-overridden comparison method can return an object pointer that is then read as a number. An attacker could use this to leak a memory address and defeat address space layout randomisation.
Originally disclosed on HackerOne
- Triaged in 9 days
- Fixed in 17 days
- Paid in 24 days
- Disclosed after 24 days
There are several different places in which arguments are treated as fixnums without a prior check for their type. Since mrb_value is a union that holds all value types, it can cause a mixup between an object pointer and an integer value:
typedef struct mrb_value {
union {
mrb_float f;
void *p;
mrb_int i;
mrb_sym sym;
} value;
enum mrb_vtype tt;
} mrb_value;
PoC Script:
class Integer
def <=>(arg1)
return arg1
end
end
s = "hello"
s.<=>(1)
And the output varies between runs (because of ASLR) and between architecture (32/64 bit) and seems like this: -69972254725992 meaning an address of: 0x3fa3af631768.
Vulnerable Code:
The mrb_str_cmp_m function (s.<=>()) in string.c uses the <=> function of the argument, if the argument is not a string. That function can be overridden (like was demonstrated in the PoC), and the returned value is not checked to be a fixnum, while it is treated as a fixnum:
mrb_value str2;
mrb_int result;
mrb_get_args(mrb, "o", &str2);
if (!mrb_string_p(str2)) {
if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "to_s"))) {
return mrb_nil_value();
}
else if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "<=>"))) {
return mrb_nil_value();
}
else {
mrb_value tmp = mrb_funcall(mrb, str2, "<=>", 1, str1);
if (mrb_nil_p(tmp)) return mrb_nil_value();
if (!mrb_fixnum(tmp)) {
return mrb_funcall(mrb, mrb_fixnum_value(0), "-", 1, tmp);
}
result = -mrb_fixnum(tmp);
}
}
else {
result = mrb_str_cmp(mrb, str1, str2);
}
return mrb_fixnum_value(result);
This means that the PoC code gets tmp as the original string (since 1.<=>(str1) returns str1), and mrb_fixnum(tmp) will be the address of the string object. Since it is returned as -mrb_fixnum(tmp) our value was negative.
More minor examples:
mrb_str_aref_mfunction instring.cdoes not check the fixnum's type. can cause only a very minor information-leak over the MSbit (pos < 0).mrb_str_indexfunction instring.cdoes not check the 2nd arg, but has no security implications.mrb_str_rindexfunction instring.cdoes not check the 2nd arg, can again leak the MSbit of the address (again a vpos < 0 check).
Suggested Fix:
Before the argument / returned value is treated as a fixnum, it should be checked to match it in type using the mrb_fixnum_p macro, or any other chosen way.
Context
This report concerns mruby, the embeddable Ruby implementation, and specifically its string comparison path. The feature involved is the spaceship operator <=> on strings, implemented in string.c by the function mrb_str_cmp_m. When a string is compared against something that is not a string, that function delegates to the argument's own <=> method and then treats the returned value as an integer. The report shows that this assumption is not enforced, and that the resulting type confusion leaks an address, which is an ASLR bypass.
Setup
No accounts, roles or hosted services are involved. The researcher worked against the mruby source and its C value representation. The report does not name a specific mruby version.
Finding it
The starting point is the definition of mrb_value, a tagged union that can hold a float, a pointer, an integer or a symbol, together with a type tag:
typedef struct mrb_value {
union {
mrb_float f;
void *p;
mrb_int i;
mrb_sym sym;
} value;
enum mrb_vtype tt;
} mrb_value;
Because the union stores an object pointer and an integer in the same storage, any code path that reads the integer member without first confirming the type will interpret a pointer as a number. The researcher found several places in string.c where arguments are treated as fixnums without a prior type check, and the most serious of them is the string comparison operator.
Exploitation
Step 1: override the comparison method on Integer
The PoC reopens the Integer class and replaces <=> so that it returns its argument unchanged instead of an integer:
class Integer
def <=>(arg1)
return arg1
end
end
s = "hello"
s.<=>(1)
When s.<=>(1) runs, the argument 1 is not a string, so mrb_str_cmp_m calls the argument's <=> method with str1 (the string "hello") as the parameter. The overridden method returns that string object. The returned mrb_value is therefore a string, not a fixnum.
Step 2: observe the leaked value
The output varies between runs because of ASLR, and between 32-bit and 64-bit architectures. In the researcher's run it looked like this:
-69972254725992
which corresponds to the address:
0x3fa3af631768
The negative sign comes from the code path described below: the value is returned as -mrb_fixnum(tmp), so the leaked address appears negated.
Root cause
The vulnerable code is in mrb_str_cmp_m in string.c. When the argument is not a string, the function calls the argument's <=> and then handles the result without checking that it is a fixnum:
mrb_value str2;
mrb_int result;
mrb_get_args(mrb, "o", &str2);
if (!mrb_string_p(str2)) {
if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "to_s"))) {
return mrb_nil_value();
}
else if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "<=>"))) {
return mrb_nil_value();
}
else {
mrb_value tmp = mrb_funcall(mrb, str2, "<=>", 1, str1);
if (mrb_nil_p(tmp)) return mrb_nil_value();
if (!mrb_fixnum(tmp)) {
return mrb_funcall(mrb, mrb_fixnum_value(0), "-", 1, tmp);
}
result = -mrb_fixnum(tmp);
}
}
else {
result = mrb_str_cmp(mrb, str1, str2);
}
return mrb_fixnum_value(result);
In the PoC, tmp holds the original string, because 1.<=>(str1) returns str1. The call mrb_fixnum(tmp) then reads the address of the string object out of the union, and because the result is returned as -mrb_fixnum(tmp), the leaked value is negative. The type tag is never consulted before the integer member is read.
The report lists three further, less severe instances of the same pattern in string.c:
mrb_str_aref_mdoes not check the fixnum's type. This can cause only a very minor information leak over the most significant bit, through thepos < 0check.mrb_str_indexdoes not check the second argument, but has no security implications.mrb_str_rindexdoes not check the second argument and can again leak the most significant bit of the address, through thevpos < 0check.
Impact
The primary result is an ASLR bypass: the leaked value is the address of the string object, which undermines address-space randomisation. The two string.c variants that leak only the most significant bit of an address are described as very minor.
Fix
Before an argument or a returned value is treated as a fixnum, it should be checked to match that type using the mrb_fixnum_p macro, or any other chosen way. The report presents this as the suggested fix rather than describing a patch that was applied.
Variants to test
- Look for other C functions that call a user-overridable Ruby method and then read the result with
mrb_fixnumor similar accessors without anmrb_fixnum_pcheck. A positive result is a returned value that varies between runs and looks like a negated or truncated pointer. - Audit the remaining
string.chelpers named in the report (mrb_str_aref_m,mrb_str_index,mrb_str_rindex) for the same missing check. A positive result there is a single-bit leak through a sign comparison rather than a full address. - Override other coercion methods (
to_s,to_i, comparison and arithmetic operators) on built-in classes and pass the objects into C-implemented methods that expect a specific type. A positive result is a crash, a type confusion, or output derived from object memory.
Hunting notes
This class of bug hides in the boundary between a managed language runtime and its C implementation: anywhere the C side calls back into user-defined methods and then trusts the returned value's type. During recon, the signals are a tagged-union or variant value representation, macros such as mrb_fixnum that read a union member directly, and call sites that invoke methods like <=> or to_s on arbitrary objects. The output changes between runs and between architectures, and the report traces the line where the unchecked value is converted. That combination, a reproducible PoC plus the responsible code path, is what makes the information disclosure concrete enough to act on.
What happened, in plain words
A researcher found a bug in mruby, a small version of the Ruby programming language designed to be built into other programs. Knowing a memory address is a serious step towards breaking a security defence called ASLR, which we will explain below.
The bug is a type confusion: the code treats one kind of value (a pointer to an object in memory) as if it were a completely different kind (a whole number).
Background: the pieces you need
Before the details make sense, here are the ideas involved, from zero.
- Ruby and mruby. Ruby is a programming language. mruby is a cut-down version of it that other programs can embed, the way a word processor might embed a small scripting language so users can automate tasks. If that embedding program runs scripts written by its users, then those users are running code inside the host program.
- Memory addresses. Your computer's memory is like a huge street of numbered houses. Every object a program creates lives at some address, a number such as
0x3fa3af631768. Programs use these numbers internally to find their data. - ASLR (Address Space Layout Randomisation). If an attacker knows exactly where things sit in memory, it is much easier to abuse a bug. ASLR is an operating-system defence that shuffles the layout every time a program starts, so addresses are different on each run. A leak of one real address can undo much of that protection, because the attacker learns where things are in that particular run.
- Heap. The area of memory where a program stores objects it creates while running. A heap address is the location of one such object.
- Type. Every value in a program has a type: a number, a piece of text (a string), a list, and so on. Mixing types up is the root of this bug.
- Fixnum. Ruby's name for a small whole number, such as
1or42. - String. A piece of text, such as
"hello". - Union (in C). A C language feature where several different types share the same storage space. Only one of them is meaningful at a time, and the program is supposed to remember which. Think of a single locker that different people can use on different days: if you assume today's occupant is the person you expected without checking, you may hand their belongings to the wrong person.
- Tagged union. A union plus a small label (a tag) saying which type is currently stored. The label is the only reliable way to know what is inside.
- Pointer. A value that holds a memory address, pointing at an object.
<=>(the spaceship operator). A Ruby comparison method.a <=> breturns a negative number ifais less thanb, zero if they are equal, and a positive number ifais greater. The name comes from its shape.- Overriding a method. Replacing a built-in behaviour with your own version. Ruby lets you reopen a class and redefine its methods, which is exactly what the proof of concept does.
- Proof of concept (PoC). A short piece of code that demonstrates a bug actually happens.
- Type confusion. A vulnerability where code treats a value as one type when it is really another, so the bytes are misinterpreted.
- Information disclosure. A weakness that leaks data the attacker should not see, here a memory address.
- Most significant bit (MSB). The leftmost binary digit of a number, which carries the largest value. Leaking only that one bit reveals very little, which is why those variants are described as minor.
The setup
There are no accounts, roles or online services in this report. The researcher worked directly with the mruby source code and the way it stores values in C. The report does not name a specific mruby version.
Finding it
The starting point is how mruby stores every value. It uses a tagged union called mrb_value, which can hold a decimal number, a pointer, a whole number or a symbol, plus a tag saying which one it currently is:
typedef struct mrb_value {
union {
mrb_float f;
void *p;
mrb_int i;
mrb_sym sym;
} value;
enum mrb_vtype tt;
} mrb_value;
Line by line:
typedef struct mrb_value {starts the definition of a new C type namedmrb_value.union {opens the shared storage. All the members below occupy the same bytes.mrb_float f;is a slot for a decimal number.void *p;is a slot for a pointer, a memory address of an object.mrb_int i;is a slot for a whole number.mrb_sym sym;is a slot for a symbol, Ruby's name for an interned identifier.} value;closes the union and names itvalue.enum mrb_vtype tt;is the tag: a small label recording which of the four slots is currently valid.} mrb_value;closes the struct.
Because the pointer and the whole number share the same bytes, any code that reads the number slot without first checking that the tag says "whole number" will read a pointer as if it were a number. The researcher found several places in a file called string.c where arguments are treated as whole numbers without that check. The most serious is the string comparison operator.
Exploitation
Step 1: override the comparison method on Integer
The proof of concept reopens Ruby's Integer class and replaces <=> so that it returns its argument unchanged instead of a number:
class Integer
def <=>(arg1)
return arg1
end
end
s = "hello"
s.<=>(1)
Line by line:
class Integerreopens the built-in class for whole numbers.def <=>(arg1)defines a new version of the spaceship comparison method, taking one argument.return arg1hands that argument straight back, instead of returning a negative number, zero or a positive number as a comparison normally would.endcloses the method, and the secondendcloses the class.s = "hello"creates a string and stores it ins.s.<=>(1)asks the string to compare itself with the number1.
Why this step matters: when s.<=>(1) runs, the argument 1 is not a string, so mruby's string comparison function calls the argument's own <=> method, passing the string "hello" in. The overridden method returns that string object. The value coming back is therefore a string, not a number, which is the mismatch the rest of the bug exploits.
Step 2: observe the leaked value
The output changes between runs because of ASLR, and also differs between 32-bit and 64-bit systems. In the researcher's run it looked like this:
-69972254725992
which corresponds to the address:
0x3fa3af631768
The minus sign comes from the code path below: the value is returned as -mrb_fixnum(tmp), so the leaked address appears negated. The fact that the number changes every run is the signature of an address leak, because ASLR reshuffles memory each time.
Root cause
The vulnerable code is the function mrb_str_cmp_m in string.c. When the argument is not a string, the function calls the argument's <=> and then handles the result without checking that it really is a whole number:
mrb_value str2;
mrb_int result;
mrb_get_args(mrb, "o", &str2);
if (!mrb_string_p(str2)) {
if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "to_s"))) {
return mrb_nil_value();
}
else if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "<=>"))) {
return mrb_nil_value();
}
else {
mrb_value tmp = mrb_funcall(mrb, str2, "<=>", 1, str1);
if (mrb_nil_p(tmp)) return mrb_nil_value();
if (!mrb_fixnum(tmp)) {
return mrb_funcall(mrb, mrb_fixnum_value(0), "-", 1, tmp);
}
result = -mrb_fixnum(tmp);
}
}
else {
result = mrb_str_cmp(mrb, str1, str2);
}
return mrb_fixnum_value(result);
Line by line:
mrb_value str2;declares a variable to hold the argument being compared against.mrb_int result;declares a whole number to hold the comparison result.mrb_get_args(mrb, "o", &str2);reads the argument passed in and stores it instr2.if (!mrb_string_p(str2)) {checks whether the argument is not a string. If it is not, the special handling begins.if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "to_s"))) {checks whether the argument has ato_smethod (convert to string). If not, the next line returns nil, Ruby's "nothing" value.else if (!mrb_respond_to(mrb, str2, mrb_intern_lit(mrb, "<=>"))) {checks whether it has a<=>method. If not, it also returns nil.else {is the branch where the argument does have<=>.mrb_value tmp = mrb_funcall(mrb, str2, "<=>", 1, str1);calls the argument's<=>method, passing the original stringstr1, and stores whatever comes back intmp.if (mrb_nil_p(tmp)) return mrb_nil_value();if the method returned nil, return nil.if (!mrb_fixnum(tmp)) {checks whethertmpis not a whole number. If it is not, the next line calls the subtraction method on zero withtmp, which is a slower path that does not read the raw bytes.result = -mrb_fixnum(tmp);is the dangerous line: it readstmpas a whole number and negates it, without checking the tag.else {handles the case where the argument really is a string.result = mrb_str_cmp(mrb, str1, str2);does a normal string comparison.return mrb_fixnum_value(result);wraps the result back up as a whole number and returns it.
In the proof of concept, tmp holds the original string, because 1.<=>(str1) returns str1. The call mrb_fixnum(tmp) then reads the address of the string object out of the union, and because the result is returned as -mrb_fixnum(tmp), the leaked value is negative. The type tag is never consulted before the number slot is read.
The report lists three further, less severe instances of the same pattern in string.c:
mrb_str_aref_mdoes not check the type of the number it receives. This can cause only a very minor information leak over the most significant bit, through thepos < 0check.mrb_str_indexdoes not check the second argument, but has no security implications.mrb_str_rindexdoes not check the second argument and can again leak the most significant bit of the address, through thevpos < 0check.
Impact
The main result is an ASLR bypass: the leaked value is the address of the string object, which weakens address randomisation. The two variants that leak only the most significant bit of an address are described as very minor.
Fix
Before an argument or a returned value is treated as a whole number, it should be checked to match that type using the mrb_fixnum_p macro, or any other chosen way. The report presents this as a suggested fix rather than describing a patch that was applied.
Variants to test
- Look for other C functions that call a Ruby method the user can override and then read the result with
mrb_fixnumor similar accessors without anmrb_fixnum_pcheck. A positive result is a returned value that changes between runs and looks like a negated or truncated pointer. - Audit the remaining
string.chelpers named in the report (mrb_str_aref_m,mrb_str_index,mrb_str_rindex) for the same missing check. A positive result there is a single-bit leak through a sign comparison rather than a full address. - Override other conversion methods (
to_s,to_i, comparison and arithmetic operators) on built-in classes and pass the objects into C-implemented methods that expect a specific type. A positive result is a crash, a type confusion, or output derived from object memory.
Hunting notes
This class of bug hides at the boundary between a managed language runtime and its C implementation: anywhere the C side calls back into user-defined methods and then trusts the type of the returned value. During recon, the signals are a tagged-union or variant value representation, macros such as mrb_fixnum that read a union member directly, and call sites that invoke methods like <=> or to_s on arbitrary objects. The output changes between runs and between architectures, and the report traces the line where the unchecked value is converted. That combination, a reproducible proof of concept plus the responsible code path, is what makes the information disclosure concrete enough to act on.
Glossary
- ASLR (Address Space Layout Randomisation): an operating-system defence that shuffles where things sit in memory on each run, so attackers cannot rely on fixed addresses.
- C: a low-level programming language in which mruby is written.
- Fixnum: Ruby's name for a small whole number.
- Heap: the area of memory where a running program keeps the objects it creates.
- mruby: a small, embeddable implementation of the Ruby programming language.
- Most significant bit (MSB): the leftmost binary digit of a number, carrying the largest value; leaking only it reveals very little.
- Override: replacing a built-in method with your own version.
- Ruby: a programming language; mruby is a compact version of it.
- String: a piece of text.
- Tagged union: a union plus a small label saying which type is currently stored.
- Type: the kind of a value, such as a number or a string.
- Type confusion: a vulnerability where code treats a value as one type when it is really another, misreading its bytes.
- Union: a C feature where several types share the same storage, with only one meaningful at a time.
<=>(spaceship operator): a Ruby comparison method returning a negative number, zero or a positive number depending on how two values compare.