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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/921
## Description:
Issue: ImageMagick/ImageMagick#921
Title: memory leaks in ReadPWPImage
State: closed
Created by: just0day
Created at: 2017-12-27 01:42:51+00:00
Labels: bug

Issue Body:
Version: ImageMagick 7.0.7-17 Q16 x86_64 2017-12-27 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 djvu fftw fontconfig freetype jbig jng jpeg lcms lqr lzma openexr pangocairo png tiff wmf x xml zlib

### magick convert Memory-Leak-ReadPWPImage /dev/null

```
=================================================================
==15572==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be93602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x440319 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x414423 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x416851 in AcquireImageInfo MagickCore/image.c:346
    #4 0x41976a in CloneImageInfo MagickCore/image.c:947
    #5 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #6 0x824acb in ReadImage MagickCore/constitute.c:497
    #7 0x826c06 in ReadImages MagickCore/constitute.c:866
    #8 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #9 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #10 0x40fdfc in MagickMain utilities/magick.c:149
    #11 0x40ffdd in main utilities/magick.c:180
    #12 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be93602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x440319 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x496424 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x49a267 in NewSplayTree MagickCore/splay-tree.c:1148
    #4 0x4974a9 in CloneSplayTree MagickCore/splay-tree.c:360
    #5 0x445010 in CloneImageOptions MagickCore/option.c:2131
    #6 0x41abd4 in CloneImageInfo MagickCore/image.c:1012
    #7 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #8 0x824acb in ReadImage MagickCore/constitute.c:497
    #9 0x826c06 in ReadImages MagickCore/constitute.c:866
    #10 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #11 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x40fdfc in MagickMain utilities/magick.c:149
    #13 0x40ffdd in main utilities/magick.c:180
    #14 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be94076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x49257e in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x49263a in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x49a458 in NewSplayTree MagickCore/splay-tree.c:1159
    #4 0x4974a9 in CloneSplayTree MagickCore/splay-tree.c:360
    #5 0x445010 in CloneImageOptions MagickCore/option.c:2131
    #6 0x41abd4 in CloneImageInfo MagickCore/image.c:1012
    #7 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #8 0x824acb in ReadImage MagickCore/constitute.c:497
    #9 0x826c06 in ReadImages MagickCore/constitute.c:866
    #10 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #11 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x40fdfc in MagickMain utilities/magick.c:149
    #13 0x40ffdd in main utilities/magick.c:180
    #14 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be93602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x440319 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x496955 in AddValueToSplayTree MagickCore/splay-tree.c:189
    #3 0x497632 in CloneSplayTree MagickCore/splay-tree.c:372
    #4 0x445010 in CloneImageOptions MagickCore/option.c:2131
    #5 0x41abd4 in CloneImageInfo MagickCore/image.c:1012
    #6 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #7 0x824acb in ReadImage MagickCore/constitute.c:497
    #8 0x826c06 in ReadImages MagickCore/constitute.c:866
    #9 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #10 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x40fdfc in MagickMain utilities/magick.c:149
    #12 0x40ffdd in main utilities/magick.c:180
    #13 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 31 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be93602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x440319 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x44036d in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x4b2caa in ConstantString MagickCore/string.c:700
    #4 0x4975c6 in CloneSplayTree MagickCore/splay-tree.c:373
    #5 0x445010 in CloneImageOptions MagickCore/option.c:2131
    #6 0x41abd4 in CloneImageInfo MagickCore/image.c:1012
    #7 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #8 0x824acb in ReadImage MagickCore/constitute.c:497
    #9 0x826c06 in ReadImages MagickCore/constitute.c:866
    #10 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #11 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x40fdfc in MagickMain utilities/magick.c:149
    #13 0x40ffdd in main utilities/magick.c:180
    #14 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x7f867be93602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x440319 in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x44036d in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x4b2caa in ConstantString MagickCore/string.c:700
    #4 0x49761d in CloneSplayTree MagickCore/splay-tree.c:372
    #5 0x445010 in CloneImageOptions MagickCore/option.c:2131
    #6 0x41abd4 in CloneImageInfo MagickCore/image.c:1012
    #7 0x6891f6 in ReadPWPImage coders/pwp.c:177
    #8 0x824acb in ReadImage MagickCore/constitute.c:497
    #9 0x826c06 in ReadImages MagickCore/constitute.c:866
    #10 0xb3749a in ConvertImageCommand MagickWand/convert.c:641
    #11 0xcb7219 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x40fdfc in MagickMain utilities/magick.c:149
    #13 0x40ffdd in main utilities/magick.c:180
    #14 0x7f867768b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 13248 byte(s) leaked in 6 allocation(s).
```
test case: https://github.com/just0day/poc/blob/master/Memory-Leak-ReadPWPImage

by future-sec


Comments:
Comment by urban-warrior on 2017-12-27 02:36:36+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 abergmann on 2018-01-02 10:18:42+00:00:
CVE-2017-18008 was assigned to this issue.

https://nvd.nist.gov/vuln/detail/CVE-2017-18008


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