CVE-2023-46219: HSTS long file name clears contents
cURL creates temporary filenames that can exceed the filesystem limit when updating the HSTS database, causing the rename to fail and the original HSTS file to be truncated to zero bytes, clearing the HSTS database.
- Triaged in 9 hours
- Fixed in 34 days
- Disclosed after 36 days
Summary:
I've discovered a significant security flaw in cURL's file handling, particularly affecting the HSTS (HTTP Strict Transport Security) database when handling long filenames.
Vulnerability Description
cURL erroneously creates temporary files with names potentially exceeding the filesystem's maximum filename length (typically 255 bytes for ext4, etc.). If a filename used in the HSTS database is longer than 243 bytes (255 bytes minus 9 for the random suffix and 4 for the '.tmp' extension), an unexpected security error occurs, leading to the HSTS database being overwritten.
Affected Code
File: curl/lib/fopen.c
CURLcode Curl_fopen(struct Curl_easy *data, const char *filename, FILE **fh, char **tempname) {
CURLcode result = CURLE_WRITE_ERROR;
unsigned char randsuffix[9]; // Random suffix generation <=======
...
tempstore = aprintf("%s.%s.tmp", filename, randsuffix); // Temporary filename creation <=======
if(!tempstore) {
result = CURLE_OUT_OF_MEMORY;
goto fail;
}
}
File: curl/lib/hsts.c
result = Curl_fopen(data, file, &out, &tempstore);
...
if(!result && tempstore && Curl_rename(tempstore, file)) // Attempt to rename temp file to actual file <======
result = CURLE_WRITE_ERROR;
if(result && tempstore)
unlink(tempstore); // Remove temp file if error occurs
Steps To Reproduce:
First let’s check the correct behaviour. I’ve created simple hsts file for cxsecurity.com domain
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
$ curl --hsts ok.hsts.txt http://cxsecurity.com -v
* Switched from HTTP to HTTPS due to HSTS => https://cxsecurity.com/
* Trying 188.114.97.1:443...
…
So works great. Let’s try update the database and add Facebook
$ curl --hsts ok.hsts.txt https://facebook.com -v
* Trying 31…
* Connected to facebook.com …
…
< Strict-Transport-Security: max-age=15552000; preload
…
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
The file has been successfully updated.
Let’s see what will happen if the user will define filename longer that 243 (let’s use the content from previous file)
$ cp ok.hsts.txt hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
Let’s validate the file size as it will be important to prove security issue.
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 179 Nov 1 19:14 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
we have 179 bytes.
If the user will use such file, curl will reset the content due to improper rename action
$ cat hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
$ curl --hsts hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt https://facebook.com -v
* Trying …
* Connected to facebook.com …
…
Let’s check the file size again..
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 0 Nov 1 19:17 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
Now the HSTS database is empty!
Tested on
#curl 8.4.1-DEV (Linux) libcurl/8.4.1-DEV OpenSSL/3.0.10 zlib/1.2.13
Possible solutions:
- Check the max length of supported file and create shortcut eg by using “~”
- Create random short file name at /tmp/
I believe addressing this vulnerability is crucial for maintaining the integrity of the HSTS database in cURL, and I'm keen to assist in any way possible to resolve this issue. Looking forward to your response.
Impact
Bypass HSTS
Context
curl is a command-line tool and library for transferring data with URLs. One of its features is an HSTS cache: when a server responds with a Strict-Transport-Security header, curl records the host and an expiry time in a local file, and on later requests it upgrades http:// URLs for that host to https:// before connecting. The cache file is user-selected with the --hsts option and is rewritten by libcurl whenever the in-memory cache changes.
The report concerns how libcurl writes that file. The HSTS database is not written in place: libcurl writes the new contents to a temporary file and then renames the temporary file over the original. The researcher found that when the chosen filename is long enough, this write-and-rename sequence fails in a way that leaves the HSTS database empty.
The weakness is classified as missing encryption of sensitive data, with low severity. The report was disclosed on 8 December 2023 under CVE-2023-46219.
Setup
The researcher tested on a Linux system with the following curl build:
#curl 8.4.1-DEV (Linux) libcurl/8.4.1-DEV OpenSSL/3.0.10 zlib/1.2.13
No accounts or special roles are involved. The only precondition is control over the filename passed to --hsts, which is the normal situation for any user of the option. The researcher used two domains, cxsecurity.com and facebook.com, to populate and then update a cache file.
Finding it
The researcher looked at the code that creates the temporary file and the code that renames it into place.
In curl/lib/fopen.c, Curl_fopen builds the temporary filename by appending a random suffix and a .tmp extension to the target filename:
CURLcode Curl_fopen(struct Curl_easy *data, const char *filename, FILE **fh, char **tempname) {
CURLcode result = CURLE_WRITE_ERROR;
unsigned char randsuffix[9]; // Random suffix generation <=======
...
tempstore = aprintf("%s.%s.tmp", filename, randsuffix); // Temporary filename creation <=======
if(!tempstore) {
result = CURLE_OUT_OF_MEMORY;
goto fail;
}
}
In curl/lib/hsts.c, the caller writes through that temporary file and then renames it over the real file:
result = Curl_fopen(data, file, &out, &tempstore);
...
if(!result && tempstore && Curl_rename(tempstore, file)) // Attempt to rename temp file to actual file <======
result = CURLE_WRITE_ERROR;
if(result && tempstore)
unlink(tempstore); // Remove temp file if error occurs
The problem the researcher identified is that the temporary name is longer than the original name. The random suffix is 9 bytes and the .tmp extension is 4 bytes, so the temporary name adds 13 bytes. On a filesystem whose maximum filename length is 255 bytes (the researcher cites ext4 as the typical case), a target filename longer than 243 bytes (255 minus 9 minus 4) produces a temporary name that exceeds the limit. The temporary name exceeds the limit, and the researcher describes the outcome as the HSTS database being overwritten. The researcher describes the outcome as the HSTS database being overwritten.
Exploitation
Step 1: confirm normal behaviour
The researcher first created a small HSTS file for cxsecurity.com:
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
A request to the plain HTTP URL is then upgraded to HTTPS because of the cached entry:
$ curl --hsts ok.hsts.txt http://cxsecurity.com -v
* Switched from HTTP to HTTPS due to HSTS => https://cxsecurity.com/
* Trying [redacted]:443...
…
The verbose output shows the upgrade happening before the connection is made, which is the feature working as intended.
Step 2: confirm the cache is updated
The researcher then made a request to facebook.com using the same file. The response carried an HSTS header:
$ curl --hsts ok.hsts.txt https://facebook.com -v
* Trying 31…
* Connected to facebook.com …
…
< Strict-Transport-Security: max-age=15552000; preload
…
Reading the file back shows the new entry appended alongside the existing one:
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
The file has been successfully updated, so the write-and-rename path works for a short filename.
Step 3: create a filename longer than 243 bytes
The researcher copied the populated file to a name built by repeating hsts. many times and ending in .txt, using the content from the previous file:
$ cp ok.hsts.txt hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
The size of the file is checked because it matters for proving the issue later:
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 179 Nov 1 19:14 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
The file is 179 bytes and contains the two entries shown earlier.
Step 4: use the long filename and watch the database empty
Before the request, the file still holds both entries:
$ cat hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
A request is then made with the long filename:
$ curl --hsts hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt https://facebook.com -v
* Trying …
* Connected to facebook.com …
…
Checking the size again shows the file has been truncated to zero bytes:
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 0 Nov 1 19:17 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
The HSTS database is now empty. The 179 bytes of cached entries are gone, and the file that remains is 0 bytes.
Root cause
The temporary filename is derived from the target filename by appending a 9-byte random suffix and a 4-byte .tmp extension, so it is always 13 bytes longer than the target. When the target name is longer than 243 bytes on a filesystem with a 255-byte limit, the temporary name cannot exist. The researcher's description is that the HSTS database is overwritten as a result.
The report does not show the exact error output from the failing Curl_fopen call or the rename, so the precise sequence of failures inside libcurl is not documented. What is demonstrated is the observable result: a populated 179-byte cache file becomes a 0-byte file after a single request made with a long --hsts filename.
Impact
The stated impact is bypassing HSTS. If the HSTS cache is emptied, the entries that would have upgraded http:// requests to https:// are no longer present, so a later request to an affected host can proceed over plain HTTP. The stated impact is bypassing HSTS. The report does not describe a specific attack chain beyond this, and the severity assigned is low. The condition is that the user chooses an HSTS filename longer than 243 bytes on a filesystem with a 255-byte filename limit.
Fix
The report proposes two possible solutions rather than describing an implemented change:
- Check the maximum supported filename length and create a shortened name, for example by using
~. - Create a random short filename in
/tmp/.
Variants to test
- Try other curl features that write state files through a temporary file and rename, such as cookie jars, with a long output filename. A positive result is the state file being truncated to 0 bytes after a request.
- Try filenames just below and just above the 243-byte threshold on a filesystem with a 255-byte limit. A positive result is the file surviving at 243 bytes and being emptied at 244 bytes and above.
- Try the same long filename on filesystems with different maximum filename lengths. A positive result is the truncation appearing at the corresponding threshold (limit minus 13 bytes).
- Try a long filename where the target file does not yet exist. A positive result is the file being created empty or not created at all, depending on where the failure occurs.
What happened, in plain words
curl is a small program that fetches things from the internet when you type a command in a terminal, instead of clicking in a browser. It is also a library, meaning other programs can borrow its code to do the same job. One of curl's features is an HSTS cache. HSTS stands for HTTP Strict Transport Security, and it is a rule a website can send to say "always talk to me over the encrypted version of the web, never the plain one". When a site sends that rule, curl writes the site's name and an expiry date into a small text file on your computer, so it remembers the rule next time. On later visits it quietly turns http:// into https:// before connecting.
You choose that text file yourself with the --hsts option. Every time the list of remembered sites changes, curl rewrites the file. It does not write over the old file directly. Instead it writes the new contents into a temporary file next to it, then renames the temporary file so it takes the place of the original. This is a common trick: if something goes wrong halfway through writing, the original file is still intact.
The researcher found that when the filename you choose is long enough, this write-then-rename dance goes wrong, and the HSTS file ends up empty. The weakness is filed as "missing encryption of sensitive data" with low severity, and it was made public on 8 December 2023 under the identifier CVE-2023-46219. A CVE is a public catalogue number for a known security flaw, so people can look it up and track fixes.
Setup
The researcher tested on a Linux system with this curl build:
#curl 8.4.1-DEV (Linux) libcurl/8.4.1-DEV OpenSSL/3.0.10 zlib/1.2.13
That line is curl's own description of itself: the version number, the operating system, and the helper libraries it was built with (OpenSSL handles encryption, zlib handles compression). You do not need any account or special permission to trigger the issue. The only thing required is that you pick the filename passed to --hsts, which is exactly what any user of that option does. The researcher used two websites, cxsecurity.com and facebook.com, first to fill the cache file and then to update it.
Finding it
The researcher read the parts of curl's source code that create the temporary file and then rename it into place. Source code is the human-written recipe that the computer follows; reading it is how you spot a mistake before it bites anyone.
In the file curl/lib/fopen.c, a function called Curl_fopen builds the temporary filename by sticking a random suffix and a .tmp ending onto the name you gave:
CURLcode Curl_fopen(struct Curl_easy *data, const char *filename, FILE **fh, char **tempname) {
CURLcode result = CURLE_WRITE_ERROR;
unsigned char randsuffix[9]; // Random suffix generation <=======
...
tempstore = aprintf("%s.%s.tmp", filename, randsuffix); // Temporary filename creation <=======
if(!tempstore) {
result = CURLE_OUT_OF_MEMORY;
goto fail;
}
}
Line by line:
CURLcode Curl_fopen(...)is the start of the function. It takes the name of the file you want to write (filename) and hands back a handle to the temporary file (fh) plus the temporary file's name (tempname).CURLcode result = CURLE_WRITE_ERROR;sets up a default answer meaning "writing failed", so that if anything goes wrong later the function already has a sensible error to report.unsigned char randsuffix[9];reserves space for 9 random bytes. These are added to the temporary name so two curl runs at the same time do not fight over the same temporary file.tempstore = aprintf("%s.%s.tmp", filename, randsuffix);is the key line. It glues together your filename, a dot, the random suffix, and.tmp. Somyfilebecomes something likemyfile.a1b2c3d4.tmp.if(!tempstore) { ... }checks whether that name could be built at all. If the computer ran out of memory, it recordsCURLE_OUT_OF_MEMORYand jumps to the cleanup labelfail.
In the file curl/lib/hsts.c, the code that manages the HSTS list calls that function, writes through the temporary file, and then renames it over the real file:
result = Curl_fopen(data, file, &out, &tempstore);
...
if(!result && tempstore && Curl_rename(tempstore, file)) // Attempt to rename temp file to actual file <======
result = CURLE_WRITE_ERROR;
if(result && tempstore)
unlink(tempstore); // Remove temp file if error occurs
Line by line:
result = Curl_fopen(data, file, &out, &tempstore);opens the temporary file for writing and stores its name intempstore.if(!result && tempstore && Curl_rename(tempstore, file))says: if opening worked, and we have a temporary name, try to rename the temporary file to the real filename. If the rename fails, the whole thing is marked as a write error.if(result && tempstore) unlink(tempstore);is the cleanup. If anything went wrong, delete the leftover temporary file so it does not clutter the disk.unlinkis the system call that removes a file.
The problem the researcher spotted is that the temporary name is always longer than the original name. The random suffix is 9 bytes and the .tmp ending is 4 bytes, so 13 extra bytes are added. Filesystems, the systems that organise files on disk, put a limit on how long a single filename can be. The researcher cites 255 bytes as the typical limit on ext4, the common Linux filesystem. If your chosen filename is longer than 243 bytes (255 minus 9 minus 4), the temporary name is over the limit. The temporary name is over the limit, and the researcher describes the end result as the HSTS database being overwritten. The researcher describes the end result as the HSTS database being overwritten.
Exploitation
Step 1: confirm normal behaviour
First the researcher made a small HSTS file for cxsecurity.com:
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
cat is a command that prints a file's contents to the screen. The first two lines are comments starting with #, written by curl itself. The third line is the actual entry: the site name and, in quotes, the date and time the rule expires.
A request to the plain HTTP address is then upgraded to HTTPS because of that cached entry:
$ curl --hsts ok.hsts.txt http://cxsecurity.com -v
* Switched from HTTP to HTTPS due to HSTS => https://cxsecurity.com/
* Trying [redacted]:443...
…
--hsts ok.hsts.txt tells curl which file to use as its memory. The -v means verbose: print the details of what is happening. The output line Switched from HTTP to HTTPS due to HSTS shows the upgrade happening before any connection is made. That is the feature working as intended, and it gives a baseline to compare against later.
Step 2: confirm the cache is updated
The researcher then visited facebook.com using the same file. The reply from the site carried an HSTS header, which is a line of information the server sends along with the page:
$ curl --hsts ok.hsts.txt https://facebook.com -v
* Trying 31…
* Connected to facebook.com …
…
< Strict-Transport-Security: max-age=15552000; preload
…
Lines starting with * are curl's own notes about what it is doing. Lines starting with < are what the server sent back. Strict-Transport-Security is the header that carries the HSTS rule. max-age=15552000 says how many seconds the rule lasts, and preload is a hint that the site wants to be on built-in lists shipped with browsers.
Reading the file back shows the new entry added next to the old one:
$ cat ok.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
The file has been successfully updated, so the write-and-rename path works fine when the filename is short. This step matters because it proves the mechanism is sound in normal conditions, so any later breakage is caused by the long name and nothing else.
Step 3: create a filename longer than 243 bytes
The researcher copied the filled-in file to a new name made by repeating hsts. many times and ending in .txt, keeping the same contents:
$ cp ok.hsts.txt hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
cp copies a file. The first name is the source, the second is the destination. The destination is deliberately long, past the 243-byte threshold described above, so that curl's temporary name will go over the filesystem limit.
The size of the file is checked because it matters for proving the issue later:
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 179 Nov 1 19:14 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
ls -la lists files with details. The 179 in the middle is the size in bytes, and the rest shows permissions, owner and date. The file is 179 bytes and holds the two entries shown earlier. Recording this number now means that if the file later shrinks, we can say exactly how much was lost.
Step 4: use the long filename and watch the database empty
Before the request, the file still holds both entries:
$ cat hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
# Your HSTS cache. https://curl.se/docs/hsts.html
# This file was generated by libcurl! Edit at your own risk.
cxsecurity.com "20241031 12:12:12"
facebook.com "20240430 00:11:44"
This is the "before" picture. Both remembered sites are present, so the cache is in a healthy state.
A request is then made with the long filename:
$ curl --hsts hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt https://facebook.com -v
* Trying …
* Connected to facebook.com …
…
Nothing in the visible output looks alarming. curl connects to the site as usual. The damage happens quietly behind the scenes when curl tries to save the updated cache.
Checking the size again shows the file has been cut down to zero bytes:
$ ls -la hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
-rw-r--r-- 1 cx cx 0 Nov 1 19:17 hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.hsts.txt
The 0 where 179 used to be is the proof. The HSTS database is now empty. The 179 bytes of remembered sites are gone, and the file that remains is 0 bytes.
Root cause
The temporary filename is built from the target filename by adding a 9-byte random suffix and a 4-byte .tmp ending, so it is always 13 bytes longer than the target. When the target name is longer than 243 bytes on a filesystem with a 255-byte limit, the temporary name cannot exist. The researcher's description is that the HSTS database is overwritten as a result.
The report does not show the exact error message from the failing Curl_fopen call or the rename, so the precise sequence of failures inside curl is not documented. What is demonstrated is the visible result: a filled 179-byte cache file becomes a 0-byte file after a single request made with a long --hsts filename.
Impact
The stated impact is bypassing HSTS. If the HSTS cache is emptied, the entries that would have turned http:// into https:// are no longer there, so a later request to an affected site can go over plain HTTP. The stated impact is bypassing HSTS. The report does not describe a specific attack chain beyond this, and the severity assigned is low. The condition is that the user chooses an HSTS filename longer than 243 bytes on a filesystem with a 255-byte filename limit.
Fix
The report proposes two possible solutions rather than describing a change that was actually made:
- Check the maximum supported filename length and create a shortened name, for example by using
~. - Create a random short filename in
/tmp/.
/tmp/ is a standard scratch folder on Linux where programs can put temporary files.
Variants to test
- Try other curl features that save state files through a temporary file and rename, such as cookie jars, with a long output filename. A positive result is the state file being cut down to 0 bytes after a request.
- Try filenames just below and just above the 243-byte threshold on a filesystem with a 255-byte limit. A positive result is the file surviving at 243 bytes and being emptied at 244 bytes and above.
- Try the same long filename on filesystems with different maximum filename lengths. A positive result is the emptying appearing at the matching threshold (limit minus 13 bytes).
- Try a long filename where the target file does not yet exist. A positive result is the file being created empty or not created at all, depending on where the failure happens.
Glossary
- curl: a command-line program and library for fetching data from URLs.
- libcurl: the library part of curl that other programs can use.
- HSTS (HTTP Strict Transport Security): a rule a website sends telling clients to always use the encrypted HTTPS version and never the plain HTTP one.
- HSTS cache: the local file where curl remembers HSTS rules and their expiry dates.
--hsts: the curl option that chooses which file to use as the HSTS cache.Strict-Transport-Security: the HTTP response header that carries an HSTS rule.max-age: the part of that header saying how many seconds the rule lasts.preload: a hint in that header that the site wants to be on built-in HSTS lists.- HTTPS: the encrypted version of the web protocol, protected by TLS.
- HTTP: the plain, unencrypted version of the web protocol.
- Header: a line of extra information sent along with a web request or response.
- Temporary file: a scratch file written first, then renamed over the real one, so the real file is not damaged if writing fails.
- Rename: the operation that gives a file a new name, or moves it over an existing file.
unlink: the system call that deletes a file.- Filesystem: the system that organises files and folders on a disk.
- ext4: a common Linux filesystem.
- Byte: a unit of data; one character of text is usually one byte.
- Filename length limit: the maximum number of bytes a filesystem allows in a single file name, cited here as 255.
- Source code: the human-written instructions that make up a program.
- Function: a named block of code that does one job.
cat: a command that prints a file's contents.cp: a command that copies a file.ls -la: a command that lists files with details such as size and permissions.-v(verbose): a curl option that prints what curl is doing.- CVE: a public catalogue number for a known security flaw.
- Severity: a rough rating of how serious a flaw is, here low.
- Missing encryption of sensitive data: the weakness category assigned to this report.
- Bypass: getting around a protection so it no longer applies.
/tmp/: a standard scratch folder on Linux for temporary files.- Cookie jar: a curl feature that saves cookies to a file, mentioned as another place to look for the same pattern.