nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.85k stars 7.72k forks source link

heap-buffer-overflow in function stbtt__find_table at stb_truetype.h:1313 #1287

Closed Vincebye closed 2 years ago

Vincebye commented 2 years ago

Describe A heap-buffer-overflow was discovered in stb_truetype. The issue is being triggered in function stbtt__find_table () at stb_truetype.h:1313 To Reproduce test program

#include <stdio.h>  
#include <stdlib.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
int main(int argc, const char *argv[])
{
    long int size = 0;
    unsigned char *fontBuffer = NULL;
    FILE *fontFile = fopen(argv[1], "rb");
    if (fontFile == NULL)
    {
        printf("Can not open font file!\n");
        return 0;
    }
    fseek(fontFile, 0, SEEK_END);
    size = ftell(fontFile);   
    fseek(fontFile, 0, SEEK_SET); 
    fontBuffer = calloc(size, sizeof(unsigned char));
    fread(fontBuffer, size, 1, fontFile);
    fclose(fontFile);
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0))
    {
        printf("stb init font failed\n");
    }
    int bitmap_w = 512; 
    int bitmap_h = 128;
    free(fontBuffer);
    return 0;
}  

Compile test program with address sanitizer with this command:

AFL_HARDEN=1 afl-gcc -I /src/stb/include ttf.c -o ttf -lm

You can get program here Asan Reports ./Asanttf crash/10 Get ASan reports

==3164==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60f0000000fc at pc 0x561fa86e2c28 bp 0x7ffd44ebe720 sp 0x7ffd44ebe710
READ of size 1 at 0x60f0000000fc thread T0
    #0 0x561fa86e2c27 in stbtt__find_table /src/stb/include/stb_truetype.h:1313
    #1 0x561fa8702e31 in stbtt_InitFont_internal /src/stb/include/stb_truetype.h:1392
    #2 0x561fa8702e31 in stbtt_InitFont /src/stb/include/stb_truetype.h:4954
    #3 0x561fa87051fc in main /src/stb/ttf.c:32
    #4 0x7f613bb2c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #5 0x561fa86de70d in _start (/src/stb/Asanttf+0x470d)

0x60f0000000fc is located 13 bytes to the right of 175-byte region [0x60f000000040,0x60f0000000ef)
allocated by thread T0 here:
    #0 0x7f613befae17 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x561fa87051c6 in main /src/stb/ttf.c:26
    #2 0x7f613bb2c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/stb/include/stb_truetype.h:1313 in stbtt__find_table
Shadow bytes around the buggy address:
  0x0c1e7fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
=>0x0c1e7fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 07 fa[fa]
  0x0c1e7fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8060: 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
==3164==ABORTING

Poc Poc file is here

nothings commented 2 years ago

see #1286