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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1561
## Description:
Issue: ImageMagick/ImageMagick#1561
Title: heap-buffer-overflow in WritePNGImage of png.c
State: closed
Created by: galycannon
Created at: 2019-04-29 02:55:44+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
<!-- A description of the bug or feature -->
There is a heap buffer overflow vulnerability in function WritePNGImage of png.c.
### Steps to Reproduce
<!-- List of steps, sample code, failing test or link to a project that reproduces the behavior.
     Make sure you place a stack trace inside a code (```) block to avoid linking unrelated issues -->
[poc](https://drive.google.com/open?id=11dbKOgAj3s8BPTO4P4Xescp2-2HlQUBP)
magick convert $poc ./test.png
`=================================================================`
`==41625==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000b251 at pc 0x7f57acd61145 bp 0x7fff0c9e88f0 sp 0x7fff0c9e8098`
`READ of size 1 at 0x60200000b251 thread T0`
`    #0 0x7f57acd61144  (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x4b144)`
`    #1 0x435b4e in LocaleNCompare MagickCore/locale.c:1581`
`    #2 0x76bab9 in Magick_png_write_raw_profile coders/png.c:8183`
`    #3 0x77be6c in WriteOnePNGImage coders/png.c:11083`
`    #4 0x783ffa in WritePNGImage coders/png.c:12744`
`    #5 0x849036 in WriteImage MagickCore/constitute.c:1159`
`    #6 0x849d5b in WriteImages MagickCore/constitute.c:1376`
`    #7 0xbf16d0 in ConvertImageCommand MagickWand/convert.c:3305`
`    #8 0xcdf180 in MagickCommandGenesis MagickWand/mogrify.c:185`
`    #9 0x4100a1 in MagickMain utilities/magick.c:149`
`    #10 0x410282 in main utilities/magick.c:180`
`    #11 0x7f57a860f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)`
`    #12 0x40fbb8 in _start (/home/ImageMagick/utilities/magick+0x40fbb8)`
``
`0x60200000b251 is located 0 bytes to the right of 1-byte region [0x60200000b250,0x60200000b251)`
`allocated by thread T0 here:`
`    #0 0x7f57acdae602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)`
`    #1 0x44096a in AcquireMagickMemory MagickCore/memory.c:478`
`    #2 0x4409be in AcquireQuantumMemory MagickCore/memory.c:551`
`    #3 0x4c4165 in ConstantString MagickCore/string.c:713`
`    #4 0x49a6fb in CloneSplayTree MagickCore/splay-tree.c:372`
`    #5 0x487eab in CloneImageProfiles MagickCore/profile.c:190`
`    #6 0x418926 in CloneImage MagickCore/image.c:838`
`    #7 0x76d056 in WriteOnePNGImage coders/png.c:8528`
`    #8 0x783ffa in WritePNGImage coders/png.c:12744`
`    #9 0x849036 in WriteImage MagickCore/constitute.c:1159`
`    #10 0x849d5b in WriteImages MagickCore/constitute.c:1376`
`    #11 0xbf16d0 in ConvertImageCommand MagickWand/convert.c:3305`
`    #12 0xcdf180 in MagickCommandGenesis MagickWand/mogrify.c:185`
`    #13 0x4100a1 in MagickMain utilities/magick.c:149`
`    #14 0x410282 in main utilities/magick.c:180`
`    #15 0x7f57a860f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)`
``
`SUMMARY: AddressSanitizer: heap-buffer-overflow ??:0 ??`
`Shadow bytes around the buggy address:`
`  0x0c047fff95f0: fa fa 07 fa fa fa 00 01 fa fa 00 fa fa fa 04 fa`
`  0x0c047fff9600: fa fa 00 03 fa fa 00 fa fa fa 00 02 fa fa 06 fa`
`  0x0c047fff9610: fa fa 00 05 fa fa 05 fa fa fa 00 fa fa fa 01 fa`
`  0x0c047fff9620: fa fa 00 02 fa fa 00 04 fa fa 00 04 fa fa 00 04`
`  0x0c047fff9630: fa fa 03 fa fa fa 00 02 fa fa 07 fa fa fa 07 fa`
`=>0x0c047fff9640: fa fa 00 01 fa fa 04 fa fa fa[01]fa fa fa 00 04`
`  0x0c047fff9650: fa fa 04 fa fa fa 04 fa fa fa 00 02 fa fa 06 fa`
`  0x0c047fff9660: fa fa 04 fa fa fa 00 02 fa fa 06 fa fa fa 04 fa`
`  0x0c047fff9670: fa fa 00 02 fa fa 06 fa fa fa 04 fa fa fa 00 02`
`  0x0c047fff9680: fa fa 06 fa fa fa 04 fa fa fa 00 02 fa fa 00 03`
`  0x0c047fff9690: fa fa 06 fa fa fa 04 fa fa fa 00 02 fa fa 05 fa`
`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`
`  Heap right redzone:      fb`
`  Freed heap region:       fd`
`  Stack left redzone:      f1`
`  Stack mid redzone:       f2`
`  Stack right redzone:     f3`
`  Stack partial redzone:   f4`
`  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`
`==41625==ABORTING`
### System Configuration
<!-- Tell us about the environment where you are experiencing the bug -->

- ImageMagick version:
`Version: ImageMagick 7.0.8-43 Q16 x86_64 2019-04-29 https://imagemagick.org`
`Copyright: ? 1999-2019 ImageMagick Studio LLC`
`License: https://imagemagick.org/script/license.php`
`Features: Cipher DPC HDRI OpenMP(4.0) `
`Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr lzma openexr pangocairo png tiff wmf x xml zlib`
- Environment (Operating system, version and so on):
`Distributor ID:	Ubuntu`
`Description:	Ubuntu 16.04.1 LTS`
`Release:	16.04`
`Codename:	xenial`
- Additional information:

<!-- Thanks for reporting the issue to ImageMagick! -->



Comments:
Comment by urban-warrior on 2019-04-29 11:57:53+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 NicoleG25 on 2019-12-26 07:11:37+00:00:
Hi, It appears that MITRE is reporting 7.0.8-43 as vulnerable in their CVE : 
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-19949

where 7.0.8-43 is the fixed version you are reporting.
Is it a mistake from their end?
Thanks. :)



