Yeraze / ytnef

Yeraze's TNEF Stream Reader - for winmail.dat files
GNU General Public License v2.0
32 stars 22 forks source link

Heap buffer overflow via TNEFVersion #86

Closed jasperla closed 3 years ago

jasperla commented 3 years ago

A heap buffer overflow can be triggered as of ecc9d87 with crafted input, despite ytnef recognizing that the provided input file is not valid:

ERROR: Checksum mismatch. Data corruption?:

However when built with ASAN we see that SwapWord attempts to read invalid memory before said error message can be printed:

=================================================================
==2699631==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000032 at pc 0x000000498907 bp 0x7fffffffdf50 sp 0x7fffffffd718
READ of size 2 at 0x602000000032 thread T0
    #0 0x498906 in __asan_memcpy (/home/kali/ytnef/asan/bin/ytnef+0x498906)
    #1 0x7ffff7f51e90 in SwapWord /home/kali/ytnef/lib/ytnef.c:155:26
    #2 0x7ffff7f51e90 in TNEFVersion /home/kali/ytnef/lib/ytnef.c:343:11
    #3 0x7ffff7f68243 in TNEFParse /home/kali/ytnef/lib/ytnef.c:1220:15
    #4 0x7ffff7f664d9 in TNEFParseFile /home/kali/ytnef/lib/ytnef.c:1077:10
    #5 0x4c975c in main /home/kali/ytnef/ytnef/main.c:140:9
    #6 0x7ffff7bf9d09 in __libc_start_main csu/../csu/libc-start.c:308:16
    #7 0x41f439 in _start (/home/kali/ytnef/asan/bin/ytnef+0x41f439)

0x602000000032 is located 1 bytes to the right of 1-byte region [0x602000000030,0x602000000031)
allocated by thread T0 here:
    #0 0x499622 in calloc (/home/kali/ytnef/asan/bin/ytnef+0x499622)
    #1 0x7ffff7f676d9 in TNEFParse /home/kali/ytnef/lib/ytnef.c:1190:12
    #2 0x7ffff7f664d9 in TNEFParseFile /home/kali/ytnef/lib/ytnef.c:1077:10
    #3 0x4c975c in main /home/kali/ytnef/ytnef/main.c:140:9
    #4 0x7ffff7bf9d09 in __libc_start_main csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: heap-buffer-overflow (/home/kali/ytnef/asan/bin/ytnef+0x498906) in __asan_memcpy
Shadow bytes around the buggy address:
  0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  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 fd fa fa fa[01]fa fa fa fa fa fa fa fa fa
  0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: 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
==2699631==ABORTING

I have attached a minimal reproducer of this crash: heap.zip

carnil commented 3 years ago

This issue was assigned CVE-2021-3404