Heap Overflow in fiber_switch triggered from Fiber.transfer
The mruby fiber implementation copies arguments passed to Fiber.transfer into a fixed-size stack buffer without checking how many were supplied. An attacker who can run Ruby code could pass 64 or more arguments and overflow the heap, potentially crashing or corrupting memory.
- Triaged in 2 hours
- Fixed in 14 days
- Paid in 18 days
- Disclosed after 19 days
It appears as if my recommendations were ignored in the GitHub issue, so I've repeated the issue here.
PoC
Fiber.new{}.transfer(
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0)
Explanation
The cause of this is that in mrbgems/mruby-fiber/src/fiber.c the stack Fiber stack is initialized lines 90-96 to a default of 64 (FIBER_STACK_INIT_SIZE), however in fiber_switch lines 191-198, if the Fiber's status is currently MRB_FIBER_CREATED, then it will copy the arguments provided from fiber_transfer irregardless of whether or not there are more arguments than 64 (the PoC doesn't need ~127 arguments, anything 64 or above will work!). The quick fix would be to make sure that theres not more than 64 arguments passed to fiber_transfer.
Solution
diff --git a/mrbgems/mruby-fiber/src/fiber.c b/mrbgems/mruby-fiber/src/fiber.c
index bd1d09d4..2d0fc39a 100644
--- a/mrbgems/mruby-fiber/src/fiber.c
+++ b/mrbgems/mruby-fiber/src/fiber.c
@@ -188,6 +188,9 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
mrb->c->status = resume ? MRB_FIBER_RESUMED : MRB_FIBER_TRANSFERRED;
c->prev = resume ? mrb->c : (c->prev ? c->prev : mrb->root_c);
if (c->status == MRB_FIBER_CREATED) {
+ if (len >= FIBER_STACK_INIT_SIZE) {
+ mrb_raise(mrb, E_FIBER_ERROR, "too many arguments to fiber");
+ }
mrb_value *b = c->stack+1;
mrb_value *e = b + len;
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
The fiber_switch function copies transfer arguments into a stack buffer sized FIBER_STACK_INIT_SIZE (64) without validating that the argument count is below that limit.
Impact
Passing 64 or more arguments to transfer overflows the heap, which could crash the interpreter or corrupt memory; the text does not demonstrate code execution.
What the attacker needed
Ability to run Ruby code using mruby fibers
The fix
The suggested fix raises a FiberError when the argument count reaches or exceeds the initial stack size, and the text does not say whether it was applied.
How it was found
- Create a new Fiber object.
- Call transfer on it with 64 or more zero arguments.
- The arguments are copied past the end of the fixed-size fiber stack.
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 fiber is a lightweight unit of execution inside a program, and it has a small fixed area of memory for its arguments. The code copied however many arguments the caller gave into that fixed area without checking the count, so giving too many spilled past the end and overwrote neighbouring memory. This kind of mistake is called a heap overflow and can crash the program or let an attacker influence what it does next.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.