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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/931
## Description:
Issue: ImageMagick/ImageMagick#931
Title: memory leaks in ReadDCMImage
State: closed
Created by: henices
Created at: 2018-01-04 07:55:45+00:00
Labels: bug

Issue Body:
## INFO
Version: ImageMagick 7.0.7-20 Q16 x86_64 2018-01-04 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI 
Delegates (built-in): bzlib djvu fftw fontconfig freetype gvc jbig jng jpeg lcms lqr lzma pangocairo png tiff webp wmf x xml zlib


Trigger Command: **/usr/local/bin/magick ReadDCMImage-memory-leaks /dev/null**


## ASAN OUTPUT
```
magick: insufficient image data in file `ReadDCMImage-memory-leaks' @ error/dcm.c/ReadDCMImage/3268.

=================================================================
==12759==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 252 byte(s) in 1 object(s) allocated from:
    #0 0x4cf7f0 in __interceptor_malloc (/usr/local/bin/magick+0x4cf7f0)
    #1 0x7fd403bd9846 in AcquireMagickMemory /home/henices/tests/ImageMagick/MagickCore/memory.c:464:10
    #2 0x7fd403bd98a8 in AcquireQuantumMemory /home/henices/tests/ImageMagick/MagickCore/memory.c:537:10
    #3 0x7fd404344a99 in ReadDCMImage /home/henices/tests/ImageMagick/coders/dcm.c:3569:28
    #4 0x7fd4036c357a in ReadImage /home/henices/tests/ImageMagick/MagickCore/constitute.c:497:13
    #5 0x7fd4036cb3a7 in ReadImages /home/henices/tests/ImageMagick/MagickCore/constitute.c:866:9
    #6 0x7fd4022dd127 in CLINoImageOperator /home/henices/tests/ImageMagick/MagickWand/operation.c:4763:22
    #7 0x7fd4022e6f60 in CLIOption /home/henices/tests/ImageMagick/MagickWand/operation.c:5258:7
    #8 0x7fd401e344cb in ProcessCommandOptions /home/henices/tests/ImageMagick/MagickWand/magick-cli.c:424:13
    #9 0x7fd401e3779d in MagickImageCommand /home/henices/tests/ImageMagick/MagickWand/magick-cli.c:796:5
    #10 0x7fd401ee99a5 in MagickCommandGenesis /home/henices/tests/ImageMagick/MagickWand/mogrify.c:183:14
    #11 0x50a313 in MagickMain /home/henices/tests/ImageMagick/utilities/magick.c:149:10
    #12 0x5096b1 in main /home/henices/tests/ImageMagick/utilities/magick.c:180:10
    #13 0x7fd3fb27f009 in __libc_start_main (/lib64/libc.so.6+0x21009)

SUMMARY: AddressSanitizer: 252 byte(s) leaked in 1 allocation(s).
```

testcase:  https://github.com/henices/pocs/raw/master/ReadDCMImage-memory-leaks

Credit: NSFocus Security Team <security (at) nsfocus (dot) com>


Comments:
Comment by urban-warrior on 2018-01-04 11:49:32+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 2018-06-04 00:50:45+00:00:
This was assigned CVE-2018-11656.

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