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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/746
## Description:
Issue: ImageMagick/ImageMagick#746
Title: Null Pointer Dereference in GetVirtualPixels MagickCore/cache.c:3185
State: closed
Created by: jerryl3e
Created at: 2017-09-11 09:38:24+00:00
Labels: bug

Issue Body:
poc1
version: 
ImageMagick 7.0.7-1 Q16 x86_64
gcc 7.1

crash link : 
https://raw.githubusercontent.com/jerryl3e/poc/master/im_poc_1505120100

trigger command : 
./magick convert im_poc_1505120100 output.mpc
./magick convert output.mpc output.uil

detail :
<pre>
<code>
root@work:/home/work/fuzzing/ImageMagick/utilities# ./magick  im_poc_1505120100  out.mpc
root@work:/home/work/fuzzing/ImageMagick/utilities# ./magick out.mpc out.aai
ASAN:SIGSEGV
=================================================================
==49786==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f9150edb72b bp 0x7ffd830679e0 sp 0x7ffd83067980 T0)
    #0 0x7f9150edb72a in GetVirtualPixels MagickCore/cache.c:3185
    #1 0x7f9151203e88 in WriteAAIImage coders/aai.c:386
    #2 0x7f9150f44782 in WriteImage MagickCore/constitute.c:1114
    #3 0x7f9150f453fe in WriteImages MagickCore/constitute.c:1333
    #4 0x7f9150a4097a in CLINoImageOperator MagickWand/operation.c:4795
    #5 0x7f9150a431d8 in CLIOption MagickWand/operation.c:5255
    #6 0x7f91508e0d5f in ProcessCommandOptions MagickWand/magick-cli.c:529
    #7 0x7f91508e1f27 in MagickImageCommand MagickWand/magick-cli.c:794
    #8 0x7f915091a246 in MagickCommandGenesis MagickWand/mogrify.c:183
    #9 0x4017e1 in MagickMain utilities/magick.c:149
    #10 0x4019c2 in main utilities/magick.c:180
    #11 0x7f915014682f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #12 0x4012f8 in _start (/home/work/fuzzing/ImageMagick/utilities/.libs/lt-magick+0x4012f8)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV MagickCore/cache.c:3185 GetVirtualPixels
==49786==ABORTING</code>
</pre>

Credit:Baidu Security Lab



Comments:
Comment by mikayla-grace on 2017-09-11 16:45:02+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-13 04:14:30+00:00:
This was assigned CVE-2017-14400.

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