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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/552
## Description:
Issue: ImageMagick/ImageMagick#552
Title: memory leak in ReadMPCImage 
State: closed
Created by: jgj212
Created at: 2017-07-09 01:39:20+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.6-1 Q16 x86_64

```
#./magick identify $FILE

=================================================================
==9771==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 56 byte(s) in 1 object(s) allocated from:
    #0 0x4def96 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7fe5639f3f76 in AcquireMagickMemory memory.c:463:10
    #2 0x7fe5639d1657 in NewLinkedList linked-list.c:717
    #3 0x7fe563df4919 in ReadMPCImage mpc.c:625:32
    #4 0x7fe5637e3f98 in ReadImage constitute.c:497:13
    #5 0x7fe563b5abd9 in ReadStream stream.c:1045:9
    #6 0x7fe5637e2b3f in PingImage constitute.c:226:9
    #7 0x7fe5637e32e3 in PingImages constitute.c:327:10
    #8 0x7fe562f44126 in IdentifyImageCommand identify.c:319:18
    #9 0x7fe563001dff in MagickCommandGenesis mogrify.c:183:14
    #10 0x514f77 in MagickMain magick.c:151:10
    #11 0x5149d1 in main magick.c:263:10
    #12 0x7fe55d83ff44 in __libc_start_main libc-start.c:287

Indirect leak of 4128 byte(s) in 1 object(s) allocated from:
    #0 0x4def96 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7fe5639f3f76 in AcquireMagickMemory memory.c:463:10
    #2 0x7fe5639f3fd8 in AcquireQuantumMemory memory.c:536:10
    #3 0x7fe563b6d527 in AcquireString string.c:136:24
    #4 0x7fe563df4941 in ReadMPCImage mpc.c:627:23
    #5 0x7fe5637e3f98 in ReadImage constitute.c:497:13
    #6 0x7fe563b5abd9 in ReadStream stream.c:1045:9
    #7 0x7fe5637e2b3f in PingImage constitute.c:226:9
    #8 0x7fe5637e32e3 in PingImages constitute.c:327:10
    #9 0x7fe562f44126 in IdentifyImageCommand identify.c:319:18
    #10 0x7fe563001dff in MagickCommandGenesis mogrify.c:183:14
    #11 0x514f77 in MagickMain magick.c:151:10
    #12 0x5149d1 in main magick.c:263:10
    #13 0x7fe55d83ff44 in __libc_start_main libc-start.c:287

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x4dfaf5 in posix_memalign asan_malloc_linux.cc:142
    #1 0x7fe563b30788 in AcquireSemaphoreMemory semaphore.c:154:7
    #2 0x7fe563b2fffc in AcquireSemaphoreInfo semaphore.c:200:36
    #3 0x7fe5639d1763 in NewLinkedList linked-list.c:726
    #4 0x7fe563df4919 in ReadMPCImage mpc.c:625:32
    #5 0x7fe5637e3f98 in ReadImage constitute.c:497:13
    #6 0x7fe563b5abd9 in ReadStream stream.c:1045:9
    #7 0x7fe5637e2b3f in PingImage constitute.c:226:9
    #8 0x7fe5637e32e3 in PingImages constitute.c:327:10
    #9 0x7fe562f44126 in IdentifyImageCommand identify.c:319:18
    #10 0x7fe563001dff in MagickCommandGenesis mogrify.c:183:14
    #11 0x514f77 in MagickMain magick.c:151:10
    #12 0x5149d1 in main magick.c:263:10
    #13 0x7fe55d83ff44 in __libc_start_main libc-start.c:287

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x4def96 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7fe5639f3f76 in AcquireMagickMemory memory.c:463:10
    #2 0x7fe5639d0af5 in AppendValueToLinkedList linked-list.c:120
    #3 0x7fe563df4950 in ReadMPCImage mpc.c:626:28
    #4 0x7fe5637e3f98 in ReadImage constitute.c:497:13
    #5 0x7fe563b5abd9 in ReadStream stream.c:1045:9
    #6 0x7fe5637e2b3f in PingImage constitute.c:226:9
    #7 0x7fe5637e32e3 in PingImages constitute.c:327:10
    #8 0x7fe562f44126 in IdentifyImageCommand identify.c:319:18
    #9 0x7fe563001dff in MagickCommandGenesis mogrify.c:183:14
    #10 0x514f77 in MagickMain magick.c:151:10
    #11 0x5149d1 in main magick.c:263:10
    #12 0x7fe55d83ff44 in __libc_start_main libc-start.c:287

SUMMARY: AddressSanitizer: 4264 byte(s) leaked in 4 allocation(s).
```

testcase: https://github.com/jgj212/poc/blob/master/leak-ReadMPCImage

Credit : ADLab of Venustech


Comments:
Comment by mikayla-grace on 2017-07-09 12:19:05+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 nohmask on 2017-09-08 08:01:19+00:00:
This was assigned CVE-2017-12642.

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