Skip to content
Memory corruption Disclosed report Advanced

Double Free Vulnerability in `libcurl` Cookie Management (`cookie.c`)

Two double-free bugs were reported in libcurl's cookie handling code, one when replacing an existing cookie and one on an error path when adding a cookie. The text claims an attacker could crash the program, corrupt memory, or possibly execute code, but gives no concrete attack scenario.

tannicarcher Follow

Originally disclosed on HackerOne

  • Disclosed after 10 hours

Description:

Two Double Free vulnerabilities have been identified in the cookie.c file of the libcurl library. These issues occur due to improper memory management, where the same memory area is freed multiple times under certain conditions. Below are clear steps to reproduce each vulnerability.


Summary:

The vulnerabilities occur in the following scenarios:

  1. replace_existing Function: A cookie object is freed without ensuring it has not already been removed from the list, leading to double-free.
  2. Curl_cookie_add Function: On errors, memory allocated for a cookie object is freed again, even if it was previously released.

Steps To Reproduce:

Case 1: Vulnerability in replace_existing Function

  1. Create multiple cookies with identical names, domains, and paths.

  2. Trigger a scenario where an existing cookie needs to be replaced (e.g., by adding a new cookie with the same attributes).

  3. Observe the program crash or unexpected behavior due to double-free in the following code:

    C
    if (replace_n) {
        struct Cookie *repl = Curl_node_elem(replace_n);
        Curl_node_remove(replace_n); // Remove from the list
        freecookie(repl);            // Free memory (double-free if already removed)
    }
    

Case 2: Vulnerability in Curl_cookie_add Function

  1. Add a malformed or invalid cookie header line to the cookie input file.

  2. Ensure the program encounters an error during cookie parsing.

  3. Observe the program crash or unexpected behavior due to double-free in the following code:

    C
    fail:
        freecookie(co); // Memory freed again even if already released
        return NULL;
    

Impact

Summary

The Double Free vulnerabilities in libcurl can have severe security implications:

  1. Program Crash: A double-free leads to undefined behavior, often resulting in segmentation faults.
  2. Arbitrary Code Execution: Attackers could exploit this issue to execute arbitrary code in the context of the vulnerable program.
  3. Data Integrity: Memory corruption caused by this vulnerability can lead to data leakage or integrity issues, compromising the application's reliability.
  4. Denial of Service (DoS): Exploiting this vulnerability could result in a denial-of-service condition.

These vulnerabilities can be exploited by attackers to compromise the security and stability of any applications or systems using libcurl.


The original report as published on HackerOne, by tannicarcher. 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. 4 min read.

Context

libcurl is the library behind the curl command line tool and a very large number of applications that speak HTTP and other protocols. Part of its job is cookie handling: parsing Set-Cookie headers, storing cookies, matching them against requests, and replacing cookies that already exist for the same name, domain and path. That logic lives in cookie.c.

The report describes two double free bugs in that file. In both cases the same memory area is released more than once under particular conditions, which is undefined behaviour in C. The affected asset is the curl repository at https://github.com/curl/curl.

Setup

The report does not describe any accounts, roles, tooling versions or build configuration. The reproduction steps are written against the source of cookie.c and describe the conditions under which each bug is reached, rather than a concrete harness or command line. What is needed, per the report, is a build of libcurl that exercises the cookie code paths described below.

Finding it

The researcher identified two places in cookie.c where memory management is not consistent with the state of the cookie list:

  1. In the replace_existing function, a cookie object is freed without ensuring it has not already been removed from the list.
  2. In the Curl_cookie_add function, on error paths, memory allocated for a cookie object is freed again even if it was previously released.

Both looked wrong because a free is performed on an object whose ownership has already been given up elsewhere, which is exactly the shape of a double free.

Exploitation

Case 1: replace_existing

  1. Create multiple cookies with identical names, domains and paths.
  2. Trigger a scenario where an existing cookie needs to be replaced, for example by adding a new cookie with the same attributes.
  3. Observe the program crash or unexpected behaviour caused by the double free in this code:
C
if (replace_n) {
    struct Cookie *repl = Curl_node_elem(replace_n);
    Curl_node_remove(replace_n); // Remove from the list
    freecookie(repl);            // Free memory (double-free if already removed)
}

The node is taken out of the list with Curl_node_remove, its element is retrieved, and then freecookie releases it. The comment in the report marks the hazard: if the object has already been removed, this second release is a double free. The precondition is having more than one cookie that collides on name, domain and path, so that the replacement path is taken.

Case 2: Curl_cookie_add

  1. Add a malformed or invalid cookie header line to the cookie input file.
  2. Ensure the program encounters an error during cookie parsing.
C
fail:
    freecookie(co); // Memory freed again even if already released
    return NULL;

The error label releases the cookie object and returns NULL. The report's comment states the problem directly: the memory is freed again even if it was already released on the path that jumped to fail. The trigger here is a malformed or invalid cookie header line in the cookie input file that drives parsing into the error path.

Root cause

Both bugs are ownership mistakes. In replace_existing, the code removes the node from the list and then frees the element without guaranteeing that the element has not already been released, so the same allocation can be freed twice. In Curl_cookie_add, the fail label unconditionally calls freecookie(co) on an error, even on paths where the object has already been released.

The report does not describe any existing defence that should have caught this, such as allocator hardening or a review of the error paths, so why it survived review is not stated.

Impact

The report lists four consequences of the double frees in libcurl:

  1. Program crash: a double free is undefined behaviour and often results in a segmentation fault.
  2. Arbitrary code execution: the report states attackers could exploit the issue to execute arbitrary code in the context of the vulnerable program.
  3. Data integrity: memory corruption can lead to data leakage or integrity issues, compromising the application's reliability.
  4. Denial of service: exploiting the vulnerability could produce a denial-of-service condition.

The report frames the exposure broadly: any application or system using libcurl can be affected.

Fix

What the code needs, based on the two snippets, is for each free to happen exactly once: in replace_existing, the element should only be released if it has not already been removed and freed, and in Curl_cookie_add, the fail path should not release an object that an earlier path has already released. The report itself does not state which of these was implemented.

Variants to test

  • Cookie replacement collisions: supply several cookies with the same name, domain and path and force the replacement path repeatedly. A positive result is a crash or allocator abort in the replacement logic.
  • Malformed cookie input: feed invalid cookie header lines through the cookie input file so that parsing reaches error labels. A positive result is a crash on the error path rather than a clean rejection.
  • Error paths after partial allocation: look for other labels in cookie handling that free an object after a previous failure has already released it. A positive result is a double free reported by an allocator or sanitiser.
  • List removal followed by free: audit other places where a node is removed from a list and its element freed, checking whether removal can happen twice. A positive result is a second free of the same allocation.

Hunting notes

This kind of bug hides in error handling and replacement logic in C libraries, especially where a single cleanup label serves several failure points. During recon, the signals are code patterns: a fail: label that frees a pointer, a remove-then-free sequence, and any path where an object can be released before the cleanup runs.

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. 10 min read.

What happened, in plain words

A security researcher looked at the source code of libcurl, a piece of software that many programs use to talk to websites, and found two places where it manages memory incorrectly. In both places, the same chunk of memory can be handed back to the system twice. In everyday terms, imagine you borrow a book from a library, return it, and then return it again. The librarian now has two records saying the book is back, but only one book exists. In a computer program, this kind of mistake is called a double free, and it can make the program crash or, in the worst case, let an attacker take control of it.

The report describes the two bugs, gives the exact lines of code where they happen, and lists the possible consequences. It does not include a ready-made tool or command to trigger them, and it does not describe a fix.

Context

What libcurl and curl are

curl is a command line tool: a program you run by typing text into a terminal window. It is used to fetch web pages, download files and send data to servers. libcurl is the library behind it. A library is a collection of ready-made code that other programs can reuse instead of writing everything from scratch. Many applications, not just the curl tool, use libcurl to speak HTTP and other network protocols.

HTTP is the language browsers and servers use to exchange web pages and data. When you visit a site, your browser sends an HTTP request and the server sends back an HTTP response.

What cookies are

A cookie is a small piece of text that a website asks your browser to store and send back on later requests. It is how a site remembers that you are logged in, for example. The server sends a cookie using a response header called Set-Cookie. A header is a labelled line of extra information attached to an HTTP message, a bit like the address and postmark on an envelope.

libcurl has to handle cookies for the programs that use it: it reads Set-Cookie headers, stores the cookies, matches them against outgoing requests, and replaces a cookie that already exists when a new one arrives with the same name, domain and path. The name, domain and path are the three attributes that identify which cookie is which: the domain is the website it belongs to, and the path is the part of the site it applies to. All of this cookie logic lives in a source file called cookie.c.

What the report found

The report describes two double free bugs in that file. In both cases the same memory area is released more than once under particular conditions. In the C programming language, which libcurl is written in, releasing memory twice is undefined behaviour: the language standard gives no promise about what will happen, so the program may crash, may continue silently with corrupted data, or may do something an attacker can steer.

The affected asset is the curl repository at https://github.com/curl/curl. A repository is the online store of a project's source code and its history.

Setup

The report does not describe any accounts, roles, tooling versions or build configuration. The reproduction steps are written against the source of cookie.c and describe the conditions under which each bug is reached, rather than a concrete harness or command line. What is needed, per the report, is a build of libcurl that exercises the cookie code paths described below. A build means turning source code into a runnable program.

Finding it

The researcher read the code and identified two places in cookie.c where memory management does not match the state of the cookie list:

  1. In the replace_existing function, a cookie object is freed without ensuring it has not already been removed from the list.
  2. In the Curl_cookie_add function, on error paths, memory allocated for a cookie object is freed again even if it was previously released.

A function is a named block of code that does one job and can be called from elsewhere. A cookie object here means the block of memory that holds one cookie's data. A list is a chain of items kept in order; removing an item from the list and freeing its memory are two separate actions, and the bug is that they get out of step.

Both looked wrong because a free is performed on an object whose ownership has already been given up elsewhere. Ownership is the informal rule in C about which part of the code is responsible for releasing a piece of memory. When two parts both think they own it, or one part releases it and another releases it again, you get exactly the shape of a double free.

Exploitation

Case 1: replace_existing

  1. Create multiple cookies with identical names, domains and paths.
  2. Trigger a scenario where an existing cookie needs to be replaced, for example by adding a new cookie with the same attributes.
  3. Observe the program crash or unexpected behaviour caused by the double free in this code:
C
if (replace_n) {
    struct Cookie *repl = Curl_node_elem(replace_n);
    Curl_node_remove(replace_n); // Remove from the list
    freecookie(repl);            // Free memory (double-free if already removed)
}

Line by line:

  • if (replace_n) { checks whether there is an existing cookie that needs to be replaced. replace_n refers to the list entry for that cookie. If there is none, the block is skipped.
  • struct Cookie *repl = Curl_node_elem(replace_n); fetches the actual cookie data out of the list entry and stores it in a variable called repl. A struct is a bundle of related values grouped under one name; struct Cookie is the bundle that represents one cookie.
  • Curl_node_remove(replace_n); takes the entry out of the list. The comment says exactly that: remove from the list.
  • freecookie(repl); releases the memory that holds the cookie data. The comment marks the hazard: this is a double free if the object has already been removed.
  • } closes the block.

The precondition is having more than one cookie that collides on name, domain and path, so that the replacement path is taken.

Case 2: Curl_cookie_add

  1. Add a malformed or invalid cookie header line to the cookie input file.
  2. Ensure the program encounters an error during cookie parsing.
C
fail:
    freecookie(co); // Memory freed again even if already released
    return NULL;

Line by line:

  • fail: is a label, a named point in the code that other parts can jump to when something goes wrong. It is the shared cleanup spot for several failure cases.
  • freecookie(co); releases the memory for the cookie object co. The comment states the problem directly: the memory is freed again even if it was already released on the path that jumped to fail.
  • return NULL; ends the function and hands back NULL, a special value meaning "nothing" or "failed", so the caller knows the cookie was not added.

Parsing means reading text and working out its structure, the way you might read a sentence and pick out subject and verb. A malformed cookie header line is one that does not follow the expected format, so parsing fails. The trigger here is a bad line in the cookie input file: it drives parsing into the error path, and that path frees memory that may already have been freed.

Root cause

Both bugs are ownership mistakes. In replace_existing, the code removes the node from the list and then frees the element without guaranteeing that the element has not already been released, so the same allocation can be freed twice. In Curl_cookie_add, the fail label unconditionally calls freecookie(co) on an error, even on paths where the object has already been released.

The happy path is the normal, successful route through the code; error handling only runs when something goes wrong, so it gets far less testing.

Impact

