Skip to content
Misconfiguration Disclosed report Advanced CVE-2023-27538

SFTP connection reuse skips the host-key pin of a later transfer

curl's SSH connection reuse logic compares only the private and public key file names, so a second SFTP transfer on the same handle skips its own host-key pin and reuses the first connection. An attacker who controls the server for the first accepted transfer could receive or supply data that the later transfer's pin was meant to exclude.

jfrery Follow

Originally disclosed on HackerOne

  • Disclosed after 3 days

Summary

When one curl invocation (or one libcurl handle) does two SFTP/SCP transfers to the same host, the second transfer reuses the first SSH connection even when it asks for a different host key with --hostpubsha256 (CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256). The second transfer never checks its pin and succeeds against a key it would reject on a fresh connection.

ssh_config_matches() in lib/url.c compares only the private and public key file names:

C
return sshc1 && sshc2 && Curl_safecmp(sshc1->priv_key, sshc2->priv_key) &&
       Curl_safecmp(sshc1->pub_key, sshc2->pub_key);

The host-key requirements are not part of the match. From reading the code, the same applies to --hostpubmd5 and to the known_hosts file (CURLOPT_SSH_KNOWNHOSTS); I tested only the SHA256 pin.

Affected

  • curl 8.23.0 and master at a40772f2f9, libssh2 backend (libssh2 1.11.1), Linux.
  • Not tested with the libssh backend.

Reproduce

Any SSH server with password login. BAD is a pin that does not match the server key.

Shell
BAD=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
GOOD=<the server's real SHA256 host key, base64>

# wrong pin alone: refused
echo data | curl -s -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/alone.txt; echo "exit $?"

# good pin, then wrong pin on the same command line
echo data | curl -v -u t:pw --hostpubsha256 "$GOOD" sftp://localhost/etc/hostname -o /dev/null \
  --next -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/reused.txt; echo "exit $?"

Result on both versions:

exit 60                                            <- wrong pin alone
* SSH: SHA256 checksum match                       <- first transfer, good pin
* Reusing existing sftp: connection with host localhost
exit 0                                             <- second transfer, wrong pin
/tmp/reused.txt written, /tmp/alone.txt not written

The attached repro-sftp.sh does the same and reads GOOD from curl's own mismatch message.

Impact

An application that sets a host-key pin per transfer on a shared handle or connection pool can send data to, or accept data from, a server that the later transfer's pin excludes. Example: a multi-tenant uploader that pins each destination's key, where a tenant controls one key pinned for the same host name and port.

The attacker must own the server (or its key) for the first, accepted transfer to the same host and port. So I think severity is low to medium.

This looks like the same class as CVE-2023-27538 (SSH connection reuse ignores config changes). That fix added the key file names, but not the host-key checks.

Suggested fix

Store the host-key settings (SHA256 and MD5 pins, known_hosts file) in the SSH connection meta and compare them in ssh_config_matches(), or refuse reuse when any of them differs.

Found with AI-assisted source review; the reproduction above was run by hand.

Impact

An application that sets a host-key pin per transfer on a shared handle or connection pool can send data to, or accept data from, a server that the later transfer's pin excludes.

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

curl supports SFTP and SCP transfers over SSH, and it lets the caller pin the server's host key instead of relying on the usual known_hosts trust model. With --hostpubsha256 on the command line, or [redacted] in libcurl, a transfer only proceeds if the server presents a key whose SHA256 fingerprint matches the pin. A mismatch aborts the transfer with exit code 60. The same idea exists for MD5 pins (--hostpubmd5) and for a caller-supplied known_hosts file (CURLOPT_SSH_KNOWNHOSTS).

The report concerns what happens when a single curl invocation, or a single libcurl handle, performs two SFTP/SCP transfers to the same host. curl keeps the SSH connection alive and reuses it for the second transfer. The claim is that this reuse decision ignores the host-key requirements, so the second transfer never checks its own pin and succeeds against a key it would have rejected on a fresh connection.

Setup

