Buffer Overflow in cURL Internal printf Function
The curl_msprintf() function in cURL's internal printf implementation writes formatted output to a user-provided buffer without bounds checking, allowing an attacker to overflow the buffer. This could lead to memory corruption, arbitrary code execution, or denial of service.
Originally disclosed on HackerOne
- Disclosed after 3 hours
A critical buffer overflow vulnerability exists in the curl_msprintf() function in cURL's internal printf implementation. The function writes formatted output to a user-provided buffer without performing any bounds checking, allowing attackers to overflow arbitrary memory and potentially achieve arbitrary code execution.
Affected Version
Current master branch (commit 141ce4be64) and potentially earlier versions
CVSS Score Breakdown
Base Score: 9.8 (Critical)
- Attack Vector (AV): Network (N)
- Attack Complexity (AC): Low (L)
- Privileges Required (PR): None (N)
- User Interaction (UI): None (N)
- Scope (S): Unchanged (U)
- Confidentiality (C): High (H)
- Integrity (I): High (H)
- Availability (A): High (H)
Vulnerability Details
Location
- File:
lib/mprintf.c - Function:
curl_msprintf() - Lines: 1203-1211
Root Cause
The vulnerability occurs because curl_msprintf() calls formatf() with the storebuffer() callback function, which writes directly to the provided buffer without any size validation:
int curl_msprintf(char *buffer, const char *format, ...)
{
va_list ap_save; /* argument pointer */
int retcode;
va_start(ap_save, format);
retcode = formatf(&buffer, storebuffer, format, ap_save);
va_end(ap_save);
*buffer = 0; /* we terminate this with a zero byte */
return retcode;
}
static int storebuffer(unsigned char outc, void *f)
{
char **buffer = f;
**buffer = (char)outc; // VULNERABILITY: No bounds checking
(*buffer)++;
return 0;
}
Attack Vector
An attacker can trigger this vulnerability by:
- Providing a malicious format string to any cURL function that internally uses
curl_msprintf() - Supplying format specifiers that generate output larger than the target buffer
- Causing stack corruption and potentially hijacking control flow
Proof of Concept
Test Code
import ctypes
# Load libcurl
libcurl = ctypes.CDLL("libcurl.so.4")
curl_msprintf = libcurl.curl_msprintf
# Create small buffer (16 bytes)
buffer = ctypes.create_string_buffer(16)
# Trigger overflow with long format string
long_format = b"A" * 100 + b"%s" + b"B" * 50
result = curl_msprintf(buffer, long_format)
print(f"Result: {result}")
print(f"Buffer content: {buffer.value}")
# Buffer overflow occurs - writes beyond 16-byte buffer
Impact
Business Impact
- Confidentiality: Critical - Potential memory disclosure
- Integrity: Critical - Arbitrary code execution
- Availability: High - Application crashes and DoS
Technical Impact
- Arbitrary Code Execution: Attackers can execute arbitrary code in the context of the application
- Memory Corruption: Stack and heap corruption leading to crashes
- Information Disclosure: Potential leakage of sensitive memory contents
- Denial of Service: Application crashes and system instability
Affected Components
Primary Functions
curl_msprintf()- Direct vulnerability- Any function calling
curl_msprintf()internally
Potentially Affected Areas
- URL parsing and construction
- Error message formatting
- Log message generation
- HTTP header processing
- Cookie handling
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
Likely the storebuffer() callback writes each character to the destination buffer without knowing its size, so any output longer than the buffer overflows it.
Impact
An attacker could achieve arbitrary code execution, memory disclosure, or denial of service, though the text does not demonstrate a working exploit beyond a buffer overflow.
What the attacker needed
Ability to supply a format string to curl_msprintf()
How it was found
- Provide a malicious format string to any cURL function that internally uses curl_msprintf().
- Supply format specifiers that generate output larger than the target buffer.
- Cause stack corruption and potentially hijack control flow.
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 buffer is a fixed-size area of memory. The function that formats text into it does not check if the text fits, so a long input can spill over into neighbouring memory. This can crash the program or 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.