Skip to content
File upload Disclosed report Intermediate

libcurl cache updates follow symlinks and truncate their targets

When curl updates its HSTS, Alt-Svc, or cookie cache, it opens the supplied path with write mode before validating it, so a symlink path can truncate the file it targets. An attacker who can control the cache-path symlink can erase or alter the target file.

mr4bugs Follow

Originally disclosed on HackerOne

  • Disclosed after 16 hours

Summary: When curl updates an HSTS, Alt-Svc, or cookie cache, it opens the supplied path with "w" before validating it, so a symlink path can truncate the file it targets. Root cause: Curl_fopen() calls curlx_fopen(filename, "w") before checking the path or creating the safe temporary file, and fopen follows the symlink immediately. Steps to reproduce: (1) create other.txt containing DO NOT TRUNCATE; (2) symlink hsts.txt to other.txt; (3) run /tmp/curl-build2/src/curl --max-time 2 -s -o /dev/null --hsts hsts.txt http://127.0.0.1:1/ || true; (4) read other.txt and observe it is empty while the symlink path has been replaced by curl’s cache file.

Suggested fix: Use lstat() before opening, reject symlinks or non-regular files, create the temporary file with exclusive no-follow flags, and write and rename only after validation

Impact

A local user who can control the cache-path symlink can erase or alter the target file, and a failed cache update can also destroy an existing cache.

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Curl_fopen() calls curlx_fopen(filename, "w") before checking the path or creating the safe temporary file, and fopen follows the symlink immediately.

Impact

A local user who can control the cache-path symlink can erase or alter the target file, and a failed cache update can also destroy an existing cache.

What the attacker needed

Local access to control the cache-path symlink

The fix

Use lstat() before opening, reject symlinks or non-regular files, create the temporary file with exclusive no-follow flags, and write and rename only after validation.

How it was found

  1. Create a file containing some text that should not be truncated.
  2. Create a symlink named as the cache file pointing to that file.
  3. Run curl with the cache option pointing to the symlink and a request that fails.
  4. Observe that the target file is now empty and the symlink has been replaced by curl's cache file.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A symlink is a shortcut file that points to another file. When a program opens a symlink for writing, it actually writes to the file the shortcut points to. Here, curl opened the cache file for writing before checking whether it was a symlink, so it could wipe out a different file.

What is File upload?

An upload feature accepts a file that the server or other users' browsers then treat as code or as something trusted.

More by mr4bugs

View all

1 more disclosed report, best first.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Unrestricted file upload

The lesson behind this report.

An upload feature accepts a file that the server or other users' browsers then treat as code or as something trusted.