The researcher tested against curl 8.23.0 and against master at commit a40772f2f9, both built with the libssh2 backend (libssh2 1.11.1) on Linux. The libssh backend was not tested.

The only precondition is an SSH server that accepts password login. The reproduction uses sftp://localhost with the credentials t:pw, and writes to /tmp/alone.txt and /tmp/reused.txt. Two values are needed: a pin that does not match the server key (BAD) and the server's real SHA256 host key in base64 (GOOD).

Finding it

The starting point is ssh_config_matches() in lib/url.c, which decides whether an existing SSH connection may be reused for a new transfer. The function compares only the private and public key file names:

C
return sshc1 && sshc2 && Curl_safecmp(sshc1->priv_key, sshc2->priv_key) &&
       Curl_safecmp(sshc1->pub_key, sshc2->pub_key);

The host-key requirements are not part of the match. Two transfers that differ only in their SHA256 pin therefore look identical to this function, and the second one inherits the connection that the first one authenticated; the researcher expects the same for MD5 pins and known_hosts from reading the code. From reading the code, the researcher expects the same behaviour for --hostpubmd5 and for CURLOPT_SSH_KNOWNHOSTS; only the SHA256 pin was tested.

The researcher also notes that this looks like the same class of bug as CVE-2023-27538, where SSH connection reuse ignored config changes. That earlier fix added the key file names to the comparison, but not the host-key checks.

Exploitation

Step 1: confirm that a wrong pin alone is refused

The first command sends a small amount of data to a file over SFTP with a pin that does not match the server key:

Shell
BAD=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
GOOD=<the server's real SHA256 host key, base64>

# wrong pin alone: refused
echo data | curl -s -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/alone.txt; echo "exit $?"

BAD is a base64 SHA256 value that does not correspond to the server key. -T - uploads stdin, -u t:pw supplies the password credentials, and --hostpubsha256 sets the pin. On its own this transfer is refused with exit code 60, and /tmp/alone.txt is not written. This establishes the baseline: a fresh connection with a bad pin fails.

Step 2: good pin first, bad pin second, in one invocation

The second command performs two transfers in a single curl invocation, joined by --next. The first uses the correct pin and downloads /etc/hostname to /dev/null. The second uses the bad pin and uploads stdin to /tmp/reused.txt:

Shell
echo data | curl -v -u t:pw --hostpubsha256 "$GOOD" sftp://localhost/etc/hostname -o /dev/null \
  --next -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/reused.txt; echo "exit $?"

Both transfers target the same host and port. The first authenticates the server against GOOD; the second asks for BAD. The verbose output shows what happens:

Text
exit 60                                            <- wrong pin alone
* SSH: SHA256 checksum match                       <- first transfer, good pin
* Reusing existing sftp: connection with host localhost
exit 0                                             <- second transfer, wrong pin
/tmp/reused.txt written, /tmp/alone.txt not written

The first transfer reports SSH: SHA256 checksum match and proceeds. The second transfer logs Reusing existing sftp: connection with host localhost and completes with exit code 0, even though its pin does not match the server key. /tmp/reused.txt is written; /tmp/alone.txt from the standalone attempt is not. The result is identical on curl 8.23.0 and on master at a40772f2f9.

The report also mentions an attached repro-sftp.sh that performs the same sequence and reads GOOD from curl's own mismatch message, so the real key does not have to be supplied by hand.

Root cause

The reuse decision in ssh_config_matches() compares only the private and public key file names. The host-key settings that a transfer asks for, the SHA256 pin, the MD5 pin and the known_hosts file, are not part of the match. When the second transfer arrives at the same host and port, the existing connection matches on the fields that are checked, so curl reuses it and never performs the host-key verification for the second transfer's requirements.

The earlier fix for CVE-2023-27538 hardened this same function against config changes but only added the key file names. The host-key checks were left out of that fix. The researcher found this through AI-assisted source review and ran the reproduction by hand.

Impact

