gm: heap-buffer-overflow in ReadTIFFImage (src/coders/tiff.c)
Swiss army knife of image processing
Brought to you by:
bfriesen
**
I am not able to reproduce this issue. What version of libtiff and libjpeg are being used? Is this a 64-bit or 32-bit build? Please post the full output from 'gm -version'.
This issue could easily be due to a bug already fixed in libtiff or related to a specific libjpeg distribution/version. I do see libtiff making two security-related corrections to the TIFF parsing. I also see that the file uses old JPEG compression TIFF format.
Thanks for your work.
It is a 64-bt build with address sanitizer enables (CFLAGS=-fsanitize=address).
I have git pull the latest commit of libtiff and libjpeg and place the code in tiff and jpeg folders of graphicmagick source code. After complication, I can still reproduce this issue.
Did you complie with address sanitizer enabled (i.e., CFLAGS=-fsanitize=address)?
You said that you saw libtiff making two security-related corrections to the TIFF parsing. Could you please send me the references URL to these two issues. I will check whehter my issue is different from these two. Thank you .
The output of ”gm -version" is like below:
GraphicsMagick 1.4 snapshot-20180107 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2018 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 no
JPEG-2000 yes
JPEG yes
Little CMS no
Loadable Modules no
OpenMP yes (201307)
PNG yes
TIFF yes
TRIO no
UMEM no
WebP yes
WMF yes
X11 yes
XML yes
ZLIB yes
Host type: x86_64-unknown-linux-gnu
Configured using the command:
/u/ProbeFuzzer/ProbeFuzzer/product/graphicsmagick/master/src/configure '--prefix=/u/ProbeFuzzer/ProbeFuzzer/product/graphicsmagick/master/exe_asan' 'CC=/u/ProbeFuzzer/ProbeFuzzer/afl/afl-gcc' 'CFLAGS=-fsanitize=address' 'LDFLAGS=-fsanitize=address' 'CXX=/u/ProbeFuzzer/ProbeFuzzer/afl/afl-g++' 'CXXFLAGS=-fsanitize=address'
Final Build Parameters:
CC = /u/ProbeFuzzer/ProbeFuzzer/afl/afl-gcc
CFLAGS = -fopenmp -fsanitize=address -Wall
CPPFLAGS = -I/usr/include/freetype2 -I/usr/include/libxml2
CXX = /u/ProbeFuzzer/ProbeFuzzer/afl/afl-g++
CXXFLAGS = -fsanitize=address
LDFLAGS = -fsanitize=address
LIBS = -lwebp -lwebpmux -ltiff -lfreetype -ljasper -ljpeg -lpng12 -lwmflite -lXext -lSM -lICE -lX11 -llzma -lbz2 -lxml2 -lz -lm -lgomp -lpthread
On Fri, 12 Jan 2018, Probe Fuzzer wrote:
You need to be clear about what is meant by "libjpeg" since there are
two major "libjpeg" projects now (as well as two other splinter
projects). I am using IJG JPEG (IJG JPEG 80), which historically has
fewer anomalies than TurboJPEG. One reason why I am using IJG JPEG is
so that the GraphicsMagick test suite could pass with valgrind and
ASAN.
I tested with ASAN and valgrind.
If you add '-debug coder' to the options, you will then see many
warnings from libtiff, many of which say that it made a correction:
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Invalid TIFF directory; tags are not sorted in
ascending order.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Unknown field with tag 32781 (0x800d) encountered.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Unknown field with tag 384 (0x180) encountered.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Unknown field with tag 1093 (0x445) encountered.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Unknown field with tag 2 (0x2) encountered.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: ASCII value for tag "Tag 32781" does not end in null
byte. Forcing it to be null.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Incorrect count for "JpegProc"; tag ignored.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: Photometric tag value assumed incorrect, assuming data
is YCbCr instead of RGB.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: SamplesPerPixel tag is missing, applying correct
SamplesPerPixel value of 3.
08:01:27 0:01 0.000u 15498 tiff.c/unknown/1007/Coder:
TIFF Warning: SamplesPerPixel tag value is changing, but
SMinSampleValue tag was read with a different value. Cancelling it.
I am suspecting that the small error which was detected can be
attributed to libjpeg behavior.
Bob
Thanks for your reply.
We have checked the issue carefully and found it is a bug in libtiff, which got fixed in the latest version of libtiff.
Thanks again.
Hi
Can you point to which libtiff upstream change did fix the issue?
Hi Bob,
what is commit that fix this issue?
Thanks!
On Thu, 28 Jun 2018, kirotawa wrote:
That is a good question since it was already fixed by the time the
problem was reported and so there is no specific change entry for this
POC.
If you really need to know, then I suggest studying the Mercurial
commits to coders/tiff.c and read the entries in the ChangeLog files.
Bob
In can confirm that this issue was a bug in the libTIFF codebase, namely http://bugzilla.maptools.org/show_bug.cgi?id=2500
The reproducer does not declare SamplesPerPixel field, so libTIFF does some guessing to set the correct value. Unfortunately it does not update the SMinSampleValue value, leading to later crash.
It was fixed in 739dcd28 (https://gitlab.com/libtiff/libtiff/commit/739dcd28a061738b317c1e9f91029d9cbc157159)
You can reproduce the issue by building a pre-739dcd28 libTIFF with asan: