heap-buffer-overflow in function CloneImage of magick/image.c
Swiss army knife of image processing
Brought to you by:
bfriesen
There is a heap buffer overflow in function CloneImage of image.c whick can be reproduced as below.
test@test-virtual-machine:~/graphicsmagick$ ./utilities/gm convert heap_buffer_overflow_in_ReadMNGImage /dev/null
=================================================================
==25178==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000e994 at pc 0x7f4bf9c01935 bp 0x7ffcacc1cde0 sp 0x7ffcacc1c588
READ of size 8 at 0x60200000e994 thread T0
#0 0x7f4bf9c01934 in __asan_memcpy (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x8c934)
#1 0x4c56b9 in CloneImage magick/image.c:1149
#2 0x46f632 in CompositeImage magick/composite.c:3221
#3 0x57acf9 in CoalesceImages magick/transform.c:437
#4 0x671103 in ReadMNGImage coders/png.c:6290
#5 0x47766b in ReadImage magick/constitute.c:1607
#6 0x4214e9 in ConvertImageCommand magick/command.c:4362
#7 0x436a5e in MagickCommand magick/command.c:8886
#8 0x45f205 in GMCommandSingle magick/command.c:17416
#9 0x45f451 in GMCommand magick/command.c:17469
#10 0x40cbc5 in main utilities/gm.c:61
#11 0x7f4bf6e8182f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#12 0x40cad8 in _start (/home/.test/graphicsmagick/utilities/gm+0x40cad8)
0x60200000e994 is located 0 bytes to the right of 4-byte region [0x60200000e990,0x60200000e994)
allocated by thread T0 here:
#0 0x7f4bf9c0d602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
#1 0x4de2d4 in MagickMalloc magick/memory.c:173
#2 0x8c84ea in AllocateImageColormap magick/colormap.c:76
#3 0x659c6b in ReadOnePNGImage coders/png.c:2219
#4 0x66c40b in ReadMNGImage coders/png.c:5600
#5 0x47766b in ReadImage magick/constitute.c:1607
#6 0x4214e9 in ConvertImageCommand magick/command.c:4362
#7 0x436a5e in MagickCommand magick/command.c:8886
#8 0x45f205 in GMCommandSingle magick/command.c:17416
#9 0x45f451 in GMCommand magick/command.c:17469
#10 0x40cbc5 in main utilities/gm.c:61
#11 0x7f4bf6e8182f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
SUMMARY: AddressSanitizer: heap-buffer-overflow ??:0 __asan_memcpy
Shadow bytes around the buggy address:
0x0c047fff9ce0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff9cf0: fa fa fa fa fa fa fa fa fa fa 00 fa fa fa 00 00
0x0c047fff9d00: fa fa 02 fa fa fa 02 fa fa fa 00 fa fa fa 00 00
0x0c047fff9d10: fa fa 02 fa fa fa 02 fa fa fa 00 fa fa fa 00 00
0x0c047fff9d20: fa fa 05 fa fa fa fd fd fa fa fd fa fa fa fd fa
=>0x0c047fff9d30: fa fa[04]fa fa fa fd fd fa fa 02 fa fa fa 02 fa
0x0c047fff9d40: fa fa fd fa fa fa fd fa fa fa fd fa fa fa 00 00
0x0c047fff9d50: fa fa fd fd fa fa 05 fa fa fa fd fd fa fa fd fa
0x0c047fff9d60: fa fa fd fa fa fa fd fd fa fa 02 fa fa fa 02 fa
0x0c047fff9d70: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fd
0x0c047fff9d80: fa fa fd fd fa fa fd fa fa fa fd fd fa fa 00 00
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
Heap right redzone: fb
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack partial redzone: f4
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
==25178==ABORTING
System Configuration:
Distributor ID: Ubuntu
Description: Ubuntu 16.04.2 LTS
Release: 16.04
Codename: xenial
GraphicsMagick version:
GraphicsMagick 1.4 snapshot-20190322 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2019 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Feature Support:
Native Thread Safe yes
Large Files (> 32 bit) yes
Large Memory (> 32 bit) yes
BZIP yes
DPS no
FlashPix no
FreeType yes
Ghostscript (Library) no
JBIG yes
JPEG-2000 yes
JPEG yes
Little CMS yes
Loadable Modules no
Solaris mtmalloc no
OpenMP yes (201307)
PNG yes
TIFF yes
TRIO no
Solaris umem no
WebP yes
WMF yes
X11 yes
XML yes
ZLIB yes
Host type: x86_64-pc-linux-gnu
Configured using the command:
./configure 'CFLAGS=-g -fsanitize=address' '--enable-shared=no'
Final Build Parameters:
CC = gcc
CFLAGS = -fopenmp -g -fsanitize=address -Wall -pthread
CPPFLAGS = -I/usr/include/freetype2 -I/usr/include/libxml2
CXX = g++
CXXFLAGS = -pthread
LDFLAGS =
LIBS = -ljbig -lwebp -lwebpmux -llcms2 -ltiff -lfreetype -ljasper -ljpeg -lpng12 -lwmflite -lXext -lSM -lICE -lX11 -llzma -lbz2 -lxml2 -lz -lm -lpthread
This problem is fixed by changesets 15948:40fc71472b98 and 15949:86a9295e7c83. Thank you very much for the report!