The report lists four consequences of the double frees in libcurl:

  1. Program crash: a double free is undefined behaviour and often results in a segmentation fault. A segmentation fault is the operating system stopping a program because it tried to touch memory it is not allowed to.
  2. Arbitrary code execution: the report states attackers could exploit the issue to execute arbitrary code in the context of the vulnerable program. In plain terms, an attacker might get their own instructions run by the program, with the same permissions the program has.
  3. Data integrity: memory corruption can lead to data leakage or integrity issues, compromising the application's reliability. Memory corruption means memory contents get changed in ways the program did not intend.
  4. Denial of service: exploiting the vulnerability could produce a denial-of-service condition. A denial of service means making a service unusable for its normal users, for example by crashing it repeatedly.

The report frames the exposure broadly: any application or system using libcurl can be affected.

Fix

What the code needs, based on the two snippets, is for each free to happen exactly once: in replace_existing, the element should only be released if it has not already been removed and freed, and in Curl_cookie_add, the fail path should not release an object that an earlier path has already released. The report itself does not state which of these was implemented.

Variants to test

  • Cookie replacement collisions: supply several cookies with the same name, domain and path and force the replacement path repeatedly. A positive result is a crash or allocator abort in the replacement logic. An allocator is the part of the system that hands out and takes back memory.
  • Malformed cookie input: feed invalid cookie header lines through the cookie input file so that parsing reaches error labels. A positive result is a crash on the error path rather than a clean rejection.
  • Error paths after partial allocation: look for other labels in cookie handling that free an object after a previous failure has already released it. A positive result is a double free reported by an allocator or sanitiser. A sanitiser is a debugging tool that watches a program run and shouts when it does something unsafe with memory.
  • List removal followed by free: audit other places where a node is removed from a list and its element freed, checking whether removal can happen twice. A positive result is a second free of the same allocation.

Hunting notes

This kind of bug hides in error handling and replacement logic in C libraries, especially where a single cleanup label serves several failure points. During recon, the signals are code patterns: a fail: label that frees a pointer, a remove-then-free sequence, and any path where an object can be released before the cleanup runs. Recon (reconnaissance) is the early stage of looking around a target to decide where to dig. A pointer is a variable that holds the address of a piece of memory rather than the data itself, like a note saying where a book sits on a shelf.

Glossary

  • Allocator: the part of the system that hands out and takes back memory for a program.
  • Arbitrary code execution: a situation where an attacker can get their own instructions run by a vulnerable program.
  • Build: the process of turning source code into a runnable program.
  • Cookie: a small piece of text a website asks a browser or client to store and send back on later requests.
  • Cookie object: the block of memory in the program that holds one cookie's data.
  • curl: a command line tool for fetching web pages and transferring data over network protocols.
  • Denial of service: making a service unusable for its normal users, for example by crashing it.
  • Domain: the website a cookie belongs to.
  • Double free: releasing the same piece of memory twice, which is undefined behaviour in C.
  • Function: a named block of code that does one job and can be called from elsewhere.
  • Happy path: the normal, successful route through code, as opposed to error handling.
  • Header: a labelled line of extra information attached to an HTTP message.
  • HTTP: the language browsers and servers use to exchange web pages and data.
  • Label: a named point in code that other parts can jump to, often used for shared cleanup on failure.
  • libcurl: the library behind the curl tool, used by many applications to speak HTTP and other protocols.
  • Library: a collection of ready-made code that other programs reuse.
  • List: a chain of items kept in order inside a program.
  • Malformed: not following the expected format, so it cannot be parsed correctly.
  • Memory corruption: memory contents changed in ways the program did not intend.
  • Name: the identifier of a cookie, one of the three attributes that decide which cookie is which.
  • NULL: a special value meaning "nothing" or "failed".
  • Ownership: the informal rule in C about which part of the code is responsible for releasing a piece of memory.
  • Parsing: reading text and working out its structure.
  • Path: the part of a website a cookie applies to.
  • Pointer: a variable that holds the address of a piece of memory rather than the data itself.
  • Recon: reconnaissance, the early stage of looking around a target to decide where to dig.
  • Repository: the online store of a project's source code and its history.
  • Sanitiser: a debugging tool that watches a program run and reports unsafe memory behaviour.
  • Segmentation fault: the operating system stopping a program because it tried to touch memory it is not allowed to.
  • Set-Cookie: the HTTP response header a server uses to send a cookie to the client.
  • Struct: a bundle of related values grouped under one name in C.
  • Undefined behaviour: a situation the C language standard gives no promise about, so anything may happen.

More by tannicarcher

View all

1 more disclosed report, 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.