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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/563
## Description:
Issue: ImageMagick/ImageMagick#563
Title: Memory Leak in WriteMPCImage()
State: closed
Created by: lcatro
Created at: 2017-07-16 15:36:17+00:00
Labels: bug

Issue Body:

Memory Leak Sample Link : https://raw.githubusercontent.com/lcatro/My_PoC/master/ImageMagick/imagemagick_output_mpc_memory_leak_WriteMPCImage

Trigger Command :  ./magick convert imagemagick_output_mpc_memory_leak_WriteMPCImage output.mpc

Leak Detail : 
```
fuzzing@ubuntu:~/fuzzing/ImageMagick/utilities$ ./magick convert imagemagick_output_mpc_memory_leak_WriteMPCImage output.mpc
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/715.

=================================================================
==2307==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 9096 byte(s) in 1 object(s) allocated from:
    #0 0x7f8563655602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f8562b5e981 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f8562b5e9d5 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f856294802f in AcquirePixelCache MagickCore/cache.c:195
    #4 0x7f8562948fc9 in ClonePixelCache MagickCore/cache.c:418
    #5 0x7f8562957a19 in PersistPixelCache MagickCore/cache.c:3829
    #6 0x7f8562dfc059 in WriteMPCImage coders/mpc.c:1493
    #7 0x7f85629ba1c6 in WriteImage MagickCore/constitute.c:1114
    #8 0x7f85629bae42 in WriteImages MagickCore/constitute.c:1333
    #9 0x7f856220d3eb in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7f8562305d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8561a7a82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 528 byte(s) in 1 object(s) allocated from:
    #0 0x7f8563655602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f8562b5e981 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f8562b5e9d5 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f8562948871 in AcquirePixelCacheNexus MagickCore/cache.c:268
    #4 0x7f8562948428 in AcquirePixelCache MagickCore/cache.c:211
    #5 0x7f8562948fc9 in ClonePixelCache MagickCore/cache.c:418
    #6 0x7f8562957a19 in PersistPixelCache MagickCore/cache.c:3829
    #7 0x7f8562dfc059 in WriteMPCImage coders/mpc.c:1493
    #8 0x7f85629ba1c6 in WriteImage MagickCore/constitute.c:1114
    #9 0x7f85629bae42 in WriteImages MagickCore/constitute.c:1333
    #10 0x7f856220d3eb in ConvertImageCommand MagickWand/convert.c:3280
    #11 0x7f8562305d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x4017f1 in MagickMain utilities/magick.c:149
    #13 0x4019d2 in main utilities/magick.c:180
    #14 0x7f8561a7a82f 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 0x7f8563656076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f8562b5e8de in AcquireAlignedMemory MagickCore/memory.c:262
    #2 0x7f8562948780 in AcquirePixelCacheNexus MagickCore/cache.c:264
    #3 0x7f8562948428 in AcquirePixelCache MagickCore/cache.c:211
    #4 0x7f8562948fc9 in ClonePixelCache MagickCore/cache.c:418
    #5 0x7f8562957a19 in PersistPixelCache MagickCore/cache.c:3829
    #6 0x7f8562dfc059 in WriteMPCImage coders/mpc.c:1493
    #7 0x7f85629ba1c6 in WriteImage MagickCore/constitute.c:1114
    #8 0x7f85629bae42 in WriteImages MagickCore/constitute.c:1333
    #9 0x7f856220d3eb in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7f8562305d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8561a7a82f 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 0x7f8563656076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f8562c420fc in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f8562c421b8 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f8562948631 in AcquirePixelCache MagickCore/cache.c:226
    #4 0x7f8562948fc9 in ClonePixelCache MagickCore/cache.c:418
    #5 0x7f8562957a19 in PersistPixelCache MagickCore/cache.c:3829
    #6 0x7f8562dfc059 in WriteMPCImage coders/mpc.c:1493
    #7 0x7f85629ba1c6 in WriteImage MagickCore/constitute.c:1114
    #8 0x7f85629bae42 in WriteImages MagickCore/constitute.c:1333
    #9 0x7f856220d3eb in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7f8562305d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8561a7a82f 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 0x7f8563656076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f8562c420fc in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f8562c421b8 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f85629486a1 in AcquirePixelCache MagickCore/cache.c:228
    #4 0x7f8562948fc9 in ClonePixelCache MagickCore/cache.c:418
    #5 0x7f8562957a19 in PersistPixelCache MagickCore/cache.c:3829
    #6 0x7f8562dfc059 in WriteMPCImage coders/mpc.c:1493
    #7 0x7f85629ba1c6 in WriteImage MagickCore/constitute.c:1114
    #8 0x7f85629bae42 in WriteImages MagickCore/constitute.c:1333
    #9 0x7f856220d3eb in ConvertImageCommand MagickWand/convert.c:3280
    #10 0x7f8562305d98 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f8561a7a82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 9816 byte(s) leaked in 5 allocation(s).
```



Comments:
Comment by mikayla-grace on 2017-07-16 17:48:10+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 mikayla-grace on 2017-07-23 18:21:21+00:00:
Our patch was incomplete, revert patch until we come up with a better solution.

---

Comment by mikayla-grace on 2017-07-23 22:55:09+00:00:
Fixed with https://github.com/ImageMagick/ImageMagick/commit/184a047627aa564b7062d2462aef668155caa390 and https://github.com/ImageMagick/ImageMagick/commit/46bcb8a86801d3d9c3f89ec1c01fa8a98da05645.

---

Comment by bastien-roucaries on 2017-07-26 21:03:52+00:00:
This is https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=869726 and CVE-2017-11532

Commit References:
46bcb8a86801d3d9c3f89ec1c01fa8a98da05645
184a047627aa564b7062d2462aef668155caa390

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