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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/525
## Description:
Issue: ImageMagick/ImageMagick#525
Title: memory leak in ReadMATImage in mat.c
State: closed
Created by: jgj212
Created at: 2017-06-24 14:17:50+00:00
Labels: bug

Issue Body:

Version: ImageMagick 7.0.6-1 Q16 x86_64

The ReadMATImage function in mat.c allows attackers to cause a denial of service (memory leak) via a small crafted mat file.

```
#identify $FILE

=================================================================
==31506==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d713b0613 in AcquireImageInfo image.c:347:28
    #3 0x7f7d713b9783 in CloneImageInfo image.c:952:14
    #4 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #5 0x7f7d711da788 in ReadImage constitute.c:497:13
    #6 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #7 0x7f7d711d932f in PingImage constitute.c:226:9
    #8 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #9 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #10 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #11 0x514a27 in MagickMain magick.c:149:10
    #12 0x514481 in main magick.c:180:10
    #13 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 9096 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d71400038 in AcquireQuantumMemory memory.c:536:10
    #3 0x7f7d7115c6b4 in AcquirePixelCache cache.c:195:28
    #4 0x7f7d7157314c in ReadStream stream.c:1027:20
    #5 0x7f7d711d932f in PingImage constitute.c:226:9
    #6 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #7 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #8 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #9 0x514a27 in MagickMain magick.c:149:10
    #10 0x514481 in main magick.c:180:10
    #11 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d71400038 in AcquireQuantumMemory memory.c:536:10
    #3 0x7f7d7115d174 in AcquirePixelCacheNexus cache.c:268:31
    #4 0x7f7d7115cbd4 in AcquirePixelCache cache.c:211:26
    #5 0x7f7d7157314c in ReadStream stream.c:1027:20
    #6 0x7f7d711d932f in PingImage constitute.c:226:9
    #7 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #8 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #9 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #10 0x514a27 in MagickMain magick.c:149:10
    #11 0x514481 in main magick.c:180:10
    #12 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d71553465 in NewSplayTree splay-tree.c:1106:32
    #3 0x7f7d71553094 in CloneSplayTree splay-tree.c:359:14
    #4 0x7f7d71430975 in CloneImageOptions option.c:1868:27
    #5 0x7f7d713bb034 in CloneImageInfo image.c:1007:10
    #6 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #7 0x7f7d711da788 in ReadImage constitute.c:497:13
    #8 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #9 0x7f7d711d932f in PingImage constitute.c:226:9
    #10 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #11 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #12 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #13 0x514a27 in MagickMain magick.c:149:10
    #14 0x514481 in main magick.c:180:10
    #15 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x4dfa05 in posix_memalign asan_malloc_linux.cc:142
    #1 0x7f7d713ffe12 in AcquireAlignedMemory memory.c:261:7
    #2 0x7f7d7115d07e in AcquirePixelCacheNexus cache.c:264:29
    #3 0x7f7d7115cbd4 in AcquirePixelCache cache.c:211:26
    #4 0x7f7d7157314c in ReadStream stream.c:1027:20
    #5 0x7f7d711d932f in PingImage constitute.c:226:9
    #6 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #7 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #8 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #9 0x514a27 in MagickMain magick.c:149:10
    #10 0x514481 in main magick.c:180:10
    #11 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x4dfa05 in posix_memalign asan_malloc_linux.cc:142
    #1 0x7f7d7153d748 in AcquireSemaphoreMemory semaphore.c:154:7
    #2 0x7f7d7153cfbc in AcquireSemaphoreInfo semaphore.c:200:36
    #3 0x7f7d7115ce93 in AcquirePixelCache cache.c:226:25
    #4 0x7f7d7157314c in ReadStream stream.c:1027:20
    #5 0x7f7d711d932f in PingImage constitute.c:226:9
    #6 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #7 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #8 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #9 0x514a27 in MagickMain magick.c:149:10
    #10 0x514481 in main magick.c:180:10
    #11 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x4dfa05 in posix_memalign asan_malloc_linux.cc:142
    #1 0x7f7d7153d748 in AcquireSemaphoreMemory semaphore.c:154:7
    #2 0x7f7d7153cfbc in AcquireSemaphoreInfo semaphore.c:200:36
    #3 0x7f7d715537e6 in NewSplayTree splay-tree.c:1119:25
    #4 0x7f7d71553094 in CloneSplayTree splay-tree.c:359:14
    #5 0x7f7d71430975 in CloneImageOptions option.c:1868:27
    #6 0x7f7d713bb034 in CloneImageInfo image.c:1007:10
    #7 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #8 0x7f7d711da788 in ReadImage constitute.c:497:13
    #9 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #10 0x7f7d711d932f in PingImage constitute.c:226:9
    #11 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #12 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #13 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #14 0x514a27 in MagickMain magick.c:149:10
    #15 0x514481 in main magick.c:180:10
    #16 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x4dfa05 in posix_memalign asan_malloc_linux.cc:142
    #1 0x7f7d7153d748 in AcquireSemaphoreMemory semaphore.c:154:7
    #2 0x7f7d7153cfbc in AcquireSemaphoreInfo semaphore.c:200:36
    #3 0x7f7d7115cf22 in AcquirePixelCache cache.c:228:30
    #4 0x7f7d7157314c in ReadStream stream.c:1027:20
    #5 0x7f7d711d932f in PingImage constitute.c:226:9
    #6 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #7 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #8 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #9 0x514a27 in MagickMain magick.c:149:10
    #10 0x514481 in main magick.c:180:10
    #11 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 52 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d71400038 in AcquireQuantumMemory memory.c:536:10
    #3 0x7f7d71588bb3 in ConstantString string.c:701:26
    #4 0x7f7d71553286 in CloneSplayTree splay-tree.c:372:7
    #5 0x7f7d71430975 in CloneImageOptions option.c:1868:27
    #6 0x7f7d713bb034 in CloneImageInfo image.c:1007:10
    #7 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #8 0x7f7d711da788 in ReadImage constitute.c:497:13
    #9 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #10 0x7f7d711d932f in PingImage constitute.c:226:9
    #11 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #12 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #13 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #14 0x514a27 in MagickMain magick.c:149:10
    #15 0x514481 in main magick.c:180:10
    #16 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d715523f0 in AddValueToSplayTree splay-tree.c:188:21
    #3 0x7f7d7155329c in CloneSplayTree splay-tree.c:371:12
    #4 0x7f7d71430975 in CloneImageOptions option.c:1868:27
    #5 0x7f7d713bb034 in CloneImageInfo image.c:1007:10
    #6 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #7 0x7f7d711da788 in ReadImage constitute.c:497:13
    #8 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #9 0x7f7d711d932f in PingImage constitute.c:226:9
    #10 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #11 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #12 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #13 0x514a27 in MagickMain magick.c:149:10
    #14 0x514481 in main magick.c:180:10
    #15 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

Indirect leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x4deea6 in __interceptor_malloc asan_malloc_linux.cc:66
    #1 0x7f7d713fffd6 in AcquireMagickMemory memory.c:463:10
    #2 0x7f7d71400038 in AcquireQuantumMemory memory.c:536:10
    #3 0x7f7d71588bb3 in ConstantString string.c:701:26
    #4 0x7f7d71553201 in CloneSplayTree splay-tree.c:371:43
    #5 0x7f7d71430975 in CloneImageOptions option.c:1868:27
    #6 0x7f7d713bb034 in CloneImageInfo image.c:1007:10
    #7 0x7f7d717e3aca in ReadMATImage mat.c:896:14
    #8 0x7f7d711da788 in ReadImage constitute.c:497:13
    #9 0x7f7d715734b9 in ReadStream stream.c:1045:9
    #10 0x7f7d711d932f in PingImage constitute.c:226:9
    #11 0x7f7d711d9ad3 in PingImages constitute.c:327:10
    #12 0x7f7d7091f006 in IdentifyImageCommand identify.c:319:18
    #13 0x7f7d709dccdf in MagickCommandGenesis mogrify.c:183:14
    #14 0x514a27 in MagickMain magick.c:149:10
    #15 0x514481 in main magick.c:180:10
    #16 0x7f7d6b21bf44 in __libc_start_main libc-start.c:287

SUMMARY: AddressSanitizer: 22645 byte(s) leaked in 11 allocation(s).
```

testcase:
https://github.com/jgj212/poc/blob/master/leak-ReadMATImage

Credit: ADLab of Venustech


Comments:
Comment by mikayla-grace on 2017-06-24 14:44:42+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 bastien-roucaries on 2017-07-14 09:38:44+00:00:
Where is v6 patch ?

---

Comment by dlemstra on 2017-07-14 09:47:43+00:00:
Here: bd428b8

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