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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/624
## Description:
Issue: ImageMagick/ImageMagick#624
Title: Memory-Leak in libMagickCore-6.Q16.so.5
State: closed
Created by: k0keoyo
Created at: 2017-07-27 15:07:41+00:00
Labels: bug

Issue Body:
Memory Leak File Link : https://github.com/k0keoyo/im_poc/blob/master/Memory-Leak-27_output_braille_1501166004.7

Trigger Command : ./convert Memory-Leak-27_output_braille_1501166004.7 output.png

Leak Detail :
root@ubuntu:/home/sh1/ImageMagick/ImageMagick/utilities# ./convert Memory-Leak-27_output_braille_1501166004.7  output.png

=================================================================
==5550==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 50376 byte(s) in 3 object(s) allocated from:
    #0 0x4bc322  (/home/sh1/ImageMagick/ImageMagick/utilities/.libs/lt-convert+0x4bc322)
    #1 0x7f48c4349720  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x471720)
    #2 0x7f48c4215ed0  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x33ded0)
    #3 0x7f48c42180ac  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x3400ac)
    #4 0x7f48c3a4023d  (/home/sh1/ImageMagick/ImageMagick/wand/.libs/libMagickWand-6.Q16.so.5+0x13123d)
    #5 0x7f48c3b50a13  (/home/sh1/ImageMagick/ImageMagick/wand/.libs/libMagickWand-6.Q16.so.5+0x241a13)

Direct leak of 456 byte(s) in 3 object(s) allocated from:
    #0 0x4bc322  (/home/sh1/ImageMagick/ImageMagick/utilities/.libs/lt-convert+0x4bc322)
    #1 0x7f48c43e485a  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x50c85a)
    #2 0x7f48c4215ed0  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x33ded0)
    #3 0x7f48c42180ac  (/home/sh1/ImageMagick/ImageMagick/magick/.libs/libMagickCore-6.Q16.so.5+0x3400ac)
    #4 0x7f48c3a4023d  (/home/sh1/ImageMagick/ImageMagick/wand/.libs/libMagickWand-6.Q16.so.5+0x13123d)
    #5 0x7f48c3b50a13  (/home/sh1/ImageMagick/ImageMagick/wand/.libs/libMagickWand-6.Q16.so.5+0x241a13)



Comments:
Comment by dlemstra on 2017-07-27 15:17:47+00:00:
Can you specify the version of ImageMagick that you used to produce this issue? And can you reproduce the issue with the latest version using our git repository?

---

Comment by k0keoyo on 2017-07-27 15:23:48+00:00:
I install ImageMagick by command: git clone --depth=50 --branch=ImageMagick-6 https://github.com/ImageMagick/ImageMagick.git ImageMagick/ImageMagick

---

Comment by k0keoyo on 2017-07-27 15:36:00+00:00:
Version: ImageMagick 6.9.9-4 Q16 x86_64

---

Comment by mikayla-grace on 2017-07-27 16:43:27+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-28 21:44:31+00:00:
Cold you open a CVE ? https://cveform.mitre.org/

---

Comment by k0keoyo on 2017-07-29 02:44:22+00:00:
Of course yes

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 fe4eecee8c4942cb836bc63f79ed5eaa39069f69
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;
}