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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/907
## Description:
Issue: ImageMagick/ImageMagick#907
Title: stack-buffer-overflow in SetImageProgress
State: closed
Created by: henices
Created at: 2017-12-21 08:52:20+00:00
Labels: bug

Issue Body:
/usr/local/bin/magick -version
Version: ImageMagick 7.0.7-16 Q16 x86_64 2017-12-21 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gvc jbig jng jpeg lcms lqr lzma pangocairo png rsvg tiff webp wmf x xml zlib

Trigger Command: **magick convert stack-buffer-overflow-0 /dev/null**

```
=================================================================
==32293==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffde8684f48 at pc 0x7f9afe1f585e bp 0x7ffde8681020 sp 0x7ffde8681010
READ of size 8 at 0x7ffde8684f48 thread T0
    #0 0x7f9afe1f585d in SetImageProgress MagickCore/monitor-private.h:33
    #1 0x7f9afe1f80f3 in WebPEncodeProgress coders/webp.c:558
    #2 0x7f9afb157973  (/lib64/libwebp.so.7+0x4f973)
    #3 0x7f9afb13b487  (/lib64/libwebp.so.7+0x33487)
    #4 0x7f9afb159b66  (/lib64/libwebp.so.7+0x51b66)
    #5 0x7f9afb13bd7b  (/lib64/libwebp.so.7+0x33d7b)
    #6 0x7f9afb157e36 in WebPEncode (/lib64/libwebp.so.7+0x4fe36)
    #7 0x7f9afe1f9cbe in WriteWEBPImage coders/webp.c:769
    #8 0x7f9afdb334cc in WriteImage MagickCore/constitute.c:1114
    #9 0x7f9afdb34209 in WriteImages MagickCore/constitute.c:1333
    #10 0x7f9afd35bd6f in ConvertImageCommand MagickWand/convert.c:3280
    #11 0x7f9afd46025c in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x401b16 in MagickMain utilities/magick.c:149
    #13 0x401d80 in main utilities/magick.c:180
    #14 0x7f9af704a009 in __libc_start_main (/lib64/libc.so.6+0x21009)
    #15 0x4015d9 in _start (/usr/local/bin/magick+0x4015d9)

Address 0x7ffde8684f48 is located in stack of thread T0 at offset 40 in frame
    #0 0x7f9afdb3298e in WriteImage MagickCore/constitute.c:975

  This frame has 4 object(s):
    [32, 40) 'lsb_first' <== Memory access at offset 40 overflows this variable
    [96, 4192) 'filename'
    [4224, 8320) 'image_filename'
    [8352, 12448) 'extension'
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow MagickCore/monitor-private.h:33 in SetImageProgress
Shadow bytes around the buggy address:
  0x10003d0c8990: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c89a0: 00 00 00 04 f2 f2 f2 f2 00 00 00 00 00 00 00 00
  0x10003d0c89b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c89c0: 00 00 00 00 00 00 00 00 f3 f3 f3 f3 00 00 00 00
  0x10003d0c89d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x10003d0c89e0: 00 00 00 00 f1 f1 f1 f1 00[f2]f2 f2 f2 f2 f2 f2
  0x10003d0c89f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c8a00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c8a10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c8a20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10003d0c8a30: 00 00 00 00 00 00 00 00 00 00 00 00 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
  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
==32293==ABORTING
```
testcase: https://github.com/henices/pocs/raw/master/stack-buffer-overflow-0

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


Comments:
Comment by urban-warrior on 2017-12-21 11:45:40+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-25 04:06:02+00:00:
This was assigned CVE-2017-17880.

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