Skip to content
Memory corruption Disclosed report Advanced

Heap-use-after-free in CURLOPT_REFERER when passed a CURLINFO_REFERER pointer

Setting CURLOPT_REFERER with a pointer previously returned by CURLINFO_REFERER on the same easy handle causes a heap-use-after-free, because the setopt handler frees the referer buffer before copying from that same pointer. In an unsanitised build libcurl may crash or copy stale heap bytes into the Referer value sent in a later request.

giant_anteater Follow

Originally disclosed on HackerOne

  • Disclosed after 19 days

Summary

Setting CURLOPT_REFERER with a pointer previously obtained from CURLINFO_REFERER on the same easy handle triggers a heap-use-after-free. The CURLOPT_REFERER handler frees data->state.referer (line lib/setopt.c:2089) before Curl_setstropt reads from the now-dangling pointer (line lib/setopt.c:2090→85).

The bug was introduced in commit ff300ac4aa ("setopt: defref the old referer when setting a new", closing PR #21826).

Root cause

CURLINFO_REFERER returns Curl_bufref_ptr(&data->state.referer) — a direct pointer into handle-internal heap memory allocated during redirect handling in Curl_http_follow().

The CURLOPT_REFERER handler does:

C
// lib/setopt.c:2085-2091
case CURLOPT_REFERER:
    Curl_bufref_free(&data->state.referer);          // frees the buffer
    result = Curl_setstropt(data, STRING_SET_REFERER, ptr); // reads from ptr
    break;

When ptr is the same pointer returned by CURLINFO_REFERER, the free on line 2089 invalidates the memory that Curl_setstropt then reads via strlen(s) on line 85.

This usage is not prohibited by the API documentation. CURLINFO_REFERER documents that the pointer is private memory the application must not free. curl_easy_setopt documents that string arguments are copied and the application does not need to retain them. Passing a valid const char * from one API to another that promises to copy it is a reasonable composition.

Reproduction

  • The curl source tree at or after commit ff300ac4aa
  1. Build curl at the affected commit with ASan:
Shell
cmake -S . -B build \
  -DCMAKE_BUILD_TYPE=Debug \
  -DCMAKE_C_FLAGS="-fsanitize=address -fno-omit-frame-pointer -O1" \
  -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address" \
  -DBUILD_SHARED_LIBS=ON -DBUILD_CURL_EXE=OFF -DBUILD_TESTING=OFF \
  -DCURL_USE_OPENSSL=ON
cmake --build build -j$(nproc)
  1. Compile the PoC:
Shell
cc -fsanitize=address -fno-omit-frame-pointer -O1 \
  poc.c -Iinclude -Lbuild/lib -Wl,-rpath,build/lib -lcurl -o poc
  1. Start the server and run both paths:
Shell
python3 server.py &
sleep 1

# Control (strdup'd copy) — succeeds cleanly:
./poc control

# Aliased pointer — triggers heap-use-after-free:
./poc vulnerable

Expected vs. actual

  • Control path: completes with SETOPT_RESULT=0. No ASan errors.
  • Aliased path: ASan reports heap-use-after-free in strlen called from Curl_setstropt (lib/setopt.c:85), on memory freed by Curl_bufref_free at lib/setopt.c:2089.
ERROR: AddressSanitizer: heap-use-after-free
READ of size 3
    #0 ... in strlen
    #1 ... in Curl_setstropt lib/setopt.c:85
    #2 ... in setopt_cptr_http_mqtt lib/setopt.c:2090

freed by thread T0 here:
    #1 ... in Curl_bufref_free lib/bufref.c:61
    #2 ... in setopt_cptr_http_mqtt lib/setopt.c:2089

previously allocated by thread T0 here:
    #8 ... in curl_url_get lib/urlapi.c:1713
    #9 ... in Curl_http_follow lib/http.c:1224

Impact

The immediate effect is an invalid heap read during strlen. In an unsanitized build, libcurl may crash or copy stale heap contents into its Referer value, potentially sending those bytes in a later HTTP request.

Triggering requires all of:

  1. CURLOPT_FOLLOWLOCATION + CURLOPT_AUTOREFERER enabled.
  2. The application retrieves the auto-generated referer via CURLINFO_REFERER.
  3. The application passes that exact pointer back to CURLOPT_REFERER on the same handle.

A remote peer cannot trigger this in an arbitrary libcurl application without the application performing this pointer round-trip. This is an application-side usage pattern rather than a remotely exploitable primitive, which limits the practical attack surface.

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

The CURLOPT_REFERER handler frees data->state.referer before Curl_setstropt copies from the caller's pointer, which may alias that same freed buffer.

Impact

The immediate effect is an invalid heap read during strlen; in an unsanitised build libcurl may crash or copy stale heap contents into its Referer value, potentially sending those bytes in a later HTTP request, but a remote peer cannot trigger it without the application doing the pointer round-trip.

What the attacker needed

CURLOPT_FOLLOWLOCATION and AUTOREFERER enabled; app reads referer via CURLINFO_REFERER; app passes that pointer to CURLOPT_REFERER

How it was found

  1. Build curl at the affected commit with AddressSanitizer enabled.
  2. Compile the proof-of-concept program against that build.
  3. Start the local test server and run the control path, which passes a copied string and succeeds.
  4. Run the aliased path, which passes the CURLINFO_REFERER pointer back to CURLOPT_REFERER and triggers the use-after-free.

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.

A program using the curl library can ask it for the web address of the page it came from (the referer) and then hand that same address back to curl to set it again. Curl frees the old stored address first and then tries to read from the very memory it just freed, which is a use-after-free: reading memory that no longer belongs to you. The result is a crash or garbage data being sent, but only if the program does this unusual pointer round-trip itself.

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 giant_anteater

View all

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