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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1615
## Description:
Issue: ImageMagick/ImageMagick#1615
Title: heap-buffer-overflow at MagickCore/statistic.c:559:43 in EvaluateImages
State: closed
Created by: SuhwanSong
Created at: 2019-06-22 02:11:22+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's a heap-buffer-overflow at MagickCore/statistic.c:559:43 in EvaluateImages.

### 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 -->
run_cmd:
`magick -seed 0 -monitor -bias 63 "(" magick:rose -colorize 172,35,77  ")" "(" magick:logo +repage ")" -crop 507x10'!'+20-54 -evaluate-sequence Median   tmp`


Here's ASAN log.
```
=================================================================
==10817==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x611000000c80 at pc 0x7f0648490e06 bp 0x7ffce3c96900 sp 0x7ffce3c968f8
WRITE of size 8 at 0x611000000c80 thread T0
    #0 0x7f0648490e05 in EvaluateImages MagickCore/statistic.c:559:43
    #1 0x7f0647aa55bf in CLIListOperatorImages MagickWand/operation.c:4084:22
    #2 0x7f0647aaf35e in CLIOption MagickWand/operation.c:5279:14
    #3 0x7f06478f0a99 in ProcessCommandOptions MagickWand/magick-cli.c:477:7
    #4 0x7f06478f1d0a in MagickImageCommand MagickWand/magick-cli.c:796:5
    #5 0x7f064793bba1 in MagickCommandGenesis MagickWand/mogrify.c:185:14
    #6 0x526f95 in MagickMain utilities/magick.c:149:10
    #7 0x5268e1 in main utilities/magick.c:180:10
    #8 0x7f06423b2b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x41b069 in _start (install/bin/magick+0x41b069)

0x611000000c80 is located 0 bytes to the right of 256-byte region [0x611000000b80,0x611000000c80)
allocated by thread T0 here:
    #0 0x4e5397 in __interceptor_malloc (install/bin/magick+0x4e5397)
    #1 0x7f064832b0b6 in AcquireMagickMemory MagickCore/memory.c:478:10
    #2 0x7f064832b11f in AcquireQuantumMemory MagickCore/memory.c:551:10
    #3 0x7f06484926e7 in AcquirePixelThreadSet MagickCore/statistic.c:182:33
    #4 0x7f0648490b21 in EvaluateImages MagickCore/statistic.c:499:19
    #5 0x7f0647aa55bf in CLIListOperatorImages MagickWand/operation.c:4084:22
    #6 0x7f0647aaf35e in CLIOption MagickWand/operation.c:5279:14
    #7 0x7f06478f0a99 in ProcessCommandOptions MagickWand/magick-cli.c:477:7
    #8 0x7f06478f1d0a in MagickImageCommand MagickWand/magick-cli.c:796:5
    #9 0x7f064793bba1 in MagickCommandGenesis MagickWand/mogrify.c:185:14
    #10 0x526f95 in MagickMain utilities/magick.c:149:10
    #11 0x5268e1 in main utilities/magick.c:180:10
    #12 0x7f06423b2b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

SUMMARY: AddressSanitizer: heap-buffer-overflow MagickCore/statistic.c:559:43 in EvaluateImages

```


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

- ImageMagick version:
Version: ImageMagick 7.0.8-50 Q16 x86_64 2019-06-22 https://imagemagick.org

- Environment (Operating system, version and so on):
Description:	Ubuntu 18.04.1 LTS
Release:	18.04
Codename: bionic

- Additional information:
CC=clang-7 CXX=clang++-7 ./configure --disable-openmp

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



Comments:
Comment by urban-warrior on 2019-06-22 12:24: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 @ http://www.imagemagick.org/download/beta/ by sometime tomorrow.

---

Comment by nohmask on 2019-07-08 01:52:45+00:00:
This was assigned CVE-2019-13307.

---

Comment by hlef on 2019-08-09 07:26:25+00:00:
It is worth mentioning that https://github.com/ImageMagick/ImageMagick6/commit/91e58d967a92250439ede038ccfb0913a81e59fe introduces a memory leak which is fixed later in https://github.com/ImageMagick/ImageMagick6/commit/e6d26d4e2f07375ddbf46a857d309d51eeff7ee1.

Also @urban-warrior, I'm not really sure to understand the logic behind https://github.com/ImageMagick/ImageMagick6/commit/643921ca69a20b203faebd0b287d8b7012dc749d.

What if `images->columns` is bigger than `GetImageListLength(images)` and bigger than all other `next->columns`? After https://github.com/ImageMagick/ImageMagick6/commit/643921ca69a20b203faebd0b287d8b7012dc749d, `images->columns` is basically ignored, right?

Then there might be a mismatch between the result of `AcquireImageCanvas` (`image`) and the result of `AcquirePixelThreadSet` (`evaluate_pixels`), which recreates this issue.

---

Comment by urban-warrior on 2019-08-09 20:12:13+00:00:
We check for the maximum columns in the image sequence.  `Next` is aliased to `images` so that `images->columns` is checked suggesting `image->columns` is not ignored.  Can you post a use case that we can reproduce that shows the memory corruption still persists?

---

Comment by hlef on 2019-08-10 06:20:53+00:00:
Thanks for your answer. Indeed, this patch does not recreate the issue, I was confused.

so, I suppose that `GetImageListLength(images)` is also considered here in order to avoid out of bounds write at https://github.com/ImageMagick/ImageMagick6/blob/master/magick/statistic.c#L585

---

Comment by urban-warrior on 2019-08-10 11:30:08+00:00:
Yes we use the thread set for two different use cases.  Certainly as a convenience.  Best coding practices would encourage the use of two different allocations, sized to fit, for each of these use cases.

Commit References:
643921ca69a20b203faebd0b287d8b7012dc749d
91e58d967a92250439ede038ccfb0913a81e59fe
e6d26d4e2f07375ddbf46a857d309d51eeff7ee1

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

Additional files: