Stack Buffer Overflow in mprintf.c formatting function (fallback path)
A stack buffer overflow exists in the out_double() function of mprintf.c when HAVE_SNPRINTF is undefined, because the precision calculation fails to account for negative numbers. An attacker who controls the floating-point value and format string could cause a crash or potentially achieve memory corruption.
Originally disclosed on HackerOne
- Disclosed after 11 hours
Summary
A stack-based buffer overflow exists in mprintf.c within the out_double() function. This vulnerability affects builds where HAVE_SNPRINTF is undefined, forcing the use of the legacy sprintf function.
The logic responsible for calculating the maximum safe precision (maxprec) for floating-point formatting fails to correctly handle negative numbers. Specifically, it does not properly account for the number of digits required by the integer portion of negative values. As a result, the buffer size calculation is incorrect, allowing a subsequent sprintf call to write more data than the fixed-size stack buffer can hold.
Affected Component
- File:
lib/mprintf.c - Function:
out_double() - Vulnerable Condition:
HAVE_SNPRINTFis not defined at compile time
Technical Details
The function uses a local stack buffer (work) of size BUFFSIZE (326 bytes). To prevent overflow, the code attempts to estimate the number of digits required for the integer part of the floating-point value and reduce the allowable precision accordingly.
The relevant code is shown below (approx. line 675 in master):
while(val >= 10.0) {
val /= 10;
maxprec--;
}
For negative values (e.g., -1.0e100), the condition val >= 10.0 is false on entry, so the loop is skipped entirely. As a result, maxprec is not reduced, even though the integer portion of the formatted value will consume significant buffer space. The subsequent sprintf call writes the minus sign, the full integer portion, and the requested fractional precision, exceeding the 326-byte stack buffer.
Steps To Reproduce
-
Configure the Build
Compile
libcurlwithHAVE_SNPRINTFundefined to force the fallbacksprintfpath. One way to simulate this is by modifyinglib/mprintf.c:C #if 0 /* Force fallback path for testing */ (snprintf)(work, BUFFSIZE, formatbuf, dnum); #else (sprintf)(work, formatbuf, dnum); /* Vulnerable path */ #endif -
Compile Reproduction Program
Compile the following C program against the modified
libcurlbuild:C #include <stdio.h> #include <curl/curl.h> #include <string.h> int main(void) { // -1.0e100 requires ~102 characters for the integer part // Precision .300 requires 300 characters for the fractional part // Total output length ~402 characters // Stack buffer size in mprintf.c is 326 bytes double v = -1.0e100; char *output = curl_maprintf("%.300f", v); if (output) { printf("Output length: %lu bytes\n", (unsigned long)strlen(output)); curl_free(output); } return 0; } -
Run the Program
-
Observe
The output length exceeds the 326-byte buffer size. Depending on stack protections (stack canaries, ASLR), this results in a segmentation fault or stack smashing detection.
Suggested Fix
Correct the integer digit calculation to account for negative values by operating on the absolute value (or equivalent logic):
double absval = fabs(val);
while(absval >= 10.0) {
absval /= 10.0;
maxprec--;
}
Optionally, clamp maxprec to prevent underflow:
if(maxprec < 0)
maxprec = 0;
Impact
This vulnerability results in a classic stack-based buffer overflow.
- Availability: Denial of Service (application crash)
- Security Impact: In scenarios where the floating-point value and format string are influenced by attacker-controlled input, this issue could potentially be leveraged for further memory corruption, including control-flow manipulation.
- Scope: The vulnerable code path is part of the legacy fallback implementation and is not used in default modern curl builds. It primarily affects legacy Unix systems, embedded platforms, or custom builds where
snprintfis unavailable or explicitly disabled.
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 loop that reduces the allowed precision for large numbers only checks if the value is greater than or equal to 10, so negative values skip the loop and the precision is not reduced, leading to an undersized buffer estimate.
Impact
The overflow can cause a denial of service (crash) and, if the floating-point value and format string are attacker-controlled, could potentially lead to further memory corruption including control-flow manipulation; however, the vulnerable path is only used in legacy or custom builds without snprintf.
What the attacker needed
Build without HAVE_SNPRINTF
The fix
The integer digit calculation should be corrected to use the absolute value of the number, and optionally clamp maxprec to prevent underflow.
How it was found
- Compile libcurl with HAVE_SNPRINTF undefined to force the fallback sprintf path.
- Compile a small C program that calls curl_maprintf with a large negative double and a high precision format.
- Run the program.
- Observe that the output length exceeds the 326-byte buffer, causing a crash or stack smashing detection.
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 stack buffer overflow happens when a program writes more data into a fixed-size memory area than it can hold, potentially overwriting nearby data. Here, a function that formats numbers into text miscalculates how much space a negative number will need, so a very large negative number with many decimal places can overflow the buffer. This could crash the program or, in some cases, let an attacker run their own code.
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.