Range#initialize_copy null pointer dereference
Removing the Range#initialize_copy method allows an uninitialised Range object to be created via dup or clone, and calling almost any instance method on it then dereferences a null pointer and crashes the interpreter. An attacker who can run Ruby code in the mruby sandbox could therefore cause a denial of service by segfaulting the process.
- Triaged in 3 days
- Fixed in 7 days
- Paid in 35 days
- Disclosed after 35 days
Heya!
It's possible to segfault mruby through mruby-engine with the following snippet of code:
Range.remove_method(:initialize_copy)
(1..2).dup.to_s
This can be triggered through mruby-engine like this:
MRubyEngine.new(512*1024, 1000, 1000).sandbox_eval("/tmp", %{
Range.remove_method(:initialize_copy)
(1..2).dup.to_s
})
The dup and clone methods allocate a new object and then call initialize_copy on the new object with the old object as an argument to copy over internal state.
Removing Range#initialize_copy makes it possible to construct an uninitialized Range object. Calling (pretty much) any instance method on the uninitialized Range object afterwards causes mruby to dereference a null pointer, leading to a segfault.
I've attached a patch that fixes the bug by copying internal range state before calling initialize_copy, similar to what mruby already does for classes and modules.
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 copy path called initialize_copy before the internal range state had been copied, so removing that method left the new Range object uninitialised and later method calls dereferenced a null pointer.
Impact
Any code able to run inside the mruby sandbox could crash the host process, a denial of service; the report does not claim memory corruption beyond the crash.
What the attacker needed
Ability to run Ruby code in mruby-engine
The fix
A patch was attached that fixes the bug by copying internal range state before calling initialize_copy, as mruby does for classes and modules.
How it was found
- Remove the initialize_copy method from the Range class.
- Duplicate a Range object and call a method such as to_s on the copy.
- Observe the null pointer dereference and segfault.
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 Range is a Ruby object representing a span of values, such as from 1 to 2. When you copy an object with dup, Ruby normally runs a setup step called initialize_copy to fill in the copy's internal data. Here that setup step could be deleted, so the copy was left empty, and using it made the program read from an empty (null) memory location and crash.
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.