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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/643
## Description:
Issue: ImageMagick/ImageMagick#643
Title: Memory leak in WriteImage
State: closed
Created by: EazyLov3
Created at: 2017-08-02 12:50:16+00:00
Labels: bug

Issue Body:
PoC Link : https://github.com/EazyLov3/FuzzPoC/blob/master/ImageMagick/Memory-Leak-1_output_fpx_1501588084.95

Trigger Command :  ./magick convert Memory-Leak-1_output_fpx_1501588084.95 output.fpx

Detail:
```
ubuntu@ubuntu ~/I/utilities> ./magick convert Memory-Leak-1_output_fpx_1501588084.95 output.fpx
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/715.
convert: UnableToOpenConfigureFile `delegates.xml' @ warning/configure.c/GetConfigureOptions/715.

=================================================================
==4741==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4096 byte(s) in 1 object(s) allocated from:
    #0 0x4c0cbc in malloc /home/snd-local/releases/4.0.1/release/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:66:3
    #1 0x7fd4757e2cbb in format8BIM /home/ubuntu/ImageMagick/coders/meta.c:2149:33
    #2 0x7fd4757e2cbb in WriteMETAImage /home/ubuntu/ImageMagick/coders/meta.c:2300
    #3 0x7fd475254a9e in WriteImage /home/ubuntu/ImageMagick/MagickCore/constitute.c:1183:22
    #4 0x7fd4752555bd in WriteImages /home/ubuntu/ImageMagick/MagickCore/constitute.c:1333:13
    #5 0x7fd474a99f01 in ConvertImageCommand /home/ubuntu/ImageMagick/MagickWand/convert.c:3280:11
    #6 0x7fd474b59e0f in MagickCommandGenesis /home/ubuntu/ImageMagick/MagickWand/mogrify.c:183:14
    #7 0x4edef7 in MagickMain /home/ubuntu/ImageMagick/utilities/magick.c:149:10
    #8 0x4edef7 in main /home/ubuntu/ImageMagick/utilities/magick.c:180
    #9 0x7fd4735d282f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
```


Comments:
Comment by bastien-roucaries on 2017-08-02 13:38:49+00:00:
open CVE 
http://cveform.mitre.org/

---

Comment by bastien-roucaries on 2017-08-02 13:39:00+00:00:
And post assigned CVE here

---

Comment by mikayla-grace on 2017-08-02 13:52:06+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 EazyLov3 on 2017-08-02 14:02:50+00:00:
OK,I try to request it...

---

Comment by carnil on 2017-08-04 04:38:29+00:00:
This has been assigned [CVE-2017-12418](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-12418)

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 781c31aeb16047d7123502666dea2ecdc97c072e
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;
}