Project: imagemagick (imagemagick/imagemagick), language: c++
Vulnerability instance: imagemagick.cve-2017-11310
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/517
## Description:
Issue: ImageMagick/ImageMagick#517
Title: memory leak in  read_user_chunk_callback in png.c
State: closed
Created by: jgj212
Created at: 2017-06-22 03:21:28+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.6-1 Q16 x86_64 2017-06-21

The read_user_chunk_callback function in png.c allows attackers to cause a denial of service (memory leak) via a small crafted png file.
```
#identify $FILE
=================================================================
==8732==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x4dee06 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f3b655c8a16 in AcquireMagickMemory memory.c:463:10
    #2 0x7f3b6574a00d in AcquireStringInfo string.c:174:30
    #3 0x7f3b6574a3de in BlobToStringInfo string.c:219:15
    #4 0x7f3b65c157aa in read_user_chunk_callback png.c:1878:15
    #5 0x7f3b63f28fe0  (/lib/x86_64-linux-gnu/libpng12.so.0+0x9fe0)

Indirect leak of 4102 byte(s) in 1 object(s) allocated from:
    #0 0x4df1bd in realloc asan_malloc_linux.cc:79
    #1 0x7f3b655ca383 in ResizeMagickMemory memory.c:1219:9
    #2 0x7f3b655ca41d in ResizeQuantumMemory  memory.c:1283:10
    #3 0x7f3b6574a5de in BlobToStringInfo  string.c:227:42
    #4 0x7f3b65c157aa in read_user_chunk_callback  png.c:1878:15
    #5 0x7f3b63f28fe0  (/lib/x86_64-linux-gnu/libpng12.so.0+0x9fe0)

SUMMARY: AddressSanitizer: 4134 byte(s) leaked in 2 allocation(s).
```

testcase
https://github.com/jgj212/poc/blob/master/read_user_chunk_callback.png
```
$xxd -g -1 1.png 
0000000: 8950 4e47 0d0a 1a0a 0000 0001 6666 6666  .PNG........ffff
0000010: 6600 0000 0000 0000 0066 6666 7066 6691  f........fffpff.
0000020: 6600 0000 0065 5849 6666 2741 534d 0a    f....eXIff'ASM.
```



Credit: ADLab of Venustech


Comments:
Comment by mikayla-grace on 2017-06-22 23:08:49+00:00:
Thanks for the problem report.  We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today.  The patch will be available in the beta releases of ImageMagick @ http://www.imagemagick.org/download/beta/ by sometime tomorrow. 


---

Comment by bastien-roucaries on 2017-07-09 16:36:34+00:00:
Does it affect v6 ? If so could you add commit ?

---

Comment by stevebeattie on 2017-07-14 23:36:31+00:00:
This was assigned [CVE-2017-11310](http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-11310) 

---

Comment by glennrp on 2017-07-15 00:28:51+00:00:
I've updated the ChangeLog with the CVE number.

---

Comment by jgj212 on 2017-07-15 00:32:50+00:00:
@glennrp  could you please tell me the changelog url?

---

Comment by glennrp on 2017-07-15 00:45:55+00:00:
https://github.com/ImageMagick/ImageMagick/blob/master/ChangeLog
See the 2017-06-22 entry.

---

Comment by bastien-roucaries on 2017-07-15 08:43:18+00:00:
Could you give a ETA for v6 ?


---

Comment by glennrp on 2017-07-15 10:22:42+00:00:
I believe this bug did not affect IM6.

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
./configure --prefix="$WORK" --disable-shared --disable-docs
make "-j$(nproc)"
make install -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout 72a50e400d98d7a2fd610caedfeb9af043dc5582
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files:
filename: crop_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  uint16_t Width;
  uint16_t Height;
  if (Size < (sizeof(Width) + sizeof(Height))) {
    return 0;
  }
  Width = *reinterpret_cast<const uint16_t *>(Data);
  Height = *reinterpret_cast<const uint16_t *>(Data + sizeof(Width));
  const Magick::Blob blob(Data + sizeof(Width) + sizeof(Height),
                          Size - (sizeof(Width) + sizeof(Height)));
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  image.crop(Magick::Geometry(Width, Height));
  return 0;
}

filename: encoder_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

#define FUZZ_ENCODER_STRING_LITERAL(name) #name
#define FUZZ_ENCODER FUZZ_ENCODER_STRING_LITERAL(FUZZ_IMAGEMAGICK_ENCODER)

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }

  Magick::Blob outBlob;
  try {
    image.write(&outBlob, FUZZ_ENCODER);
  } catch (Magick::Exception &e) {
  }
  return 0;
}

filename: encoder_list.cc
content: #include <iostream>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int main() {
  size_t nFormats;
  Magick::ExceptionInfo ex;
  const Magick::MagickInfo **formats = GetMagickInfoList("*", &nFormats, &ex);

  for (size_t i = 0; i < nFormats; i++) {
    const Magick::MagickInfo *format = formats[i];
    if (format->encoder && format->name) {
      std::cout << format->name << std::endl;
    }
  }
}

filename: enhance_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
    image.enhance();
  } catch (Magick::Exception &e) {
    return 0;
  }
  return 0;
}

filename: huffman_decode_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  Magick::ExceptionInfo ex;
  auto res = HuffmanDecodeImage(image.image(), &ex);
  return 0;
}

filename: rotate_fuzzer.cc
content: #include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  if (Size < sizeof(double)) {
    return 0;
  }
  double Degrees = *reinterpret_cast<const double *>(Data);
  if (!isfinite(Degrees)) {
    return 0;
  }
  const Magick::Blob blob(Data + sizeof(Degrees), Size - sizeof(Degrees));
  Magick::Image image;
  try {
    image.read(blob);
  } catch (Magick::Exception &e) {
    return 0;
  }
  image.rotate(Degrees);
  return 0;
}