Skip to content
Race condition Disclosed report Advanced

[SFTP] TOCTOU Race Condition in Upload Resume Logic Leads to Arbitrary File Append

A time-of-check to time-of-use race condition exists in libcurl's SFTP upload resume logic: the remote file size is checked with STAT, then the file is opened without truncation. An attacker with an authenticated account on the SFTP server can swap the target file for a symlink during that window, causing curl to append data to a different file the victim can write to.

cainvsilf Follow

Originally disclosed on HackerOne

  • Disclosed after 5 days

Summary:

A Time-of-check to Time-of-use (TOCTOU) race condition exists in the SFTP upload resume functionality of libcurl. When resuming an upload with CURLOPT_RESUME_FROM set to -1 (the equivalent of the curl -C - command-line flag), libcurl first performs a STAT operation to determine the remote file's size and the correct offset to resume from. Subsequently, it performs an OPEN operation to begin writing.

A window of time exists between the STAT and OPEN calls. An attacker with authenticated access to the SFTP server can exploit this by replacing the target file with a symbolic link to a different file on the system. Because the OPEN operation for a resume operation does not use a truncation flag (e.g., O_TRUNC), curl will then proceed to seek to the previously determined offset and append data to the symlink's target file. This allows an attacker to append arbitrary data to files the user has write access to, but did not intend to modify.

Affected version

This was reproduced on curl 8.18.0-DEV (the master branch as of November 2025). The vulnerable logic appears to be fundamental to the SFTP resume-from-end feature (-C -) and likely affects many previous versions as well.

A typical curl -V output for a system where this can be reproduced would be: curl 8.18.0-DEV (x86_64-pc-linux-gnu) libcurl/8.18.0-DEV OpenSSL/3.2.1 zlib/1.2.13 libssh2/1.11.0 Protocols: sftp https ... Features: AsynchDNS libssh2 ...

Steps To Reproduce:

  1. Setup the Environment: * An SFTP server is required. For local testing, openssh-server can be used on a Linux machine, allowing connections to localhost. * The attacker needs an authenticated user account on the server with write permissions in a specific directory. * The victim is a curl user who will upload a file to this specific directory using the resume flag.

  2. Prepare the Attack Scripts:

    • Create the server-side script race_server.sh on the SFTP server. This script will rapidly swap the target file with a symlink to a sensitive file.
    • Create the client-side script race_client.sh on the client machine. This script will repeatedly attempt the curl upload to trigger the race condition.
  3. Execute the Attack:

    1. On the server, run ./race_server.sh. It will begin creating and deleting the symlink in a tight loop.
    2. On the client, configure race_client.sh with the correct credentials and paths, then run ./race_client.sh. It will start sending upload requests.
    3. Let both scripts run for 15-30 seconds. The client will show some "No such file or directory" errors, which is expected.
  4. Verify the Result:

    1. Stop both scripts (Ctrl+C).
    2. On the server, inspect the contents of the sensitive file targeted by the symlink (e.g., /tmp/test_vuln.log).
    3. If the exploit was successful, the contents of the client's payload file will be appended to the sensitive file.

Supporting Material/References:

  • race_client.sh (F5023270: race_client.sh)

  • race_server.sh (F5023271: race_server.sh)

  • Vulnerable Code Flow Analysis.txt (F5023279: Vulnerable Code Flow Analysis.txt)

Vulnerable Code Pointer

The core of the vulnerability is in the sftp_upload_init function within:

  • lib/vssh/libssh2.c
  • lib/vssh/libssh.c

Specifically the logic branch:

  • if(data->state.resume_from < 0) → performs a STAT
  • else if(data->state.resume_from > 0) → performs a non-truncating OPEN

Attached PoC Scripts

  • race_server.sh: Server-side PoC to create the race condition.
  • race_client.sh: Client-side PoC used to repeatedly trigger the vulnerable logic in curl.

Impact

The primary impact is a loss of Integrity. An attacker with authenticated access to an SFTP server can leverage this vulnerability to append arbitrary data to any file that the victim user has write permissions for.

This can lead to further, more severe impacts depending on the targeted file:

  • Remote Code Execution (RCE): If the attacker appends malicious commands to a user's startup script (e.g., .bashrc, .profile) or a script executed by a cron job.
  • Denial of Service (DoS): If the attacker corrupts a critical system or application configuration file.
  • Log Injection / Tampering: The attacker could inject false log entries to mislead administrators or hide other malicious activity.

The severity is rated as Medium, as exploitation requires the attacker to have prior authenticated access to the server and relies on winning a race condition.

Suggested Mitigation

The root cause is the lack of verification that the file opened is the same one that was checked. The most robust solution is to perform a check after the file is opened.

  1. After libssh2_sftp_open_ex() is called in sftp_upload_init, a subsequent call to libssh2_sftp_fstat_ex() should
  be made on the returned file handle.
   2. The attributes from this fstat call (e.g., inode number, if the SFTP server extension provides it) should be
  compared to the attributes gathered from the initial stat call.
   3. If the attributes do not match, it indicates the file was swapped. The transfer must be aborted, and an error
  should be returned to the user.

The original report as published on HackerOne, by cainvsilf. 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

Likely the resume path opens the remote file without truncation and never verifies that the opened file is the same one that was stat'ed, leaving a window for a symlink swap.

Impact

An attacker with an authenticated SFTP account could append arbitrary data to any file the victim can write, potentially leading to code execution via startup scripts, denial of service through config corruption, or log tampering; exploitation depends on winning the race.

What the attacker needed

Attacker has an authenticated SFTP account; attacker can write in the target directory; victim uses curl resume upload (-C -); attacker wins the timing race

The fix

After opening the file, libcurl should fstat the handle and compare its attributes (such as the inode) with the earlier stat, aborting the transfer if they differ.

How it was found

  1. Set up an SFTP server with an attacker account that has write access to a directory, and a victim curl user who uploads there with the resume flag.
  2. On the server, run a script that rapidly swaps the target file with a symlink to a sensitive file.
  3. On the client, run a script that repeatedly performs the curl upload with resume-from-end.
  4. Let both scripts run for 15-30 seconds; some 'No such file or directory' errors are expected.
  5. Stop both scripts and inspect the sensitive file to see whether the payload was appended.

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.

When curl resumes an upload, it first asks the server how big the file is, then opens the file to keep writing. If someone with an account on that server quickly swaps the file for a shortcut (a symlink) pointing at another file, curl follows the shortcut and writes into the wrong file. The attacker needs an account on the same server and has to win a timing race.

What is Race condition?

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

More from curl

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.