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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/877
## Description:
Issue: ImageMagick/ImageMagick#877
Title: memory leaks in ReadPGXImage
State: closed
Created by: henices
Created at: 2017-11-24 05:49:54+00:00
Labels: bug

Issue Body:
$ magick -version
Version: ImageMagick 7.0.7-12 Q16 x86_64 2017-11-21 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): bzlib fontconfig freetype jng jpeg lzma pangocairo png tiff x xml zlib

Trigger Command: **magick ReadPGXImage-memory-leaks /dev/null**

```

=================================================================
==11698==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 152 byte(s) in 1 object(s) allocated from:
    #0 0x7f1ce5e32850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7f1ce52473cc in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f1ce52da540 in AcquireCriticalMemory MagickCore/memory-private.h:57
    #3 0x7f1ce52da66f in AcquireQuantumInfo MagickCore/quantum.c:119
    #4 0x7f1ce55c26a7 in ReadPGXImage coders/pgx.c:204
    #5 0x7f1ce507a3cf in ReadImage MagickCore/constitute.c:497
    #6 0x7f1ce507d298 in ReadImages MagickCore/constitute.c:866
    #7 0x7f1ce4ad586a in CLINoImageOperator MagickWand/operation.c:4763
    #8 0x7f1ce4ad8b72 in CLIOption MagickWand/operation.c:5258
    #9 0x7f1ce49815f8 in ProcessCommandOptions MagickWand/magick-cli.c:424
    #10 0x7f1ce4982ea0 in MagickImageCommand MagickWand/magick-cli.c:794
    #11 0x7f1ce49bc0e6 in MagickCommandGenesis MagickWand/mogrify.c:183
    #12 0x401a36 in MagickMain utilities/magick.c:149
    #13 0x401ca0 in main utilities/magick.c:180
    #14 0x7f1ce0b51889 in __libc_start_main (/lib64/libc.so.6+0x20889)

Indirect leak of 2402 byte(s) in 2 object(s) allocated from:
    #0 0x7f1ce5e32850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7f1ce52473cc in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f1ce5247420 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f1ce52da9dc in AcquireQuantumPixels MagickCore/quantum.c:174
    #4 0x7f1ce52dc6c3 in SetQuantumDepth MagickCore/quantum.c:692
    #5 0x7f1ce52da70d in AcquireQuantumInfo MagickCore/quantum.c:124
    #6 0x7f1ce55c26a7 in ReadPGXImage coders/pgx.c:204
    #7 0x7f1ce507a3cf in ReadImage MagickCore/constitute.c:497
    #8 0x7f1ce507d298 in ReadImages MagickCore/constitute.c:866
    #9 0x7f1ce4ad586a in CLINoImageOperator MagickWand/operation.c:4763
    #10 0x7f1ce4ad8b72 in CLIOption MagickWand/operation.c:5258
    #11 0x7f1ce49815f8 in ProcessCommandOptions MagickWand/magick-cli.c:424
    #12 0x7f1ce4982ea0 in MagickImageCommand MagickWand/magick-cli.c:794
    #13 0x7f1ce49bc0e6 in MagickCommandGenesis MagickWand/mogrify.c:183
    #14 0x401a36 in MagickMain utilities/magick.c:149
    #15 0x401ca0 in main utilities/magick.c:180
    #16 0x7f1ce0b51889 in __libc_start_main (/lib64/libc.so.6+0x20889)

Indirect leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7f1ce5e334a0 in posix_memalign (/lib64/libasan.so.4+0xdf4a0)
    #1 0x7f1ce533d7bd in AcquireSemaphoreMemory MagickCore/semaphore.c:154
    #2 0x7f1ce533d8a7 in AcquireSemaphoreInfo MagickCore/semaphore.c:200
    #3 0x7f1ce52db698 in GetQuantumInfo MagickCore/quantum.c:426
    #4 0x7f1ce52da6bd in AcquireQuantumInfo MagickCore/quantum.c:121
    #5 0x7f1ce55c26a7 in ReadPGXImage coders/pgx.c:204
    #6 0x7f1ce507a3cf in ReadImage MagickCore/constitute.c:497
    #7 0x7f1ce507d298 in ReadImages MagickCore/constitute.c:866
    #8 0x7f1ce4ad586a in CLINoImageOperator MagickWand/operation.c:4763
    #9 0x7f1ce4ad8b72 in CLIOption MagickWand/operation.c:5258
    #10 0x7f1ce49815f8 in ProcessCommandOptions MagickWand/magick-cli.c:424
    #11 0x7f1ce4982ea0 in MagickImageCommand MagickWand/magick-cli.c:794
    #12 0x7f1ce49bc0e6 in MagickCommandGenesis MagickWand/mogrify.c:183
    #13 0x401a36 in MagickMain utilities/magick.c:149
    #14 0x401ca0 in main utilities/magick.c:180
    #15 0x7f1ce0b51889 in __libc_start_main (/lib64/libc.so.6+0x20889)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7f1ce5e32850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7f1ce52473cc in AcquireMagickMemory MagickCore/memory.c:464
    #2 0x7f1ce5247420 in AcquireQuantumMemory MagickCore/memory.c:537
    #3 0x7f1ce52da89b in AcquireQuantumPixels MagickCore/quantum.c:165
    #4 0x7f1ce52dc6c3 in SetQuantumDepth MagickCore/quantum.c:692
    #5 0x7f1ce52da70d in AcquireQuantumInfo MagickCore/quantum.c:124
    #6 0x7f1ce55c26a7 in ReadPGXImage coders/pgx.c:204
    #7 0x7f1ce507a3cf in ReadImage MagickCore/constitute.c:497
    #8 0x7f1ce507d298 in ReadImages MagickCore/constitute.c:866
    #9 0x7f1ce4ad586a in CLINoImageOperator MagickWand/operation.c:4763
    #10 0x7f1ce4ad8b72 in CLIOption MagickWand/operation.c:5258
    #11 0x7f1ce49815f8 in ProcessCommandOptions MagickWand/magick-cli.c:424
    #12 0x7f1ce4982ea0 in MagickImageCommand MagickWand/magick-cli.c:794
    #13 0x7f1ce49bc0e6 in MagickCommandGenesis MagickWand/mogrify.c:183
    #14 0x401a36 in MagickMain utilities/magick.c:149
    #15 0x401ca0 in main utilities/magick.c:180
    #16 0x7f1ce0b51889 in __libc_start_main (/lib64/libc.so.6+0x20889)

SUMMARY: AddressSanitizer: 2634 byte(s) leaked in 5 allocation(s).
```

testcase：
https://github.com/henices/pocs/raw/master/ReadPGXImage-memory-leaks

Credit: NSFocus Security Team <security (at) nsfocus (dot) com>


Comments:
Comment by urban-warrior on 2017-11-24 14:37:51+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 nohmask on 2017-12-25 04:04:37+00:00:
This was assigned CVE-2017-17883.

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