An application that sets a host-key pin per transfer on a shared handle or connection pool can send data to, or accept data from, a server that the later transfer's pin excludes. A concrete example from the report is a multi-tenant uploader that pins each destination's key, where one tenant controls a key pinned for the same host name and port.

The attacker must own the server, or its key, for the first, accepted transfer to the same host and port. Given that precondition, the researcher rates severity as low to medium. The platform records the report as low severity with the weakness "Missing Critical Step in Authentication".

Fix

The suggested fix is to store the host-key settings (the SHA256 and MD5 pins and the known_hosts file) in the SSH connection meta and compare them in ssh_config_matches(), or to refuse reuse when any of them differs. The report does not describe a fix that was applied.

Variants to test

  1. MD5 pin reuse. Run the same two-transfer sequence with --hostpubmd5 instead of --hostpubsha256, a correct MD5 pin first and a wrong one second. A positive result is the second transfer logging connection reuse and completing with exit code 0 despite the wrong pin.
  2. known_hosts reuse. Use CURLOPT_SSH_KNOWNHOSTS with two different known_hosts files, the first accepting the server key and the second not. A positive result is the second transfer succeeding against a key its known_hosts file would reject on a fresh connection.
  3. libssh backend. Repeat the SHA256 pin sequence with curl built against libssh rather than libssh2. A positive result is the same reuse message and exit code 0 for the mismatched pin.
  4. Shared libcurl handle. Instead of --next on one command line, perform the two transfers through one libcurl handle or connection pool, setting [redacted] per transfer. A positive result is the second transfer reusing the connection and ignoring its own pin.
  5. Mixed settings. Combine a pinned first transfer with a second transfer that changes only the host-key setting while keeping the same host, port and key file names. A positive result is reuse where the settings differ, which confirms the comparison covers only the key file names.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

curl is a command-line tool that moves files and data around the internet. Among other things it can talk to an SSH server, the same kind of server you reach with a tool like ssh to get a remote terminal. Over SSH, curl can copy files with two protocols called SFTP and SCP. Think of SFTP as the file-copy part of SSH: it lets you upload and download files on a remote machine.

When curl connects to an SSH server, the server proves who it is by presenting a "host key". A host key is a long string of characters, a bit like a passport for the server. Normally curl checks that key against a list of keys it already trusts, kept in a file called known_hosts. But curl also lets you do something stricter: you can hand it a fingerprint of the exact key you expect, and curl will refuse to continue unless the server's key matches. That is called pinning. A fingerprint is a short, fixed-length code computed from a longer piece of data; if the data changes, the fingerprint changes too, so a fingerprint is a compact way to say "this exact key and nothing else".

The report describes a flaw in that pinning. If one curl command does two transfers to the same server, curl keeps the first SSH connection open and reuses it for the second transfer. The problem is that the decision to reuse the connection compares only the private and public key file names, not the pin. So the second transfer never checks its own pin. It happily talks to a server whose key it would have rejected if it had started a fresh connection.

That matters because a program built on curl's library (libcurl) can set a different pin for each transfer while sharing one connection. If the pins are not enforced, data can go to, or come from, a server that the later transfer was supposed to exclude.

Background you need

A few ideas come up again and again, so here they are from zero.

  • SSH: a protocol for logging into another computer securely. It encrypts everything so nobody in between can read or change it.
  • SFTP and SCP: two ways to copy files over SSH. SFTP is the file-transfer subsystem; SCP is an older, simpler copy command. curl supports both.
  • Host key: the server's own cryptographic key, used to prove the server is who it claims to be. If an attacker sits between you and the server, they cannot present the real server's key, so the check fails.
  • known_hosts: a file on your machine listing the host keys you have already accepted. It is the usual, more relaxed trust model: trust a key once, remember it, warn if it changes.
  • Pinning: instead of trusting a list, you state the exact key (or its fingerprint) you expect. Anything else is refused. It is like checking a courier's ID against a photo you already have, rather than against a list of couriers the office trusts.
  • SHA256 and MD5: two fingerprint algorithms. SHA256 produces a longer, stronger fingerprint; MD5 is older and weaker. curl can pin either one.
  • libcurl: the library version of curl. Programs link against it and call functions instead of typing commands. Its settings are called options, and they have names like [redacted].
  • Handle: in libcurl, a handle is the object that represents one transfer or one connection setup. A program can reuse a handle, or a pool of connections, for many transfers.
  • Exit code: the number a command-line program returns when it finishes. Zero usually means success; anything else means something went wrong. curl uses 60 for a failed SSH host-key check.
  • Connection reuse: keeping an already-open connection and using it again instead of opening a new one. It saves time, the same way you keep a phone call open rather than hanging up and redialling for every sentence.

