Skip to content
Memory corruption Disclosed report Advanced

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.

ankitsingh015 Follow

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_SNPRINTF is 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):

C
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

  1. Configure the Build

    Compile libcurl with HAVE_SNPRINTF undefined to force the fallback sprintf path. One way to simulate this is by modifying lib/mprintf.c:

    C
    #if 0 /* Force fallback path for testing */
      (snprintf)(work, BUFFSIZE, formatbuf, dnum);
    #else
      (sprintf)(work, formatbuf, dnum); /* Vulnerable path */
    #endif
    
  2. Compile Reproduction Program

    Compile the following C program against the modified libcurl build:

    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;
    }
    
  3. Run the Program

  4. 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):

C
double absval = fabs(val);

while(absval >= 10.0) {
  absval /= 10.0;
  maxprec--;
}

Optionally, clamp maxprec to prevent underflow:

C
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 snprintf is unavailable or explicitly disabled.

The original report as published on HackerOne, by ankitsingh015. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

  1. Compile libcurl with HAVE_SNPRINTF undefined to force the fallback sprintf path.
  2. Compile a small C program that calls curl_maprintf with a large negative double and a high precision format.
  3. Run the program.
  4. 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.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.