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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/927
## Description:
Issue: ImageMagick/ImageMagick#927
Title: heap-buffer-overflow in sixel_decode
State: closed
Created by: littleputa
Created at: 2018-01-04 01:25:57+00:00
Labels: bug

Issue Body:
ubuntu@ubuntu:~/fuzz_py$ magick -version
Version: ImageMagick 7.0.7-17 Q16 x86_64 2017-12-26 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): fontconfig freetype png x zlib

[poc.zip](https://github.com/ImageMagick/ImageMagick/files/1602188/poc.zip)

ubuntu@ubuntu:~/fuzz_py$ magick montage poc.sixel /dev/null
=================================================================
==108451==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x621000007500 at pc 0x7fcb10a33585 bp 0x7ffde25a2fd0 sp 0x7ffde25a2fc8
READ of size 1 at 0x621000007500 thread T0
    #0 0x7fcb10a33584 in sixel_decode /home/ubuntu/ImageMagick/coders/sixel.c:289:12
    #1 0x7fcb10a3455e in ReadSIXELImage /home/ubuntu/ImageMagick/coders/sixel.c:1027:7
    #2 0x7fcb103a9af1 in ReadImage /home/ubuntu/ImageMagick/MagickCore/constitute.c:497:13
    #3 0x7fcb103ac2f4 in ReadImages /home/ubuntu/ImageMagick/MagickCore/constitute.c:866:9
    #4 0x7fcb0fcbe3c7 in MontageImageCommand /home/ubuntu/ImageMagick/MagickWand/montage.c:410:20
    #5 0x7fcb0fc452d2 in MagickCommandGenesis /home/ubuntu/ImageMagick/MagickWand/mogrify.c:183:14
    #6 0x4e4ce7 in MagickMain /home/ubuntu/ImageMagick/utilities/magick.c:149:10
    #7 0x4e4ce7 in main /home/ubuntu/ImageMagick/utilities/magick.c:180
    #8 0x7fcb0d7c5f44 in __libc_start_main /build/eglibc-oGUzwX/eglibc-2.19/csu/libc-start.c:287
    #9 0x41a10b in _start (/usr/local/bin/magick+0x41a10b)

0x621000007500 is located 0 bytes to the right of 4096-byte region [0x621000006500,0x621000007500)
allocated by thread T0 here:
    #0 0x4b9ad3 in malloc (/usr/local/bin/magick+0x4b9ad3)
    #1 0x7fcb10a3429d in ReadSIXELImage /home/ubuntu/ImageMagick/coders/sixel.c:1002:25
    #2 0x7fcb103a9af1 in ReadImage /home/ubuntu/ImageMagick/MagickCore/constitute.c:497:13
    #3 0x7fcb103ac2f4 in ReadImages /home/ubuntu/ImageMagick/MagickCore/constitute.c:866:9
    #4 0x7fcb0fcbe3c7 in MontageImageCommand /home/ubuntu/ImageMagick/MagickWand/montage.c:410:20
    #5 0x7fcb0fc452d2 in MagickCommandGenesis /home/ubuntu/ImageMagick/MagickWand/mogrify.c:183:14
    #6 0x4e4ce7 in MagickMain /home/ubuntu/ImageMagick/utilities/magick.c:149:10
    #7 0x4e4ce7 in main /home/ubuntu/ImageMagick/utilities/magick.c:180
    #8 0x7fcb0d7c5f44 in __libc_start_main /build/eglibc-oGUzwX/eglibc-2.19/csu/libc-start.c:287

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/ubuntu/ImageMagick/coders/sixel.c:289:12 in sixel_decode
Shadow bytes around the buggy address:
  0x0c427fff8e50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8e60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8e70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c427fff8e90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c427fff8ea0:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff8eb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff8ec0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff8ed0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff8ee0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c427fff8ef0: 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
==108451==ABORTING



Comments:
Comment by dlemstra on 2018-01-04 08:55:49+00:00:
@littleputa Could you put the trace in a `code` block next time so you don't reference all those issues?

---

Comment by urban-warrior on 2018-01-04 14:13:25+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 littleputa on 2018-01-05 00:49:41+00:00:
credit: nsfocus security team.

---

Comment by littleputa on 2018-01-08 01:00:51+00:00:
CVE-2018-5248

Attachments:
https://github.com/ImageMagick/ImageMagick/files/1602188/poc.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 c9d4c4ecd1f7fb5168e512a0ba183b4bf8b47ce3
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;
}