sheredom / json.h

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

Heap Overflow in json_parse_key() #96

Closed hyrathon closed 1 year ago

hyrathon commented 1 year ago

Heap Overflow in json_parse_key()

In json_parse_key(), 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 parameter string transferred to json_parse_key() is not properly sanitized, and it is pointed to out-of-bound position assigned by upper layer function.

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

The flags is 0xd6d7d6d6247bff7f
=================================================================
==5286==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61f000000c58 at pc 0x5651b3173086 bp 0x7ffcafde8e10 sp 0x7ffcafde8e08
WRITE of size 8 at 0x61f000000c58 thread T0
    #0 0x5651b3173085 in json_parse_key /home/hyrathon/Desktop/jsonh/./json.h:1576:22
    #1 0x5651b3174e8b in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1694:11
    #2 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #3 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #4 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #5 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #6 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #7 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #8 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #9 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #10 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #11 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #12 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #13 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #14 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #15 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #16 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #17 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #18 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #19 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #20 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #21 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #22 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #23 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #24 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #25 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #26 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #27 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #28 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #29 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #30 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #31 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #32 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #33 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #34 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #35 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #36 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #37 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #38 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #39 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #40 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #41 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #42 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #43 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #44 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #45 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #46 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #47 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #48 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #49 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #50 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #51 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #52 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #53 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #54 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #55 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #56 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #57 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #58 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #59 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #60 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #61 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #62 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #63 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #64 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #65 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #66 0x5651b3176ad9 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1955:7
    #67 0x5651b317ad4b in json_parse_array /home/hyrathon/Desktop/jsonh/./json.h:1809:5
    #68 0x5651b3176ed5 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1962:7
    #69 0x5651b31758f0 in json_parse_object /home/hyrathon/Desktop/jsonh/./json.h:1719:5
    #70 0x5651b31761c8 in json_parse_value /home/hyrathon/Desktop/jsonh/./json.h:1940:5
    #71 0x5651b317ec85 in json_parse_ex /home/hyrathon/Desktop/jsonh/./json.h:2129:3
    #72 0x5651b3195192 in main /home/hyrathon/Desktop/jsonh/fuzz.c:29:5
    #73 0x7f1945b88d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #74 0x7f1945b88e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #75 0x5651b30a3314 in _start (/home/hyrathon/Desktop/jsonh/san+0x33314) (BuildId: a0e983e1c9c38a8763824dc5481f79d22fe8c82a)

0x61f000000c58 is located 14 bytes to the right of 3018-byte region [0x61f000000080,0x61f000000c4a)
allocated by thread T0 here:
    #0 0x5651b312615e in __interceptor_malloc (/home/hyrathon/Desktop/jsonh/san+0xb615e) (BuildId: a0e983e1c9c38a8763824dc5481f79d22fe8c82a)
    #1 0x5651b317df15 in json_parse_ex /home/hyrathon/Desktop/jsonh/./json.h:2088:18
    #2 0x5651b3195192 in main /home/hyrathon/Desktop/jsonh/fuzz.c:29:5
    #3 0x7f1945b88d8f 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:1576:22 in json_parse_key
Shadow bytes around the buggy address:
  0x0c3e7fff8130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c3e7fff8140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c3e7fff8150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c3e7fff8160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c3e7fff8170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c3e7fff8180: 00 00 00 00 00 00 00 00 00 02 fa[fa]fa fa fa fa
  0x0c3e7fff8190: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c3e7fff81a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c3e7fff81b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c3e7fff81c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c3e7fff81d0: 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
==5286==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)

Also I will attach another input that can trigger similar bug in json_parse_key(). crash-another.zip