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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/633
## Description:
Issue: ImageMagick/ImageMagick#633
Title: Memory-Leak in AcquireAlignedMemory MagickCore/memory.c:262
State: closed
Created by: zhouat
Created at: 2017-07-30 11:13:07+00:00
Labels: duplicate

Issue Body:
Poc link : 
https://github.com/zhouat/poc_IM/blob/master/Memory-Leak-21_output_picon_1501391824.23

Trigger Command： `magick  convert Memory-Leak-21_output_picon_1501391824.23 out.picon`

```
=================================================================
==32246==ERROR: LeakSanitizer: detected memory leaks

...
...

Indirect leak of 512 byte(s) in 1 object(s) allocated from:
    #0 0x7f89eef2d076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f89ee671ab3 in AcquireAlignedMemory MagickCore/memory.c:262
    #2 0x7f89ee4b5c5b in OpenPixelCache MagickCore/cache.c:3575
    #3 0x7f89ee4ae43a in GetImagePixelCache MagickCore/cache.c:1682
    #4 0x7f89ee4bd4d7 in SyncImagePixelCache MagickCore/cache.c:5269
    #5 0x7f89ee63d86b in SetImageStorageClass MagickCore/image.c:2513
    #6 0x7f89ee4dc3af in AcquireImageColormap MagickCore/colormap.c:144
    #7 0x7f89ee842995 in ReadGIFImage coders/gif.c:1280
    #8 0x7f89ee516ed4 in ReadImage MagickCore/constitute.c:497
    #9 0x7f89ee49118b in BlobToImage MagickCore/blob.c:428
    #10 0x7f89ee97cf40 in WritePICONImage coders/xpm.c:689
    #11 0x7f89ee51a032 in WriteImage MagickCore/constitute.c:1114
    #12 0x7f89ee51acae in WriteImages MagickCore/constitute.c:1333
    #13 0x7f89ede735cf in ConvertImageCommand MagickWand/convert.c:3280
    #14 0x7f89edef370b in MagickCommandGenesis MagickWand/mogrify.c:183
    #15 0x4017e1 in MagickMain utilities/magick.c:149
    #16 0x4019c2 in main utilities/magick.c:180
    #17 0x7f89ed71f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)




```


Comments:
Comment by mikayla-grace on 2017-07-30 21:53:06+00:00:
Unfortunately, we cannot reproduce this problem.  Its possible the fix for #631 also fixed this problem as well.

---

Comment by bastien-roucaries on 2017-08-02 13:32:16+00:00:
CVE-2017-11754

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