Skip to content
Memory corruption Disclosed report Advanced

WebSocket send queue use-after-free during nested curl_ws_send()

A use-after-free in libcurl's WebSocket send path occurs when curl_ws_send() is called from within the WebSocket write callback and a debug callback triggers a nested send, causing the outer send to read from a freed buffer. A WebSocket peer could receive bytes from reused heap memory.

rat5ak Follow

Originally disclosed on HackerOne

  • Disclosed after 44 hours

At lib/ws.c:1734, ws_flush() borrows a pointer from the outgoing sendbuf and passes it to ws_send_raw_blocking(); it skips those bytes only after the send returns. A curl_ws_send() from the WebSocket write callback can be interrupted by a TCP-trace debug callback that calls curl_ws_send() again, and the nested flush frees the chunk the outer send is using. On curl-8_23_0 with ENABLE_DEBUG=OFF, ASan reports a 32,767-byte use-after-free read in send(), while the no-nested control is clean. In a Release run, the first decoded frame differs at byte 32,754, a duplicate frame header appears inside it at raw offset 32,768, and 32,767 bytes from a test allocation that reused the freed chunk reach the server in the raw tail. This path does not use curl_easy_pause() and survives the proposed fixes for my earlier reports.

Steps to reproduce

  1. ASan build, nested trigger, and no-nested control:

    Shell
    POC=/absolute/path/to/h1-attachments
    git checkout curl-8_23_0
    cmake -S . -B bld-asan -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DENABLE_DEBUG=OFF -DCURL_DISABLE_VERBOSE_STRINGS=OFF -DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer -g"
    cmake --build bld-asan --target libcurl_static -j2
    sh "$POC/run.sh" "$PWD/bld-asan" nested   # UAF, READ of size 32767
    sh "$POC/run.sh" "$PWD/bld-asan" control  # CLEAN
    
  2. Release build, canary, and control:

    Shell
    cmake -S . -B bld-release -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON -DENABLE_DEBUG=OFF -DCURL_DISABLE_VERBOSE_STRINGS=OFF
    cmake --build bld-release --target libcurl_static -j2
    sh "$POC/run.sh" "$PWD/bld-release" canary   # CORRUPT, 32767-byte 0xA5 run
    sh "$POC/run.sh" "$PWD/bld-release" control  # CLEAN
    
  3. Fixed control and regression test:

    Shell
    git apply "$POC/ws-send-fix-and-test.diff"
    cmake --build bld-asan --target libcurl_static -j2
    sh "$POC/run.sh" "$PWD/bld-asan" nested   # CLEAN
    

    Test 2313 in the diff fails before the fix and passes after it.

Source history points to commit 02edae54e8, first included in 8.12.0; the earliest release where I reproduced the UAF is 8.15.0, while 8.10.0 was clean. I did not run 8.12.0.

Impact

A WebSocket peer can receive bytes from reused heap memory when an application uses this nested callback pattern

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely ws_flush() borrows a pointer into the outgoing sendbuf and only advances past those bytes after the send returns, so a nested send can free the chunk while the outer send still uses it.

Impact

A WebSocket peer could receive bytes from reused heap memory when an application uses this nested callback pattern.

What the attacker needed

Application calls curl_ws_send in write callback; debug callback triggers nested send; libcurl 8.15.0 (reproduced); 8.12.0 untested

The fix

The reporter supplied a fix and regression test that make the nested case clean, but the text does not say it was merged or released.

How it was found

  1. Build libcurl with AddressSanitizer and run the nested trigger, which reports a use-after-free read.
  2. Run the no-nested control against the same build, which is clean.
  3. Build a Release version and run the canary case, which shows corrupted bytes.
  4. Run the Release control, which is clean.
  5. Apply the provided fix and test diff, rebuild, and confirm the nested case is now clean.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When a program sends data over a network connection, it usually keeps the data in a temporary memory buffer until the send finishes. Here, a second send triggered from inside the first one freed that buffer early, so the first send kept reading memory that no longer belonged to it. That leftover memory could contain data from somewhere else in the program, and it gets sent to the other side of the connection.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by rat5ak

View all

3 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.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.