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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/1612
## Description:
Issue: ImageMagick/ImageMagick#1612
Title: stack-buffer-overflow at coders/pnm.c:1902 in WritePNMImage
State: closed
Created by: SuhwanSong
Created at: 2019-06-21 06:13:39+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 stack-buffer-overflow at coders/pnm.c:1902 in WritePNMImage.
I think this is related with [#1540](https://github.com/ImageMagick/ImageMagick/issues/1540), but it was heap and this issue is about the stack buffer overflow.

### 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 -dispose Previous -compress None "(" magick:rose +repage ")" "(" magick:logo -level 64,0%,0.874  ")" -loop 5   tmp`

Here's ASAN log.
```
==12131==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc7100b060 at pc 0x0000004477c4 bp 0x7ffc710065b0 sp 0x7ffc71005d60
WRITE of size 10 at 0x7ffc7100b060 thread T0
    #0 0x4477c3 in strncpy (install/bin/magick+0x4477c3)
    #1 0x7f1cca22b433 in WritePNMImage coders/pnm.c:1902:20
    #2 0x7f1cc9ac3065 in WriteImage MagickCore/constitute.c:1159:16
    #3 0x7f1cc9ac3f8c in WriteImages MagickCore/constitute.c:1376:13
    #4 0x7f1cc948750d in CLINoImageOperator MagickWand/operation.c:4796:14
    #5 0x7f1cc948b1cc in CLIOption MagickWand/operation.c:5258:7
    #6 0x7f1cc92ccf6d in ProcessCommandOptions MagickWand/magick-cli.c:529:3
    #7 0x7f1cc92cdd0a in MagickImageCommand MagickWand/magick-cli.c:796:5
    #8 0x7f1cc9317ba1 in MagickCommandGenesis MagickWand/mogrify.c:185:14
    #9 0x526f95 in MagickMain utilities/magick.c:149:10
    #10 0x5268e1 in main utilities/magick.c:180:10
    #11 0x7f1cc3d8eb96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #12 0x41b069 in _start (install/bin/magick+0x41b069)

Address 0x7ffc7100b060 is located in stack of thread T0 at offset 19104 in frame
    #0 0x7f1cca228daf in WritePNMImage coders/pnm.c:1522

  This frame has 6 object(s):
    [32, 4128) 'buffer' (line 1523)
    [4256, 8352) 'magick' (line 1523)
    [8480, 12576) 'type117' (line 1675)
    [12704, 14752) 'pixels' (line 1736)
    [14880, 16928) 'pixels255' (line 1788)
    [17056, 19104) 'pixels381' (line 1857) <== Memory access at offset 19104 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow (install/bin/magick+0x4477c3) in strncpy

```

### 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-21 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 00:17:58+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:26+00:00:
This was assigned CVE-2019-13306.

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

Additional files: