Segfault when passing invalid values to `values_at`
Passing a primitive non-integer value such as a boolean to values_at on Struct or Range caused a segfault in mruby and the parent Ruby process of mruby-engine, via an invalid pointer dereference.
- Triaged in 2 days
- Fixed in 6 days
- Paid in 6 days
- Disclosed after 7 days
Introduction
Passing primitive non-Fixnum values to the values_at method of Struct and Range leads to a segfault in both mruby and mruby-engine/parent Ruby process.
Proof of concept
list-crash.rb
[].values_at true
struct-crash.rb
Struct.new.new.values_at true
- Save either of the above as
crash.rb. - Run either:
a)
mruby crash.rbb)sandbox crash.rb - Both cause a segfault due to an invalid pointer dereference.
Discussion
The crash is introduced in commit 79a621dd739faf4cc0958e11d6a887331cf79e48.
The underlying cause is that the new mrb_range_ptr attempts to dereference a pointer derived from a mrb_value to check whether the edges member is NULL without ensuring that the mrb_value in question is a non-primitive (and therefore has a valid pointer value). In the case of the values_at methods, its arguments are passed to mrb_get_values_at which in turn passes it to range_beg_len. There it is passed to mrb_range_ptr before checking its type.
Solution
To fix the crash, the type of the value passed to range_beg_len should be checked before calling mrb_range_ptr. We've also looked through the codebase for other problematic instances, but found none.
diff --git a/src/range.c b/src/range.c
index 4179574..73fe758 100644
--- a/src/range.c
+++ b/src/range.c
@@ -252,9 +252,10 @@ static mrb_bool
range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc)
{
mrb_int beg, end;
- struct RRange *r = mrb_range_ptr(mrb, range);
+ struct RRange *r;
if (mrb_type(range) != MRB_TT_RANGE) return FALSE;
+ r = mrb_range_ptr(mrb, range);
beg = mrb_int(mrb, r->edges->beg);
end = mrb_int(mrb, r->edges->end);
-- Denis Kasak Damir Jelić
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 range helper dereferenced a pointer derived from the argument before verifying that the argument was actually a Range object.
Impact
Any caller that can pass a primitive value to values_at can crash the interpreter, and in mruby-engine this also crashes the parent Ruby process.
What the attacker needed
Ability to call values_at with a primitive argument
The fix
The type of the value should be checked before calling mrb_range_ptr, and the report includes a patch that moves the type check ahead of the dereference.
How it was found
- Save a small script that calls values_at with a boolean argument on an array or a new Struct.
- Run it with the mruby interpreter or the sandbox runner.
- Observe the segfault from the invalid pointer dereference.
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 segfault is a crash where a program tries to read memory it is not allowed to touch. Here the interpreter assumed a value passed to a method was a special range object and tried to follow a pointer inside it, even when the value was just a simple true/false. Because the check happened too late, the program crashed instead of raising a normal error.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.