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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/566
## Description:
Issue: ImageMagick/ImageMagick#566
Title: Memory-Leak in WriteHISTOGRAMImage() coders/histogram.c
State: closed
Created by: lcatro
Created at: 2017-07-16 18:36:27+00:00
Labels: bug

Issue Body:

Memory Leak File Link : https://raw.githubusercontent.com/lcatro/My_PoC/master/ImageMagick/memory-leak_output_histogram_WriteHISTOGRAMImage

Trigger Command : ./magick convert memory-leak_output_histogram_WriteHISTOGRAMImage output.histogram

Leak Detail : 
```
fuzzing@ubuntu:~/fuzzing/ImageMagick/utilities$ ./magick convert graphicsmagick_fuzzing/Memory-Leak-6_output_histogram_1500226138.33 output.histogram 
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/715.
convert: UnableToOpenConfigureFile `type.xml' @ warning/configure.c/GetConfigureOptions/715.
convert: UnableToReadFont `graphicsmagick_fuzzing/Memory-Leak-6_output_histogram_1500226138.33' @ error/annotate.c/RenderFreetype/1389.
convert: NonconformingDrawingPrimitiveDefinition `text' @ error/draw.c/DrawImage/3269.
convert: UnableToOpenConfigureFile `delegates.xml' @ warning/configure.c/GetConfigureOptions/715.
convert: UnableToOpenConfigureFile `colors.xml' @ warning/configure.c/GetConfigureOptions/715.

=================================================================
==26711==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 280 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e4602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f8b8eeed981 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f8b8ecbe82b in CloneBlobInfo MagickCore/blob.c:504
    #3 0x7f8b8eeb221f in CloneImage MagickCore/image.c:874
    #4 0x7f8b8f13cd0c in WriteHISTOGRAMImage coders/histogram.c:233
    #5 0x7f8b8ed496f7 in WriteImage MagickCore/constitute.c:1183
    #6 0x7f8b8ed49e42 in WriteImages MagickCore/constitute.c:1333
    #7 0x7f8b8e59c3eb in ConvertImageCommand MagickWand/convert.c:3280
    #8 0x7f8b8e694d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #9 0x4017f1 in MagickMain utilities/magick.c:149
    #10 0x4019d2 in main utilities/magick.c:180
    #11 0x7f8b8de0982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Direct leak of 280 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e4602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f8b8eeed981 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f8b8ecbe82b in CloneBlobInfo MagickCore/blob.c:504
    #3 0x7f8b8eeae1e1 in AcquireImage MagickCore/image.c:209
    #4 0x7f8b8f28764a in ReadTTFImage coders/ttf.c:214
    #5 0x7f8b8ed46068 in ReadImage MagickCore/constitute.c:497
    #6 0x7f8b8ed48267 in ReadImages MagickCore/constitute.c:866
    #7 0x7f8b8e5060ad in ConvertImageCommand MagickWand/convert.c:641
    #8 0x7f8b8e694d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #9 0x4017f1 in MagickMain utilities/magick.c:149
    #10 0x4019d2 in main utilities/magick.c:180
    #11 0x7f8b8de0982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Direct leak of 256 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e4602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f8b8c9490b9  (/usr/lib/x86_64-linux-gnu/libfontconfig.so.1+0x1d0b9)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e5076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f8b8efd10fc in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f8b8efd11b8 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f8b8ecc3756 in GetBlobInfo MagickCore/blob.c:1414
    #4 0x7f8b8ecbe917 in CloneBlobInfo MagickCore/blob.c:507
    #5 0x7f8b8eeb221f in CloneImage MagickCore/image.c:874
    #6 0x7f8b8f13cd0c in WriteHISTOGRAMImage coders/histogram.c:233
    #7 0x7f8b8ed496f7 in WriteImage MagickCore/constitute.c:1183
    #8 0x7f8b8ed49e42 in WriteImages MagickCore/constitute.c:1333
    #9 0x7f8b8e59c3eb in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7f8b8e694d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8b8de0982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e5076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f8b8efd10fc in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f8b8efd11b8 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f8b8ecc3756 in GetBlobInfo MagickCore/blob.c:1414
    #4 0x7f8b8ecbe917 in CloneBlobInfo MagickCore/blob.c:507
    #5 0x7f8b8eeae1e1 in AcquireImage MagickCore/image.c:209
    #6 0x7f8b8f28764a in ReadTTFImage coders/ttf.c:214
    #7 0x7f8b8ed46068 in ReadImage MagickCore/constitute.c:497
    #8 0x7f8b8ed48267 in ReadImages MagickCore/constitute.c:866
    #9 0x7f8b8e5060ad in ConvertImageCommand MagickWand/convert.c:641
    #10 0x7f8b8e694d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8b8de0982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7f8b8f9e479a in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x9879a)
    #1 0x7f8b8c9497c8  (/usr/lib/x86_64-linux-gnu/libfontconfig.so.1+0x1d7c8)

SUMMARY: AddressSanitizer: 976 byte(s) leaked in 6 allocation(s).

```



Comments:
Comment by mikayla-grace on 2017-07-16 23:59: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 carnil on 2017-07-23 06:10:38+00:00:
This has been assigned CVE-2017-11531

---

Comment by bastien-roucaries on 2017-07-25 21:54:39+00:00:
This is https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=869725

---

Comment by lcatro on 2017-07-26 03:15:27+00:00:
@bastien-roucaries Hello bastien-roucaries ,is that me or you apply CVE ? 

---

Comment by carnil on 2017-07-26 05:59:08+00:00:
@lcatro 

On Wed, Jul 26, 2017 at 03:15:31AM +0000, LCatro wrote:
> @bastien-roucaries Hello bastien-roucaries ,is that me or you apply CVE ? 

See:
https://github.com/ImageMagick/ImageMagick/issues/566#issuecomment-317231828

CVE-2017-11531 was already assigned for this issue.


---

Comment by lcatro on 2017-07-26 07:14:19+00:00:
@carnil I know ,because i had submit CVE apply after your assigned at last week .Thanks .

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 9871b4ad7400606bbecf7f979b198080f4b107c3
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;
}