heap-buffer-overflow in TIFFWriteDirectoryTagTransferfunction() at /libtiff/tif_dirwrite.c:2110 (SIGSEGV)
Summary
An SIGSEGV caused when using tiffcrop.
AddressSanitizer reports it as heap-buffer-overflow.
Version
$ ./tools/tiffcrop -v
Library Release: LIBTIFF, Version 4.5.0
Copyright (c) 1988-1996 Sam Leffler
Copyright (c) 1991-1996 Silicon Graphics, Inc.
Tiffcp code: Copyright (c) 1988-1997 Sam Leffler
: Copyright (c) 1991-1997 Silicon Graphics, Inc
Tiffcrop additions: Copyright (c) 2007-2010 Richard Nolde
$ git log --oneline -1
17adf430 (HEAD -> master, origin/master, origin/HEAD) Merge branch 'tif_ovrcache_TIFFSetSubDirectory' into 'master'
Steps to reproduce
make
git clone https://gitlab.com/libtiff/libtiff.git
cd libtiff
./autogen.sh
./configure
make
run
$ ./tools/tiffcrop -X 12 pocmin /dev/null
TIFFReadDirectoryCheckOrder: Warning, Invalid TIFF directory; tags are not sorted in ascending order.
TIFFReadDirectory: Warning, Unknown field with tag 12336 (0x3030) encountered.
TIFFFetchNormalTag: Defined set_field_type of custom tag 12336 (Tag 12336) is TIFF_SETGET_UNDEFINED and thus tag is not read from file.
TIFFFetchNormalTag: Warning, ASCII value for tag "Software" does not end in null byte.
TIFFAdvanceDirectory: Error fetching directory link.
loadImage: Image lacks Photometric interpretation tag.
Fax4Decode: Uncompressed data (not supported) at line 0 of strip 0 (x 10).
Fax4Decode: Warning, Premature EOL at line 0 of strip 0 (got 10, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 1 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 1 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 2 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 2 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 3 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 3 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 4 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 4 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 5 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 5 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 6 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 6 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 7 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 7 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 8 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 8 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 9 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 9 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 10 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 10 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 11 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 11 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 12 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 12 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 13 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 13 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 14 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 14 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 15 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 15 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 16 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 16 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 17 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 17 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 18 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 18 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 19 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 19 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 20 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 20 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 21 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 21 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 22 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 22 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 23 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 23 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 24 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 24 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 25 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 25 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 26 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 26 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 27 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 27 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 28 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 28 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 29 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 29 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 30 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 30 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 31 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 31 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 32 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 32 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 33 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 33 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 34 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 34 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 35 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 35 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 36 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 36 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 37 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 37 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 38 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 38 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 39 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 39 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 40 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 40 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 41 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 41 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 42 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 42 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 43 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 43 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 44 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 44 of strip 0 (got 3, expected 12336).
Fax4Decode: Uncompressed data (not supported) at line 45 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 45 of strip 0 (got 3, expected 12336).
Fax4Decode: Warning, Premature EOF at line 46 of strip 0 (x 3).
Fax4Decode: Warning, Premature EOL at line 46 of strip 0 (got 3, expected 12336).
TIFFWriteDirectoryTagData: IO error writing tag data.
: Failed to write IFD for page number 0.
writeRegions: Unable to write new image.
TIFFReadDirectoryCheckOrder: Warning, Invalid TIFF directory; tags are not sorted in ascending order.
TIFFReadDirectory: Warning, Unknown field with tag 12336 (0x3030) encountered.
TIFFFetchNormalTag: Defined set_field_type of custom tag 12336 (Tag 12336) is TIFF_SETGET_UNDEFINED and thus tag is not read from file.
TIFFReadDirectory: Warning, Invalid data type for tag StripOffsets.
TIFFReadDirectory: Warning, TIFF directory is missing required "StripByteCounts" field, calculating from imagelength.
loadImage: Image lacks Photometric interpretation tag.
fish: './tools/tiffcrop -X 12 pocmin /dev/null' terminated by signal SIGSEGV (Address boundary error)
Platform
$ uname -a
Linux ncyang 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ASAN report
=================================================================
==2046721==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000194 at pc 0x7fc27fa85490 bp 0x7ffd426ccfb0 sp 0x7ffd426cc758
READ of size 33554432 at 0x602000000194 thread T0
#0 0x7fc27fa8548f in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:790
#1 0x7fc27f98f275 in _TIFFmemcpy /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_unix.c:345
#2 0x7fc27f907052 in TIFFWriteDirectoryTagTransferfunction /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dirwrite.c:2110
#3 0x7fc27f900dad in TIFFWriteDirectorySec /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dirwrite.c:824
#4 0x7fc27f8fd4f0 in TIFFWriteDirectory /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dirwrite.c:237
#5 0x5613b42412eb in writeCroppedImage /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:9216
#6 0x5613b423b440 in writeSelections /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:7999
#7 0x5613b42202d0 in main /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:2825
#8 0x7fc27f56e082 in __libc_start_main ../csu/libc-start.c:308
#9 0x5613b4216b6d in _start (/home/ncyang/fuzz/new_libtiff/libtiff/tools/.libs/tiffcrop+0x9b6d)
0x602000000194 is located 0 bytes to the right of 4-byte region [0x602000000190,0x602000000194)
allocated by thread T0 here:
#0 0x7fc27faf7808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7fc27f98f18f in _TIFFmalloc /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_unix.c:326
#2 0x7fc27f96a754 in _TIFFmallocExt /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_open.c:129
#3 0x7fc27f8b6a1c in setByteArray /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dir.c:55
#4 0x7fc27f8b6baf in _TIFFsetShortArrayExt /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dir.c:81
#5 0x7fc27f8bcc5c in _TIFFVSetField /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dir.c:562
#6 0x7fc27f924dcc in Fax3VSetField /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_fax3.c:1293
#7 0x7fc27f8c24c0 in TIFFVSetField /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dir.c:1208
#8 0x7fc27f8c1ea7 in TIFFSetField /home/ncyang/fuzz/new_libtiff/libtiff/libtiff/tif_dir.c:1152
#9 0x5613b421a43b in cpTag /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:1736
#10 0x5613b424095a in writeCroppedImage /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:9080
#11 0x5613b423b440 in writeSelections /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:7999
#12 0x5613b42202d0 in main /home/ncyang/fuzz/new_libtiff/libtiff/tools/tiffcrop.c:2825
#13 0x7fc27f56e082 in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: heap-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:790 in __interceptor_memcpy
Shadow bytes around the buggy address:
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff8000: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa fd fa
0x0c047fff8010: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
0x0c047fff8020: fa fa fd fa fa fa 00 00 fa fa 00 00 fa fa 00 00
=>0x0c047fff8030: fa fa[04]fa fa fa 00 fa fa fa 00 fa fa fa fd fa
0x0c047fff8040: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
0x0c047fff8050: fa fa fd fa fa fa 00 fa fa fa 00 fa fa fa fd fa
0x0c047fff8060: fa fa fd fa fa fa fd fa fa fa fa fa fa fa fa fa
0x0c047fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8080: 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
Shadow gap: cc
==2046721==ABORTING