Buffer overflow in yywarning_s
A warning-formatting function in the mruby compiler concatenated a message and a string into a fixed 256-byte stack buffer without checking length. A crafted input could overflow the buffer and crash the process, with the authors suggesting possible control of execution flow via stack smashing.
- Triaged in 10 hours
- Fixed in 28 hours
- Paid in 28 hours
- Disclosed after 5 months
PoC
The following demonstrates a crash:
300000000000000000000000000000000000000000000000E0030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Debug info
In the same vein as our last two reports, we've found more instances of problematic string concatenation. This crash happens due to a buffer overflow in yywarning_s. strcat is called without making sure buf is large enough. The patch below adds the check.
diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y
index cb62ec3f..82df8c08 100644
--- a/mrbgems/mruby-compiler/core/parse.y
+++ b/mrbgems/mruby-compiler/core/parse.y
@@ -3759,10 +3759,15 @@ static void
yywarning_s(parser_state *p, const char *msg, const char *s)
{
char buf[256];
-
- strcpy(buf, msg);
- strcat(buf, ": ");
- strcat(buf, s);
+ const char delim[] = ": ";
+
+ if (strlen(msg) + strlen(s) + strlen(delim) + 1 > sizeof(buf)) {
+ strcpy(buf, msg);
+ } else {
+ strcpy(buf, msg);
+ strcat(buf, delim);
+ strcat(buf, s);
+ }
yywarning(p, buf);
}
Test platform
- Arch Linux
mruby SHA: 9c252410cf6e43eb7e19683844c83581445fc089
Thank you, Dinko Galetic Denis Kasak
Impact
DOS through crashing the mruby process and probable execution flow control through stack smashing.
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 function copied a message and a string into a fixed 256-byte stack buffer using strcpy and strcat without checking that the combined length fits.
Impact
The process could be crashed (denial of service), and the authors say stack smashing could probably allow control of execution flow.
What the attacker needed
Ability to supply input to the mruby compiler
The fix
The patch adds a length check so the message is copied alone when the combined text would exceed the buffer.
How it was found
- Supply the long crafted input to the mruby compiler.
- Observe the crash caused by the buffer overflow in yywarning_s.
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 program often builds text by copying pieces into a small fixed-size box in memory. If the pieces are longer than the box, they spill over into neighbouring memory, which is called a buffer overflow. Here a very long input made the compiler write past its box and crash, and the authors say it could possibly let an attacker steer the program.
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.