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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/964
## Description:
Issue: ImageMagick/ImageMagick#964
Title: coders/dcm.c Memory Leak
State: closed
Created by: ksyang
Created at: 2018-01-30 04:23:18+00:00
Labels: bug

Issue Body:
### System Configuration

- ImageMagick version: ImageMagick 7.0.7-23
- Environment (Operating system, version and so on): Ubuntu 14.04 64bit
- Additional information: compilation with asan



### Description

https://github.com/ImageMagick/ImageMagick/blob/23246df8dfdebe0002bf9abfa0a5490238300ad9/coders/dcm.c#L3583 
https://github.com/ImageMagick/ImageMagick/blob/23246df8dfdebe0002bf9abfa0a5490238300ad9/coders/dcm.c#L3611
https://github.com/ImageMagick/ImageMagick/blob/23246df8dfdebe0002bf9abfa0a5490238300ad9/coders/dcm.c#L3639

Each redmap, greenmap, and bluemap can be overwritten by a new pointer, so it loses previous pointer, which leads to memory leak. I guess we should check if the each XXXmap variable is already allocated or not.

```
$ ./magick convert ~/leak1.dcm ./out.png
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714.
convert: InsufficientImageDataInFile `/home/skid/leak1.dcm' @ error/dcm.c/ReadDCMImage/3280.
convert: NoImagesDefined `./out.png' @ error/convert.c/ConvertImageCommand/3275.

=================================================================
==62826==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 20 byte(s) in 1 object(s) allocated from:
    #0 0x4bd753 in __interceptor_malloc /home/brian/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:67:3
    #1 0x537d1d in AcquireMagickMemory (/imagemagick.latest/utilities/magick+0x537d1d)
    #2 0x537dcd in AcquireQuantumMemory (/imagemagick.latest/utilities/magick+0x537dcd)
    #3 0x6c91c2 in ReadDCMImage (/imagemagick.latest/utilities/magick+0x6c91c2)
    #4 0xaf0948 in ReadImage (/imagemagick.latest/utilities/magick+0xaf0948)
    #5 0xaf4ff4 in ReadImages (/imagemagick.latest/utilities/magick+0xaf4ff4)
    #6 0xe61486 in ConvertImageCommand (/imagemagick.latest/utilities/magick+0xe61486)
    #7 0xf41f40 in MagickCommandGenesis (/imagemagick.latest/utilities/magick+0xf41f40)
    #8 0x4e8af9 in MagickMain (/imagemagick.latest/utilities/magick+0x4e8af9)
    #9 0x4e8401 in main (/imagemagick.latest/utilities/magick+0x4e8401)
    #10 0x7f09662ab82f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291
```


```
$ ./magick convert ~/leak2.dcm ./a.png
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714.
convert: ImproperImageHeader `/home/skid/leak2.dcm' @ error/dcm.c/ReadDCMImage/3151.
convert: NoImagesDefined `./a.png' @ error/convert.c/ConvertImageCommand/3275.

=================================================================
==63144==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 52 byte(s) in 1 object(s) allocated from:
    #0 0x4bd753 in __interceptor_malloc /home/brian/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:67:3
    #1 0x537d1d in AcquireMagickMemory (/imagemagick.latest/utilities/magick+0x537d1d)
    #2 0x537dcd in AcquireQuantumMemory (/imagemagick.latest/utilities/magick+0x537dcd)
    #3 0x6c8a80 in ReadDCMImage (/imagemagick.latest/utilities/magick+0x6c8a80)
    #4 0xaf0948 in ReadImage (/imagemagick.latest/utilities/magick+0xaf0948)
    #5 0xaf4ff4 in ReadImages (/imagemagick.latest/utilities/magick+0xaf4ff4)
    #6 0xe61486 in ConvertImageCommand /imagemagick.latest/utilities/magick+0xe61486)
    #7 0xf41f40 in MagickCommandGenesis (/imagemagick.latest/utilities/magick+0xf41f40)
    #8 0x4e8af9 in MagickMain (/imagemagick.latest/utilities/magick+0x4e8af9)
    #9 0x4e8401 in main (/imagemagick.latest/utilities/magick+0x4e8401)
    #10 0x7f8ea250982f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: 52 byte(s) leaked in 1 allocation(s).
```



```
$ ./magick convert ~/leak3.dcm ./a.png
convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714.
convert: InsufficientImageDataInFile `/home/skid/leak3.dcm' @ error/dcm.c/ReadDCMImage/3280.
convert: NoImagesDefined `./a.png' @ error/convert.c/ConvertImageCommand/3275.

=================================================================
==63151==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 196 byte(s) in 1 object(s) allocated from:
    #0 0x4bd753 in __interceptor_malloc /home/brian/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:67:3
    #1 0x537d1d in AcquireMagickMemory (/imagemagick.latest/utilities/magick+0x537d1d)
    #2 0x537dcd in AcquireQuantumMemory (/imagemagick.latest/utilities/magick+0x537dcd)
    #3 0x6c9904 in ReadDCMImage (/imagemagick.latest/utilities/magick+0x6c9904)
    #4 0xaf0948 in ReadImage (/imagemagick.latest/utilities/magick+0xaf0948)
    #5 0xaf4ff4 in ReadImages (/imagemagick.latest/utilities/magick+0xaf4ff4)
    #6 0xe61486 in ConvertImageCommand (/imagemagick.latest/utilities/magick+0xe61486)
    #7 0xf41f40 in MagickCommandGenesis (/imagemagick.latest/utilities/magick+0xf41f40)
    #8 0x4e8af9 in MagickMain (/imagemagick.latest/utilities/magick+0x4e8af9)
    #9 0x4e8401 in main (/imagemagick.latest/utilities/magick+0x4e8401)
    #10 0x7fb320bdc82f in __libc_start_main /build/glibc-Cl5G7W/glibc-2.23/csu/../csu/libc-start.c:291

SUMMARY: AddressSanitizer: 196 byte(s) leaked in 1 allocation(s).
```


Crash File : [crashes.zip](https://github.com/ImageMagick/ImageMagick/files/1676368/crashes.zip)




Credit : Kyeongseok Yang & Choongwoo Han, Naver Security Team




Comments:
Comment by dlemstra on 2018-01-30 06:19:31+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 nohmask on 2018-02-01 01:55:32+00:00:
This was assigned CVE-2018-6405.

Attachments:
https://github.com/ImageMagick/ImageMagick/files/1676368/crashes.zip

Commit References:
23246df8dfdebe0002bf9abfa0a5490238300ad9

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
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout fede9f9dd52a6793042c8744cf6fbf6cafa8565f
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files: