On GraphicsMagick 1.3.26 2017-07-04 Q8
A heap use after free vulnerability was found in function CloseBlob ,which allow attackers to cause a denial of service via a crafted file.
#./gm identify $FILE
=================================================================
==7551==ERROR: heap-use-after-free on address 0x6230000053c0 at pc 0x000000efab0f bp 0x7fffc3bfdd70 sp 0x7fffc3bfdd68
READ of size 8 at 0x6230000053c0 thread T0
#0 0xefab0e in CloseBlob /home/test/Downloads/GraphicsMagick-1.3.26/magick/blob.c:859:3
#1 0xa7ada3 in ReadMNGImage /home/test/Downloads/GraphicsMagick-1.3.26/coders/png.c:5144:11
#2 0x63f90d in ReadImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1607:13
#3 0x63ed64 in PingImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1370:9
#4 0x5ab160 in IdentifyImageCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:8379:17
#5 0x5b0232 in MagickCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:8869:17
#6 0x5f621e in GMCommandSingle /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17396:10
#7 0x5f4aab in GMCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17449:16
#8 0x7f14d2329b34 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:274
#9 0x4247fb in _start (/home/test/Downloads/IM-GM-build/bin/gm+0x4247fb)
0x6230000053c0 is located 6848 bytes inside of 6856-byte region [0x623000003900,0x6230000053c8)
freed by thread T0 here:
#0 0x4e93b0 in free /home/test/Downloads/llvm-clang/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:47
#1 0xa7ad9b in ReadMNGImage /home/test/Downloads/GraphicsMagick-1.3.26/coders/png.c:5143:11
#2 0x63f90d in ReadImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1607:13
#3 0x63ed64 in PingImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1370:9
#4 0x5b0232 in MagickCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:8869:17
#5 0x5f621e in GMCommandSingle /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17396:10
#6 0x5f4aab in GMCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17449:16
#7 0x7f14d2329b34 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:274
previously allocated by thread T0 here:
#0 0x4e96f6 in __interceptor_malloc /home/test/Downloads/llvm-clang/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:66
#1 0x6dca7f in AllocateImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/image.c:336:18
#2 0xa5ad02 in ReadMNGImage /home/test/Downloads/GraphicsMagick-1.3.26/coders/png.c:3872:9
#3 0x63f90d in ReadImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1607:13
#4 0x63ed64 in PingImage /home/test/Downloads/GraphicsMagick-1.3.26/magick/constitute.c:1370:9
#5 0x5b0232 in MagickCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:8869:17
#6 0x5f621e in GMCommandSingle /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17396:10
#7 0x5f4aab in GMCommand /home/test/Downloads/GraphicsMagick-1.3.26/magick/command.c:17449:16
#8 0x7f14d2329b34 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:274
heap-use-after-free /home/test/Downloads/GraphicsMagick-1.3.26/magick/blob.c:859:3 in CloseBlob
Shadow bytes around the buggy address:
0x0c467fff8a20: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x0c467fff8a30: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x0c467fff8a40: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x0c467fff8a50: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x0c467fff8a60: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x0c467fff8a70: fd fd fd fd fd fd fd fd[fd]fa fa fa fa fa fa fa
0x0c467fff8a80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c467fff8a90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c467fff8aa0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c467fff8ab0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c467fff8ac0: 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
The poc file is in the attachment.
Credit: ADLab of Venustech
I am able to reproduce this issue. Assigned to Glenn to look at.
This issue was already reported by me on 2017-07-10 and you fixed it here:
http://hg.code.sf.net/p/graphicsmagick/code/rev/d0a76868ca37
Reference: https://blogs.gentoo.org/ago/2017/07/12/graphicsmagick-use-after-free-in-closeblob-blob-c/
Testing shows that Mercurial changeset 15138:98721124e51f resolves this problem.