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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/639
## Description:
Issue: ImageMagick/ImageMagick#639
Title: memory leak in ReadWEBPImage
State: closed
Created by: jgj212
Created at: 2017-08-01 14:48:29+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.6-5 Q16 x86_64

```
./magick identify $FILE  

==114==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4294904328 byte(s) in 1 object(s) allocated from:
    #0  in malloc /mnt/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:66
    #1  in AcquireMagickMemory /mnt/im/ImageMagick-clang/MagickCore/memory.c:464:10
    #2  in AcquireQuantumMemory /mnt/im/ImageMagick-clang/MagickCore/memory.c:537:10
    #3  in ReadWEBPImage /mnt/im/ImageMagick-clang/coders/webp.c:268:28
    #4  in ReadImage /mnt/im/ImageMagick-clang/MagickCore/constitute.c:497:13
    #5  in ReadStream /mnt/im/ImageMagick-clang/MagickCore/stream.c:1045:9
    #6  in PingImage /mnt/im/ImageMagick-clang/MagickCore/constitute.c:226:9
    #7  in PingImages /mnt/im/ImageMagick-clang/MagickCore/constitute.c:327:10
    #8  in IdentifyImageCommand /mnt/im/ImageMagick-clang/MagickWand/identify.c:319:18
    #9  in MagickCommandGenesis /mnt/im/ImageMagick-clang/MagickWand/mogrify.c:183:14
    #10  in MagickMain /mnt/im/ImageMagick-clang/utilities/magick.c:149:10
    #11  in main /mnt/im/ImageMagick-clang/utilities/magick.c:180:10
    #12  in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21f44)
```

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

Credit: ADLab of Venustech



Comments:
Comment by bastien-roucaries on 2017-08-01 20:18:46+00:00:
Pease open CVE

---

Comment by dlemstra on 2017-08-01 22:00:18+00:00:
This is not confirmed yet? Better wait with the CVE?

---

Comment by jgj212 on 2017-08-01 23:41:52+00:00:
@dlemstra  is this bug being fixed？i do not  request cve

---

Comment by mikayla-grace on 2017-08-02 00:00:16+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 fgeek on 2017-09-07 08:25:14+00:00:
Please use CVE-2017-14138 for this issue. For the record the ID was not requested by me.

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