sheredom / json.h

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

Heap Overflow in json_parse_value() #94

Closed hyrathon closed 1 year ago

hyrathon commented 1 year ago

Heap Overflow in json_parse_value()

In json_parse_value(), there is a heap out-of-bound write. The bug can be triggered with an invocation of json_parse_ex() with specific flags combination. The root cause of this vulnerability is that the value transferred to json_parse_value() is not properly sanitized, and it is pointed to out-of-bound position.

Here is the output of Google ASAN when the vulnerability is triggerred:

The flags is 0x5fd6d7d6d6247b16
=================================================================
==5035==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x611000000110 at pc 0x5615f43f0b7e bp 0x7ffc2be6fcd0 sp 0x7ffc2be6fcc8
WRITE of size 8 at 0x611000000110 thread T0
    #0 0x5615f43f0b7d in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1959:19
    #1 0x5615f43ef8f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #2 0x5615f43f01c8 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1940:5
    #3 0x5615f43f8c85 in json_parse_ex /home/hyrathon/Desktop/jsonh/./json.h:2129:3
    #4 0x5615f440f192 in main /home/hyrathon/Desktop/jsonh/fuzz.c:29:5
    #5 0x7fee4e17bd8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #6 0x7fee4e17be3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #7 0x5615f431d314 in _start (/home/hyrathon/Desktop/jsonh/san+0x33314) (BuildId: a0e983e1c9c38a8763824dc5481f79d22fe8c82a)

0x611000000111 is located 0 bytes to the right of 209-byte region [0x611000000040,0x611000000111)
allocated by thread T0 here:
    #0 0x5615f43a015e in __interceptor_malloc (/home/hyrathon/Desktop/jsonh/san+0xb615e) (BuildId: a0e983e1c9c38a8763824dc5481f79d22fe8c82a)
    #1 0x5615f43f7f15 in json_parse_ex /home/hyrathon/Desktop/jsonh/./json.h:2088:18
    #2 0x5615f440f192 in main /home/hyrathon/Desktop/jsonh/fuzz.c:29:5
    #3 0x7fee4e17bd8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/hyrathon/Desktop/jsonh/./json.h:1959:19 in json_parse_value
Shadow bytes around the buggy address:
  0x0c227fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff8000: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c227fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c227fff8020: 00 00[01]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8070: 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
==5035==ABORTING

The input to trigger this bug is attached. The first 8 bytes are the flags for json_parse_ex() and the rest is the content(src). Please use address sanitizer to reproduce the bug, as non-crash overflow is hard to detect or locate without shadow memory.

Version: latest commit (bdcf2e1912aa4c0127f755f5f6271c539d6cbf9f) crash-input.zip