What curl offers for host keys

curl supports SFTP and SCP transfers over SSH, and it lets the caller pin the server's host key instead of relying on the usual known_hosts trust model. With --hostpubsha256 on the command line, or [redacted] in libcurl, a transfer only proceeds if the server presents a key whose SHA256 fingerprint matches the pin. A mismatch aborts the transfer with exit code 60. The same idea exists for MD5 pins (--hostpubmd5) and for a caller-supplied known_hosts file (CURLOPT_SSH_KNOWNHOSTS).

The report concerns what happens when a single curl invocation, or a single libcurl handle, performs two SFTP/SCP transfers to the same host. curl keeps the SSH connection alive and reuses it for the second transfer. The claim is that this reuse decision ignores the host-key requirements, so the second transfer never checks its own pin and succeeds against a key it would have rejected on a fresh connection.

The setup used to test it

The researcher tested against curl 8.23.0 and against master at commit a40772f2f9, both built with the libssh2 backend (libssh2 1.11.1) on Linux. A backend here is the library curl uses under the hood to speak SSH; libssh2 is one such library, libssh is another. The libssh backend was not tested.

The only precondition is an SSH server that accepts password login. The reproduction uses sftp://localhost with the credentials t:pw, and writes to /tmp/alone.txt and /tmp/reused.txt. Two values are needed: a pin that does not match the server key (BAD) and the server's real SHA256 host key in base64 (GOOD). Base64 is just a way of writing binary data as plain letters and digits so it can be pasted into a command.

Finding it

The starting point is ssh_config_matches() in lib/url.c, which decides whether an existing SSH connection may be reused for a new transfer. The function compares only the private and public key file names:

C
return sshc1 && sshc2 && Curl_safecmp(sshc1->priv_key, sshc2->priv_key) &&
       Curl_safecmp(sshc1->pub_key, sshc2->pub_key);

Line by line:

  1. return means the function hands back the result of the expression that follows.
  2. sshc1 && sshc2 checks that both sides of the comparison actually have SSH settings to compare. If either is missing, the whole thing is false and no reuse happens.
  3. Curl_safecmp(sshc1->priv_key, sshc2->priv_key) compares the private key file names of the two transfers. Curl_safecmp is curl's own string comparison helper.
  4. Curl_safecmp(sshc1->pub_key, sshc2->pub_key) does the same for the public key file names.

If all of that is true, the function says the connections match and the old one can be reused. Notice what is missing: the SHA256 pin is never looked at, and from reading the code the researcher expects the same for the MD5 pin and the known_hosts file. Two transfers that differ only in their SHA256 pin, MD5 pin or known_hosts file therefore look identical to this function, and the second one inherits the connection that the first one authenticated. From reading the code, the researcher expects the same behaviour for --hostpubmd5 and for CURLOPT_SSH_KNOWNHOSTS; only the SHA256 pin was tested.

The researcher also notes that this looks like the same class of bug as CVE-2023-27538, where SSH connection reuse ignored config changes. A CVE is a public identifier given to a known security flaw so people can track it. That earlier fix added the key file names to the comparison, but not the host-key checks.

Exploitation

Step 1: confirm that a wrong pin alone is refused

The first command sends a small amount of data to a file over SFTP with a pin that does not match the server key:

Shell
BAD=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
GOOD=<the server's real SHA256 host key, base64>

