pcmacdon / jsish

Jsi is a small, C-embeddable javascript interpreter with tightly woven Web and DB support.
https://jsish.org/
MIT License
42 stars 9 forks source link

Heap-use-after-free src/jsiEval.c:213 in jsiVarDeref #80

Closed hope-fly closed 2 years ago

hope-fly commented 2 years ago
Jsish revision

Commit: 9fa798e

Version: v3.5.0

Build platform

Ubuntu 18.04.5 LTS (Linux 5.4.0-44-generic x86_64)

Build steps
export CFLAGS='-fsanitize=address'
make
Test case
var JSEtest = [
  'gg',
  'hhh',
  'mm'
];
'sort:' + JSEtest.sort(function (str, position) {
  return JSEtest.unshift('pop:' + JSEtest.pop());
});
!'concat:' + JSEtest.concat().every(function (str, position) {
  return JSEtest.unshift('pop:' + JSEtest.pop());
});

Execution steps & Output
$ ./jsish/jsish poc.js
==120808==ERROR: AddressSanitizer: heap-use-after-free on address 0x603000007008 at pc 0x56385c0656d1 bp 0x7ffc654f4680 sp 0x7ffc654f4670
READ of size 1 at 0x603000007008 thread T0
    #0 0x56385c0656d0 in jsiVarDeref src/jsiEval.c:213
    #1 0x56385c073443 in jsiEvalCodeSub src/jsiEval.c:1422
    #2 0x56385c0a315e in jsi_evalcode src/jsiEval.c:2204
    #3 0x56385bdc6834 in jsi_FuncCallSub src/jsiProto.c:220
    #4 0x56385bd42fec in jsi_FunctionInvoke src/jsiFunc.c:777
    #5 0x56385bd42fec in Jsi_FunctionInvoke src/jsiFunc.c:789
    #6 0x56385be09f64 in jsi_ArrayFindSubCmd src/jsiArray.c:576
    #7 0x56385be09f64 in jsi_ArrayEveryCmd src/jsiArray.c:663
    #8 0x56385bdc5818 in jsi_FuncCallSub src/jsiProto.c:244
    #9 0x56385c08f71a in jsiFunctionSubCall src/jsiEval.c:796
    #10 0x56385c08f71a in jsiEvalFunction src/jsiEval.c:837
    #11 0x56385c08f71a in jsiEvalCodeSub src/jsiEval.c:1264
    #12 0x56385c0a315e in jsi_evalcode src/jsiEval.c:2204
    #13 0x56385c0a7274 in jsi_evalStrFile src/jsiEval.c:2665
    #14 0x56385bd9666a in Jsi_Main src/jsiInterp.c:936
    #15 0x56385c59b03a in jsi_main src/main.c:47
    #16 0x7faf31ad8bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
    #17 0x56385bd2a969 in _start (/usr/local/bin/jsish+0xe8969)

0x603000007008 is located 8 bytes inside of 32-byte region [0x603000007000,0x603000007020)
freed by thread T0 here:
    #0 0x7faf327477a8 in __interceptor_free (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xde7a8)
    #1 0x56385bd4b6cf in Jsi_DecrRefCount src/jsiValue.c:52

previously allocated by thread T0 here:
    #0 0x7faf32747d28 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xded28)
    #1 0x56385bd9baa4 in Jsi_Calloc src/jsiUtils.c:57

SUMMARY: AddressSanitizer: heap-use-after-free src/jsiEval.c:213 in jsiVarDeref
Shadow bytes around the buggy address:
  0x0c067fff8db0: fa fa fd fd fd fd fa fa fd fd fd fd fa fa fd fd
  0x0c067fff8dc0: fd fd fa fa fd fd fd fd fa fa 00 00 00 00 fa fa
  0x0c067fff8dd0: 00 00 00 00 fa fa 00 00 00 00 fa fa 00 00 00 00
  0x0c067fff8de0: fa fa 00 00 00 00 fa fa fd fd fd fd fa fa fd fd
  0x0c067fff8df0: fd fd fa fa fd fd fd fd fa fa fd fd fd fd fa fa
=>0x0c067fff8e00: fd[fd]fd fd fa fa fd fd fd fd fa fa fd fd fd fd
  0x0c067fff8e10: fa fa fd fd fd fd fa fa fd fd fd fd fa fa fd fd
  0x0c067fff8e20: fd fd fa fa fd fd fd fd fa fa fd fd fd fd fa fa
  0x0c067fff8e30: fd fd fd fd fa fa fd fd fd fd fa fa 00 00 00 00
  0x0c067fff8e40: fa fa fd fd fd fd fa fa fd fd fd fd fa fa fd fd
  0x0c067fff8e50: fd fd fa fa fd fd fd fd fa fa fd fd fd fd 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
==120808==ABORTING

Credits: Found by OWL337 team.

pcmacdon commented 2 years ago

Duplicate

hope-fly commented 2 years ago

were #75 #77 #79 #80 all duplicates of #78 or #74? During debugging, I found that if commented out the last 2 or 3 lines of poc, the result was the same as #74. I want to record them to avoid the duplicate

pcmacdon commented 2 years ago

Fixed in #85

hope-fly commented 2 years ago

Got it!

pcmacdon commented 2 years ago

I think it is more like were #75 #77 #80 are duplicates of #85 (array in-place bug) and #78 #74 are duplicate of #79 (prototype bug)

But with this many simultaneously submitted bugs, it is hard to keep it all straight. I assume these bugs were batch agregated, and submitted in one go.

hope-fly commented 2 years ago

okay, The time I submitted these issues, I'm a little confused. I'll submit similar issues in one next time. Thanks for your info

pcmacdon commented 2 years ago

I didn't mean to sound like I was complaining. I know it isn't always possible to tell when issues are similar/the-same. I just meant there were so many issues (37) submitted in the span of 90 minutes. But I guess that is the nature of automated tools like fuzzers.

Thanks for the great work.

hope-fly commented 2 years ago

hey my friend , I just know that and never thought you were complaining . On the contrary, I really appreciate your work to fix all these issues in time. these issues were detected in a fuzzing process over a week. then I sorted them and reported them together.