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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/636
## Description:
Issue: ImageMagick/ImageMagick#636
Title: Memory leaks in WriteMSLImage
State: closed
Created by: zhouat
Created at: 2017-07-31 12:54:49+00:00
Labels: bug

Issue Body:
poc_link: https://github.com/zhouat/poc_IM/blob/master/Memory-Leak-19_output_msl_1501504023.36

Trigger Command：
```
echo "" >output.msl
magick convert Memory-Leak-19_output_msl_1501504023.36 output.msl
```

```
=================================================================
==108698==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f8068d1fb98 in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cc:62
    #1 0x7f806825b7ad in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f806851a2bf in ProcessMSLScript coders/msl.c:7833
    #3 0x7f806851c382 in WriteMSLImage coders/msl.c:8334
    #4 0x7f80680ccaa1 in WriteImage MagickCore/constitute.c:1183
    #5 0x7f80680cd1d8 in WriteImages MagickCore/constitute.c:1333
    #6 0x7f8067936265 in ConvertImageCommand MagickWand/convert.c:3280
    #7 0x7f8067a2e1f1 in MagickCommandGenesis MagickWand/mogrify.c:183
    #8 0x40199b in MagickMain utilities/magick.c:149
    #9 0x401b68 in main utilities/magick.c:180
    #10 0x7f8064092b14 in __libc_start_main (/lib64/libc.so.6+0x21b14)

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f8068d1fb98 in __interceptor_malloc ../../.././libsanitizer/asan/asan_malloc_linux.cc:62
    #1 0x7f806825b7ad in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f806851a2f2 in ProcessMSLScript coders/msl.c:7839
    #3 0x7f806851c382 in WriteMSLImage coders/msl.c:8334
    #4 0x7f80680ccaa1 in WriteImage MagickCore/constitute.c:1183
    #5 0x7f80680cd1d8 in WriteImages MagickCore/constitute.c:1333
    #6 0x7f8067936265 in ConvertImageCommand MagickWand/convert.c:3280
    #7 0x7f8067a2e1f1 in MagickCommandGenesis MagickWand/mogrify.c:183
    #8 0x40199b in MagickMain utilities/magick.c:149
    #9 0x401b68 in main utilities/magick.c:180
    #10 0x7f8064092b14 in __libc_start_main (/lib64/libc.so.6+0x21b14)
```


Comments:
Comment by mikayla-grace on 2017-07-31 13:57:56+00:00:
Unfortunately we cannot reproduce the problem with ImageMagick 6.0.9-5 beta, gcc 7.1.1, and ASan.  We get:

```
magick convert Memory-Leak-19_output_msl_1501504023.36 output.msl
convert: unexpected end-of-file `Memory-Leak-19_output_msl_1501504023.36': No such file or directory @ error/pwp.c/ReadPWPImage/259.
convert: negative or zero image size `output.msl' @ error/image.c/CloneImage/825.
convert: unable to open image 'output.msl': No such file or directory @ error/blob.c/OpenBlob/3109.
convert: unable to open file 'output.msl': No such file or directory @ error/msl.c/ProcessMSLScript/7822.

```

---

Comment by zhouat on 2017-08-01 01:38:36+00:00:
@mikayla-grace 

1. need a empty file: output.msl
2. magick --version
```
Version: ImageMagick 7.0.6-5 Q16 x86_64 2017-07-31 http://www.imagemagick.org
```

---

Comment by mikayla-grace on 2017-08-01 11:05:39+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-08-01 20:19:18+00:00:
Please open CVE

---

Comment by bastien-roucaries on 2017-08-02 13:41:26+00:00:
And post CVE number when assigned (fill here http://cveform.mitre.org/)

---

Comment by zhouat on 2017-08-04 12:45:21+00:00:
@bastien-roucaries  done，cve-2017-12427

---

Comment by zhouat on 2017-08-04 12:54:24+00:00:
@dlemstra @mikayla-grace @bastien-roucaries  hi all, does ImageMagick think about publish a bulletin ever when release a new version， which contain cve and “credit-to” info？thks.

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 1f5e947893fe0ed3910b770768993e5cacd5539e
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;
}