CVE-2022-27778: curl removes wrong file on error
When curl is used with both --no-clobber and --remove-on-error, and an error occurs during transfer, it removes the original file instead of the newly created numbered file. This can cause data loss of an existing file that was meant to be preserved.
- Fixed in 36 hours
- Paid in 36 hours
- Disclosed after 37 hours
Summary by the program
curl removes wrong file on error
Project curl Security Advisory, May 11 2022 - Permalink
VULNERABILITY
curl might remove the wrong file when --no-clobber is used together with --remove-on-error.
The --remove-on-error option tells curl to remove the output file when it returns an error, and not leave a partial file behind. The --no-clobber option prevents curl from overwriting a file if it already exists, and instead append a number to the name to create a new unused file name.
If curl adds a number to not "clobber" the output and an error occur during transfer, the remove on error logic would remove the original file name without the added number.
We are not aware of any exploit of this flaw.
INFO
This flaw is only present in the command line tool, not the library.
This flaw was introduced in curl 7.83.0 with the addition of these two options that together created this problem. Since both these command line options are brand new in this version, chances are not too many users have actually had time to experience this problem.
The Common Vulnerabilities and Exposures (CVE) project has assigned the name CVE-2022-27778 to this issue.
CWE-706: Use of Incorrectly-Resolved Name or Reference
Severity: Medium
AFFECTED VERSIONS
THE SOLUTION
RECOMMENDATIONS
A - Upgrade curl to version 7.83.1
B - Apply the patch to your local version
C - Do not use --no-clobber with --remove-on-error
TIMELINE
This issue was reported to the curl project on April 28, 2022. We contacted distros@openwall on May 5.
libcurl 7.83.1 was released on May 11 2022, coordinated with the publication of this advisory.
LINK TO THE ADVISORY
https://curl.se/docs/CVE-2022-27778.html
The report
Summary:
Curl command has a logic flaw that results in removal of a wrong file when combining --no-clobber and --remove-on-error if the target file name exists and an error occurs.
Steps To Reproduce:
echo "important file" > fooecho -ne "HTTP/1.1 200 OK\r\nContent-Length: 666\r\n\r\nHello\n" | nc -l -p 9999curl -m 3 --no-clobber --remove-on-error --output foo http://testserver.tld:9999/ls -l foo*cat foo.1
-m 3 is used here to simulate a denial of service of the connection performed by the attacker.
Impact
Removal of a file that was supposed not to be overwritten (data loss). Incomplete file left of disk when it should have been removed. This can lead to potential loss of integrity or availability.
Context
curl is a command line tool for transferring data with URLs, and the same codebase ships the libcurl library. This report concerns two command line options that were both introduced in curl 7.83.0:
--remove-on-errortells curl to remove the output file when it returns an error, so that a partial file is not left behind.--no-clobberprevents curl from overwriting a file that already exists. Instead of overwriting, curl appends a number to the name to create a new, unused file name.
The flaw is a business logic error in how those two options interact. When curl has appended a number to avoid clobbering an existing file and an error then occurs during the transfer, the remove-on-error logic removes the original file name, without the added number. The result is that the file the user explicitly asked curl not to overwrite is deleted, while the partial file that should have been cleaned up stays on disk.
The issue is tracked as CVE-2022-27778 and classified as CWE-706: Use of Incorrectly-Resolved Name or Reference. The severity is Medium. The flaw is only present in the command line tool, not in the library.
Setup
The reproduction uses a shell, nc and an affected curl build. The affected version is curl 7.83.0. Versions before 7.83.0 are not affected, because neither option existed yet, and versions from 7.83.1 onwards are not affected.
The reproduction uses a listener on port 9999 and a pre-existing file named foo. The -m 3 timeout in the curl command is there to simulate a denial of service of the connection performed by the attacker: the server sends a response that promises more data than it delivers, and the transfer fails when the timeout expires.
Finding it
The report describes a logic flaw in the combination of --no-clobber and --remove-on-error. The condition is specific: the target file name already exists, so curl picks a numbered alternative, and then an error occurs during the transfer. In that situation the two features disagree about which file they are operating on. The cleanup path resolves the name without the number that the clobber-avoidance path added.
Exploitation
Step 1: create the file that must not be overwritten
echo "important file" > foo
This creates foo with known content. It stands in for any pre-existing file the user wants to keep, and it is the file that --no-clobber is supposed to protect.
Step 2: start a server that will fail mid-transfer
echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 666\r\n\r\nHello\n" | nc -l -p 9999
This listens on port 9999 and answers with a 200 OK response whose Content-Length header announces 666 bytes, while the body actually sent is only Hello. The client therefore waits for the remaining bytes, and the transfer cannot complete successfully.
Step 3: run curl with both options against the listener
curl -m 3 --no-clobber --remove-on-error --output foo http://testserver.tld:9999/
--output foo names the intended output file. Because foo already exists, --no-clobber makes curl write to a numbered name instead. --remove-on-error instructs curl to delete the output file if the transfer ends in an error. -m 3 is used to simulate a denial of service of the connection performed by the attacker.
Step 4: list the files
ls -l foo*
The report states that the original file name is removed on error.
Step 5: inspect the leftover file
cat foo.1
The report states that an incomplete file is left on disk when it should have been removed.
Root cause
The remove-on-error path removes the original name it was given, without the number that was added. --no-clobber detects the existing file and derives a new name by appending a number. The remove-on-error path, when the transfer fails, removes the file under the original name it was given, without the number that was added. The name is resolved incorrectly at the point of deletion, which is why the CVE is classified as CWE-706.
The flaw was introduced in curl 7.83.0 together with the two options themselves. Because both options were brand new in that version, the advisory notes that chances are not too many users had actually had time to experience the problem. The advisory also states that the curl project is not aware of any exploit of this flaw.
Impact
The direct consequence is data loss: a file that was explicitly not supposed to be overwritten is removed. At the same time, an incomplete file is left on disk where it should have been removed. The report describes this as a potential loss of integrity or availability.
Two conditions must hold for the bug to trigger: the output file name must already exist, so that --no-clobber picks a numbered alternative, and an error must occur during the transfer. The -m 3 timeout in the reproduction simulates a denial of service of the connection. The flaw affects only the command line tool, not the library, and only version 7.83.0.
Fix
The advisory links a fix for CVE-2022-27778. The advisory gives three recommendations:
- Upgrade curl to version 7.83.1.
- Apply the patch to your local version.
- Do not use
--no-clobberwith--remove-on-error.
curl 7.83.1 was released on May 11 2022, coordinated with the publication of the advisory.
Variants to test
- Other pairs of options that derive a file name and later act on it. A positive result is a cleanup, rename or overwrite step that uses the name originally supplied rather than the name actually written.
- Numbered-name generation in other tools that avoid clobbering (downloaders, archivers, editors with backup files). Look for a failure path that deletes or truncates the base name while the numbered file survives.
- Interrupted transfers against a server that stalls or closes early, combined with any automatic cleanup flag. A positive result is the pre-existing file disappearing while the partial file remains.
Hunting notes
This class of bug hides in option combinations rather than in a single request handler: one code path chooses a resource name, another later resolves the name again from the original input. The signal is a feature that renames or avoids overwriting, paired with a feature that deletes or rolls back on failure. Reading the advisory and commit history for a project is often enough to spot where the two paths meet.
The issue was reported to the curl project on April 28 2022, distributions were contacted on May 5, and 7.83.1 was released on May 11 2022.
What happened, in plain words
curl is a program you run in a terminal (the text window where you type commands) to download things from the internet. You give it a web address, and it fetches the content and saves it to a file. It is also the engine behind many other programs, packaged as a reusable piece of code called a library, libcurl.
In version 7.83.0, curl gained two new options, which are extra instructions you add to a command to change its behaviour:
--remove-on-errortells curl: if the download fails, delete the file you were writing, so no half-finished file is left behind.--no-clobbertells curl: if a file with the name I asked for already exists, do not overwrite it. Instead, add a number to the name and write to that new name.
The bug is in how these two options interact. Suppose you ask curl to save to a file called foo, and foo already exists. --no-clobber does its job and writes to a numbered name instead. Then the download fails. --remove-on-error should delete the numbered file. Instead it deletes foo, the original file you wanted to protect. So the file you cared about is gone, and the junk file stays.
This is a logic error: the program does exactly what its code says, but the code's reasoning is wrong. The security world calls this kind of mistake a business logic error, and this particular one is catalogued as CWE-706, "Use of Incorrectly-Resolved Name or Reference", because the cleanup step looks up the wrong name. The issue is tracked as CVE-2022-27778. A CVE is a public ID number given to a known security flaw so everyone can refer to the same thing. Its severity is rated Medium.
The flaw exists only in the curl command line tool, not in the libcurl library. It was introduced in curl 7.83.0, the same version that added the two options, and it was fixed in 7.83.1.
Setup
To reproduce the bug you need a terminal, the nc tool (netcat, a small program that can listen on a network port and send back whatever you tell it), and a copy of curl 7.83.0. Older versions are not affected because the two options did not exist yet. Versions from 7.83.1 onwards are not affected because the bug was fixed.
The reproduction uses a fake server on your own machine, listening on port 9999. A port is like a numbered door on a computer that network traffic can arrive at. The reproduction also uses a file called foo in the current folder as the file that already exists.
The -m 3 part of the curl command sets a three-second time limit. It is there to simulate an attacker who makes the connection hang: the fake server promises more data than it sends, so curl waits, the clock runs out, and the download ends in an error.
Finding it
The two options disagree about which file they are working on. The condition is narrow: the target file name must already exist, so --no-clobber picks a numbered name, and then an error must happen during the transfer. In that situation, the part of curl that cleans up after a failure uses the original name, without the number that the other part added.
Exploitation
Step 1: create the file that must not be overwritten
echo "important file" > foo
echo prints text. The > symbol redirects that text into a file instead of the screen. So this line creates a file named foo containing the words important file. It stands for any file you already have and want to keep, and it is the file --no-clobber is supposed to protect.
Step 2: start a server that will fail mid-transfer
echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 666\r\n\r\nHello\n" | nc -l -p 9999
This is a hand-made web response. echo -ne prints the text without adding a newline and interprets the backslash codes. \r\n means a carriage return followed by a line feed, the pair of invisible characters that end a line in the HTTP protocol, the language browsers and servers use to talk. | (the pipe) sends that text into nc, which listens (-l) on port -p 9999 and hands the text to whoever connects.
The response says HTTP/1.1 200 OK, which means "the request succeeded". The header Content-Length: 666 announces that the body will be 666 bytes long. The body actually sent is only Hello. A header is a labelled line of extra information sent before the content. Because the announced length is far bigger than the real content, curl keeps waiting for the missing bytes and the transfer cannot finish successfully.
Step 3: run curl with both options against the listener
curl -m 3 --no-clobber --remove-on-error --output foo http://testserver.tld:9999/
--output foo says where to save the download. Because foo already exists, --no-clobber makes curl write to a numbered name instead. --remove-on-error tells curl to delete the output file if the transfer ends in an error. -m 3 is used to simulate a denial of service of the connection performed by the attacker. http://testserver.tld:9999/ is the address being fetched, with :9999 naming the port where the fake server is listening.
Step 4: list the files
ls -l foo*
ls lists files, -l asks for the long format with details such as size and date, and foo* means "every file whose name starts with foo". The report states that the original file name is removed on error.
Step 5: inspect the leftover file
cat foo.1
cat prints a file's contents to the screen. The report states that an incomplete file is left on disk when it should have been removed.
Root cause
The remove-on-error path deletes the file under the original name it was given. --no-clobber sees the existing file and builds a new name by adding a number. The remove-on-error path, when the transfer fails, deletes the file under the original name it was given, without the number that was added. The name is resolved incorrectly at the moment of deletion, which is why the CVE is classified as CWE-706.
The flaw arrived in curl 7.83.0 together with the two options. Because both were brand new in that version, the advisory notes that chances are not too many users had actually had time to experience the problem. The advisory also states that the curl project is not aware of any exploit of this flaw.
Impact
The direct result is data loss: a file that was explicitly not supposed to be overwritten is removed. At the same time, an incomplete file is left on disk where it should have been removed. The report describes this as a potential loss of integrity or availability. Integrity means data staying correct and unchanged; availability means data being there when you need it.
Two conditions must both hold for the bug to trigger: the output file name must already exist, so that --no-clobber picks a numbered alternative, and an error must occur during the transfer. The -m 3 timeout in the reproduction simulates a denial of service of the connection. The flaw affects only the command line tool, not the library, and only version 7.83.0.
Fix
The advisory links a fix for CVE-2022-27778. The advisory gives three recommendations:
- Upgrade curl to version 7.83.1.
- Apply the patch to your local version. A patch is a small change to source code that repairs the flaw.
- Do not use
--no-clobberwith--remove-on-error.
curl 7.83.1 was released on May 11 2022, coordinated with the publication of the advisory.
Variants to test
- Other pairs of options that derive a file name and later act on it. A positive result is a cleanup, rename or overwrite step that uses the name originally supplied rather than the name actually written.
- Numbered-name generation in other tools that avoid clobbering (downloaders, archivers, editors with backup files). Look for a failure path that deletes or truncates the base name while the numbered file survives.
- Interrupted transfers against a server that stalls or closes early, combined with any automatic cleanup flag. A positive result is the pre-existing file disappearing while the partial file remains.
Hunting notes
This class of bug hides in option combinations rather than in a single request handler. One code path chooses a resource name, another later resolves the name again from the original input. The signal is a feature that renames or avoids overwriting, paired with a feature that deletes or rolls back on failure. Reading the advisory and commit history for a project is often enough to spot where the two paths meet.
The issue was reported to the curl project on April 28 2022, distributions were contacted on May 5, and 7.83.1 was released on May 11 2022.
Glossary
- curl: a command line program for transferring data with URLs, also available as the libcurl library.
- libcurl: the reusable library version of curl, used by other programs.
- Command line tool: a program you run by typing commands in a terminal rather than clicking in a window.
- Terminal: the text window where you type commands.
- Option / flag: an extra word starting with
--added to a command to change its behaviour. --remove-on-error: a curl option that deletes the output file if the transfer fails.--no-clobber: a curl option that avoids overwriting an existing file by adding a number to the name.- Business logic error: a flaw where the program follows its own rules correctly but the rules themselves are wrong.
- CWE-706: the catalogue entry "Use of Incorrectly-Resolved Name or Reference", for bugs that act on the wrong name.
- CVE: a public ID number assigned to a known security flaw.
- Severity: a rating of how serious a flaw is; here, Medium.
- nc (netcat): a small tool that can listen on a network port and send or receive data.
- Port: a numbered door on a computer where network traffic arrives.
- HTTP: the protocol browsers and servers use to exchange requests and responses.
- Header: a labelled line of extra information sent before the content of an HTTP message.
- Content-Length: an HTTP header that announces how many bytes the body will contain.
- Timeout: a time limit after which an operation is stopped and treated as failed.
- Denial of service: making a service or connection unusable, for example by stalling it.
- Pipe (
|): a shell symbol that sends the output of one command into another. - Redirect (
>): a shell symbol that sends output into a file instead of the screen. ls: a command that lists files.cat: a command that prints a file's contents.- Patch: a small code change that repairs a flaw.
- Integrity: data staying correct and unchanged.
- Availability: data being there when you need it.
- Data loss: the disappearance of data you wanted to keep.