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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/582
## Description:
Issue: ImageMagick/ImageMagick#582
Title: Memory-Leak in ReadOnePNGImage() coders/png.c
State: closed
Created by: lcatro
Created at: 2017-07-18 04:52:17+00:00
Labels: bug

Issue Body:

Crash Link : https://raw.githubusercontent.com/lcatro/My_PoC/master/ImageMagick/memory-leak_output_art_ReadOnePNGImage

Trigger Command : ./magick convert memory-leak_output_art_ReadOnePNGImage output.art

Crash Detail :  
```
fuzzing@ubuntu:~/fuzzing/ImageMagick/utilities$ ./magick convert memory-leak_output_art_ReadOnePNGImage output.art
gamma = (3846543/100000)wx=39594.542969, wy=0.329000, rx=0.640000, ry=0.614160
gx=0.300000, gy=0.600000, bx=0.150000, by=0.060000
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/715.
convert: Ignoring incorrect gAMA value when sRGB is also present `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring incorrect cHRM value when sRGB is also present `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: Ignoring bad adaptive filter type `memory-leak_output_art_ReadOnePNGImage' @ warning/png.c/MagickPNGWarningHandler/1693.
convert: incorrect data check `memory-leak_output_art_ReadOnePNGImage' @ error/png.c/MagickPNGErrorHandler/1660.

=================================================================
==53617==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 152 byte(s) in 1 object(s) allocated from:
    #0 0x7f4833557602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f4832a55dba in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f4832adf331 in AcquireQuantumInfo MagickCore/quantum.c:118
    #3 0x7f4832e59e27 in ReadOnePNGImage coders/png.c:3291
    #4 0x7f4832e5d499 in ReadPNGImage coders/png.c:4156
    #5 0x7f48328a80cd in ReadImage MagickCore/constitute.c:497
    #6 0x7f48328aa2cc in ReadImages MagickCore/constitute.c:866
    #7 0x7f483206507d in ConvertImageCommand MagickWand/convert.c:641
    #8 0x7f48321f3d68 in MagickCommandGenesis MagickWand/mogrify.c:183
    #9 0x4017f1 in MagickMain utilities/magick.c:149
    #10 0x4019d2 in main utilities/magick.c:180
    #11 0x7f483196882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 3174 byte(s) in 6 object(s) allocated from:
    #0 0x7f4833557602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f4832a55dba in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f4832a55e0e in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f4832adf77a in AcquireQuantumPixels MagickCore/quantum.c:175
    #4 0x7f4832ae145e in SetQuantumDepth MagickCore/quantum.c:693
    #5 0x7f4832ae167a in SetQuantumEndian MagickCore/quantum.c:733
    #6 0x7f4832e59e73 in ReadOnePNGImage coders/png.c:3296
    #7 0x7f4832e5d499 in ReadPNGImage coders/png.c:4156
    #8 0x7f48328a80cd in ReadImage MagickCore/constitute.c:497
    #9 0x7f48328aa2cc in ReadImages MagickCore/constitute.c:866
    #10 0x7f483206507d in ConvertImageCommand MagickWand/convert.c:641
    #11 0x7f48321f3d68 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x4017f1 in MagickMain utilities/magick.c:149
    #13 0x4019d2 in main utilities/magick.c:180
    #14 0x7f483196882f 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 0x7f4833558076 in __interceptor_posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99076)
    #1 0x7f4832b39f83 in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f4832b3a03f in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f4832ae0436 in GetQuantumInfo MagickCore/quantum.c:427
    #4 0x7f4832adf45b in AcquireQuantumInfo MagickCore/quantum.c:122
    #5 0x7f4832e59e27 in ReadOnePNGImage coders/png.c:3291
    #6 0x7f4832e5d499 in ReadPNGImage coders/png.c:4156
    #7 0x7f48328a80cd in ReadImage MagickCore/constitute.c:497
    #8 0x7f48328aa2cc in ReadImages MagickCore/constitute.c:866
    #9 0x7f483206507d in ConvertImageCommand MagickWand/convert.c:641
    #10 0x7f48321f3d68 in MagickCommandGenesis MagickWand/mogrify.c:183
    #11 0x4017f1 in MagickMain utilities/magick.c:149
    #12 0x4019d2 in main utilities/magick.c:180
    #13 0x7f483196882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Indirect leak of 48 byte(s) in 1 object(s) allocated from:
    #0 0x7f4833557602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7f4832a55dba in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f4832a55e0e in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f4832adf639 in AcquireQuantumPixels MagickCore/quantum.c:166
    #4 0x7f4832ae145e in SetQuantumDepth MagickCore/quantum.c:693
    #5 0x7f4832ae167a in SetQuantumEndian MagickCore/quantum.c:733
    #6 0x7f4832e59e73 in ReadOnePNGImage coders/png.c:3296
    #7 0x7f4832e5d499 in ReadPNGImage coders/png.c:4156
    #8 0x7f48328a80cd in ReadImage MagickCore/constitute.c:497
    #9 0x7f48328aa2cc in ReadImages MagickCore/constitute.c:866
    #10 0x7f483206507d in ConvertImageCommand MagickWand/convert.c:641
    #11 0x7f48321f3d68 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x4017f1 in MagickMain utilities/magick.c:149
    #13 0x4019d2 in main utilities/magick.c:180
    #14 0x7f483196882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 3438 byte(s) leaked in 9 allocation(s).
```



Comments:
Comment by mikayla-grace on 2017-07-18 11:23:19+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-27 23:16:18+00:00:
Is is v7 only ?

---

Comment by lcatro on 2017-07-28 06:26:27+00:00:
@bastien-roucaries My source is v7 ,i had not try reproduct in v6 ..

---

Comment by glennrp on 2017-07-28 12:05:45+00:00:
IM6 is still leaking; I'll patch it later today.

---

Comment by glennrp on 2017-07-28 12:17:40+00:00:
IM6 fixed by commit 4e81160d66f02bf7b4f569669ca7dd80d416ba6e

---

Comment by bastien-roucaries on 2017-07-29 12:08:14+00:00:
CVE-2017-11539

Commit References:
4e81160d66f02bf7b4f569669ca7dd80d416ba6e

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