# wrong pin alone: refused
echo data | curl -s -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/alone.txt; echo "exit $?"

Line by line:

  1. BAD=AAAA... stores a made-up fingerprint in a shell variable called BAD. It is deliberately not the server's real key.
  2. GOOD=<the server's real SHA256 host key, base64> is a placeholder for the real fingerprint, which you would paste in yourself.
  3. echo data | curl ... sends the word data into curl's standard input. The vertical bar, the pipe, connects the output of one command to the input of the next.
  4. -s tells curl to be quiet and not print progress.
  5. -u t:pw supplies the username t and password pw for the SSH login.
  6. --hostpubsha256 "$BAD" sets the pin to the wrong fingerprint.
  7. -T - uploads whatever arrives on standard input, which here is data.
  8. sftp://localhost/tmp/alone.txt is the destination: a file on the server reached over SFTP.
  9. ; echo "exit $?" runs afterwards and prints the exit code of curl, so you can see whether it succeeded.

On its own this transfer is refused with exit code 60, and /tmp/alone.txt is not written. This establishes the baseline: a fresh connection with a bad pin fails. Without this step you would not know whether the later success came from the reuse bug or from pinning simply not working at all.

Step 2: good pin first, bad pin second, in one invocation

The second command performs two transfers in a single curl invocation, joined by --next. The first uses the correct pin and downloads /etc/hostname to /dev/null. The second uses the bad pin and uploads standard input to /tmp/reused.txt:

Shell
echo data | curl -v -u t:pw --hostpubsha256 "$GOOD" sftp://localhost/etc/hostname -o /dev/null \
  --next -u t:pw --hostpubsha256 "$BAD" -T - sftp://localhost/tmp/reused.txt; echo "exit $?"

Line by line:

  1. echo data | again feeds the word data into curl.
  2. -v makes curl verbose, so it prints what it is doing. That output is how you see the reuse message.
  3. -u t:pw --hostpubsha256 "$GOOD" logs in with the same credentials and pins the correct fingerprint.
  4. sftp://localhost/etc/hostname is the first transfer: download the file /etc/hostname from the server.
  5. -o /dev/null throws the downloaded content away. /dev/null is a special file that discards anything written to it.
  6. The backslash at the end of the line continues the command on the next line, purely for readability.
  7. --next starts a second, separate transfer inside the same curl run, with its own options.
  8. -u t:pw --hostpubsha256 "$BAD" logs in again and pins the wrong fingerprint this time.
  9. -T - uploads standard input, and sftp://localhost/tmp/reused.txt is where it goes.
  10. ; echo "exit $?" prints the final exit code.

Both transfers target the same host and port. The first authenticates the server against GOOD; the second asks for BAD. The verbose output shows what happens:

Text
exit 60                                            <- wrong pin alone
* SSH: SHA256 checksum match                       <- first transfer, good pin
* Reusing existing sftp: connection with host localhost
exit 0                                             <- second transfer, wrong pin
/tmp/reused.txt written, /tmp/alone.txt not written

Reading the output:

  1. exit 60 is the result of the standalone attempt from step 1: the wrong pin on a fresh connection was refused.
  2. SSH: SHA256 checksum match is curl saying the first transfer's pin matched the server key, so it proceeded.
  3. Reusing existing sftp: connection with host localhost is curl saying it did not open a new connection for the second transfer; it kept the first one.
  4. exit 0 is the final result: success, even though the second transfer's pin does not match the server key.
  5. /tmp/reused.txt written, /tmp/alone.txt not written confirms the second upload happened and the standalone one did not.

The result is identical on curl 8.23.0 and on master at a40772f2f9.

The report also mentions an attached repro-sftp.sh that performs the same sequence and reads GOOD from curl's own mismatch message, so the real key does not have to be supplied by hand.

Root cause

The reuse decision in ssh_config_matches() compares only the private and public key file names. The host-key settings that a transfer asks for, the SHA256 pin, the MD5 pin and the known_hosts file, are not part of the match. When the second transfer arrives at the same host and port, the existing connection matches on the fields that are checked, so curl reuses it and never performs the host-key verification for the second transfer's requirements.

