# [curl] CVE-2023-32001: fopen race condition

TOCTOU in curl's Curl_fopen: stat() then fopen() lets an attacker swap a symlink/dir via renameat2 so curl writes cookie/HSTS data to a protected file or leaks cookies to attacker-owned file.

- Payout: $2,480
- Severity: medium
- Program: Internet Bug Bounty
- Found by: selmelc
- Disclosed: 2023-07-27
- CVE: CVE-2023-32001
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/2078571
- Library page: https://bugbountylibrary.com/r/h1%3A2078571
- Tags: area:oss domain:oss impact:data-leak impact:integrity kind:bounty platform:hackerone sev:medium tech:linux technique:race-window technique:source-review vuln:path-traversal vuln:race-condition vuln:secrets-leak

**Target:** curl/libcurl Curl_fopen (cookie-jar, HSTS, alt-svc cache file writes)
**Root cause:** Curl_fopen calls stat() to check S_ISREG then separately fopen()s the path; the path can be swapped between check and use, so a non-regular file (symlink to protected file) is followed instead of overwritten.
**Steps:**
1. Set up shared dir: `a -> flag` symlink, `b` directory, `flag` owned by root (or attacker).
2. Attacker runs `./rename a b` looping `renameat2(AT_FDCWD,a,AT_FDCWD,b,RENAME_EXCHANGE)`.
3. Victim runs `curl --cookie-jar a google.com`.
4. When stat() sees `a` as directory (non-regular) and fopen() then opens `a` as symlink to `flag`, curl writes cookie data into `flag`.
5. Variant B: make `flag` attacker-owned so leaked cookies land in attacker-readable file, hiding them from victim.
**How found:** Code reading of lib/fopen.c lines 59-61: noticed stat() check for S_ISREG followed by a separate fopen() on the same path, an obvious TOCTOU window; then built a renameat2 RENAME_EXCHANGE swapper to win the race.
**Exploitation:** Attacker needs a shared writable directory and a victim running curl with --cookie-jar (or HSTS/alt-svc cache) pointing at a path in that dir. A tight renameat2 RENAME_EXCHANGE loop races the stat/fopen window; on win, curl follows the symlink and writes sensitive cookie data into an attacker-controlled or protected file, or overwrites a protected file with cookie data.
**Escalation:** Same primitive affects HSTS and alt-svc cache files, widening targets; can overwrite protected files like /etc/passwd (integrity/availability) though attacker doesn't control written content.
**Insight:** When auditing file-write paths, grep for stat()/access()/lstat() immediately followed by fopen()/open() on the same pathname — that check-then-use gap is exploitable with renameat2(RENAME_EXCHANGE) even without controlling file contents, and cache/cookie-jar writers are prime targets.

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

**renameat2 RENAME_EXCHANGE race helper program**

```
// source https://github.com/sroettger/35c3ctf_chals/blob/master/logrotate/exploit/rename.c
int main(int argc, char *argv[]) {
  while (1) {
    syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE);
```

**Victim curl cookie-jar command and attacker rename invocation**

```
The attacker executes ./rename a b, which will not stop to swap the name of the directory "b" and the symlink "a". 

The victim executes their command "curl --cookie-jar a google.com".
```

