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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1554
## Description:
Issue: ImageMagick/ImageMagick#1554
Title: AddressSanitizer: heap-use-after-free at constitute.c:659
State: closed
Created by: hongxuchen
Created at: 2019-04-27 07:14:42+00:00

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
ImageMagick convert suffers from a heap-use-after-free vulnerability.

### Steps to Reproduce
* run `convert $FILE /dev/null`
```
=================================================================                                                                                                                                                                              
==1962==ERROR: AddressSanitizer: heap-use-after-free on address 0x62700003811c at pc 0x7f2584984c25 bp 0x7ffe24e14170 sp 0x7ffe24e14168                                                                                                        
WRITE of size 4 at 0x62700003811c thread T0                                                                                                                                                                                                    
    #0 0x7f2584984c24 in ReadImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:659:16                                                                                                                               
    #1 0x7f2584987a94 in ReadImages /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:917:9                                                                                                                               
    #2 0x7f2583f5a9a4 in ConvertImageCommand /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/convert.c:644:18                                                                                                                        
    #3 0x7f2584142021 in MagickCommandGenesis /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/mogrify.c:185:14                                                                                                                       
    #4 0x50c8a7 in MagickMain /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:149:10                                                                                                                                         
    #5 0x50c301 in main /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:180:10                                                                                                                                               
    #6 0x7f257cdb3b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310                                                                                                                                          
    #7 0x41ce19 in _start (/home/hongxu/work/imagemagick/ImageMagick-asan/install/bin/magick+0x41ce19)                                                                                                                                         
                                                                                                                                                                                                                                               
0x62700003811c is located 28 bytes inside of 13504-byte region [0x627000038100,0x62700003b5c0)                                                                                                                                                 
freed by thread T0 here:                                                                                                                                                                                                                       
    #0 0x4d22b8 in __interceptor_free.localalias.0 (/home/hongxu/work/imagemagick/ImageMagick-asan/install/bin/magick+0x4d22b8)                                                                                                                
    #1 0x7f2584c5b8d0 in RelinquishMagickMemory /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/memory.c:1074:3                                                                                                                      
    #2 0x7f2584bfa716 in DestroyImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/image.c:1225:19                                                                                                                                
    #3 0x7f25850c3a8a in ReadMATImage /home/hongxu/work/imagemagick/ImageMagick-asan/coders/mat.c:1479:12                                                                                                                                      
    #4 0x7f2584984089 in ReadImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:547:15                                                                                                                               
    #5 0x7f2584987a94 in ReadImages /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:917:9                                                                                                                               
    #6 0x7f2583f5a9a4 in ConvertImageCommand /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/convert.c:644:18                                                                                                                        
    #7 0x7f2584142021 in MagickCommandGenesis /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/mogrify.c:185:14                                                                                                                       
    #8 0x50c8a7 in MagickMain /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:149:10                                                                                                                                         
    #9 0x50c301 in main /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:180:10                                                                                                                                               
    #10 0x7f257cdb3b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310                                                                                                                                         
                                                                                                                                                                                                                                               
previously allocated by thread T0 here:                                                                                                                                                                                                        
    #0 0x4d2470 in __interceptor_malloc (/home/hongxu/work/imagemagick/ImageMagick-asan/install/bin/magick+0x4d2470)                                                                                                                           
    #1 0x7f2584c59fe6 in AcquireMagickMemory /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/memory.c:478:10                                                                                                                         
    #2 0x7f2584bf2d04 in AcquireCriticalMemory /home/hongxu/work/imagemagick/ImageMagick-asan/./MagickCore/memory-private.h:64:10                                                                                                              
    #3 0x7f2584bf075b in AcquireImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/image.c:172:19                                                                                                                                 
    #4 0x7f2584bf572b in AcquireNextImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/image.c:396:15                                                                                                                             
    #5 0x7f25850c2f97 in ReadMATImage /home/hongxu/work/imagemagick/ImageMagick-asan/coders/mat.c:1391:5                                                                                                                                       
    #6 0x7f2584984089 in ReadImage /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:547:15                                                                                                                               
    #7 0x7f2584987a94 in ReadImages /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:917:9                                                                                                                               
    #8 0x7f2583f5a9a4 in ConvertImageCommand /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/convert.c:644:18                                                                                                                        
    #9 0x7f2584142021 in MagickCommandGenesis /home/hongxu/work/imagemagick/ImageMagick-asan/MagickWand/mogrify.c:185:14                                                                                                                       
    #10 0x50c8a7 in MagickMain /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:149:10                                                                                                                                        
    #11 0x50c301 in main /home/hongxu/work/imagemagick/ImageMagick-asan/utilities/magick.c:180:10                                                                                                                                              
    #12 0x7f257cdb3b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
SUMMARY: AddressSanitizer: heap-use-after-free /home/hongxu/work/imagemagick/ImageMagick-asan/MagickCore/constitute.c:659:16 in ReadImage                                                                                                      
Shadow bytes around the buggy address:                                                                                                                                                                                                         
  0x0c4e7fffefd0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa                                                                                                                                                                              
  0x0c4e7fffefe0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa                                                                                                                                                                              
  0x0c4e7fffeff0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa                                                                                                                                                                              
  0x0c4e7ffff000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa                                                                                                                                                                              
  0x0c4e7ffff010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa                                                                                                                                                                              
=>0x0c4e7ffff020: fd fd fd[fd]fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
  0x0c4e7ffff030: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
  0x0c4e7ffff040: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
  0x0c4e7ffff050: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
  0x0c4e7ffff060: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
  0x0c4e7ffff070: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd                                                                                                                                                                              
Shadow byte legend (one shadow byte represents 8 application bytes):                                                                                                                                                                           
  Addressable:           00                                                                                                                                                                                                                    
  Partially addressable: 01 02 03 04 05 06 07                                                                                                                                                                                                  
  Heap left redzone:       fa                                                                                                                                                                                                                  
  Freed heap region:       fd                                                                                                                                                                                                                  
  Stack left redzone:      f1                                                                                                                                                                                                                  
  Stack mid redzone:       f2                                                                                                                                                                                                                  
  Stack right redzone:     f3                                                                                                                                                                                                                  
  Stack after return:      f5                                                                                                                                                                                                                  
  Stack use after scope:   f8                                                                                                                                                                                                                  
  Global redzone:          f9                                                                                                                                                                                                                  
  Global init order:       f6                                                                                                                                                                                                                  
  Poisoned by user:        f7                                                                                                                                                                                                                  
  Container overflow:      fc                                                                                                                                                                                                                  
  Array cookie:            ac                                                                                                                                                                                                                  
  Intra object redzone:    bb                                                                                                                                                                                                                  
  ASan internal:           fe                                                                                                                                                                                                                  
  Left alloca redzone:     ca                                                                                                                                                                                                                  
  Right alloca redzone:    cb                                                                                                                                                                                                                  
==1962==ABORTING                     
```
* POC files are available [here](https://github.com/ntu-sec/pocs/tree/master/imagemagick/3183a88ae/crashes).

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

- ImageMagick version: 7.0.8-43 Q16 x86_64 2019-04-27 (3183a88ae)
- Environment (Operating system, version and so on): Ubuntu 18.04 LTS x86_64
- Additional information: It reports an assertion failure with regular debug mode compilation.


Comments:
Comment by urban-warrior on 2019-04-27 12:32:57+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 nluedtke on 2019-08-19 11:07:29+00:00:
This was assigned [CVE-2019-15140](https://nvd.nist.gov/vuln/detail/CVE-2019-15140).

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 3183a88ae19674b4625e447d5a29da2a12d742c0
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files: