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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1177
## Description:
Issue: ImageMagick/ImageMagick#1177
Title: out of bounds write in bmp.c
State: closed
Created by: zer0min9
Created at: 2018-06-19 13:39:47+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
```
$ ./magick -version
Version: ImageMagick 7.0.8-3 Q16 x86_64 2018-06-19 https://www.imagemagick.org
Copyright: ? 1999-2018 ImageMagick Studio LLC
License: https://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig fpx freetype heic jbig jng jp2 jpeg lcms lqr lzma pangocairo png raw tiff webp wmf x xml zlib
```

### Steps to Reproduce
* In coders/bmp.c
```
1845     pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1846       sizeof(*pixels));
1847     if (pixel_info == (MemoryInfo *) NULL)
1848       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1849     pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1850     (void) memset(pixels,0,(size_t) bmp_info.image_size);
                                       .
                                       .
                                       .
2058           q=pixels+(image->rows-y-1)*bytes_per_line;
2059           for (x=0; x < (ssize_t) image->columns; x++)
2060           {
2061             *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
```
(image->rows-y-1)*bytes_per_line may be much larger than bmp_info.image_size,it will cause an out of bounds write bug in line 2061 of coders/bmp.c

To reproduce this problem:
```
$ ./magick convert ./poc output.bmp
ASAN:DEADLYSIGNAL
=================================================================
==67082==ERROR: AddressSanitizer: SEGV on unknown address 0x7f6c2daff800 (pc 0x7f6b3cfcc33b bp 0x7ffd57579e40 sp 0x7ffd57579c20 T0)
==67082==The signal is caused by a WRITE memory access.
    #0 0x7f6b3cfcc33a in WriteBMPImage coders/bmp.c:2061
    #1 0x7f6b3cbb2a40 in WriteImage MagickCore/constitute.c:1124
    #2 0x7f6b3cbb371f in WriteImages MagickCore/constitute.c:1338
    #3 0x7f6b3c3d1ba9 in ConvertImageCommand MagickWand/convert.c:3280
    #4 0x7f6b3c4d61db in MagickCommandGenesis MagickWand/mogrify.c:183
    #5 0x5610a1e0cec0 in MagickMain utilities/magick.c:149
    #6 0x5610a1e0d146 in main utilities/magick.c:180
    #7 0x7f6b3bc34b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #8 0x5610a1e0c939 in _start (/home/zm/workspace/ImageMagick/utilities/.libs/magick+0x1939)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV coders/bmp.c:2061 in WriteBMPImage
==67082==ABORTING
```
Debugging information：
```
Breakpoint 1, WriteBMPImage (image_info=0x627000042900, image=0x627000026900, 
    exception=0x60600000ddc0) at coders/bmp.c:2058
2058	          q=pixels+(image->rows-y-1)*bytes_per_line;
(gdb) p bmp_info.image_size
$1 = 433805204
(gdb) p (image->rows-y-1)*bytes_per_line
$2 = 4728576000
(gdb)
```
### POC
[poc.zip](https://github.com/ImageMagick/ImageMagick/files/2115374/poc.zip)
### System Configuration
- ImageMagick version:ImageMagick 7.0.8-3 Q16 x86_64
- Environment (Operating system, version and so on):ubuntu 18
- Additional information:
Credit:
Zongming Wang from Chengdu Security Response Center of Qihoo 360 Technology Co. Ltd.



Comments:
Comment by urban-warrior on 2018-06-20 00:33:02+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 2018-06-21 01:38:48+00:00:
This was assigned CVE-2018-12599.

Attachments:
https://github.com/ImageMagick/ImageMagick/files/2115374/poc.zip

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

Additional files: