CVE-2016-6912 (C, published 2017-01-26T15:59Z)
Description: value: Double free vulnerability in the gdImageWebPtr function in the GD Graphics Library (aka libgd) before 2.2.4 allows remote attackers to have unspecified impact via large width and height values.
Severity: HIGH (CVSS v3 base score: 9.8)
Weakness: CWE-415 \u2014 Double Free
The product calls free() twice on the same memory address, potentially leading to modification of unexpected memory locations.
Repository: https://github.com/libgd/libgd
Files changed:
src/gd_webp.c
tests/webp/.gitignore
tests/webp/CMakeLists.txt
tests/webp/Makemodule.am
tests/webp/bug_double_free.c
Commit message: Fix double-free in gdImageWebPtr()

The issue is that gdImageWebpCtx() (which is called by gdImageWebpPtr() and
the other WebP output functions to do the real work) does not return whether
it succeeded or failed, so this is not checked in gdImageWebpPtr() and the
function wrongly assumes everything is okay, which is not, in this case,
because there is a size limitation for WebP, namely that the width and
height must by less than 16383.

We can't change the signature of gdImageWebpCtx() for API compatibility
reasons, so we introduce the static helper _gdImageWebpCtx() which returns
success respective failure, so gdImageWebpPtr() and gdImageWebpPtrEx() can
check the return value. We leave it solely to libwebp for now to report
warnings regarding the failing write.

This issue had been reported by Ibrahim El-Sayed to security@libgd.org.

CVE-2016-6912


Vulnerable code:
/*
  Function: gdImageWebpCtx

    Write the image as WebP data via a <gdIOCtx>. See <gdImageWebpEx>
    for more details.

  Parameters:

    im      - The image to write.
    outfile - The output sink.
    quality - Image quality.
  Returns:

    Nothing.
*/
BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
		return;
		gd_error("Paletter image not supported by webp");
		return;
		return;
		return;
		return;
	gdImageWebpCtx(im, out, quality);
	gdImageWebpCtx(im, out, -1);
	gdImageWebpCtx(im, out, -1);
	rv = gdDPExtractData(out, size);
	gdImageWebpCtx(im, out, quality);
	rv = gdDPExtractData(out, size);
	webp/bug00111
	webp/CMakeLists.txt

Fixed code:
/* returns 0 on success, 1 on failure */
static int _gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
    int ret = 0;
		return 1;
		gd_error("Palette image not supported by webp");
		return 1;
		return 1;
		return 1;
		return 1;
        ret = 1;

    return ret;
}


/*
  Function: gdImageWebpCtx

    Write the image as WebP data via a <gdIOCtx>. See <gdImageWebpEx>
    for more details.

  Parameters:

    im      - The image to write.
    outfile - The output sink.
    quality - Image quality.

  Returns:

    Nothing.
*/
BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
{
	_gdImageWebpCtx(im, outfile, quality);
	_gdImageWebpCtx(im, out, quality);
	_gdImageWebpCtx(im, out, -1);
	if (_gdImageWebpCtx(im, out, -1)) {
		rv = NULL;
	} else {
		rv = gdDPExtractData(out, size);
	}
	if (_gdImageWebpCtx(im, out, quality)) {
        rv = NULL;
    } else {
        rv = gdDPExtractData(out, size);
    }
/bug_double_free
	bug_double_free
	webp/bug00111 \
	webp/bug_double_free
	webp/CMakeLists.txt \
	webp/bug_double_free.jpg
/**
 * Test that a too large image doesn't trigger an double-free when written
 * to memory.
 */


#include "gd.h"
#include "gdtest.h"


int main()
{
    gdImagePtr im1, im2;
    FILE *fp;
    int size;

    fp = gdTestFileOpen2("webp", "bug_double_free.jpg");
    gdTestAssert(fp != NULL);
    im1 = gdImageCreateFromJpeg(fp);
    gdTestAssert(im1 != NULL);
    fclose(fp);

    im2 = gdImageWebpPtr(im1, &size);
    gdTestAssert(im2 == NULL);

    gdImageDestroy(im1);

    return gdNumFailures();
}