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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/765
## Description:
Issue: ImageMagick/ImageMagick#765
Title: Heap buffer over read in ReadTIFFImage(coders/tiff.c:2011)
State: closed
Created by: Twi1ight
Created at: 2017-09-19 09:55:55+00:00
Labels: bug

Issue Body:
version:
```
$ ./magick --version
Version: ImageMagick 7.0.7-4 Q16 x86_64 2017-09-19 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 djvu fftw fontconfig fpx freetype gvc jbig jng jpeg lcms lqr lzma openexr pangocairo png tiff webp wmf x xml zlib
```
gcc (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5

crash case: 
[crash-ImageMagic-ReadTIFFImage-heap-overflow.zip](https://github.com/ImageMagick/ImageMagick/files/1313758/crash-ImageMagic-ReadTIFFImage-heap-overflow.zip)

trigger command :
`./magick convert crash-ImageMagic-ReadTIFFImage-heap-overflow /dev/null`

detail:
```
Heap buffer over read in ReadTIFFImage(coders/tiff.c:2011)
==185299== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60420001f380 at pc 0x7d1605 bp 0x7fffffff2600 sp 0x7fffffff25f8
READ of size 4 at 0x60420001f380 thread T0
    #0 0x7d1604 in ReadTIFFImage /ImageMagick/coders/tiff.c:2011
    #1 0x893773 in ReadImage /ImageMagick/MagickCore/constitute.c:497
    #2 0x895d59 in ReadImages /ImageMagick/MagickCore/constitute.c:866
    #3 0xc2163d in ConvertImageCommand /ImageMagick/MagickWand/convert.c:641
    #4 0xdb7660 in MagickCommandGenesis /ImageMagick/MagickWand/mogrify.c:183
    #5 0x41065c in MagickMain /ImageMagick/utilities/magick.c:149
    #6 0x4107f1 in main /ImageMagick/utilities/magick.c:180
    #7 0x7ffff0244f44 in __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:287
    #8 0x4101a8 in _start (/ImageMagick/utilities/magick+0x4101a8)
0x60420001f380 is located 0 bytes to the right of 1024-byte region [0x60420001ef80,0x60420001f380)
allocated by thread T0 here:
    #0 0x7ffff4e6041a in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.0+0x1541a)
    #1 0x441d57 in AcquireMagickMemory /ImageMagick/MagickCore/memory.c:464
    #2 0x7cffb8 in ReadTIFFImage /ImageMagick/coders/tiff.c:1755
    #3 0x893773 in ReadImage /ImageMagick/MagickCore/constitute.c:497
    #4 0x895d59 in ReadImages /ImageMagick/MagickCore/constitute.c:866
    #5 0xc2163d in ConvertImageCommand /ImageMagick/MagickWand/convert.c:641
    #6 0xdb7660 in MagickCommandGenesis /ImageMagick/MagickWand/mogrify.c:183
    #7 0x41065c in MagickMain /ImageMagick/utilities/magick.c:149
    #8 0x4107f1 in main /ImageMagick/utilities/magick.c:180
    #9 0x7ffff0244f44 in __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:287
SUMMARY: AddressSanitizer: heap-buffer-overflow /ImageMagick/coders/tiff.c:2011 ReadTIFFImage
Shadow bytes around the buggy address:
  0x0c08bfffbe20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c08bfffbe30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c08bfffbe40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c08bfffbe50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c08bfffbe60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c08bfffbe70:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c08bfffbe80: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c08bfffbe90: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c08bfffbea0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c08bfffbeb0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c08bfffbec0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
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
==185299== ABORTING
```



Comments:
Comment by mikayla-grace on 2017-09-19 10:22:52+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-21 01:06:46+00:00:
This was assigned CVE-2017-14607.

Attachments:
https://github.com/ImageMagick/ImageMagick/files/1313758/crash-ImageMagic-ReadTIFFImage-heap-overflow.zip

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