CVE-2022-32221: POST following PUT confusion
A bug in libcurl causes an HTTP PUT to be sent instead of an HTTP POST when the user reuses a handle after an upload. This can leak sensitive data from the read buffer or cause a use-after-free if that buffer was freed.
- Triaged in 15 days
- Fixed in 37 days
- Disclosed after 2 months
Summary:
The bug I submitted at https://github.com/curl/curl/issues/9507 can have at least a few unintended security issues:
- Information Disclosure: this bug causes an HTTP PUT to occur when the user intends for an HTTP POST to occur. The user, who intended an HTTP POST, expects the POSTed information to come from CURLOPT_POSTFIELDS. However, as an HTTP PUT is performed instead, the data that is PUT comes from a buffer specified in CURLOPT_READDATA, which may be sensitive information intended for an entirely different host (host1.com below). If CURLOPT_READDATA is not specified, this data could come from stdin!
- Use after free: using the description above, if the user had already freed the data specified in CURLOPT_READDATA, then the unintended HTTP PUT (which was intended to be an HTTP POST) would attempt to read the freed data specified in CURLOPT_READDATA.
Steps To Reproduce:
The following code is similar to the code I posted at https://github.com/curl/curl/issues/9507, but now highlights the potential security issues (which I did not think wise to disclose on GitHub):
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
typedef struct
{
char *buf;
size_t len;
} put_buffer;
static size_t put_callback(char *ptr, size_t size, size_t nmemb, void *stream)
{
put_buffer *putdata = (put_buffer *)stream;
size_t totalsize = size * nmemb;
size_t tocopy = (putdata->len < totalsize) ? putdata->len : totalsize;
memcpy(ptr, putdata->buf, tocopy);
putdata->len -= tocopy;
putdata->buf += tocopy;
return tocopy;
}
int main()
{
CURL *curl = NULL;
put_buffer pbuf = {};
char *otherdata = "This is some other data";
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
// PUT
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
pbuf.buf = strdup("This is highly secret and sensitive data");
pbuf.len = strlen(pbuf.buf);
curl_easy_setopt(curl, CURLOPT_READDATA, &pbuf);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, pbuf.len);
curl_easy_setopt(curl, CURLOPT_URL, "http://host1.com/putsecretdata");
curl_easy_perform(curl);
// Without this line, a PUT instead of a POST will be sent below (this is a bug in libcurl)
//curl_easy_setopt(curl, CURLOPT_UPLOAD, 0L);
// Without this line, the POST below will send "This is highly secret and sensitive data"
// when instead the user intended to send "This is some other data"
// With this line, the program will attempt to use freed data, causing a segfault or any number
// of potential exploits.
//free(pbuf.buf);
// POST (will be a PUT without the line just above)
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, otherdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(otherdata));
curl_easy_setopt(curl, CURLOPT_URL, "http://host2.com/postotherdata");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
Supporting Material/References:
The bug that leads to these potential security issues is documented at https://github.com/curl/curl/issues/9507
Impact
An attacker could potentially inject data, either from stdin or from an unintended buffer. Further, without even an active attacker, this could lead to segfaults or sensitive information being exposed to an unintended recipient.
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 CURLOPT_UPLOAD option is not reset when switching from an upload to a POST, so libcurl continues to use the upload read callback and buffer.
Impact
An attacker could potentially inject data from stdin or an unintended buffer, and without an active attacker this could lead to segfaults or sensitive information being exposed to an unintended recipient.
What the attacker needed
Application reuses a curl handle; first request is an upload (PUT); second request is a POST
How it was found
- Set up a curl handle with CURLOPT_UPLOAD and a read callback that supplies secret data, then perform the upload.
- Without resetting CURLOPT_UPLOAD to 0, set CURLOPT_POST and CURLOPT_POSTFIELDS with different data, then perform the request.
- Observe that the second request is sent as a PUT using the original read callback data instead of a POST with the new data.
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.
When a program uses libcurl to send data, it can set options to say whether it wants to upload (PUT) or post (POST). If it first does an upload and then tries to do a post without resetting the upload option, libcurl mistakenly sends another upload. That means the wrong data gets sent, possibly secret data, or the program may crash if that data was already freed.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.