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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/629
## Description:
Issue: ImageMagick/ImageMagick#629
Title: Heap-buffer-overflow READ in GetImageDepth._omp_fn.2 MagickCore/attribute.c:491
State: closed
Created by: zhouat
Created at: 2017-07-29 12:09:40+00:00
Labels: bug

Issue Body:
Poc link: https://github.com/zhouat/poc_IM/blob/master/heap-buffer-overflow-READ-0x0000006869f0_output_json_1501326140.06.fits

Trigger Command：
```
magick convert heap-buffer-overflow-READ 0x0000006869f0_output_json_1501326140.06.fits output.json
```

AddressSanitizer output:
```
heap-buffer-overflow READ json

=================================================================
==13039==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62a000016840 at pc 0x0000006869f0 bp 0x7ffda0c438c0 sp 0x7ffda0c438b0
READ of size 4 at 0x62a000016840 thread T0
    #0 0x6869ef in GetImageDepth._omp_fn.2 MagickCore/attribute.c:491
    #1 0x7f85027a8cbe in GOMP_parallel (/usr/lib/x86_64-linux-gnu/libgomp.so.1+0xbcbe)
    #2 0x682db8 in GetImageDepth MagickCore/attribute.c:446
    #3 0x5565bd in EncodeImageAttributes coders/json.c:919
    #4 0x55ab60 in WriteJSONImage coders/json.c:1616
    #5 0x6fdf32 in WriteImage MagickCore/constitute.c:1114
    #6 0x6feb74 in WriteImages MagickCore/constitute.c:1333
    #7 0x9ef4a5 in ConvertImageCommand MagickWand/convert.c:3280
    #8 0xa16fa0 in MagickCommandGenesis MagickWand/mogrify.c:183
    #9 0x4049c1 in MagickMain utilities/magick.c:149
    #10 0x404ba2 in main utilities/magick.c:180
    #11 0x7f8501cb882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #12 0x4044d8 in _start (/ImageMagick/utilities/magick+0x4044d8)

```


Comments:
Comment by mikayla-grace on 2017-07-29 13:06:30+00:00:
Unfortunately we cannot reproduce the problem you reported.  We're using ImageMagick 7.0.6-4 beta and gcc 7.1.1 with -fsanitize=address enabled:

```
$ magick convert heap-buffer-overflow-READ-0x0000006869f0_output_json_1501326140.06.fits output.json
$ more output.json
[{
  "image": {
    "name": "output.json",
    "baseName": "heap-buffer-overflow-READ-0x0000006869f0_output_json_1501326140
.06.fits",
    "format": "FITS",
    "formatDescription": "FITS",
    "class": "DirectClass",
...
```

---

Comment by bastien-roucaries on 2017-08-02 13:31:35+00:00:
reservation open as CVE-2017-11753

---

Comment by dlemstra on 2017-08-02 16:09:50+00:00:
Fixes are 5095363 and ccc71c1. One of the fixes is for the issue reported and the other one is a similar issue at another place in the same method. This is an IM7 only issue.

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