Null pointer dereference in mrb_random_initialize
The Random class initialiser in mruby clears the object's internal data pointer before it parses the arguments, so a crafted argument that calls back into the same object causes a null pointer dereference. An attacker who can supply Ruby code could crash the interpreter.
- Triaged in 2 days
- Fixed in 3 days
- Paid in 6 days
- Disclosed after 7 days
Sorry I was not quite sure about the scope. This bug is not triggerable in the sandbox, because it does not use the Random class, but it is triggerable in mruby with the following piece of code:
$r = Random.new
a = Object.new
def a.to_int
$r.rand
end
$r.initialize a
The data pointer of $r is set to NULL before parsing arguments in mrb_random_init. The srand() call will try to dereference it to get the mt_state and this results in a crash.
The problem lies in mrbgems/mruby-random/src/random.c and is similar to #182274:
128 t = (mt_state*)DATA_PTR(self);
129 if (t) {
130 mrb_free(mrb, t);
131 }
132 mrb_data_init(self, NULL, &mt_state_type);
133
134 t = (mt_state *)mrb_malloc(mrb, sizeof(mt_state));
135 t->mti = N + 1;
136
137 seed = get_opt(mrb); /* Move this up before line 127 */
Please let me know if you are explicitly just looking for bugs affecting the sandbox, so I won't submit invalid reports. Thanks!
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 initialiser frees and nulls the object's data pointer before parsing arguments, so a re-entrant call during argument parsing dereferences the null pointer.
Impact
An attacker able to run Ruby code could crash the mruby interpreter; the bug is not triggerable in the sandbox because it does not use the Random class.
What the attacker needed
Ability to run Ruby code in mruby; Random class available
The fix
The argument should be read before the existing data pointer is freed and cleared, as the reporter's suggested reordering shows.
How it was found
- Create a Random object and keep a reference to it.
- Create a plain object and define a to_int method on it that calls rand on the Random object.
- Pass that object to the Random object's initialize method.
- The interpreter crashes when srand tries to use the null data pointer.
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 null pointer dereference is when a program tries to use a memory address that points to nothing, which usually makes it crash. Here, the Random object wipes its own internal state before reading the arguments, and an argument that calls back into the object reaches the wiped state. The result is a crash rather than stolen data.
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.