littlekernel / lk

LK embedded kernel
MIT License
3.11k stars 611 forks source link

buffer-underflow on _vsnprintf when len=0 #407

Open Helica-core opened 1 month ago

Helica-core commented 1 month ago

at _vsnprintf() function, when len=0 and str is a valid buffer. it will write str[-1] with a zero

int _vsnprintf(char *str, size_t len, const char *fmt, va_list ap)
{
    struct _output_args args;
    int wlen;

    args.outstr = str;
    args.len    = len;
    args.pos    = 0;

    wlen = _printf_engine(&_vsnprintf_output, (void *)&args, fmt, ap);

    if (args.pos >= len) { <---------- args.pos = 0, True
        str[len - 1] = '\0';  <----------  buffer underflow str[-1] = '\0'
    } else {
        str[wlen] = '\0';
    }

    return wlen;
}

asan confirmed this issue:

─$ ./a.out                                
=================================================================
==54428==ERROR: AddressSanitizer: stack-buffer-underflow on address 0x7ffc8ab6ee0f at pc 0x5581bf4bdb06 bp 0x7ffc8ab6eba0 sp 0x7ffc8ab6eb98
WRITE of size 1 at 0x7ffc8ab6ee0f thread T0
    #0 0x5581bf4bdb05 in _vsnprintf /mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/libc_printf.c:325
    #1 0x5581bf4bd736 in _snprintf /mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/libc_printf.c:278
    #2 0x5581bf4c1002 in main /mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/libc_printf.c:1017
    #3 0x7f784140f189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #4 0x7f784140f244 in __libc_start_main_impl ../csu/libc-start.c:381
    #5 0x5581bf4bd160 in _start (/mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/a.out+0x1160)

Address 0x7ffc8ab6ee0f is located in stack of thread T0 at offset 31 in frame
    #0 0x5581bf4c0e4c in main /mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/libc_printf.c:1012

  This frame has 1 object(s):
    [32, 160) 's' (line 1013) <== Memory access at offset 31 underflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-underflow /mnt/hgfs/iotcpa/tmp/bootrom/sectest/printf/libc_printf.c:325 in _vsnprintf
Shadow bytes around the buggy address:
  0x100011565d70: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
  0x100011565d80: 00 00 00 f3 f3 f3 f3 f3 00 00 00 00 00 00 00 00
  0x100011565d90: 00 00 00 00 f1 f1 f1 f1 00 00 00 f3 f3 f3 f3 f3
  0x100011565da0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100011565db0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1
=>0x100011565dc0: f1[f1]00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100011565dd0: 00 00 f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00
  0x100011565de0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100011565df0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100011565e00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100011565e10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==54428==ABORTING