curl file writing susceptible to symlink attacks
When curl downloads a file to a world-writable directory like /tmp with a predictable name, a local attacker can pre-create a symlink to redirect the write to another file owned by the victim, or replace the downloaded content after it is closed. This can overwrite user files or substitute malicious content.
- Disclosed after 17 days
Summary:
If curl command is used to download a file with predictable file name to a world writable directory (such as /tmp), a local attacker is able to mount a symlink attack to either A) redirect the target file writing to another file writable by the user or B) replace the downloaded file contents with arbitrary other data. libcurl file:// upload is similarly affected.
However, this really isn't a vulnerability in curl or libcurl itself, but use of curl or libcurl.
Steps To Reproduce:
Scenario A example:
- attacker does:
ln -s /home/victim/.bashrc /tmp/target.sh - victim does:
curl --output-dir /tmp -O https://example.com/target.shorcurl -o /tmp/target.sh https://example.com/whateveror similar
=> Instead of downloading the file to /tmp/target.sh it will be written to /home/victim/.bashrc. This attack works the best when the attacker can control which file is downloaded (granted, this is often not possible).
Scenario B example:
- attacker does:
install -m 606 /dev/null /tmp/target.sh - attacker waits for the file to be closed (inotify), and immediately replaces the file contents with malicious content once closed
- victim does:
curl --output-dir /tmp -O https://example.com/target.shorcurl -o /tmp/target.sh https://example.com/whateveror similar
=> The victim downloaded content is replaced by malicious content before it's used (copied, executed etc) by the victim.
Remediation
- Documentation should be amended to warn users against this threat. If temporary files are used, they should be put to secure temporary directory (created by
mktemp -dor similar). - While not a vulnerability in curl/libcurl a mode that uses
O_NOFOLLOWwhen opening output files could be added. Similarly in this secure mode--create-dirswould need to be amended to refuse to create entry in any location owned by another user (but making this check bulletproof is tricky), This mode could not be the default behaviour, since it would likely break too many things.
Vulnerability discussion
The obvious argument against this being a vulnerability in curl/libcurl is that the user is responsible for the permissions of the directory they download to. That is: the insecure use of curl/libcurl is actually where the vulnerability lies.
Interwebs has quite many examples of vulnerable use or examples, including: https://daniel.haxx.se/blog/2020/09/10/store-the-curl-output-over-there/
Impact
A) Overwriting files owned by the user downloading the files. B) Replacing downloaded data with malicious content
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
Likely the user chooses an output path in a world-writable directory with a predictable name, and curl opens that path without protecting against symlinks or later replacement.
Impact
A local attacker could overwrite files owned by the user running curl or replace downloaded data with malicious content, though the report notes this is often not possible because the attacker may not control which file is downloaded.
What the attacker needed
Local attacker on same system; victim downloads to world-writable dir; predictable output filename
The fix
Documentation should warn users against this threat and recommend using a secure temporary directory created with mktemp -d; optionally a non-default mode using O_NOFOLLOW could be added.
How it was found
- Attacker creates a symlink in the shared directory pointing to a victim-owned file.
- Victim runs curl to download a file with that predictable name into the shared directory.
- curl follows the symlink and writes the downloaded content to the victim-owned file instead.
- Alternatively, attacker creates a world-writable file with the target name and waits for curl to close it.
- Attacker immediately replaces the file contents with malicious data before the victim uses it.
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.
curl is a tool for downloading files. If you save a download into a shared folder like /tmp using a name anyone can guess, another person on the same computer can trick curl into writing over one of your important files, or swap the downloaded file for a malicious one. The report says this is really a misuse of curl rather than a bug in curl itself.
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.