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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/682
## Description:
Issue: ImageMagick/ImageMagick#682
Title: Heap buffer overflow in ReadSFWImage
State: closed
Created by: whiteHat001
Created at: 2017-08-18 08:55:11+00:00
Labels: bug

Issue Body:
**zhihua.yao@dbappsecurity.com.cn**

```
root@ubuntu:/home/hjy/Desktop# convert heap-buffer-overflow_ReadSFWImage /de/vnull
=================================================================
==11164==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb5111fd0 at pc 0x80e5ce9 bp 0xbf9aee18 sp 0xbf9aee0c
WRITE of size 1 at 0xb5111fd0 thread T0
    #0 0x80e5ce8 in CopyMagickMemory MagickCore/memory.c:744
    #1 0x85a30a7 in ReadSFWImage coders/sfw.c:279
    #2 0x8892bf8 in ReadImage MagickCore/constitute.c:497
    #3 0x8896470 in ReadImages MagickCore/constitute.c:866
    #4 0x8e99eb8 in ConvertImageCommand MagickWand/convert.c:641
    #5 0x8fa28f1 in MagickCommandGenesis MagickWand/mogrify.c:183
    #6 0x80749e6 in MagickMain utilities/magick.c:149
    #7 0x80556da in main utilities/magick.c:180
    #8 0xb6df9a82 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x19a82)
    #9 0x80740ea (/usr/local/bin/magick+0x80740ea)

0xb5111fd0 is located 0 bytes to the right of 32-byte region [0xb5111fb0,0xb5111fd0)
allocated by thread T0 here:
    #0 0xb728788a in __interceptor_malloc (/usr/lib/i386-linux-gnu/libasan.so.1+0x4e88a)
    #1 0x85a2df2 in ReadSFWImage coders/sfw.c:255
    #2 0x8892bf8 in ReadImage MagickCore/constitute.c:497
    #3 0x8896470 in ReadImages MagickCore/constitute.c:866
    #4 0x8e99eb8 in ConvertImageCommand MagickWand/convert.c:641
    #5 0x8fa28f1 in MagickCommandGenesis MagickWand/mogrify.c:183
    #6 0x80749e6 in MagickMain utilities/magick.c:149
    #7 0x80556da in main utilities/magick.c:180
    #8 0xb6df9a82 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x19a82)

SUMMARY: AddressSanitizer: heap-buffer-overflow MagickCore/memory.c:744 CopyMagickMemory
Shadow bytes around the buggy address:
  0x36a223a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a223b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a223c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a223d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a223e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x36a223f0: fa fa fa fa fa fa 00 00 00 00[fa]fa fd fd fd fd
  0x36a22400: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a22410: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a22420: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a22430: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x36a22440: 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
  Heap right 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
  Contiguous container OOB:fc
  ASan internal:           fe
==11164==ABORTING
root@ubuntu:/home/hjy/Desktop# magick --version
Version: ImageMagick 7.0.6-8 Q16 i686 2017-08-18 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): jng jpeg png xml zlib
```
My platform is ubuntu x86.

POC  https://github.com/whiteHat001/FUZZ_POC/blob/master/heap-buffer-overflow_ReadSFWImage


Comments:
Comment by urban-warrior on 2017-08-18 11:53:09+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 fgeek on 2017-08-21 12:27:12+00:00:
Please use CVE-2017-12983 for this issue. What method did you use to find this?

---

Comment by whiteHat001 on 2017-08-21 12:31:45+00:00:
@fgeek  Just use the fuzzing tool-AFL.And use someone else testcase fuzz out.

---

Comment by fgeek on 2017-08-21 12:35:14+00:00:
@whiteHat001 Yes. AFL is known to me. I was just curious and in the process I also want to give credits for the author of fuzzing (and similar) tools. Keep up the good work.

---

Comment by Mambaboy on 2018-02-04 15:02:45+00:00:
what is the parameters run the afl to fuzz CVE-2017-12983

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