## Summary
A heap buffer overflow exists in fmt_complex() when processing a malformed 
terminfo source file. The function copies a string using strncpy() with an 
overly large length, causing an out‑of‑bounds read past the end of a 544‑byte 
buffer allocated by _nc_tic_expand().
## Vulnerability Details
**Program**: tic (from ncurses)
**Version**: 6.6
**Crash Type**: heap-buffer-overflow (ASan)
**Root Cause**: Insufficient buffer size when expanding terminfo string 
capabilities – _nc_tic_expand() allocates a buffer based on the original string 
length, but later strncpy() attempts to copy a longer expanded string, leading 
to an over‑read into adjacent heap metadata.
##ASAN report
=================================================================
==3000064==ERROR: AddressSanitizer: heap-buffer-overflow on address 
0x516000000ea0 at pc 0x73db40cf7ebb bp 0x7ffc6c245ec0 sp 0x7ffc6c245668
READ of size 2 at 0x516000000ea0 thread T0
    #0 0x73db40cf7eba in strncpy 
../../../../src/libsanitizer/asan/asan_interceptors.cpp:613
    #1 0x56fb2629112a in strncpy_DYN ../progs/dump_entry.c:127
    #2 0x56fb26293aac in fmt_complex ../progs/dump_entry.c:872
    #3 0x56fb26295da8 in fmt_entry ../progs/dump_entry.c:1178
    #4 0x56fb26297e3d in dump_entry ../progs/dump_entry.c:1557
    #5 0x56fb2627cb54 in main ../progs/tic.c:1025
    #6 0x73db4082a1c9 in __libc_start_call_main 
../sysdeps/nptl/libc_start_call_main.h:58
    #7 0x73db4082a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #8 0x56fb26278e44 in _start 
(/home/xmut/wyf/LLM-optionFuzz/benchlatest/ncurses-asan/ncurses-6.6/progs/tic+0x38e44)
 (BuildId: 86e96ddefde07521953ce00114ad9dfc6b31ad04)
0x516000000ea0 is located 0 bytes after 544-byte region 
[0x516000000c80,0x516000000ea0)
allocated by thread T0 here:
    #0 0x73db40cfc778 in realloc 
../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:85
    #1 0x56fb262a0d25 in _nc_doalloc ../ncurses/./tinfo/doalloc.c:54
    #2 0x56fb262c20f9 in _nc_tic_expand ../ncurses/./tinfo/comp_expand.c:86
    #3 0x56fb26295d19 in fmt_entry ../progs/dump_entry.c:1169
    #4 0x56fb26297e3d in dump_entry ../progs/dump_entry.c:1557
    #5 0x56fb2627cb54 in main ../progs/tic.c:1025
    #6 0x73db4082a1c9 in __libc_start_call_main 
../sysdeps/nptl/libc_start_call_main.h:58
    #7 0x73db4082a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #8 0x56fb26278e44 in _start 
(/home/xmut/wyf/LLM-optionFuzz/benchlatest/ncurses-asan/ncurses-6.6/progs/tic+0x38e44)
 (BuildId: 86e96ddefde07521953ce00114ad9dfc6b31ad04)
SUMMARY: AddressSanitizer: heap-buffer-overflow 
../../../../src/libsanitizer/asan/asan_interceptors.cpp:613 in strncpy
Shadow bytes around the buggy address:
  0x516000000c00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x516000000c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x516000000d00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x516000000d80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x516000000e00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x516000000e80: 00 00 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa
  0x516000000f00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x516000000f80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x516000001000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x516000001080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x516000001100: 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
==3000064==ABORTING
### Root Cause
1.fmt_entry() calls _nc_tic_expand() to expand a terminfo string capability, 
passing the original string. The function allocates a buffer of size 544 bytes 
(based on the original string length plus some margin) via realloc().
2.Later in fmt_complex(), the function copies a substring into a local buffer 
using strncpy() with a length derived from the expanded string’s parsed 
structure. However, due to a malformed input, the calculated length exceeds the 
allocated buffer size.
3.strncpy() reads 2 bytes beyond the end of the 544‑byte region, causing a 
heap‑buffer‑overflow read into the following heap redzone (marked fa in shadow 
bytes).
### Vulnerable Code (dump_entry.c:127)
```c
_nc_STRNCPY(dst->text + dst->used, src, need + 1);
```
##Proof of Concept
**PoC File**: tic_heap_buffer_overflow
https://github.com/raind20001020/Poc/blob/main/tic_heap_buffer_overflow
**Reproduction**:
``bash
tic -a -c tic_heap_buffer_overflow
```
###Credit
Yufeng Wu、Zhijie Zhang、Qizhen Xu

Attachment: tic_heap_buffer_overflow
Description: Binary data

Reply via email to