# CVE-2022-27778: curl removes wrong file on error

curl with --no-clobber and --remove-on-error deletes the original existing file instead of the numbered new file when a transfer error occurs, causing data loss.

- Payout: $2,400
- Severity: medium
- Program: Internet Bug Bounty
- Found by: nyymi
- Disclosed: 2022-05-12
- CVE: CVE-2022-27778
- Write-up quality: 6/10
- Original: https://hackerone.com/reports/1565623
- Library page: https://bugbountylibrary.com/r/h1%3A1565623
- Tags: area:oss domain:oss impact:dos impact:integrity kind:bounty platform:hackerone sev:medium tech:curl technique:source-review vuln:business-logic

**Target:** curl CLI (--no-clobber + --remove-on-error options)
**Root cause:** When --no-clobber appends a number to avoid overwriting an existing file, the --remove-on-error cleanup logic still references the original unnumbered filename, so it deletes the wrong file.
**Steps:**
1. `echo "important file" > foo`
2. Serve a truncated response: `echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 666\r\n\r\nHello\n" | nc -l -p 9999`
3. `curl -m 3 --no-clobber --remove-on-error --output foo http://testserver.tld:9999/` (timeout simulates error)
4. `ls -l foo*` shows foo.1 (partial) remains and foo is gone
5. `cat foo.1` shows partial content
**How found:** Reporter reasoned about interaction between two newly added options: --no-clobber renames output to a numbered file, but --remove-on-error cleanup path likely still targets the original name; tested by pre-creating the file and forcing an error.
**Exploitation:** Attacker controls the server to force a transfer error (e.g. truncated body / connection drop) while victim uses both flags; curl then deletes the pre-existing original file, leaving the partial numbered file. Requires victim to invoke curl with both flags against attacker-influenced URL.
**Insight:** When two features both manipulate the same output filename (rename-on-collision vs delete-on-error), test their interaction: pre-create the target, force an error, and check which path the cleanup code actually unlinks.

## Proof of concept (copied exactly from the original report)

**Reproduction steps for wrong-file removal**

```
1. `echo "important file" > foo`
  2. `echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 666\r\n\r\nHello\n" | nc -l -p 9999`
  3. `curl -m 3 --no-clobber --remove-on-error --output foo http://testserver.tld:9999/
```

