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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/574
## Description:
Issue: ImageMagick/ImageMagick#574
Title: memory leak in WritePALMImage
State: closed
Created by: jgj212
Created at: 2017-07-17 01:25:28+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.6-2 Q16 x86_64

```
./magick convert $FILE  out.palm

==12351==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 524288 byte(s) in 1 object(s) allocated from:
    #0 0x4deeb6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f45d5561186 in AcquireMagickMemory memory.c:464:10
    #2 0x7f45d55611e8 in AcquireQuantumMemory memory.c:537:10
    #3 0x7f45d5613951 in SetGrayscaleImage quantize.c:3322:30
    #4 0x7f45d560ffba in QuantizeImage quantize.c:2669:16
    #5 0x7f45d5299edf in SetImageType attribute.c:1293:18
    #6 0x7f45d59e181a in WritePALMImage palm.c:770:16
    #7 0x7f45d53406fe in WriteImage constitute.c:1114:14
    #8 0x7f45d53415fd in WriteImages constitute.c:1333:13
    #9 0x7f45d49ec900 in ConvertImageCommand convert.c:3280:11
    #10 0x7f45d4b3b0cf in MagickCommandGenesis mogrify.c:183:14
    #11 0x514a37 in MagickMain magick.c:149:10
    #12 0x514491 in main magick.c:180:10
    #13 0x7f45cf378f44 in __libc_start_main libc-start.c:287

Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x4deeb6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f45d5561186 in AcquireMagickMemory memory.c:464:10
    #2 0x7f45d560ef4d in AcquireQuantizeInfo quantize.c:377:34
    #3 0x7f45d59e14f4 in WritePALMImage palm.c:747:17
    #4 0x7f45d53406fe in WriteImage constitute.c:1114:14
    #5 0x7f45d53415fd in WriteImages constitute.c:1333:13
    #6 0x7f45d49ec900 in ConvertImageCommand convert.c:3280:11
    #7 0x7f45d4b3b0cf in MagickCommandGenesis mogrify.c:183:14
    #8 0x514a37 in MagickMain magick.c:149:10
    #9 0x514491 in main magick.c:180:10
    #10 0x7f45cf378f44 in __libc_start_main libc-start.c:287
```

POC: https://github.com/jgj212/poc/blob/master/leak-WritePALM

Credit : ADLab of Venustech


Comments:
Comment by mikayla-grace on 2017-07-17 17:48:38+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-25 21:37:13+00:00:
These are bug https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=869721 and  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=869722.

---

Comment by nohmask on 2017-09-08 07:51:28+00:00:
This was assigned CVE-2017-12664.

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