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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1542
## Description:
Issue: ImageMagick/ImageMagick#1542
Title: memory leak in Huffman2DEncodeImage
State: closed
Created by: butterflyhack
Created at: 2019-04-11 02:03:07+00:00
Labels: bug

Issue Body:
### Prerequisites

- [x] I have written a descriptive issue title
- [x] I have verified that I am using the latest version of ImageMagick
- [x] I have searched [open](https://github.com/ImageMagick/ImageMagick/issues) and [closed](https://github.com/ImageMagick/ImageMagick/issues?q=is%3Aissue+is%3Aclosed) issues to ensure it has not already been reported

### Description
```
==85960==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 13024 byte(s) in 1 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x43f1f0 in AcquireMagickMemory MagickCore/memory.c:478
    #2 0x412eef in AcquireCriticalMemory MagickCore/memory-private.h:64
    #3 0x4153a9 in AcquireImageInfo MagickCore/image.c:349
    #4 0x418321 in CloneImageInfo MagickCore/image.c:944
    #5 0x64faad in Huffman2DEncodeImage coders/ps2.c:207
    #6 0x652046 in WritePS2Image coders/ps2.c:766
    #7 0x809316 in WriteImage MagickCore/constitute.c:1159
    #8 0x80a03b in WriteImages MagickCore/constitute.c:1376
    #9 0xb1573d in CompositeImageCommand MagickWand/composite.c:1676
    #10 0xc9d45d in MagickCommandGenesis MagickWand/mogrify.c:184
    #11 0x40e9e1 in MagickMain utilities/magick.c:149
    #12 0x40ebc2 in main utilities/magick.c:180
    #13 0x7ffff31f282f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 13024 byte(s) leaked in 1 allocation(s).
```
### Steps to Reproduce
/usr/local/bin/magick composite Memory-Leak-input1 Memory-Leak-input2 output.ps2

### System Configuration
<!-- Tell us about the environment where you are experiencing the bug -->

- ImageMagick version:
 '7.0.8-40'
- Environment (Operating system, version and so on):
Linux ubuntu 4.15.0-42-generic #45~16.04.1-Ubuntu SMP Mon Nov 19 13:02:27 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
- Additional information:
 ' ./configure CC="gcc" CXX="g++" CFLAGS="-g -fsanitize=address" -disable-shared'

<!-- Thanks for reporting the issue to ImageMagick! -->
testcase:
[ https://github.com/butterflyhack/pocs/blob/master/memory-leak-1.zip](https://github.com/butterflyhack/pocs/blob/master/memory-leak-1.zip) 

credit: ADlab of venustech


Comments:
Comment by urban-warrior on 2019-04-11 11:37:13+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 abergmann on 2019-09-24 07:30:35+00:00:
[CVE-2019-16711](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-16711) was assigned to this issue.

Build script:
#!/bin/bash
# Default build script template for C/C++ projects
set -eu

# Define configuration options
CONFIG_OPTS="--disable-shared"
CMAKE_OPTS="-DCMAKE_BUILD_TYPE=Debug"
MAKE_OPTS="-j$(nproc)"
BUILD_DIR="build"

# 1. Run build preparation scripts if they exist
for prep_script in buildconf autogen.sh bootstrap; do
  if [ -x "$prep_script" ]; then
    echo "Running $prep_script..."
    ./$prep_script
    break
  elif [ -f "$prep_script" ]; then
    echo "Found $prep_script but it's not executable, setting permissions..."
    chmod +x $prep_script
    ./$prep_script
    break
  fi
done

# 2. Handle different build systems
if [ -f "configure" ]; then
  echo "Found configure script, running with options: $CONFIG_OPTS"
  ./configure $CONFIG_OPTS

  echo "Building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "CMakeLists.txt" ]; then
  echo "Found CMakeLists.txt, using CMake build system"
  mkdir -p $BUILD_DIR
  cd $BUILD_DIR
  cmake $CMAKE_OPTS ..
  make $MAKE_OPTS
elif [ -f "Makefile" ] || [ -f "makefile" ]; then
  echo "Found Makefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -f "meson.build" ]; then
  echo "Found meson.build, using Meson build system"
  mkdir -p $BUILD_DIR
  meson setup $BUILD_DIR .
  cd $BUILD_DIR
  ninja
elif [ -f "GNUmakefile" ]; then
  echo "Found GNUmakefile, building with make $MAKE_OPTS"
  make $MAKE_OPTS
elif [ -d ".git" ] && [ -f "Makefile.am" ]; then
  echo "Found Makefile.am, running autoreconf"
  autoreconf -i
  ./configure $CONFIG_OPTS
  make $MAKE_OPTS
else
  echo "WARNING: No recognized build system found!"
  echo "Trying generic approach:"
  
  # Try to detect common source patterns and compile them
  if ls *.c >/dev/null 2>&1 || ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
    echo "Found C/C++ source files, attempting direct compilation"
    if ls *.cpp >/dev/null 2>&1 || ls *.cc >/dev/null 2>&1; then
      CXX=${CXX:-g++}
      echo "Compiling C++ sources with $CXX"
      $CXX -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.cpp *.cc 2>/dev/null || true
    elif ls *.c >/dev/null 2>&1; then
      CC=${CC:-gcc}
      echo "Compiling C sources with $CC"
      $CC -g -O1 -fsanitize=address -fno-omit-frame-pointer -o main *.c 2>/dev/null || true
    fi
  fi
fi

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool pkg-config cmake nasm autopoint
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout 5e0211cfa7fc8dcff380fd0a363683203385decc
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files: