I am using njs to read a JSON file and return the value of a key based on a variable. If my JSON file has more than 2 keys, I get memory corruption. I am using nginx 1.14.2 in the offical docker image.
njs script:
var fs = require('fs');
var token_string = fs.readFileSync('/etc/nginx/tokens.json', 'utf8');
var tokens = JSON.parse(token_string);
function get_token(r) {
var email = r.variables["email"];
r.headersOut['Content-Type'] = 'text/plain';
if (email == '') {
r.return(500, "No email found");
}
var token = tokens[email];
if (token == undefined) {
r.return(500, "No token found");
}
return r.return(200, token);
}
tokens.json file:
{
"[email protected]": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3Y2RhYjEzOTg5MTE0Njk3Yjk1ODU1ZWUyZmQwZDg0ZiIsImlhdCI6MTU1MTg3Njk2NywiZXhwIjoxODY3MjM2OTY3fQ.fXAbeTR7xgJm3XlRn9hv2caJSQHJUo9qUTuogPC54vM",
"[email protected]": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3Y2RhYjEzOTg5MTE0Njk3Yjk1ODU1ZWUyZmQwZDg0ZiIsImlhdCI6MTU1MTg3Njk2NywiZXhwIjoxODY3MjM2OTY3fQ.fXAbeTR7xgJm3XlRn9hv2caJSQHJUo9qUTuogPC54vM",
"[email protected]": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3Y2RhYjEzOTg5MTE0Njk3Yjk1ODU1ZWUyZmQwZDg0ZiIsImlhdCI6MTU1MTg3Njk2NywiZXhwIjoxODY3MjM2OTY3fQ.fXAbeTR7xgJm3XlRn9hv2caJSQHJUo9qUTuogPC54vM",
"[email protected]": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3Y2RhYjEzOTg5MTE0Njk3Yjk1ODU1ZWUyZmQwZDg0ZiIsImlhdCI6MTU1MTg3Njk2NywiZXhwIjoxODY3MjM2OTY3fQ.fXAbeTR7xgJm3XlRn9hv2caJSQHJUo9qUTuogPC54vM"
}
stack trace:
*** Error in `nginx: worker process': malloc(): memory corruption: 0x00005560bcd977a0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7fef70ac9bfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7fef70acffc6]
/lib/x86_64-linux-gnu/libc.so.6(+0x79089)[0x7fef70ad2089]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7fef70ad3f64]
/lib/x86_64-linux-gnu/libc.so.6(posix_memalign+0x3f)[0x7fef70ad5fef]
nginx: worker process(ngx_memalign+0x2f)[0x5560bc0a8b1f]
nginx: worker process(+0x29e12)[0x5560bc087e12]
nginx: worker process(ngx_pcalloc+0xe)[0x5560bc0882de]
nginx: worker process(ngx_create_temp_buf+0x14)[0x5560bc089c04]
nginx: worker process(ngx_ssl_send_chain+0x32e)[0x5560bc0b3b2e]
nginx: worker process(ngx_http_write_filter+0x3a1)[0x5560bc0e0b01]
nginx: worker process(+0x83b76)[0x5560bc0e1b76]
nginx: worker process(+0x8835c)[0x5560bc0e635c]
nginx: worker process(+0x8a008)[0x5560bc0e8008]
nginx: worker process(+0x8ec18)[0x5560bc0ecc18]
nginx: worker process(+0x90cbc)[0x5560bc0eecbc]
nginx: worker process(+0x912ec)[0x5560bc0ef2ec]
nginx: worker process(+0x92a91)[0x5560bc0f0a91]
nginx: worker process(ngx_output_chain+0x92e)[0x5560bc08a9fe]
nginx: worker process(+0x9378c)[0x5560bc0f178c]
nginx: worker process(ngx_http_output_filter+0xe)[0x5560bc0c093e]
nginx: worker process(ngx_http_send_response+0x186)[0x5560bc0c0ae6]
/etc/nginx/modules/ngx_http_js_module.so(+0xcbca)[0x7fef6fd4abca]
/etc/nginx/modules/ngx_http_js_module.so(+0x1044a)[0x7fef6fd4e44a]
/etc/nginx/modules/ngx_http_js_module.so(+0xeb2f)[0x7fef6fd4cb2f]
/etc/nginx/modules/ngx_http_js_module.so(njs_vm_call+0x57)[0x7fef6fd4bdb7]
/etc/nginx/modules/ngx_http_js_module.so(+0xb29c)[0x7fef6fd4929c]
nginx: worker process(ngx_http_read_client_request_body+0x40)[0x5560bc0cdb30]
/etc/nginx/modules/ngx_http_js_module.so(+0xad50)[0x7fef6fd48d50]
nginx: worker process(ngx_http_core_content_phase+0x31)[0x5560bc0c0d91]
nginx: worker process(ngx_http_core_run_phases+0x2d)[0x5560bc0bb63d]
nginx: worker process(ngx_http_run_posted_requests+0x30)[0x5560bc0c4270]
nginx: worker process(+0x50f7b)[0x5560bc0aef7b]
nginx: worker process(ngx_process_events_and_timers+0x6a)[0x5560bc0a5a8a]
nginx: worker process(+0x4f171)[0x5560bc0ad171]
nginx: worker process(ngx_spawn_process+0x17e)[0x5560bc0ab5be]
nginx: worker process(ngx_master_process_cycle+0x7c5)[0x5560bc0ae165]
nginx: worker process(main+0xaed)[0x5560bc085aad]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fef70a792e1]
nginx: worker process(_start+0x2a)[0x5560bc085c4a]
======= Memory map: ========
5560bc05e000-5560bc179000 r-xp 00000000 103:07 3132380 /usr/sbin/nginx
5560bc379000-5560bc37b000 r--p 0011b000 103:07 3132380 /usr/sbin/nginx
5560bc37b000-5560bc39c000 rw-p 0011d000 103:07 3132380 /usr/sbin/nginx
5560bc39c000-5560bc3bb000 rw-p 00000000 00:00 0
5560bcd74000-5560bce40000 rw-p 00000000 00:00 0 [heap]
5560bce40000-5560bcea8000 rw-p 00000000 00:00 0 [heap]
7fef68000000-7fef68021000 rw-p 00000000 00:00 0
7fef68021000-7fef6c000000 ---p 00000000 00:00 0
7fef6ee23000-7fef6ee39000 r-xp 00000000 103:07 3127940 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fef6ee39000-7fef6f038000 ---p 00016000 103:07 3127940 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fef6f038000-7fef6f039000 r--p 00015000 103:07 3127940 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fef6f039000-7fef6f03a000 rw-p 00016000 103:07 3127940 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fef6f03a000-7fef6fa3a000 rw-s 00000000 00:05 7939416 /dev/zero (deleted)
7fef6fa3a000-7fef6fb3d000 r-xp 00000000 103:07 3127947 /lib/x86_64-linux-gnu/libm-2.24.so
7fef6fb3d000-7fef6fd3c000 ---p 00103000 103:07 3127947 /lib/x86_64-linux-gnu/libm-2.24.so
7fef6fd3c000-7fef6fd3d000 r--p 00102000 103:07 3127947 /lib/x86_64-linux-gnu/libm-2.24.so
7fef6fd3d000-7fef6fd3e000 rw-p 00103000 103:07 3127947 /lib/x86_64-linux-gnu/libm-2.24.so
7fef6fd3e000-7fef6fd8e000 r-xp 00000000 103:07 3132304 /usr/lib/nginx/modules/ngx_http_js_module.so
7fef6fd8e000-7fef6ff8e000 ---p 00050000 103:07 3132304 /usr/lib/nginx/modules/ngx_http_js_module.so
7fef6ff8e000-7fef6ff96000 r--p 00050000 103:07 3132304 /usr/lib/nginx/modules/ngx_http_js_module.so
7fef6ff96000-7fef6ff9b000 rw-p 00058000 103:07 3132304 /usr/lib/nginx/modules/ngx_http_js_module.so
7fef6ff9b000-7fef6fff6000 r-xp 00000000 103:07 5739725 /usr/lib/nginx/modules/ngx_http_naxsi_module.so
7fef6fff6000-7fef701f5000 ---p 0005b000 103:07 5739725 /usr/lib/nginx/modules/ngx_http_naxsi_module.so
7fef701f5000-7fef7021a000 r--p 0005a000 103:07 5739725 /usr/lib/nginx/modules/ngx_http_naxsi_module.so
7fef7021a000-7fef7021b000 rw-p 0007f000 103:07 5739725 /usr/lib/nginx/modules/ngx_http_naxsi_module.so
7fef7021b000-7fef70225000 r-xp 00000000 103:07 3127962 /lib/x86_64-linux-gnu/libnss_files-2.24.so
7fef70225000-7fef70425000 ---p 0000a000 103:07 3127962 /lib/x86_64-linux-gnu/libnss_files-2.24.so
7fef70425000-7fef70426000 r--p 0000a000 103:07 3127962 /lib/x86_64-linux-gnu/libnss_files-2.24.so
7fef70426000-7fef70427000 rw-p 0000b000 103:07 3127962 /lib/x86_64-linux-gnu/libnss_files-2.24.so
7fef70427000-7fef7042d000 rw-p 00000000 00:00 0
7fef7042d000-7fef70438000 r-xp 00000000 103:07 3127966 /lib/x86_64-linux-gnu/libnss_nis-2.24.so
7fef70438000-7fef70637000 ---p 0000b000 103:07 3127966 /lib/x86_64-linux-gnu/libnss_nis-2.24.so
7fef70637000-7fef70638000 r--p 0000a000 103:07 3127966 /lib/x86_64-linux-gnu/libnss_nis-2.24.so
7fef70638000-7fef70639000 rw-p 0000b000 103:07 3127966 /lib/x86_64-linux-gnu/libnss_nis-2.24.so
7fef70639000-7fef7064d000 r-xp 00000000 103:07 3127956 /lib/x86_64-linux-gnu/libnsl-2.24.so
7fef7064d000-7fef7084d000 ---p 00014000 103:07 3127956 /lib/x86_64-linux-gnu/libnsl-2.24.so
7fef7084d000-7fef7084e000 r--p 00014000 103:07 3127956 /lib/x86_64-linux-gnu/libnsl-2.24.so
7fef7084e000-7fef7084f000 rw-p 00015000 103:07 3127956 /lib/x86_64-linux-gnu/libnsl-2.24.so
7fef7084f000-7fef70851000 rw-p 00000000 00:00 0
7fef70851000-7fef70858000 r-xp 00000000 103:07 3127958 /lib/x86_64-linux-gnu/libnss_compat-2.24.so
7fef70858000-7fef70a57000 ---p 00007000 103:07 3127958 /lib/x86_64-linux-gnu/libnss_compat-2.24.so
7fef70a57000-7fef70a58000 r--p 00006000 103:07 3127958 /lib/x86_64-linux-gnu/libnss_compat-2.24.so
7fef70a58000-7fef70a59000 rw-p 00007000 103:07 3127958 /lib/x86_64-linux-gnu/libnss_compat-2.24.so
7fef70a59000-7fef70bee000 r-xp 00000000 103:07 3127922 /lib/x86_64-linux-gnu/libc-2.24.so
7fef70bee000-7fef70dee000 ---p 00195000 103:07 3127922 /lib/x86_64-linux-gnu/libc-2.24.so
7fef70dee000-7fef70df2000 r--p 00195000 103:07 3127922 /lib/x86_64-linux-gnu/libc-2.24.so
7fef70df2000-7fef70df4000 rw-p 00199000 103:07 3127922 /lib/x86_64-linux-gnu/libc-2.24.so
7fef70df4000-7fef70df8000 rw-p 00000000 00:00 0
7fef70df8000-7fef70e11000 r-xp 00000000 103:07 3128004 /lib/x86_64-linux-gnu/libz.so.1.2.8
7fef70e11000-7fef71010000 ---p 00019000 103:07 3128004 /lib/x86_64-linux-gnu/libz.so.1.2.8
7fef71010000-7fef71011000 r--p 00018000 103:07 3128004 /lib/x86_64-linux-gnu/libz.so.1.2.8
7fef71011000-7fef71012000 rw-p 00019000 103:07 3128004 /lib/x86_64-linux-gnu/libz.so.1.2.8
7fef71012000-7fef7127c000 r-xp 00000000 103:07 3132325 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
7fef7127c000-7fef7147c000 ---p 0026a000 103:07 3132325 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
7fef7147c000-7fef7149a000 r--p 0026a000 103:07 3132325 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
7fef7149a000-7fef714a8000 rw-p 00288000 103:07 3132325 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
7fef714a8000-7fef714ab000 rw-p 00000000 00:00 0
7fef714ab000-7fef7150e000 r-xp 00000000 103:07 3132365 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
7fef7150e000-7fef7170d000 ---p 00063000 103:07 3132365 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
7fef7170d000-7fef71711000 r--p 00062000 103:07 3132365 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
7fef71711000-7fef71717000 rw-p 00066000 103:07 3132365 /usr/lib/x86_64-linux-gnu/libssl.so.1.1
7fef71717000-7fef71789000 r-xp 00000000 103:07 3127978 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
7fef71789000-7fef71988000 ---p 00072000 103:07 3127978 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
7fef71988000-7fef71989000 r--p 00071000 103:07 3127978 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
7fef71989000-7fef7198a000 rw-p 00072000 103:07 3127978 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
7fef7198a000-7fef71992000 r-xp 00000000 103:07 3127930 /lib/x86_64-linux-gnu/libcrypt-2.24.so
7fef71992000-7fef71b92000 ---p 00008000 103:07 3127930 /lib/x86_64-linux-gnu/libcrypt-2.24.so
7fef71b92000-7fef71b93000 r--p 00008000 103:07 3127930 /lib/x86_64-linux-gnu/libcrypt-2.24.so
7fef71b93000-7fef71b94000 rw-p 00009000 103:07 3127930 /lib/x86_64-linux-gnu/libcrypt-2.24.so
7fef71b94000-7fef71bc2000 rw-p 00000000 00:00 0
7fef71bc2000-7fef71bda000 r-xp 00000000 103:07 3127979 /lib/x86_64-linux-gnu/libpthread-2.24.so
7fef71bda000-7fef71dd9000 ---p 00018000 103:07 3127979 /lib/x86_64-linux-gnu/libpthread-2.24.so
7fef71dd9000-7fef71dda000 r--p 00017000 103:07 3127979 /lib/x86_64-linux-gnu/libpthread-2.24.so
7fef71dda000-7fef71ddb000 rw-p 00018000 103:07 3127979 /lib/x86_64-linux-gnu/libpthread-2.24.so
7fef71ddb000-7fef71ddf000 rw-p 00000000 00:00 0
7fef71ddf000-7fef71de2000 r-xp 00000000 103:07 3127932 /lib/x86_64-linux-gnu/libdl-2.24.so
7fef71de2000-7fef71fe1000 ---p 00003000 103:07 3127932 /lib/x86_64-linux-gnu/libdl-2.24.so
7fef71fe1000-7fef71fe2000 r--p 00002000 103:07 3127932 /lib/x86_64-linux-gnu/libdl-2.24.so
7fef71fe2000-7fef71fe3000 rw-p 00003000 103:07 3127932 /lib/x86_64-linux-gnu/libdl-2.24.so
7fef71fe3000-7fef72006000 r-xp 00000000 103:07 3127904 /lib/x86_64-linux-gnu/ld-2.24.so
7fef721fc000-7fef72202000 rw-p 00000000 00:00 0
7fef72203000-7fef72204000 rw-p 00000000 00:00 0
7fef72204000-7fef72205000 rw-s 00000000 00:0f 7940145 /[aio] (deleted)
7fef72205000-7fef72206000 rw-s 00000000 00:05 7939418 /dev/zero (deleted)
7fef72206000-7fef72207000 r--p 00023000 103:07 3127904 /lib/x86_64-linux-gnu/ld-2.24.so
7fef72207000-7fef72208000 rw-p 00024000 103:07 3127904 /lib/x86_64-linux-gnu/ld-2.24.so
7fef72208000-7fef72209000 rw-p 00000000 00:00 0
7ffef0f2e000-7ffef0f4f000 rw-p 00000000 00:00 0 [stack]
7ffef0fd4000-7ffef0fd7000 r--p 00000000 00:00 0 [vvar]
7ffef0fd7000-7ffef0fd9000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
2019/03/06 12:23:41 [alert] 1#1: worker process 11 exited on signal 6 (core dumped)
Just tested this with 1.15.9 as well, and had the same issue. When I remove the keys for email3 and email4 from the json file, it functions properly.
@blakeblackshear
Thank you for the report. I can confirm the issue.
BTW, as a workaround, your file looks like ASCII only, try to open the file without encoding
fs.readFileSync('/etc/nginx/tokens.json')
Alternatively, if the file is indeed 'utf8',
var token_string = fs.readFileSync('/etc/nginx/tokens.json').fromUTF8() should help.