---

Comment by galycannon on 2019-12-26 12:39:58+00:00:
> 
> 
> Hi, It appears that MITRE is reporting 7.0.8-43 as vulnerable in their CVE :
> https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2019-19949
> 
> where 7.0.8-43 is the fixed version you are reporting.
> Is it a mistake from their end?
> Thanks. :)

For ImageMagick7
These versions bellow all be affected
7.0.7-23
7.0.7-24
7.0.7-25
7.0.7-26
7.0.7-27
7.0.7-28
7.0.7-29
7.0.7-30
7.0.7-31
7.0.7-32
7.0.7-33
7.0.7-34
7.0.7-35
7.0.7-36
7.0.7-37
7.0.7-38
7.0.7-39
7.0.8-0
7.0.8-1
7.0.8-2
7.0.8-3
7.0.8-4
7.0.8-5
7.0.8-6
7.0.8-7
7.0.8-8
7.0.8-9
7.0.8-10
7.0.8-11
7.0.8-12
7.0.8-13
7.0.8-14
7.0.8-15
7.0.8-16
7.0.8-17
7.0.8-18
7.0.8-19
7.0.8-20
7.0.8-21
7.0.8-22
7.0.8-23
7.0.8-24
7.0.8-25
7.0.8-26
7.0.8-27
7.0.8-28
7.0.8-29
7.0.8-30
7.0.8-31
7.0.8-32
7.0.8-33
7.0.8-34
7.0.8-35
7.0.8-36
7.0.8-37
7.0.8-38
7.0.8-39
7.0.8-40
7.0.8-41
7.0.8-42
and fixed in release version 7.0.8-43

For ImageMagick6
These versions bellow all be affected
6.9.9-33
6.9.9-34
6.9.9-35
6.9.9-36
6.9.9-37
6.9.9-38
6.9.9-39
6.9.9-40
6.9.9-41
6.9.9-42
6.9.9-43
6.9.9-44
6.9.9-45
6.9.9-46
6.9.9-47
6.9.9-48
6.9.9-49
6.9.9-50
6.9.9-51
6.9.10-0
6.9.10-1
6.9.10-2
6.9.10-3
6.9.10-4
6.9.10-5
6.9.10-6
6.9.10-7
6.9.10-8
6.9.10-9
6.9.10-10
6.9.10-11
6.9.10-12
6.9.10-13
6.9.10-14
6.9.10-15
6.9.10-16
6.9.10-17
6.9.10-18
6.9.10-19
6.9.10-20
6.9.10-21
6.9.10-22
6.9.10-23
6.9.10-24
6.9.10-25
6.9.10-26
6.9.10-27
6.9.10-28
6.9.10-29
6.9.10-30
6.9.10-31
6.9.10-32
6.9.10-33
6.9.10-34
6.9.10-35
6.9.10-36
6.9.10-37
6.9.10-38
6.9.10-39
6.9.10-40
6.9.10-41
6.9.10-42
and fixed in release version 6.9.10-43

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

Additional files: