CVE-2020-8177: curl overwrite local file with -J
curl's handling of the Content-disposition filename option had two logic flaws that let a malicious server overwrite an existing local file when curl was run with the -OJi flags. Because the overwritten file could be something like a shell startup file in the user's home directory, this could lead to code execution or data loss while the user was told the file was refused.
- Triaged in 3 days
- Fixed in 25 days
- Paid in 3 days
- Disclosed after 6 months
Summary:
curl supports the Content-disposition header, including the filename= option. By design, curl does not allow server-provided local file override by verifying that the filename= argument does not exist before opening it.
However, the implementation contains 2 minor logical bugs that allow a server to override an arbitrary local file (without path traversal) when running curl with specific command line args (-OJi)
This bug can trigger a logical RCE when curl is used from the user's home dir (or other specific directories), by overriding specific files (e.g. ".bashrc"), while keeping the user completely uninformed of the side effects.
The 2 bugs are:
curl -iJis not supported howevercurl -Jiis available -- The standard
Content-dispositionhandling flow does not allow opening existing files: https://github.com/curl/curl/blob/master/src/tool_cb_wrt.c#L54, however by using-OJiit is possible to reach a flow that overrides a local file with the response headers, without verification: https://github.com/curl/curl/blob/master/src/tool_cb_hdr.c#L196
Steps To Reproduce:
- Return the following http response form a server :
HTTP/1.1 200 OK
<PAYLOAD>
Content-disposition: attachment; filename=".bashrc"
Where <PAYLOAD> is the bash payload, e.g. echo pwn
- Run
curl -OJifrom the user's home dir
Note that curl falsely claims that .bashrc was refused to be overwritten.
Supporting Material/References:
First bug:
case 'i':
config->show_headers = toggle; /* show the headers as well in the
general output stream */
break;
...
case 'J': /* --remote-header-name */
if(config->show_headers) {
warnf(global,
"--include and --remote-header-name cannot be combined.\n");
return PARAM_BAD_USE;
}
config->content_disposition = toggle;
break;
Second bug:
if(filename) {
if(outs->stream) {
int rc;
/* already opened and possibly written to */
if(outs->fopened)
fclose(outs->stream);
outs->stream = NULL;
/* rename the initial file name to the new file name */
rc = rename(outs->filename, filename);
if(rc != 0) {
warnf(per->config->global, "Failed to rename %s -> %s: %s\n",
outs->filename, filename, strerror(errno));
}
if(outs->alloc_filename)
Curl_safefree(outs->filename);
if(rc != 0) {
free(filename);
return failure;
}
}
Impact
Local file override without path traversal, possibly leading to an RCE or loss of data.
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 option-parsing check only rejected one ordering of the -i and -J flags, and the header-writing path could rename over an existing file without the existence check used by the normal download path.
Impact
A malicious server could overwrite a local file without path traversal, possibly leading to code execution or loss of data, while the user is misled into thinking the file was protected.
What the attacker needed
Curl run with -OJi; run from home or similar directory; server controls response headers
How it was found
- Have the server return a response whose Content-disposition header names an existing local file such as a shell startup file.
- Run curl with the -OJi options from the user's home directory.
- Observe that curl claims the file was refused but the local file is overwritten with the response content.
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.
curl is a command-line tool for downloading things from the internet. Normally it refuses to save a download over a file that already exists, but a mistake in how it combined two options meant a hostile server could still replace a file on your computer. If that file is one your shell reads at startup, the attacker's content could run as code the next time you open a terminal.
What is File upload?
An upload feature accepts a file that the server or other users' browsers then treat as code or as something trusted.