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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1629
## Description:
Issue: ImageMagick/ImageMagick#1629
Title: Division by Zero at MagickCore/layer.c:1616
State: closed
Created by: SuhwanSong
Created at: 2019-07-07 02:34:55+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
<!-- A description of the bug or feature -->
There's a division by zero at MagickCore/layer.c:1616:30

### 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 following cmd:
`magick "-seed" "0" "-delay" "34<" "(" "magick:rose" "+repage" ")" "(" "magick:rose" "+repage" ")" "-encoding" "Symbol" "-layers" "remove-dups" "-quiet" "tmp"`
 
This is triggered at `time = curr->delay*1000/curr->ticks_per_second;` due to curr->ticks_per_second. 
```
1605   for (; (next=GetNextImageInList(curr)) != (Image *) NULL; curr=next)
1606   { 
1607     if ( curr->columns != next->columns || curr->rows != next->rows
1608          || curr->page.x != next->page.x || curr->page.y != next->page.y )
1609       continue;
1610     bounds=CompareImagesBounds(curr,next,CompareAnyLayer,exception);
1611     if ( bounds.x < 0 ) {
1612       /*
1613         the two images are the same, merge time delays and delete one.
1614       */
1615       size_t time;
1616       time = curr->delay*1000/curr->ticks_per_second;
1617       time += next->delay*1000/next->ticks_per_second;
1618       next->ticks_per_second = 100L;
1619       next->delay = time*curr->ticks_per_second/1000;
1620       next->iterations = curr->iterations;
1621       *images = curr;
1622       (void) DeleteImageFromList(images);
1623     }
1624   }

```

Here's USAN log.
```
MagickCore/layer.c:1616:30: runtime error: division by zero
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior MagickCore/layer.c:1616:30 in 
AddressSanitizer:DEADLYSIGNAL
=================================================================
==28916==ERROR: AddressSanitizer: UNKNOWN SIGNAL on unknown address 0x000000000000 (pc 0x7fe11baee9e7 bp 0x7ffc60e12250 sp 0x7ffc60e11ec0 T0)
    #0 0x7fe11baee9e6 in RemoveDuplicateLayers MagickCore/layer.c:1616:30
    #1 0x7fe11a2600a8 in CLIListOperatorImages MagickWand/operation.c:4266:15
    #2 0x7fe11a273102 in CLIOption MagickWand/operation.c:5308:14
    #3 0x7fe119c6aef4 in ProcessCommandOptions MagickWand/magick-cli.c:477:7
    #4 0x7fe119c6dc54 in MagickImageCommand MagickWand/magick-cli.c:796:5
    #5 0x7fe119d1400e in MagickCommandGenesis MagickWand/mogrify.c:185:14
    #6 0x527976 in MagickMain utilities/magick.c:149:10
    #7 0x5268e1 in main utilities/magick.c:180:10
    #8 0x7fe113884b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
    #9 0x41b069 in _start (install/bin/magick+0x41b069)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: UNKNOWN SIGNAL MagickCore/layer.c:1616:30 in RemoveDuplicateLayers
==28916==ABORTING

```

### System Configuration
<!-- Tell us about the environment where you are experiencing the bug -->
- ImageMagick version:
Version: ImageMagick 7.0.8-54 Q16 x86_64 2019-07-07 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 CFLAGS="-fsanitize=address,undefined -g" CXXFLAGS="-fsanitize=address,undefined -g" ./configure --disable-openmp --without-png

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



Comments:
Comment by urban-warrior on 2019-07-08 10:23:32+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-10 01:44:57+00:00:
This was assigned CVE-2019-13454.

---

Comment by boo0m on 2019-07-31 06:07:25+00:00:
> This was assigned CVE-2019-13454.

hello, nohmask. I have requested two CVE IDs about the ImageMagick vulnerabilities(https://github.com/ImageMagick/ImageMagick/issues/1552  and https://github.com/ImageMagick/ImageMagick6/issues/43) on https://cveform.mitre.org/. But I don't receive any reply yet. Do you know what happen? Thanks


---

Comment by nohmask on 2019-08-18 16:37:24+00:00:
> hello, nohmask. I have requested two CVE IDs about the ImageMagick vulnerabilities(#1552 and [ImageMagick/ImageMagick6#43](https://github.com/ImageMagick/ImageMagick6/issues/43)) on https://cveform.mitre.org/. But I don't receive any reply yet. Do you know what happen? Thanks

I’m sorry I don’t know.
The two problems are assigned [CVE-2019-14980](https://github.com/ImageMagick/ImageMagick6/issues/43) and [CVE-2019-14981](https://github.com/ImageMagick/ImageMagick/issues/1552) .

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

Additional files: