sheredom / json.h

🗄️ single header json parser for C and C++
The Unlicense
698 stars 77 forks source link

Heap buffer overflow in json_parse_array #90

Closed zodf0055980 closed 1 year ago

zodf0055980 commented 1 year ago

Hi, I use fuzzer find this overflow.

#include "json.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    char s1[40] = "{\"k\" : true/ \"b\" : [false, null, \"foo\"]}";
    json_parse_ex(s1, 40, 0x2ffff, 0, 0, 0);
}

this is asan report:

==1419822==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x615000000258 at pc 0x55555555c3ce bp 0x7fffffffdaa0 sp 0x7fffffffda90
WRITE of size 8 at 0x615000000258 thread T0
    #0 0x55555555c3cd in json_parse_array json.h:1782
    #1 0x55555555d517 in json_parse_value json.h:1958
    #2 0x55555555c067 in json_parse_object json.h:1715
    #3 0x55555555d265 in json_parse_value json.h:1936
    #4 0x55555555eb54 in json_parse_ex json.h:2125
    #5 0x555555565f5e in main overflow.c:8
    #6 0x7ffff73ae082 in __libc_start_main ../csu/libc-start.c:308
    #7 0x5555555552cd in _start (/home/yuan/json.h/crash+0x12cd)

0x615000000258 is located 0 bytes to the right of 472-byte region [0x615000000080,0x615000000258)
allocated by thread T0 here:
    #0 0x7ffff7689808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55555555e760 in json_parse_ex json.h:2084
    #2 0x555555565f5e in main overflow.c:8
    #3 0x7ffff73ae082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-buffer-overflow json.h:1782 in json_parse_array
Shadow bytes around the buggy address:
  0x0c2a7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2a7fff8040: 00 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa
  0x0c2a7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2a7fff8090: 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
sheredom commented 1 year ago

Great find! Will work on a fix 💪