vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 145 forks source link

heap-buffer-overflow in `def_symbol` #365

Open clesmian opened 11 months ago

clesmian commented 11 months ago

When executing c2m on poc.txt, a segfault occurs

POC

enum {x}x;

ASAN Output

=================================================================
==2370992==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000002558 at pc 0x55c3244e208e bp 0x7fc6d8afbf80 sp 0x7fc6d8afbf70
READ of size 32 at 0x602000002558 thread T1
    #0 0x55c3244e208d in def_symbol c2mir/c2mir.c:6404
    #1 0x55c32452edec in create_decl c2mir/c2mir.c:7771
    #2 0x55c32450d5c7 in check c2mir/c2mir.c:9159
    #3 0x55c324510b1f in check c2mir/c2mir.c:8286
    #4 0x55c324510099 in check c2mir/c2mir.c:9431
    #5 0x55c324553314 in do_context c2mir/c2mir.c:9697
    #6 0x55c324553314 in c2mir_compile c2mir/c2mir.c:13481
    #7 0x55c324556d6a in compile c2mir/c2mir-driver.c:498
    #8 0x7fc6dc181608 in start_thread /build/glibc-SzIz7B/glibc-2.31/nptl/pthread_create.c:477
    #9 0x7fc6dc0a6132 in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x11f132)

Address 0x602000002558 is a wild pointer.
SUMMARY: AddressSanitizer: heap-buffer-overflow c2mir/c2mir.c:6404 in def_symbol
Shadow bytes around the buggy address:
  0x0c047fff8450: fa fa 00 02 fa fa 06 fa fa fa 07 fa fa fa 00 01
  0x0c047fff8460: fa fa 07 fa fa fa 00 01 fa fa 00 01 fa fa 00 00
  0x0c047fff8470: fa fa 00 00 fa fa 00 00 fa fa 00 00 fa fa 00 fa
  0x0c047fff8480: fa fa 00 01 fa fa 00 fa fa fa 00 02 fa fa 00 01
  0x0c047fff8490: fa fa 00 03 fa fa 00 03 fa fa 00 05 fa fa 02 fa
=>0x0c047fff84a0: fa fa 04 fa fa fa 00 fa fa fa fa[fa]fa fa fa fa
  0x0c047fff84b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff84f0: 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
Thread T1 created by T0 here:
    #0 0x7fc6dc32b815 in __interceptor_pthread_create ../../../../src/libsanitizer/asan/asan_interceptors.cc:208
    #1 0x55c32446b6f8 in init_compilers c2mir/c2mir-driver.c:540
    #2 0x55c32446b6f8 in main c2mir/c2mir-driver.c:656

==2370992==ABORTING

Found while fuzzing https://github.com/vnmakarov/mir/commit/d51b45f6c76d2ca03a5b2e1968c195b867eaed30, verified with https://github.com/vnmakarov/mir/commit/cf3c9c106afdda59c402bdd40e61241aa20a755d

vnmakarov commented 11 months ago

I fixed this one by 86456a6a351e458deb593e5b6565544fb0b6b8f9