The earlier fix for CVE-2023-27538 hardened this same function against config changes but only added the key file names. The host-key checks were left out of that fix. The researcher found this through AI-assisted source review and ran the reproduction by hand.

Impact

An application that sets a host-key pin per transfer on a shared handle or connection pool can send data to, or accept data from, a server that the later transfer's pin excludes. A concrete example from the report is a multi-tenant uploader that pins each destination's key, where one tenant controls a key pinned for the same host name and port. Multi-tenant means several customers share the same running program; each one is supposed to be kept apart from the others.

The attacker must own the server, or its key, for the first, accepted transfer to the same host and port. Given that precondition, the researcher rates severity as low to medium. The platform records the report as low severity with the weakness "Missing Critical Step in Authentication".

Fix

The suggested fix is to store the host-key settings (the SHA256 and MD5 pins and the known_hosts file) in the SSH connection meta and compare them in ssh_config_matches(), or to refuse reuse when any of them differs. The report does not describe a fix that was applied.

Variants to test

  1. MD5 pin reuse. Run the same two-transfer sequence with --hostpubmd5 instead of --hostpubsha256, a correct MD5 pin first and a wrong one second. A positive result is the second transfer logging connection reuse and completing with exit code 0 despite the wrong pin.
  2. known_hosts reuse. Use CURLOPT_SSH_KNOWNHOSTS with two different known_hosts files, the first accepting the server key and the second not. A positive result is the second transfer succeeding against a key its known_hosts file would reject on a fresh connection.
  3. libssh backend. Repeat the SHA256 pin sequence with curl built against libssh rather than libssh2. A positive result is the same reuse message and exit code 0 for the mismatched pin.
  4. Shared libcurl handle. Instead of --next on one command line, perform the two transfers through one libcurl handle or connection pool, setting [redacted] per transfer. A positive result is the second transfer reusing the connection and ignoring its own pin.
  5. Mixed settings. Combine a pinned first transfer with a second transfer that changes only the host-key setting while keeping the same host, port and key file names. A positive result is reuse where the settings differ, which confirms the comparison covers only the key file names.

Glossary

  • Backend: the library curl uses under the hood to speak a protocol; libssh2 and libssh are two SSH backends.
  • Base64: a way of writing binary data as plain letters and digits so it can be pasted into a command or message.
  • Connection pool: a set of already-open connections that a program keeps around and reuses for later work.
  • Connection reuse: keeping an open connection and using it again instead of opening a new one.
  • curl: a command-line tool for transferring data over many protocols, including SFTP and SCP.
  • CVE: a public identifier given to a known security flaw so people can track it.
  • Exit code: the number a command-line program returns when it finishes; zero usually means success.
  • Fingerprint: a short, fixed-length code computed from a longer piece of data, used to identify that data compactly.
  • Handle: in libcurl, the object that represents one transfer or one connection setup.
  • known_hosts: a file listing host keys you have already accepted, used as the usual trust model.
  • libcurl: the library version of curl, called by programs through options such as [redacted].
  • libssh / libssh2: two libraries that implement the SSH protocol; curl can be built against either.
  • MD5: an older fingerprint algorithm; weaker than SHA256.
  • Multi-tenant: a setup where several customers share the same running program, each kept apart from the others.
  • Pin / pinning: stating the exact key or fingerprint you expect, and refusing anything else.
  • SCP: an older, simple file-copy protocol over SSH.
  • SFTP: the file-transfer subsystem of SSH, used to upload and download files.
  • SHA256: a fingerprint algorithm that produces a longer, stronger fingerprint.
  • SSH: a protocol for logging into another computer securely, with everything encrypted.
  • Standard input: the stream of data a command reads when you pipe something into it.
  • Variable: a named slot in the shell that holds a value, such as BAD or GOOD.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Security misconfiguration

The lesson behind this report.

Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.