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

================= Bug Report (1/2) ==================
## Source: GitHub Issue
## URL: https://github.com/ImageMagick/ImageMagick/issues/4729
## Description:
Issue: ImageMagick/ImageMagick#4729
Title: Heap Buffer Overflow 
State: closed
Created by: R0fM1a
Created at: 2022-01-19 07:07:49+00:00

Issue Body:
### ImageMagick version

7.1.0-20

### Operating system

Linux

### Operating system, version and so on

Linux ubuntu 5.4.0-73-generic #82~18.04.1-Ubuntu SMP Fri Apr 16 15:10:02 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

### Description

Hi, ImageMagick security team
This is ZhangJiaxing (@r0fm1a) from Codesafe Team of Legendsec at Qi'anxin Group.
I've found a Heap Buffer Overflow vulnerability in ImageMagick 7.1.0-20.(github commit  ID f54aa4e7ba8a8fb82d200844dc59804f5f6c8cbf in Tue Jan 18 20:00:38 2022 -0500).When someone uses magick to convert a tiff-format image into a picon-format file, the bug will be traggered on.
Please feel free to contact me.
Regards,
ZhangJiaxing

### Steps to Reproduce

1. git clone ImageMagick
2. ./configure CC=gcc CFLAGS="-g -fsanitize=address" && make
3. cd utilities &&./magick convert /path/to/poc.tiff output.picon
4. The Asan logs are as follows:
==46632==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62a00000b540 at pc 0x7f7e88ca3257 bp 0x7fffdb9f7370 sp 0x7fffdb9f7360
READ of size 4 at 0x62a00000b540 thread T0
    #0 0x7f7e88ca3256 in GetPixelAlpha MagickCore/pixel-accessor.h:59
    #1 0x7f7e88ca763e in WritePICONImage coders/xpm.c:807
    #2 0x7f7e885f73ef in WriteImage MagickCore/constitute.c:1221
    #3 0x7f7e885f84a0 in WriteImages MagickCore/constitute.c:1442
    #4 0x7f7e87e5239f in ConvertImageCommand MagickWand/convert.c:3332
    #5 0x7f7e87f604cf in MagickCommandGenesis MagickWand/mogrify.c:188
    #6 0x55a7a3ebefcf in MagickMain utilities/magick.c:150
    #7 0x55a7a3ebf25a in main utilities/magick.c:182
    #8 0x7f7e876c2bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
    #9 0x55a7a3ebe9e9 in _start (/home/r0fm1a/ImageMagick/utilities/.libs/magick+0x19e9)

0x62a00000b540 is located 0 bytes to the right of 21312-byte region [0x62a000006200,0x62a00000b540)
allocated by thread T0 here:
    #0 0x7f7e893e3790 in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdf790)
    #1 0x7f7e887d1c99 in AcquireAlignedMemory_POSIX MagickCore/memory.c:299
    #2 0x7f7e887d1ea8 in AcquireAlignedMemory MagickCore/memory.c:377
    #3 0x7f7e88582e0e in OpenPixelCache MagickCore/cache.c:3746
    #4 0x7f7e8857b296 in GetImagePixelCache MagickCore/cache.c:1776
    #5 0x7f7e8858b2de in SyncImagePixelCache MagickCore/cache.c:5516
    #6 0x7f7e88798568 in SetImageStorageClass MagickCore/image.c:2626
    #7 0x7f7e885ab718 in AcquireImageColormap MagickCore/colormap.c:152
    #8 0x7f7e888731cd in SetGrayscaleImage MagickCore/quantize.c:3772
    #9 0x7f7e888714e7 in QuantizeImage MagickCore/quantize.c:3118
    #10 0x7f7e88866f5d in CompressImageColormap MagickCore/quantize.c:1204
    #11 0x7f7e88ca6f6a in WritePICONImage coders/xpm.c:755
    #12 0x7f7e885f73ef in WriteImage MagickCore/constitute.c:1221
    #13 0x7f7e885f84a0 in WriteImages MagickCore/constitute.c:1442
    #14 0x7f7e87e5239f in ConvertImageCommand MagickWand/convert.c:3332
    #15 0x7f7e87f604cf in MagickCommandGenesis MagickWand/mogrify.c:188
    #16 0x55a7a3ebefcf in MagickMain utilities/magick.c:150
    #17 0x55a7a3ebf25a in main utilities/magick.c:182
    #18 0x7f7e876c2bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)

SUMMARY: AddressSanitizer: heap-buffer-overflow MagickCore/pixel-accessor.h:59 in GetPixelAlpha
Shadow bytes around the buggy address:
  0x0c547fff9650: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c547fff9660: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c547fff9670: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c547fff9680: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c547fff9690: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c547fff96a0: 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa
  0x0c547fff96b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c547fff96c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c547fff96d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c547fff96e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c547fff96f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 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
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  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
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==46632==ABORTING


### Images

[magick_heap_bof.zip](https://github.com/ImageMagick/ImageMagick/files/7894812/magick_heap_bof.zip)



Comments:
Comment by urban-warrior on 2022-01-19 21:26:32+00:00:
Thanks for the problem report.  We can reproduce it and will have a patch to fix it in the GIT main branch @ https://github.com/ImageMagick/ImageMagick later today.  The patch will be available in the beta releases of ImageMagick @ https://imagemagick.org/download/beta/ by sometime tomorrow. 


---

Comment by R0fM1a on 2022-01-24 03:15:42+00:00:
Do you guys have any trouble reproducing the vulnerability?

---

Comment by urban-warrior on 2022-01-24 11:28:29+00:00:
Why do you ask?  In our reply we say "We can reproduce it and will have a patch..."

---

Comment by R0fM1a on 2022-01-24 11:40:46+00:00:
Emmm… Sorry，I misunderstood your reply as "I will reproduce it and patch…”. So embarrassing😨

Attachments:
https://github.com/ImageMagick/ImageMagick/files/7894812/magick_heap_bof.zip

Commit References:
f54aa4e7ba8a8fb82d200844dc59804f5f6c8cbf

================= Bug Report (2/2) ==================
## Source: Red Hat Bugzilla
## URL: https://bugzilla.redhat.com/show_bug.cgi?id=2045943
## Description:
Bugzilla ID: 2045943
Title: CVE-2022-0284 ImageMagick: Heap buffer overread in GetPixelAlpha() declared in MagickCore/pixel-accessor.h

Description
Todd Cullum 2022-01-26 02:24:21 UTC
There's a heap buffer overread that was discovered in ImageMagick version 7.1.0-20 in GetPixelAlpha() declared in MagickCore/pixel-accessor.h. A specially crafted file could trigger this and potentially cause a denial of service or information leak.

Reference: https://github.com/ImageMagick/ImageMagick/issues/4729
Upstream patch commit: https://github.com/ImageMagick/ImageMagick/commit/e50f19fd73c792ebe912df8ab83aa51a243a3da7


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 groff libtool pkg-config cmake nasm autopoint po4a libc6:i386 libc6-dev:i386
RUN apt-get install -y make autoconf automake groff libtool pkg-config cmake nasm autopoint po4a libc6:i386 libc6-dev:i386
RUN git clone https://github.com/imagemagick/imagemagick imagemagick
RUN git -C imagemagick checkout 40fd5dbf305e4d7b8ee268bb6f715e27843a0d2f
WORKDIR $SRC/imagemagick
COPY build.sh $SRC/

Additional files: