Skip to content
DoS Disclosed report Advanced

Infinite loop issue in the state machine of the curl project

A malicious FTP server can open an EPSV data port but never send any data, which makes curl's transfer state machine loop forever instead of finishing or failing. An attacker who controls an FTP server a victim connects to could hang the curl process indefinitely.

kak1 Follow

Originally disclosed on HackerOne

  • Disclosed after under an hour

Summary:

Vulnerability impact: When curl attempts to download files from a malicious FTP server, it triggers an infinite loop in the code execution.

I discovered this issue in the FTP functionality of the curl project .As described in https://github.com/curl/curl/blob/master/docs/cmdline-opts/disable-epsv.md, curl uses EPSV mode as the default FTP file transfer method. In simple terms, the EPSV mode works as follows: the FTP server opens a TCP port and waits for the client to connect, then sends data to the client through this port.

In the state machine of curl, the state_performing function in /lib/multi.c calls the Curl_sendrecv function at [1] to receive data from the peer:

static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp); // [1] call Curl_sendrecv

  if(data->req.done || (result == CURLE_RECV_ERROR)) {
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
...

The Curl_sendrecv function calls sendrecv_dl at [2]:

CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
{
  struct SingleRequest *k = &data->req;
  CURLcode result = CURLE_OK;

  DEBUGASSERT(nowp);
  if(Curl_xfer_is_blocked(data)) {
    result = CURLE_OK;
    goto out;
  }

  /* We go ahead and do a read if we have a readable socket or if the stream
     was rewound (in which case we have data in a buffer) */
  if(k->keepon & KEEP_RECV) {
    result = sendrecv_dl(data, k); //[2] call sendrecv_dl
    if(result || data->req.done)
      goto out;
  }
  
...

The sendrecv_dl function further calls xfer_recv_resp to receive data. If our malicious FTP server opens an EPSV port but does not send any data to the client, xfer_recv_resp will return -1, and the value of result will be set to CURLE_AGAIN. Subsequently, at [4], the result is set to CURLE_OK. This means that even if xfer_recv_resp fails to receive any data, the sendrecv_dl function still returns CURLE_OK.

static CURLcode sendrecv_dl(struct Curl_easy *data,
                            struct SingleRequest *k)
{
...
    rcvd_eagain = FALSE;
    nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result); // [3] call xfer_recv_resp
    if(nread < 0) {
      if(CURLE_AGAIN != result)
        goto out; /* real error */
      rcvd_eagain = TRUE;
      result = CURLE_OK; //[4]set result to CURLE_OK
      if(data->req.download_done && data->req.no_body &&
         !data->req.resp_trailer) {
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
               "not waiting for EOS"));
        nread = 0;
        /* continue as if we received the EOS */
      }
      else
        break; /* get out of loop */
    }
...

Let's continue examining the state_performing function:

static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp);

  if(data->req.done || (result == CURLE_RECV_ERROR)) { // [5] data->req.done == 0, result==CURLE_OK
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
#ifndef CURL_DISABLE_HTTP
  else if((CURLE_HTTP2_STREAM == result) &&
          Curl_h2_http_1_1_error(data)) {
    CURLcode ret = Curl_retry_request(data, &newurl);

    if(!ret) {
      infof(data, "Downgrades to HTTP/1.1");
      streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
      data->state.http_neg.wanted = CURL_HTTP_V1x;
      data->state.http_neg.allowed = CURL_HTTP_V1x;
      /* clear the error message bit too as we ignore the one we got */
      data->state.errorbuf = FALSE;
      if(!newurl)
        /* typically for HTTP_1_1_REQUIRED error on first flight */
        newurl = strdup(data->state.url);
      if(!newurl) {
        result = CURLE_OUT_OF_MEMORY;
      }
      else {
        /* if we are to retry, set the result to OK and consider the request
          as done */
        retry = TRUE;
        result = CURLE_OK;
        data->req.done = TRUE;
      }
    }
    else
      result = ret;
  }
#endif

  if(result) { //[6] result==CURLE_OK
    /*
     * The transfer phase returned error, we mark the connection to get closed
     * to prevent being reused. This is because we cannot possibly know if the
     * connection is in a good shape or not now. Unless it is a protocol which
     * uses two "channels" like FTP, as then the error happened in the data
     * connection.
     */

    if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
       result != CURLE_HTTP2_STREAM)
      streamclose(data->conn, "Transfer returned error");

    multi_posttransfer(data);
    multi_done(data, result, TRUE);
  }
  else if(data->req.done && !Curl_cwriter_is_paused(data)) { //[7] data->req.done == 0
    const struct Curl_handler *handler = data->conn->handler;

    /* call this even if the readwrite function returned error */
    multi_posttransfer(data);

    /* When we follow redirects or is set to retry the connection, we must to
       go back to the CONNECT state */
    if(data->req.newurl || retry) {
      followtype follow = FOLLOW_NONE;
      if(!retry) {
        /* if the URL is a follow-location and not just a retried request then
           figure out the URL here */
        free(newurl);
        newurl = data->req.newurl;
        data->req.newurl = NULL;
        follow = FOLLOW_REDIR;
      }
      else
        follow = FOLLOW_RETRY;
      (void)multi_done(data, CURLE_OK, FALSE);
      /* multi_done() might return CURLE_GOT_NOTHING */
      result = multi_follow(data, handler, newurl, follow);
      if(!result) {
        multistate(data, MSTATE_SETUP);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
    else {
      /* after the transfer is done, go DONE */

      /* but first check to see if we got a location info even though we are
         not following redirects */
      if(data->req.location) {
        free(newurl);
        newurl = data->req.location;
        data->req.location = NULL;
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
        if(result) {
          *stream_errorp = TRUE;
          result = multi_done(data, result, TRUE);
        }
      }

      if(!result) {
        multistate(data, MSTATE_DONE);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
  }
  else { /* not errored, not done */
    mspeed_check(data, nowp); // [8]
  }
  free(newurl);
  *resultp = result;
  return rc;
}

Since no data was received, the data->req.done flag remains 0. Furthermore, because the result is CURLE_OK, the conditional checks at [5], [6], and [7] all evaluate to false. The code then proceeds to [8] (not errored, not done).

This causes curl's current state machine flag (data->mstate) to remain unchanged, still set to MSTATE_PERFORMING. Subsequently, the curl code repeatedly enters the state_performing function, resulting in an infinite loop.

Affected version

curl : https://github.com/curl/curl/archive/refs/tags/curl-8_17_0.tar.gz platform : ubuntu22.04

➜  src ./curl -V 
curl 8.17.0-DEV (x86_64-pc-linux-gnu) libcurl/8.17.0-DEV zlib/1.2.11 libpsl/0.19.1
Release-Date: [unreleased]
Protocols: dict file ftp gopher http imap ipfs ipns mqtt pop3 rtsp smtp telnet tftp ws
Features: alt-svc AsynchDNS IPv6 Largefile libz PSL threadsafe UnixSockets

Steps To Reproduce:

  1. Download the attached ./ftp_poc.py file and run: sudo python3 ./ftp_poc.py. This will start a malicious FTP service on port 21 of the current machine.
  2. Run the following curl command, replacing 192.168.23.1 in the command with the address of your own malicious FTP server:
./curl -u anonymous:123 'ftp://192.168.23.1/test' -o ./test
  1. The curl program will enter an infinite code loop and will not exit on its own.

Impact

Summary:

When curl attempts to download files from a malicious FTP server, it triggers an infinite loop in the code execution.

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

Context

curl is a command line tool and library for transferring data with URLs. Among the protocols it supports is FTP, and the report focuses on the FTP client path in curl's multi interface state machine. By default curl uses EPSV mode for FTP transfers, as documented in docs/cmdline-opts/disable-epsv.md. In EPSV mode the FTP server opens a TCP port and waits for the client to connect, then sends data to the client through that port.

The issue described here is a denial of service condition: a malicious FTP server can drive curl into an infinite loop in its state machine, so the client never exits on its own.

Setup

The researcher tested against the curl source tree at tag curl-8_17_0, on Ubuntu 22.04. The build reports itself as:

Text
➜  src ./curl -V
curl 8.17.0-DEV (x86_64-pc-linux-gnu) libcurl/8.17.0-DEV zlib/1.2.11 libpsl/0.19.1
Release-Date: [unreleased]
Protocols: dict file ftp gopher http imap ipfs ipns mqtt pop3 rtsp smtp telnet tftp ws
Features: alt-svc AsynchDNS IPv6 Largefile libz PSL threadsafe UnixSockets

Two preconditions are needed to reproduce: a malicious FTP server that behaves in a specific way on the EPSV data connection, and a curl client pointed at it. The researcher provides a Python proof of concept, ftp_poc.py, which starts such a server on port 21 of the machine running it.

Finding it

The researcher discovered the issue in the FTP functionality of curl. The relevant state is MSTATE_PERFORMING, handled by state_performing in /lib/multi.c. That function calls Curl_sendrecv to read and write data with the peer:

C
static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp); // [1] call Curl_sendrecv

  if(data->req.done || (result == CURLE_RECV_ERROR)) {
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
...

Curl_sendrecv in turn calls sendrecv_dl when the request still wants to receive data:

C
CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
{
  struct SingleRequest *k = &data->req;
  CURLcode result = CURLE_OK;

  DEBUGASSERT(nowp);
  if(Curl_xfer_is_blocked(data)) {
    result = CURLE_OK;
    goto out;
  }

  /* We go ahead and do a read if we have a readable socket or if the stream
     was rewound (in which case we have data in a buffer) */
  if(k->keepon & KEEP_RECV) {
    result = sendrecv_dl(data, k); //[2] call sendrecv_dl
    if(result || data->req.done)
      goto out;
  }

...

sendrecv_dl calls xfer_recv_resp to receive data. The researcher's observation is that if the malicious FTP server opens the EPSV port but never sends any data, xfer_recv_resp returns -1 and result is set to CURLE_AGAIN. The code then overwrites result with CURLE_OK before leaving the receive branch, so sendrecv_dl reports success even though nothing was received:

C
static CURLcode sendrecv_dl(struct Curl_easy *data,
                            struct SingleRequest *k)
{
...
    rcvd_eagain = FALSE;
    nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result); // [3] call xfer_recv_resp
    if(nread < 0) {
      if(CURLE_AGAIN != result)
        goto out; /* real error */
      rcvd_eagain = TRUE;
      result = CURLE_OK; //[4]set result to CURLE_OK
      if(data->req.download_done && data->req.no_body &&
         !data->req.resp_trailer) {
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
               "not waiting for EOS"));
        nread = 0;
        /* continue as if we received the EOS */
      }
      else
        break; /* get out of loop */
    }
...

That combination, a successful return with no data and no completion, is what makes the state machine misbehave.

Exploitation

Step 1: start the malicious FTP server

Download the attached ftp_poc.py and run it with root privileges so it can bind port 21 on the current machine:

Bash
sudo python3 ./ftp_poc.py

This starts a malicious FTP service on port 21 of the current machine. The server opens an EPSV data port and then sends nothing to the client.

Step 2: point curl at the server

Run curl against the server, replacing [redacted] with the address of your own malicious FTP server:

Bash
./curl -u anonymous:123 'ftp://[redacted]/test' -o ./test

Step 3: observe the loop

curl enters an infinite code loop and does not exit on its own.

Root cause

The loop comes from how state_performing interprets the return of Curl_sendrecv. Walking through the same function with the values produced by the silent EPSV server:

C
static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp);

  if(data->req.done || (result == CURLE_RECV_ERROR)) { // [5] data->req.done == 0, result==CURLE_OK
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
#ifndef CURL_DISABLE_HTTP
  else if((CURLE_HTTP2_STREAM == result) &&
          Curl_h2_http_1_1_error(data)) {
    CURLcode ret = Curl_retry_request(data, &newurl);

    if(!ret) {
      infof(data, "Downgrades to HTTP/1.1");
      streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
      data->state.http_neg.wanted = CURL_HTTP_V1x;
      data->state.http_neg.allowed = CURL_HTTP_V1x;
      /* clear the error message bit too as we ignore the one we got */
      data->state.errorbuf = FALSE;
      if(!newurl)
        /* typically for HTTP_1_1_REQUIRED error on first flight */
        newurl = strdup(data->state.url);
      if(!newurl) {
        result = CURLE_OUT_OF_MEMORY;
      }
      else {
        /* if we are to retry, set the result to OK and consider the request
          as done */
        retry = TRUE;
        result = CURLE_OK;
        data->req.done = TRUE;
      }
    }
    else
      result = ret;
  }
#endif

  if(result) { //[6] result==CURLE_OK
    /*
     * The transfer phase returned error, we mark the connection to get closed
     * to prevent being reused. This is because we cannot possibly know if the
     * connection is in a good shape or not now. Unless it is a protocol which
     * uses two "channels" like FTP, as then the error happened in the data
     * connection.
     */

    if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
       result != CURLE_HTTP2_STREAM)
      streamclose(data->conn, "Transfer returned error");

    multi_posttransfer(data);
    multi_done(data, result, TRUE);
  }
  else if(data->req.done && !Curl_cwriter_is_paused(data)) { //[7] data->req.done == 0
    const struct Curl_handler *handler = data->conn->handler;

    /* call this even if the readwrite function returned error */
    multi_posttransfer(data);

    /* When we follow redirects or is set to retry the connection, we must to
       go back to the CONNECT state */
    if(data->req.newurl || retry) {
      followtype follow = FOLLOW_NONE;
      if(!retry) {
        /* if the URL is a follow-location and not just a retried request then
           figure out the URL here */
        free(newurl);
        newurl = data->req.newurl;
        data->req.newurl = NULL;
        follow = FOLLOW_REDIR;
      }
      else
        follow = FOLLOW_RETRY;
      (void)multi_done(data, CURLE_OK, FALSE);
      /* multi_done() might return CURLE_GOT_NOTHING */
      result = multi_follow(data, handler, newurl, follow);
      if(!result) {
        multistate(data, MSTATE_SETUP);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
    else {
      /* after the transfer is done, go DONE */

      /* but first check to see if we got a location info even though we are
         not following redirects */
      if(data->req.location) {
        free(newurl);
        newurl = data->req.location;
        data->req.location = NULL;
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
        if(result) {
          *stream_errorp = TRUE;
          result = multi_done(data, result, TRUE);
        }
      }

      if(!result) {
        multistate(data, MSTATE_DONE);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
  }
  else { /* not errored, not done */
    mspeed_check(data, nowp); // [8]
  }
  free(newurl);
  *resultp = result;
  return rc;
}

Because no data was received, data->req.done stays 0. Because the result was rewritten to CURLE_OK, the conditions at [5], [6] and [7] all evaluate to false, and execution falls through to [8], the "not errored, not done" branch. That branch only calls mspeed_check and returns; it never changes data->mstate, which remains MSTATE_PERFORMING. curl therefore re-enters state_performing again and again, producing the infinite loop.

In short, the receive path converts a would-block condition into a success return, so the state machine stays in MSTATE_PERFORMING.

Impact

When curl attempts to download files from a malicious FTP server, it triggers an infinite loop in code execution. The client does not exit on its own. The report does not describe any memory corruption, data exposure or code execution beyond the loop itself.

Fix

The report does not describe a fix or a patch.

  1. Other protocols with a separate data channel, such as the dual-channel protocols curl marks with PROTOPT_DUAL. A positive result is a client that stops making progress while its state remains in the performing phase.
  2. Servers that accept the data connection and then stall without closing it, rather than refusing or resetting. A positive result is a client that neither errors nor completes.
  3. Partial sends followed by silence, to see whether a success return with no completion can be produced after some data has already been delivered. A positive result is the same non-advancing state after a partial transfer.
  4. Timeout and low-speed settings, to check whether mspeed_check eventually breaks the loop or whether the state remains stuck regardless. A positive result is a client that stays in the loop even with speed limits configured.

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. 15 min read.

What happened, in plain words

curl is a small program that you run in a terminal (the text window where you type commands) to download things from the internet or to talk to servers. It speaks many languages, called protocols, and one of them is FTP, an old way of transferring files between computers. This report is about what happens when curl talks FTP to a server that is deliberately badly behaved.

A server is just another computer somewhere that answers requests. Normally you trust it to answer sensibly. Here, the server is malicious: it is set up to trick curl into getting stuck. The result is that curl never finishes and never exits. It just spins forever, doing the same check over and over. In security language this is called a denial of service: the attacker does not steal anything or break into your machine, they simply stop your program from working.

To understand the rest, you need one idea: curl is built around a state machine. Think of it like a board game with a fixed set of squares. On each turn the program looks at which square it is on, does the thing that square says, and then moves to another square. If a square ever sends you back to itself without any change, you are stuck in a loop, like a record skipping. That is exactly what happens here.

Context

curl is a command line tool and a library. A command line tool is a program you type into a terminal instead of clicking. A library is a bundle of code that other programs can reuse, so the same curl code sits inside many apps. It transfers data using URLs, which are the addresses you type in a browser, like ftp://....

Among the protocols curl supports is FTP. FTP works with two connections: one for giving instructions (like "send me this file") and a second one for the actual file data. This two-channel design matters later.

By default curl uses a mode called EPSV for FTP transfers, as documented in docs/cmdline-opts/disable-epsv.md. In EPSV mode the FTP server opens a TCP port and waits for the client to connect, then sends data to the client through that port. A port is like a numbered door on a computer; a TCP port is one of the doors used for network conversations. So the server says "connect to door number X", curl connects, and then the server is supposed to push the file through that door.

The issue described here is a denial of service condition: a malicious FTP server can drive curl into an infinite loop in its state machine, so the client never exits on its own.

Setup

The researcher tested against the curl source tree at tag curl-8_17_0, on Ubuntu 22.04. A source tree is the collection of code files that make up a program before it is compiled into something you can run. A tag is a label on a particular version in a code repository, like a bookmark. Ubuntu 22.04 is a version of the Linux operating system.

The build reports itself as:

Text
➜  src ./curl -V
curl 8.17.0-DEV (x86_64-pc-linux-gnu) libcurl/8.17.0-DEV zlib/1.2.11 libpsl/0.19.1
Release-Date: [unreleased]
Protocols: dict file ftp gopher http imap ipfs ipns mqtt pop3 rtsp smtp telnet tftp ws
Features: alt-svc AsynchDNS IPv6 Largefile libz PSL threadsafe UnixSockets

Line by line: the first line is the shell prompt (➜ src) followed by the command ./curl -V. The ./ means "run the program called curl that sits in this folder", and -V is the flag that asks curl to print its version instead of downloading anything. The second line says the program is curl 8.17.0-DEV, a development build, built for 64-bit Intel-style processors on Linux, using a helper library called libcurl of the same version, plus compression support (zlib) and a library for handling public suffix domain rules (libpsl). Release-Date: [unreleased] means this build was never formally released. The Protocols: line lists every protocol this build can speak, and you can see ftp in it. The Features: line lists optional capabilities compiled in, such as IPv6 support, large file support and thread safety.

Two preconditions are needed to reproduce: a malicious FTP server that behaves in a specific way on the EPSV data connection, and a curl client pointed at it. The researcher provides a Python proof of concept, ftp_poc.py, which starts such a server on port 21 of the machine running it. A proof of concept is a small demonstration program that shows a problem is real without being a full attack tool. Port 21 is the standard door for FTP instructions.

Finding it

The researcher discovered the issue in the FTP functionality of curl. The multi interface is curl's way of juggling several transfers at once, and it is the part that runs the state machine. The relevant state is MSTATE_PERFORMING, handled by state_performing in /lib/multi.c. That function calls Curl_sendrecv to read and write data with the peer:

C
static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp); // [1] call Curl_sendrecv

  if(data->req.done || (result == CURLE_RECV_ERROR)) {
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
...

This is C, the programming language curl is written in. You do not need to read it like a programmer; treat it as a recipe. The function state_performing is one square of the board game. It first calls mspeed_check, which measures whether the transfer is going fast enough; if that check says "not yet" (CURLE_AGAIN), the function returns immediately and nothing else happens this turn. Otherwise it calls Curl_sendrecv at the point marked [1], which is the step that actually reads or writes data. Then it checks a condition: has the request finished (data->req.done), or did receiving fail with a specific error (CURLE_RECV_ERROR)? If either is true, it asks whether the request should be retried. The ... at the end means the code continues beyond what is shown.

Curl_sendrecv in turn calls sendrecv_dl when the request still wants to receive data:

C
CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
{
  struct SingleRequest *k = &data->req;
  CURLcode result = CURLE_OK;

  DEBUGASSERT(nowp);
  if(Curl_xfer_is_blocked(data)) {
    result = CURLE_OK;
    goto out;
  }

  /* We go ahead and do a read if we have a readable socket or if the stream
     was rewound (in which case we have data in a buffer) */
  if(k->keepon & KEEP_RECV) {
    result = sendrecv_dl(data, k); //[2] call sendrecv_dl
    if(result || data->req.done)
      goto out;
  }

...

Here Curl_sendrecv is a middleman. It first checks whether the transfer is blocked, and if so jumps to the end (goto out). Otherwise, if the request still wants to receive (KEEP_RECV), it calls sendrecv_dl at [2], the function that does the actual reading. If that call returns an error or the request is done, it jumps to the end. Again the ... means there is more code below.

sendrecv_dl calls xfer_recv_resp to receive data. The researcher's observation is that if the malicious FTP server opens the EPSV port but never sends any data, xfer_recv_resp returns -1 and result is set to CURLE_AGAIN. CURLE_AGAIN is curl's way of saying "nothing available right now, try again later", not a real failure. The code then overwrites result with CURLE_OK before leaving the receive branch, so sendrecv_dl reports success even though nothing was received:

C
static CURLcode sendrecv_dl(struct Curl_easy *data,
                            struct SingleRequest *k)
{
...
    rcvd_eagain = FALSE;
    nread = xfer_recv_resp(data, buf, bytestoread, is_multiplex, &result); // [3] call xfer_recv_resp
    if(nread < 0) {
      if(CURLE_AGAIN != result)
        goto out; /* real error */
      rcvd_eagain = TRUE;
      result = CURLE_OK; //[4]set result to CURLE_OK
      if(data->req.download_done && data->req.no_body &&
         !data->req.resp_trailer) {
        DEBUGF(infof(data, "EAGAIN, download done, no trailer announced, "
               "not waiting for EOS"));
        nread = 0;
        /* continue as if we received the EOS */
      }
      else
        break; /* get out of loop */
    }
...

Line by line: nread is the number of bytes read, and xfer_recv_resp at [3] fills it in. If nread is less than zero, something went wrong. The code then asks: was the error anything other than CURLE_AGAIN? If so, it is a real error and the function jumps out. If it was CURLE_AGAIN, it sets a flag rcvd_eagain to true and, at [4], replaces the result with CURLE_OK, meaning "success". Then there is a special case: if the download was already done, there was no body expected and no trailer was announced, it pretends it received the end of the stream. Otherwise it breaks out of the loop. The key point is that a "nothing to read yet" situation has been turned into a "success" answer.

That combination, a successful return with no data and no completion, is what makes the state machine misbehave.

Exploitation

Step 1: start the malicious FTP server

Download the attached ftp_poc.py and run it with root privileges so it can bind port 21 on the current machine:

Bash
sudo python3 ./ftp_poc.py

sudo means "run this as the administrator", because opening a low-numbered port like 21 is a privileged action on Linux. python3 runs the Python interpreter, and ./ftp_poc.py is the script to run. This starts a malicious FTP service on port 21 of the current machine. The server opens an EPSV data port and then sends nothing to the client. In everyday terms: it answers the phone, says "hold on, I will send the file", and then stays silent forever.

Step 2: point curl at the server

Run curl against the server, replacing [redacted] with the address of your own malicious FTP server:

Bash
./curl -u anonymous:123 'ftp://[redacted]/test' -o ./test

./curl runs the curl program from the current folder. -u anonymous:123 supplies a username and password; anonymous is the conventional FTP name for a guest login, and 123 is the password used in the demonstration. The quoted ftp://[redacted]/test is the URL: the protocol is FTP, the address is the server, and /test is the file being requested. -o ./test tells curl to save the downloaded content into a local file called test. The command authenticates as anonymous with password 123, requests the file /test over FTP, and writes the output to ./test.

Step 3: observe the loop

curl enters an infinite code loop and does not exit on its own. You would see it never finish and never return you to the prompt.

Root cause

The loop comes from how state_performing interprets the return of Curl_sendrecv. Walking through the same function with the values produced by the silent EPSV server:

C
static CURLMcode state_performing(struct Curl_easy *data,
                                  struct curltime *nowp,
                                  bool *stream_errorp,
                                  CURLcode *resultp)
{
  char *newurl = NULL;
  bool retry = FALSE;
  CURLMcode rc = CURLM_OK;
  CURLcode result = *resultp = CURLE_OK;
  *stream_errorp = FALSE;

  if(mspeed_check(data, nowp) == CURLE_AGAIN)
    return CURLM_OK;

  /* read/write data if it is ready to do so */
  result = Curl_sendrecv(data, nowp);

  if(data->req.done || (result == CURLE_RECV_ERROR)) { // [5] data->req.done == 0, result==CURLE_OK
    /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
     * condition and the server closed the reused connection exactly when we
     * wanted to use it, so figure out if that is indeed the case.
     */
    CURLcode ret = Curl_retry_request(data, &newurl);
    if(!ret)
      retry = !!newurl;
    else if(!result)
      result = ret;

    if(retry) {
      /* if we are to retry, set the result to OK and consider the
         request as done */
      result = CURLE_OK;
      data->req.done = TRUE;
    }
  }
#ifndef CURL_DISABLE_HTTP
  else if((CURLE_HTTP2_STREAM == result) &&
          Curl_h2_http_1_1_error(data)) {
    CURLcode ret = Curl_retry_request(data, &newurl);

    if(!ret) {
      infof(data, "Downgrades to HTTP/1.1");
      streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
      data->state.http_neg.wanted = CURL_HTTP_V1x;
      data->state.http_neg.allowed = CURL_HTTP_V1x;
      /* clear the error message bit too as we ignore the one we got */
      data->state.errorbuf = FALSE;
      if(!newurl)
        /* typically for HTTP_1_1_REQUIRED error on first flight */
        newurl = strdup(data->state.url);
      if(!newurl) {
        result = CURLE_OUT_OF_MEMORY;
      }
      else {
        /* if we are to retry, set the result to OK and consider the request
          as done */
        retry = TRUE;
        result = CURLE_OK;
        data->req.done = TRUE;
      }
    }
    else
      result = ret;
  }
#endif

  if(result) { //[6] result==CURLE_OK
    /*
     * The transfer phase returned error, we mark the connection to get closed
     * to prevent being reused. This is because we cannot possibly know if the
     * connection is in a good shape or not now. Unless it is a protocol which
     * uses two "channels" like FTP, as then the error happened in the data
     * connection.
     */

    if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
       result != CURLE_HTTP2_STREAM)
      streamclose(data->conn, "Transfer returned error");

    multi_posttransfer(data);
    multi_done(data, result, TRUE);
  }
  else if(data->req.done && !Curl_cwriter_is_paused(data)) { //[7] data->req.done == 0
    const struct Curl_handler *handler = data->conn->handler;

    /* call this even if the readwrite function returned error */
    multi_posttransfer(data);

    /* When we follow redirects or is set to retry the connection, we must to
       go back to the CONNECT state */
    if(data->req.newurl || retry) {
      followtype follow = FOLLOW_NONE;
      if(!retry) {
        /* if the URL is a follow-location and not just a retried request then
           figure out the URL here */
        free(newurl);
        newurl = data->req.newurl;
        data->req.newurl = NULL;
        follow = FOLLOW_REDIR;
      }
      else
        follow = FOLLOW_RETRY;
      (void)multi_done(data, CURLE_OK, FALSE);
      /* multi_done() might return CURLE_GOT_NOTHING */
      result = multi_follow(data, handler, newurl, follow);
      if(!result) {
        multistate(data, MSTATE_SETUP);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
    else {
      /* after the transfer is done, go DONE */

      /* but first check to see if we got a location info even though we are
         not following redirects */
      if(data->req.location) {
        free(newurl);
        newurl = data->req.location;
        data->req.location = NULL;
        result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
        if(result) {
          *stream_errorp = TRUE;
          result = multi_done(data, result, TRUE);
        }
      }

      if(!result) {
        multistate(data, MSTATE_DONE);
        rc = CURLM_CALL_MULTI_PERFORM;
      }
    }
  }
  else { /* not errored, not done */
    mspeed_check(data, nowp); // [8]
  }
  free(newurl);
  *resultp = result;
  return rc;
}

Read this as a series of doors. At [5] the code asks: is the request done, or did we get a receive error? With the silent server, the request is not done (data->req.done == 0) and the result is CURLE_OK, so this door stays shut. The block between #ifndef CURL_DISABLE_HTTP and #endif handles an HTTP/2 downgrade case; it is compiled out if HTTP support is disabled, and in any case it only applies when the result is a specific HTTP/2 error, which is not our situation. At [6] the code asks: is there an error result? The result is CURLE_OK, so no. At [7] it asks: is the request done and is the writer not paused? The request is not done, so no. All three doors are shut, and execution falls to the final else at [8], the "not errored, not done" branch.

Because no data was received, data->req.done stays 0. Because the result was rewritten to CURLE_OK, the conditions at [5], [6] and [7] all evaluate to false, and execution falls through to [8], the "not errored, not done" branch. That branch only calls mspeed_check and returns; it never changes data->mstate, which remains MSTATE_PERFORMING. data->mstate is the variable that records which square of the board game curl is on. curl therefore re-enters state_performing again and again, producing the infinite loop.

In short, the receive path converts a would-block condition into a success return, and the state machine has no path from "success with nothing done" to any other state.

Impact

When curl attempts to download files from a malicious FTP server, it triggers an infinite loop in code execution. The client does not exit on its own, so a user or automated job that connects to such a server is left hanging. The report does not describe any memory corruption, data exposure or code execution beyond the loop itself.

Fix

The report does not describe a fix or a patch.

These are follow-up ideas: other situations worth checking to see whether the same kind of stuck state appears elsewhere. A positive result in each case means the client stops making progress without reporting an error or finishing.

  1. Other protocols with a separate data channel, such as the dual-channel protocols curl marks with PROTOPT_DUAL. A positive result is a client that stops making progress while its state remains in the performing phase.
  2. Servers that accept the data connection and then stall without closing it, rather than refusing or resetting. A positive result is a client that neither errors nor completes.
  3. Partial sends followed by silence, to see whether a success return with no completion can be produced after some data has already been delivered. A positive result is the same non-advancing state after a partial transfer.
  4. Timeout and low-speed settings, to check whether mspeed_check eventually breaks the loop or whether the state remains stuck regardless. A positive result is a client that stays in the loop even with speed limits configured.

Glossary

  • curl: a command line tool and library for transferring data with URLs.
  • Command line tool: a program you run by typing its name in a terminal rather than clicking an icon.
  • Terminal: the text window where you type commands.
  • Library: a bundle of reusable code that other programs can call.
  • Protocol: an agreed language two computers use to talk, such as FTP or HTTP.
  • FTP: File Transfer Protocol, an older protocol for sending files between computers, using one connection for instructions and another for data.
  • URL: the address of something on a network, like ftp://[redacted]/test.
  • Server: a computer that answers requests from clients.
  • Client: the program making requests, here curl.
  • Denial of service: an attack that stops a program or service from working, without necessarily stealing data.
  • State machine: a design where a program is always in one named state, and each state decides what happens next and which state follows.
  • Infinite loop: a loop that never ends because nothing changes to let it exit.
  • EPSV: an FTP mode where the server opens a port and waits for the client to connect, then sends data through it.
  • TCP port: a numbered door on a computer used for a network conversation.
  • Port 21: the standard port for FTP instructions.
  • Source tree: the collection of code files that make up a program before it is compiled.
  • Tag: a label marking a particular version in a code repository.
  • Ubuntu 22.04: a version of the Linux operating system.
  • Proof of concept: a small demonstration program showing a problem is real.
  • Multi interface: curl's mechanism for running several transfers at once, which drives the state machine.
  • MSTATE_PERFORMING: the state name meaning curl is in the middle of transferring data.
  • CURLE_OK: curl's result code meaning success.
  • CURLE_AGAIN: curl's result code meaning nothing is available right now, try again later.
  • CURLE_RECV_ERROR: curl's result code meaning receiving data failed.
  • data->req.done: a flag recording whether the request has finished.
  • data->mstate: the variable holding which state curl is currently in.
  • mspeed_check: the function that checks whether the transfer is meeting its speed requirements.
  • PROTOPT_DUAL: a flag marking protocols that use two channels, like FTP.
  • Root privileges: administrator rights on Linux, needed to open low-numbered ports.
  • sudo: the command that runs another command with administrator rights.
  • Python: a programming language, used here to write the demonstration server.
  • anonymous: the conventional FTP username for a guest login.
  • Memory corruption: a class of bug where a program writes where it should not, potentially leading to crashes or code execution.
  • Patch: a code change that fixes a bug.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.