Skip to content
Memory corruption Disclosed report Advanced

Use-after-free in php_curl related to CURLOPT_FILE/_INFILE/_WRITEHEADER

A use-after-free vulnerability in PHP's curl extension allows an attacker to close a stream after it has been verified, leading to a dangling FILE pointer that can be exploited for arbitrary code execution. The flaw affects multiple PHP versions and can be triggered by a crafted script using curl callbacks.

mongo Follow

Originally disclosed on HackerOne

  • Fixed in 3 months
  • Paid in 3 months
  • Disclosed after 18 days

https://bugs.php.net/bug.php?id=69316

Description:

All the relevant code mentioned below is in ext/curl/interface.c. As far as I can see, every PHP version after (at least) 5.0 is affected; possibly older versions too.

When using CURLOPT_WRITEHEADER, CURLOPT_INFILE or CURLOPT_FILE, in _php_curl_setopt, the provided stream is cast to a stdio FILE*:

if (FAILURE == php_stream_cast((php_stream *) what, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
        return FAILURE;
}

This FILE* is then stored in the php_curl structure "ch", at the following locations, depending on which CURLOPT_ was used:

  • ch->handlers->write->fp = fp;
  • ch->handlers->write_header->fp = fp;
  • ch->handlers->read->fp = fp;

Upon curl_exec(), _php_curl_verify_handlers() is called, which verifies if the user-set stream(s) are still open, and resets ->fp to 0 if they are not.

However, there are a number of curl callbacks we can use to close the stream after _php_curl_verify_handlers() has been called, resulting in the FILE* being free()'d. By allocating memory that ends up a the same address where the FILE structure was, its possible to achieve arbitrary code execution.

The following functions use *->fp without checking if the corresponding streams are still open (and thus if *->fp still points to a valid FILE structure or not):

  • static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
  • static size_t curl_read(char *data, size_t size, size_t nmemb, void *ctx)
  • static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx)
  • curl_exec (after curl processing is finished, there are 2 fflush() calls)

On Linux, with PHP linked against GLIBC, arbitrary code execution is trivial, since FILE structures conveniently have a "vtable" full of function pointers which we now control. On Windows, exploitability depends on the version of the C runtime being used. Recent MS C runtimes keep a cache of FILE structures (they aren't free()'d upon fclose()), which complicates things.

Please see the test script attached, tested against:

  • 64-bit PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:47:39)
  • 32-bit PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:48:03)
  • 32/64-bit PHP 5.6.7 (cli) (built: Mar 27 2015 07:04:21) (DEBUG) - custom build with ./configure --with-curl --enable-debug

Test script:

<?php
function hdr_callback($ch, $data) {
    global $f_file;

    if ($f_file) {
            // close the stream, causing the FILE structure to be free()'d
        fclose($f_file); $f_file = 0;

        // cause an allocation of approx the same size as a FILE structure, size varies a bit depending on platform/libc
        $FILE_size = (PHP_INT_SIZE == 4 ? 0x160 : 0x238);
        curl_setopt($ch, CURLOPT_COOKIE, str_repeat("a", $FILE_size - 1));
    }

    return strlen($data);
}

$ch = curl_init('http://www.php.net/');
$f_file = fopen("body", "w") or die("failed to open file\n");
curl_setopt($ch, CURLOPT_BUFFERSIZE, 10);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "hdr_callback");
curl_setopt($ch, CURLOPT_FILE, $f_file);
curl_exec($ch);
?>

The original report as published on HackerOne, by mongo. 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 curl extension stores a FILE* pointer from a user-supplied stream but does not re-check its validity after callbacks that can close the stream, leading to a use-after-free.

Impact

On Linux with glibc, arbitrary code execution is trivial; on Windows, exploitability depends on the C runtime version.

What the attacker needed

PHP with curl extension; ability to run PHP script

How it was found

  1. Create a curl handle and open a file stream for writing.
  2. Set CURLOPT_FILE to that stream and set a header callback.
  3. In the header callback, close the stream and allocate memory of similar size to a FILE structure.
  4. Execute the curl request, causing the freed FILE pointer to be used.

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.

This is a memory safety bug in PHP's curl extension. When you give curl a file to write to, PHP stores a pointer to that file's internal structure. If you close the file during a callback, the pointer becomes stale (a 'use-after-free'), and an attacker can trick PHP into using that freed memory, potentially running their own code.

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 mongo

View all

4 more disclosed reports, best first.

More from Internet Bug Bounty

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.