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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/717
## Description:
Issue: ImageMagick/ImageMagick#717
Title: Heap-buffer-overflow in function SampleImage() in  resize.c
State: closed
Created by: 0ahu
Created at: 2017-09-01 04:37:53+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.6-8 Q16 x86_64

A heap buffer overflow vulnerability was found in function SampleImage() in resize.c ,which allow attackers to cause a denial of service or remote code execution via a crafted file.

A bug was triggered when convert a file to pdf using ImageMagick , I use the command line
" ./magick convert 1-im2pdf out.pdf " and the asan shows:
```
==75137==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f6b7aafeb00 at pc 0x000000d1d99c bp 0x7ffc336f6110 sp 0x7ffc336f6108
READ of size 4 at 0x7f6b7aafeb00 thread T0
    #0 0xd1d99b in SampleImage /home/share/imagemagic/source-imagemagick/MagickCore/resize.c:3154:46
    #1 0xd24d30 in ThumbnailImage /home/share/imagemagic/source-imagemagick/MagickCore/resize.c:3703:22
    #2 0x7edef1 in WritePDFImage /home/share/imagemagic/source-imagemagick/coders/pdf.c:2269:16
    #3 0xa290ff in WriteImage /home/share/imagemagic/source-imagemagick/MagickCore/constitute.c:1114:14
    #4 0xa2a32d in WriteImages /home/share/imagemagic/source-imagemagick/MagickCore/constitute.c:1333:13
    #5 0xeb5a6a in ConvertImageCommand /home/share/imagemagic/source-imagemagick/MagickWand/convert.c:3280:11
    #6 0xfc75da in MagickCommandGenesis /home/share/imagemagic/source-imagemagick/MagickWand/mogrify.c:183:14
    #7 0x519269 in MagickMain /home/share/imagemagic/source-imagemagick/utilities/magick.c:162:10
    #8 0x519269 in main /home/share/imagemagic/source-imagemagick/utilities/magick.c:197
    #9 0x7f6b7e02782f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #10 0x420f98 in _start (/home/share/imagemagic/test/magick+0x420f98)

0x7f6b7aafeb00 is located 0 bytes to the right of 12000000-byte region [0x7f6b79f8d000,0x7f6b7aafeb00)
allocated by thread T0 here:
    #0 0x4e2c60 in __interceptor_posix_memalign /home/share/libfuzzer/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:156
    #1 0x55b55c in AcquireAlignedMemory /home/share/imagemagic/source-imagemagick/MagickCore/memory.c:262:7
    #2 0x9cc740 in OpenPixelCache /home/share/imagemagic/source-imagemagick/MagickCore/cache.c:3561:46
    #3 0x9d2a03 in GetImagePixelCache /home/share/imagemagic/source-imagemagick/MagickCore/cache.c:1668:18
    #4 0x9d78a4 in SyncImagePixelCache /home/share/imagemagic/source-imagemagick/MagickCore/cache.c:5266:28
    #5 0x922705 in ReadPNGImage /home/share/imagemagic/source-imagemagick/coders/png.c:4254:9
    #6 0xa2518f in ReadImage /home/share/imagemagic/source-imagemagick/MagickCore/constitute.c:497:13
    #7 0xa2816e in ReadImages /home/share/imagemagic/source-imagemagick/MagickCore/constitute.c:866:9
    #8 0xea8112 in ConvertImageCommand /home/share/imagemagic/source-imagemagick/MagickWand/convert.c:641:18
    #9 0xfc75da in MagickCommandGenesis /home/share/imagemagic/source-imagemagick/MagickWand/mogrify.c:183:14
    #10 0x519269 in MagickMain /home/share/imagemagic/source-imagemagick/utilities/magick.c:162:10
    #11 0x519269 in main /home/share/imagemagic/source-imagemagick/utilities/magick.c:197
    #12 0x7f6b7e02782f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/share/imagemagic/source-imagemagick/MagickCore/resize.c:3154:46 in SampleImage
Shadow bytes around the buggy address:
  0x0fedef557d10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fedef557d20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fedef557d30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fedef557d40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fedef557d50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0fedef557d60:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fedef557d70: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fedef557d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fedef557d90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fedef557da0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fedef557db0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
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
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==75137==ABORTING
```

The poc was at: https://github.com/lifuhao123/feijidepoc/blob/master/1-im2pdf

Note that this issue was found by lifuhao from Aliyun Security Team.
Thanks


Comments:
Comment by urban-warrior on 2017-09-01 12:39:08+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-09-12 00:48:18+00:00:
This was assigned CVE-2017-14248.

---

Comment by carnil on 2017-09-18 12:54:14+00:00:
@urban-warrior, @dlemstra,  can you confirm if this issue does not affect ImageMagick-6?

---

Comment by dlemstra on 2017-09-18 13:12:06+00:00:
This is an IM7 only issue.

---

Comment by mikayla-grace on 2017-09-18 13:12:50+00:00:
Confirmed.  This bug was exclusive to IMv7, not IMv6.

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