CVE-2021-22945: UAF and double-free in MQTT sending
A use-after-free and double-free in libcurl's MQTT handling occurs when a partial send leaves a buffer pointer that is freed but not reset, so a later call sends freed memory and frees it again. An attacker could potentially leak heap metadata or, on older glibc, exploit the double-free for code execution.
- Triaged in 21 days
- Fixed in 58 days
- Paid in 25 days
- Disclosed after 58 days
Vulnerability Description
libcurl version 7.77.0 has a Use-After-Free and a Double-Free in lib/mqtt.c in the function mqtt_doing on lines 556 - 563:
if(mq->nsend) {
/* send the remainder of an outgoing packet */
char *ptr = mq->sendleftovers;
result = mqtt_send(data, mq->sendleftovers, mq->nsend);
free(ptr);
if(result)
return result;
}
As can be seen in the code above mq->sendleftovers gets freed in line 560 but not set to NULL. If mqtt_doing gets called repeatedly and the values of mq->nsend and mq->sendleftovers don't change this can result in
- Sending the metadata of the freed chunk over the network via
mqtt_send - Freeing
mq->sendleftoversmultiple times
mq->nsend and mq->sendleftovers get set in the function mqtt_send if Curl_write cannot send all bytes in the write-buffer at once. This can e.g. happen if write() returns EAGAIN or EWOULDBLOCK. Then Curl_write sets the number of written bytes to 0 and returns CURLE_OK.
This can trigger the vulnerabilities as follows:
- Supply an
mqtt://URL to curl - Have some successfull transmissions with
mqtt_send - At some point have an unsuccessfull transmission such that not all bytes of the write-buffer can be sent.
This causes mq->sendleftovers and mq->nsend to be set.
4. Have another invocation of mqtt_doing. The code mentioned above gets executed. mq->sendleftovers gets freed.
If mqtt_send could send all remaining bytes successfully mq->sendleftovers and mq->nsend don't get reset.
5. Have another invocation of mqtt_doing. Since mq->nsend didn't change curl tries to send the leftover bytes again, triggering the vulnerabilities
How to reproduce the bug
- Checkout tag
curl-7_77_0in the curl repository - Apply the following patch that artificially creates a scenario as described above:
diff --git a/lib/sendf.c b/lib/sendf.c
index e41bb805f..773d4b5b6 100644
--- a/lib/sendf.c
+++ b/lib/sendf.c
@@ -294,6 +294,7 @@ void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
* If the write would block (CURLE_AGAIN), we return CURLE_OK and
* (*written == 0). Otherwise we return regular CURLcode value.
*/
+static int CUSTOM_blocked = 0;
CURLcode Curl_write(struct Curl_easy *data,
curl_socket_t sockfd,
const void *mem,
@@ -322,8 +323,13 @@ CURLcode Curl_write(struct Curl_easy *data,
}
#endif
bytes_written = conn->send[num](data, num, mem, len, &result);
+ if(!CUSTOM_blocked) {
+ bytes_written = 0;
+ CUSTOM_blocked = 1;
+ }
*written = bytes_written;
+
if(bytes_written >= 0)
/* we completely ignore the curlcode value when subzero is not returned */
return CURLE_OK;
- Rebuild curl
- Start a simple netcat session with:
nc -lp 5678 - Invoke curl with:
curl mqtt://127.0.0.1:5678/
The output:
free(): double free detected in tcache 2
[1] 199104 abort (core dumped) ./curl mqtt://127.0.0.1:5678
And in the terminal where netcat was launched it can be seen that the content of the freed heap chunk was sent.
Impact
Since double frees of tcache chunks are not detected until glibc version 2.29
this vulnerability is perfectly exploitable for operationg systems using an older
glibc. Causing write() to return EAGAIN is more difficult but not impossible
to manage, e.g. this can always be the case if the peer is not reading as fast as
the curl client is writing (source).
At minimum this can be used to leak heap metadata which can help in exploitation.
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 code frees `mq->sendleftovers` but does not set it to NULL, and does not reset `mq->nsend`, so subsequent calls reuse the dangling pointer.
Impact
This could leak heap metadata and, on systems with glibc older than 2.29, allow exploitation of the double-free for code execution; triggering the required write() failure is difficult but possible when the peer reads slowly.
What the attacker needed
Curl built with MQTT support; network write returns EAGAIN or EWOULDBLOCK; peer reads slowly or not at all
How it was found
- Checkout curl tag curl-7_77_0 and apply a patch that forces Curl_write to return zero bytes written once.
- Rebuild curl.
- Start a netcat listener on port 5678.
- Run curl with an mqtt:// URL pointing to the listener.
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.
This can crash the program or, on older glibc, be exploited and leak heap metadata.
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.