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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/872
## Description:
Issue: ImageMagick/ImageMagick#872
Title: heap-buffer-overflow in Magick_png_read_raw_profile
State: closed
Created by: henices
Created at: 2017-11-21 05:39:48+00:00
Labels: bug

Issue Body:
$ convert -version
Version: ImageMagick 7.0.7-12 Q16 x86_64 2017-11-21 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): bzlib fontconfig freetype jng jpeg pangocairo png x xml zlib

commit: 6645a122ee83105008e8447f92555e4a9e307ecf  compile at ubuntu 14.04 x86_64

Trigger Command:  **convert Magick_png_read_raw_profile-heap-overflow /dev/null**

```
==25042== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60060001dd58 at pc 0x7f27e3c6f8a7 bp 0x7fff78c5f4c0 sp 0x7fff78c5f4b8
READ of size 1 at 0x60060001dd58 thread T0
    #0 0x7f27e3c6f8a6 in Magick_png_read_raw_profile /home/henices/ImageMagick/coders/png.c:1804
    #1 0x7f27e3c7982f in ReadOnePNGImage /home/henices/ImageMagick/coders/png.c:3855
    #2 0x7f27e3c7ac76 in ReadPNGImage /home/henices/ImageMagick/coders/png.c:4236
    #3 0x7f27e361f721 in ReadImage /home/henices/ImageMagick/MagickCore/constitute.c:497
    #4 0x7f27e3621d87 in ReadImages /home/henices/ImageMagick/MagickCore/constitute.c:866
    #5 0x7f27e2e3b79b in ConvertImageCommand /home/henices/ImageMagick/MagickWand/convert.c:641
    #6 0x7f27e2fdea10 in MagickCommandGenesis /home/henices/ImageMagick/MagickWand/mogrify.c:183
    #7 0x40164c in MagickMain /home/henices/ImageMagick/utilities/magick.c:149
    #8 0x4017e1 in main /home/henices/ImageMagick/utilities/magick.c:180
    #9 0x7f27e2761f44 in __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:287
    #10 0x401198 in _start (/usr/local/bin/magick+0x401198)
0x60060001dd58 is located 0 bytes to the right of 24-byte region [0x60060001dd40,0x60060001dd58)
allocated by thread T0 here:
    #0 0x7f27e42ba41a in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.0+0x1541a)
    #1 0x7f27e3800279 in AcquireMagickMemory /home/henices/ImageMagick/MagickCore/memory.c:464
    #2 0x7f27e3c6f389 in Magick_png_malloc /home/henices/ImageMagick/coders/png.c:1756
    #3 0x7f27e1ec5fff in png_malloc (/lib/x86_64-linux-gnu/libpng12.so.0+0x1afff)
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/henices/ImageMagick/coders/png.c:1804 Magick_png_read_raw_profile
Shadow bytes around the buggy address:
  0x0c013fffbb50: 00 00 fa fa 00 00 00 00 fa fa 00 00 00 00 fa fa
  0x0c013fffbb60: 00 00 00 02 fa fa 00 00 03 fa fa fa 00 00 00 00
  0x0c013fffbb70: fa fa 00 00 04 fa fa fa 00 00 00 00 fa fa 00 00
  0x0c013fffbb80: 03 fa fa fa 00 00 00 00 fa fa 00 00 06 fa fa fa
  0x0c013fffbb90: 00 00 00 00 fa fa 00 00 00 fa fa fa 00 00 00 00
=>0x0c013fffbba0: fa fa 00 00 00 01 fa fa 00 00 00[fa]fa fa fd fd
  0x0c013fffbbb0: fd fd fa fa fd fd fd fa fa fa 00 00 00 00 fa fa
  0x0c013fffbbc0: 00 00 06 fa fa fa 00 00 00 00 fa fa 00 00 06 fa
  0x0c013fffbbd0: fa fa 00 00 00 00 fa fa 00 00 05 fa fa fa 00 00
  0x0c013fffbbe0: 00 00 fa fa 00 00 04 fa fa fa 00 00 00 00 fa fa
  0x0c013fffbbf0: 00 00 00 07 fa fa 00 00 00 03 fa fa 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:     fa
  Heap righ redzone:     fb
  Freed Heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==25042== ABORTING
```

testcase:
https://github.com/henices/pocs/raw/master/Magick_png_read_raw_profile-heap-overflow

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


Comments:
Comment by urban-warrior on 2017-11-21 12:45:50+00:00:
Unfortunately we cannot reproduce the problem.  With the latest ImageMagick release from the master branch, we get:

```
$ convert Magick_png_read_raw_profile-heap-overflow /dev/null
convert: gAMA: gamma value out of range `Magick_png_read_raw_profile-heap-overflow' @ warning/png.c/MagickPNGWarningHandler/1744.
convert: zTXt: invalid distance too far back `Magick_png_read_raw_profile-heap-overflow' @ warning/png.c/MagickPNGWarningHandler/1744.
```


---

Comment by henices on 2017-11-21 13:58:54+00:00:
I got the same output on fedora 26, but on ubuntu it makes crash.  on ubuntu 14.04  convert command use libpng12,  on fedora convert command use libpng16,   what's your linux distribution?

---

Comment by henices on 2017-11-22 04:23:44+00:00:
I find the root cause,  when i use the libpng16 in ubuntu this crash is gone away. 
use libpng12 in ubuntu,  we will get a asan heap-buffer-overflow output.

---

Comment by urban-warrior on 2017-11-22 15:05:23+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 @ https://www.imagemagick.org/download/beta/ by sometime tomorrow. 


---

Comment by nohmask on 2017-12-11 04:08:55+00:00:
This was assigned CVE-2017-17504.

Commit References:
6645a122ee83105008e8447f92555e4a9e307ecf

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