Skip to content
Memory corruption Disclosed report Advanced

Cookie Replacement Use-After-Free Vulnerability

The cookie replacement logic in curl's lib/cookie.c contains a use-after-free vulnerability in the replace_existing() function, where a linked list is modified while iterating, leading to memory corruption. An attacker could potentially trigger heap corruption, information disclosure, or arbitrary code execution through crafted cookie operations.

bhaskar_ram Follow

Originally disclosed on HackerOne

Informational View original
  • Disclosed after 1 hour

Summary:

The cookie replacement logic in lib/cookie.c contains a use-after-free vulnerability in the replace_existing() function. The function modifies a linked list while iterating over it, creating potential for memory corruption in concurrent or complex cookie operations.

Vulnerable Code Location: lib/cookie.c:822-925

C
static bool replace_existing(struct Curl_easy *data,
                             struct Cookie *co,
                             struct CookieInfo *ci,
                             bool secure,
                             bool *replacep)
{
    bool replace_old = FALSE;
    struct Curl_llist_node *replace_n = NULL;
    struct Curl_llist_node *n;
    
    // Iterate over cookie list
    for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
        struct Cookie *clist = Curl_node_elem(n);
        // ... comparison logic ...
        if(replace_old)
            replace_n = n;  // Store node for later deletion
    }
    
    // Delete node after iteration
    if(replace_n) {
        struct Cookie *repl = Curl_node_elem(replace_n);
        co->creationtime = repl->creationtime;  // Access freed memory
        Curl_node_remove(replace_n);
        freecookie(repl, TRUE);  // Free memory
    }
}

Affected version

curl 7.x - 8.x

Steps To Reproduce:

PoC Server

A race condition server has been developed to trigger the vulnerability:

File: poc_cookie_uaf.py

Bash
# Run the PoC server
python3 poc_cookie_uaf.py

# Server starts on http://localhost:8000
# Provides 3 different exploit scenarios

Testing with AddressSanitizer

CRITICAL: This vulnerability requires AddressSanitizer to detect reliably.

Build curl with ASan

Bash
cd curl
export CFLAGS="-fsanitize=address -fno-omit-frame-pointer -g"
export LDFLAGS="-fsanitize=address"
./buildconf
./configure --enable-debug --disable-shared
make clean
make

Test 1: Rapid Cookie Replacement

Bash
# Single request with 50 cookies
./src/curl -c cookies.txt http://localhost:8000/exploit1

# Check for ASan errors
# Look for: "heap-use-after-free"

Expected ASan Output (if vulnerable):

==12345==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000001234
READ of size 8 at 0x602000001234 thread T0
    #0 0x... in replace_existing lib/cookie.c:915
    #1 0x... in Curl_cookie_add lib/cookie.c:1234
    #2 0x... in Curl_http_input_auth lib/http.c:567

Test 2: Concurrent Requests

Bash
# Trigger race conditions with concurrent requests
for i in {1..20}; do
    ./src/curl -c cookies_$i.txt http://localhost:8000/exploit2 &
done
wait

# Check for crashes or ASan errors

Test 3: Complex Replacement Patterns

Bash
# Test edge cases
./src/curl -c cookies.txt http://localhost:8000/exploit3

# Run multiple times to trigger race
for i in {1..100}; do
    ./src/curl -c cookies_test_$i.txt http://localhost:8000/exploit3
done

Verification Script

Python
#!/usr/bin/env python3
# verify_uaf.py - Check for use-after-free indicators

import subprocess
import sys

def run_test(url):
    """Run curl and check for ASan errors"""
    result = subprocess.run(
        ['./src/curl', '-c', 'test.txt', url],
        capture_output=True,
        text=True
    )
    
    # Check for ASan errors
    if 'heap-use-after-free' in result.stderr:
        print(f"[VULN] Use-after-free detected: {url}")
        print(result.stderr)
        return True
    elif 'double-free' in result.stderr:
        print(f"[VULN] Double-free detected: {url}")
        print(result.stderr)
        return True
    elif result.returncode != 0:
        print(f"[CRASH] curl crashed: {url}")
        return True
    
    return False

# Test all exploits
exploits = [
    'http://localhost:8000/exploit1',
    'http://localhost:8000/exploit2',
    'http://localhost:8000/exploit3',
]

vulnerable = False
for exploit in exploits:
    if run_test(exploit):
        vulnerable = True

if vulnerable:
    print("\\n[RESULT] Vulnerability confirmed!")
    sys.exit(1)
else:
    print("\\n[RESULT] No vulnerability detected (or patched)")
    sys.exit(0)

Supporting Material/References:

Attachments

  1. poc_cookie_uaf.py - Proof of concept race condition server
  2. fix_cookie_uaf.patch - Security patch

Impact

Summary:

  • Heap corruption from use-after-free
  • Potential for arbitrary code execution
  • Information disclosure through freed memory
  • Crashes from accessing freed memory
  • Segmentation faults in cookie handling
  • Application instability
  • Freed cookie data may contain sensitive information
  • Session tokens, authentication data exposed
  • Memory layout information leaked
  • If attacker can control freed memory contents
  • Heap spraying techniques applicable
  • Function pointer corruption possible

The original report as published on HackerOne, by bhaskar_ram. 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 function stores a pointer to a list node during iteration, then removes and frees that node, but later accesses the freed memory to copy the creation time.

Impact

The vulnerability could lead to heap corruption, information disclosure, crashes, and potentially arbitrary code execution if an attacker can control freed memory contents.

What the attacker needed

Curl vulnerable version; attacker controls server response; victim uses curl to connect

How it was found

  1. Build curl with AddressSanitizer enabled.
  2. Run the provided PoC server that serves crafted cookie responses.
  3. Send a request to the server with curl, saving cookies.
  4. Check the output for AddressSanitizer errors indicating heap-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 use-after-free is a memory safety bug where a program keeps using a piece of memory after it has been freed. In this case, curl's cookie handling code frees a cookie but then reads from it, which can crash the program or let an attacker run their own code. The bug is in a function that replaces old cookies with new ones.

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 bhaskar_ram

View all

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