tiffcrop: heap-use-after-free in extractContigSamplesShifted32bits, tiffcrop.c:3701

Summary

There is an heap-use-after-free error in extractContigSamplesShifted32bits in tools/tiffcrop.c:3701. Remote attackers could leverage this vulnerability to cause a denial-of-service via a crafted tiff file.

Version

LIBTIFF, Version master (post 4.4.0), commit id 1bdbd03f (Tue Nov 29 17:02:09 2022 +0000)

Steps to reproduce

# CFLAGS="-g -fsanitize=address -fno-omit-frame-pointer" CXXFLAGS="-g -fsanitize=address -fno-omit-frame-pointer" ./configure --prefix=$PWD/build_asan --disable-shared

# make -j; make install; make clean

$ ./build_asan/bin/tiffcrop -e multiple  -z 1,1,2048,2048:1,2049,2048,4097 -R 270 -i poc /tmp/foo
TIFFReadDirectoryCheckOrder: Warning, Invalid TIFF directory; tags are not sorted in ascending order.
TIFFReadDirectory: Warning, Unknown field with tag 6 (0x6) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 0 (0x0) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 27487 (0x6b5f) encountered.
TIFFFetchNormalTag: Warning, ASCII value for tag "DocumentName" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.
_TIFFVSetField: poc_3516: Null count for "Tag 0" (type 1, writecount -3, passcount 1).
TIFFFetchNormalTag: Defined set_field_type of custom tag 27487 (Tag 27487) is TIFF_SETGET_UNDEFINED and thus tag is not read from file.
TIFFFetchStripThing: Warning, Incorrect count for "StripOffsets"; tag ignored.
TIFFFetchStripThing: Warning, Incorrect count for "StripByteCounts"; tag ignored.
TIFFAdvanceDirectory: Error fetching directory link.
loadImage: Image lacks Photometric interpretation tag.
ZIPDecode: Decoding error at scanline 0.
: Strip 1: read -1 bytes, strip size 3200.
TIFFFillStrip: Invalid strip byte count 0, strip 1.
: Strip 2: read -1 bytes, strip size 3200.
TIFFFillStrip: Invalid strip byte count 0, strip 2.
: Strip 3: read -1 bytes, strip size 3200.
TIFFFillStrip: Invalid strip byte count 0, strip 3.
: Strip 4: read -1 bytes, strip size 3200.
TIFFFillStrip: Invalid strip byte count 0, strip 4.
: Strip 5: read -1 bytes, strip size 3200.
...
TIFFFillStrip: Invalid strip byte count 0, strip 1920.
=================================================================
==145202==ERROR: AddressSanitizer: heap-use-after-free on address 0x7fc4aeba2800 at pc 0x55e6bc8783db bp 0x7ffe6f221f10 sp 0x7ffe6f221f00
READ of size 1 at 0x7fc4aeba2800 thread T0
    #0 0x55e6bc8783da in extractContigSamplesShifted32bits /root/programs_latest/libtiff/tools/tiffcrop.c:3701
    #1 0x55e6bc88dc2c in extractSeparateRegion /root/programs_latest/libtiff/tools/tiffcrop.c:6966
    #2 0x55e6bc891a94 in processCropSelections /root/programs_latest/libtiff/tools/tiffcrop.c:7835
    #3 0x55e6bc873bb1 in main /root/programs_latest/libtiff/tools/tiffcrop.c:2511
    #4 0x7fc4b1b1c082 in __libc_start_main ../csu/libc-start.c:308
    #5 0x55e6bc86a60d in _start (/root/programs_latest/libtiff/build_asan/bin/tiffcrop+0x2e60d)

0x7fc4aeba2800 is located 32768 bytes inside of 409903-byte region [0x7fc4aeb9a800,0x7fc4aebfe92f)
freed by thread T0 here:
    #0 0x7fc4b24c440f in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:122
    #1 0x55e6bc90d0fa in _TIFFfree /root/programs_latest/libtiff/libtiff/tif_unix.c:350
    #2 0x55e6bc89673c in rotateImage /root/programs_latest/libtiff/tools/tiffcrop.c:8956
    #3 0x55e6bc8922b8 in processCropSelections /root/programs_latest/libtiff/tools/tiffcrop.c:7897
    #4 0x55e6bc873bb1 in main /root/programs_latest/libtiff/tools/tiffcrop.c:2511
    #5 0x7fc4b1b1c082 in __libc_start_main ../csu/libc-start.c:308

previously allocated by thread T0 here:
    #0 0x7fc4b24c4808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55e6bc90d09e in _TIFFmalloc /root/programs_latest/libtiff/libtiff/tif_unix.c:336
    #2 0x55e6bc86a7a0 in limitMalloc /root/programs_latest/libtiff/tools/tiffcrop.c:644
    #3 0x55e6bc8917e7 in processCropSelections /root/programs_latest/libtiff/tools/tiffcrop.c:7808
    #4 0x55e6bc873bb1 in main /root/programs_latest/libtiff/tools/tiffcrop.c:2511
    #5 0x7fc4b1b1c082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-use-after-free /root/programs_latest/libtiff/tools/tiffcrop.c:3701 in extractContigSamplesShifted32bits
Shadow bytes around the buggy address:
  0x0ff915d6c4b0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c4c0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c4d0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c4e0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c4f0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x0ff915d6c500:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c510: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c520: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c530: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c540: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0ff915d6c550: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
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
==145202==ABORTING

Platform

# uname -a
Linux dd189b3c7b86 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

poc.zip