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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/586
## Description:
Issue: ImageMagick/ImageMagick#586
Title: Null-Point reference in WriteOnePNGImage
State: closed
Created by: lcatro
Created at: 2017-07-18 18:31:46+00:00
Labels: bug

Issue Body:

Crash Link : https://raw.githubusercontent.com/lcatro/My_PoC/master/ImageMagick/SEGV_output_mng_WriteOnePNGImage

Trigger Command : ./magick convert SEGV_output_mng_WriteOnePNGImage output.mng

Crash Detail : 
```
fuzzing@ubuntu:~/fuzzing/ImageMagick/utilities$ ./magick convert graphicsmagick_fuzzing/SEGV-0x000000000000_output_mng_1500402017.82 output.mng
ASAN:SIGSEGV
=================================================================
==56607==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f3ea2329122 bp 0x7ffdd59968d0 sp 0x7ffdd597f460 T0)
    #0 0x7f3ea2329121 in WriteOnePNGImage coders/png.c:8494
    #1 0x7f3ea2349eba in WriteMNGImage coders/png.c:14027
    #2 0x7f3ea1d6022b in WriteImage MagickCore/constitute.c:1114
    #3 0x7f3ea1d60ea7 in WriteImages MagickCore/constitute.c:1333
    #4 0x7f3ea15b03bb in ConvertImageCommand MagickWand/convert.c:3280
    #5 0x7f3ea16a8d68 in MagickCommandGenesis MagickWand/mogrify.c:183
    #6 0x4017f1 in MagickMain utilities/magick.c:149
    #7 0x4019d2 in main utilities/magick.c:180
    #8 0x7f3ea0e1d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #9 0x401308 in _start (/home/fuzzing/fuzzing/ImageMagick/utilities/.libs/lt-magick+0x401308)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV coders/png.c:8494 WriteOnePNGImage
==56607==ABORTING
```



Comments:
Comment by mikayla-grace on 2017-07-18 22:15:29+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 bastien-roucaries on 2017-07-19 16:13:57+00:00:
Did you opened a CVE ?

---

Comment by lcatro on 2017-07-19 16:29:49+00:00:
@bastien-roucaries no , is that you found too ?

---

Comment by bastien-roucaries on 2017-07-21 15:32:15+00:00:
magick team could you point the commit (v7 and v6).

---

Comment by bastien-roucaries on 2017-07-21 15:32:40+00:00:
cve should be opened here : https://cveform.mitre.org/

---

Comment by lcatro on 2017-07-21 18:10:31+00:00:
@bastien-roucaries im found these bug in V7.0.6-1 that is the master branch code .
and i had try apply CVE but they have not give me anynews .

---

Comment by attritionorg on 2017-07-22 18:38:34+00:00:
https://github.com/ImageMagick/ImageMagick/commit/816ecab6c532ae086ff4186b3eaf4aa7092d536f Imagine that is it and a typo on the bug number.

---

Comment by carnil on 2017-07-23 05:12:35+00:00:
This issue has been assigned CVE-2017-11522

---

Comment by dlemstra on 2017-07-25 12:32:23+00:00:
@attritionorg The IM7 commit is: 816ecab. There is no IM6 commit because this is an IM7 only issue.

Commit References:
816ecab6c532ae086ff4186b3eaf4aa7092d536f

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