How I Made A Heap Overflow In Curl
A heap buffer overflow in curl's SOCKS5 handling occurs when a hostname longer than 255 bytes is provided and the proxy is slow, causing curl to switch from remote to local resolution and then back, leading to an overflow when copying the hostname into a too-small buffer. An attacker controlling an HTTPS server accessed via a SOCKS5 proxy could redirect the client to a crafted long hostname, potentially overwriting heap memory.
Published on daniel.haxx.se
In association with the release of curl 8.4.0, we publish a security advisory and all the details for CVE-2023-38545. This problem is the worst security problem found in curl in a long time.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
The local variable deciding whether to resolve the hostname locally or remotely is reset on each invocation of the state machine, so after a too-long hostname triggers a switch to local resolution, a subsequent invocation reverts to remote resolution and the hostname is copied into a buffer that is too small.
Impact
A heap buffer overflow could occur, potentially leading to memory corruption and possibly code execution, though the text does not specify the exact impact beyond the overflow.
What the attacker needed
Attacker controls an HTTPS server; victim uses curl with SOCKS5 proxy; proxy in remote-resolve mode; automatic redirect-following enabled
The fix
curl should not switch mode from remote resolve to local resolve due to a too-long hostname; instead it should return an error, and this was fixed in curl 8.4.0.
How it was found
- An attacker controls an HTTPS server that a curl-using client accesses over a SOCKS5 proxy in remote-resolve mode.
- The attacker's server returns a 30x redirect with a Location header containing a hostname longer than 16kB (up to 64kB).
- The client follows the redirect automatically, and the SOCKS5 proxy is slow enough to trigger the state machine to return and be re-invoked.
- On re-invocation, the local variable is reset to remote-resolve mode, and curl copies the long hostname into the too-small buffer, causing a heap overflow.
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 write-up has not been written yet. The Original tab links to the source.
This is a bug in curl, a tool for downloading files. When curl uses a proxy (a middleman server) to connect to a website, it sometimes needs to handle very long website names. If the name is too long, curl makes a mistake and writes more data than it should into a small memory area, which can crash the program or let an attacker run their own code. The attacker needs to control a website that the victim visits through a slow proxy.
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.