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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/632
## Description:
Issue: ImageMagick/ImageMagick#632
Title: Null pointer Dereference in DestroyJNG()
State: closed
Created by: k0keoyo
Created at: 2017-07-30 07:34:09+00:00
Labels: bug

Issue Body:
PoC URL:https://github.com/k0keoyo/im_poc/blob/master/SEGV-0x000000000000_output_aai_1501399328.45
Version: ImageMagick 7.0.6-4 Q16 x86_64 2017-07-29
Trigger Command: ./magick convert SEGV-0x000000000000_output_aai_1501399328.45 output.aai
Crash Detail:
ASAN:SIGSEGV
=================================================================
==5479==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f1fd0f1cde7 bp 0x7ffdf83ad5c0 sp 0x7ffdf83ad480 T0)
    #0 0x7f1fd0f1cde6 in DestroyJNG coders/png.c:4318
    #1 0x7f1fd0f1f076 in ReadOneJNGImage coders/png.c:4473
    #2 0x7f1fd0f20d79 in ReadJNGImage coders/png.c:5145
    #3 0x7f1fd0a279cc in ReadImage MagickCore/constitute.c:497
    #4 0x7f1fd0a293ed in ReadImages MagickCore/constitute.c:866
    #5 0x7f1fd03739de in ConvertImageCommand MagickWand/convert.c:641
    #6 0x7f1fd0439bae in MagickCommandGenesis MagickWand/mogrify.c:183
    #7 0x4016e9 in MagickMain utilities/magick.c:149
    #8 0x7f1fcfcdba3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20a3f)
    #9 0x401358 in _start (/home/sh1/Desktop/ImageMagick/utilities/.libs/lt-magick+0x401358)

AddressSanitizer can not provide additional info.



Comments:
Comment by glennrp on 2017-07-30 08:09:25+00:00:
Confirmed that both IM6.9.9-4 and IM7.0.6-4 dump core while reading this file.

---

Comment by glennrp on 2017-07-30 08:47:52+00:00:
IM7: commit 1828667  
IM6: commit 253d560

---

Comment by glennrp on 2017-07-30 14:43:58+00:00:
I introduced this bug recently in
IM7: commit 8cc53f1
IM6: commit 3cba1bb

---

Comment by bastien-roucaries on 2017-08-01 20:19:45+00:00:
Please open CVE

---

Comment by glennrp on 2017-08-01 21:04:29+00:00:
A CVE will have to say bug was introduced in 6.9.9.4 and 7.0.6-4, fixed in 6.9.9-5 and 7.0.6-5.

---

Comment by bastien-roucaries on 2017-08-02 13:11:24+00:00:
CVE-2017-11750

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