From 243347edc86d9dc1ee0e35f86acc839d50db1d9d Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 1 Oct 2025 20:07:14 -0700 Subject: [PATCH] Module: fixed heap-use-after-free while module loading. Making a copy of file argument because the engine may outlive current ngx_cycle. The bug became visible since 283282f (0.8.8). --- nginx/ngx_js.c | 10 ++++++++-- nginx/t/js_import2.t | 14 +++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/nginx/ngx_js.c b/nginx/ngx_js.c index 7d2522bb..75a28735 100644 --- a/nginx/ngx_js.c +++ b/nginx/ngx_js.c @@ -557,11 +557,17 @@ ngx_engine_njs_init(ngx_engine_t *engine, ngx_engine_opts_t *opts) vm_options.backtrace = 1; vm_options.addons = opts->u.njs.addons; vm_options.metas = opts->u.njs.metas; - vm_options.file = opts->file; vm_options.argv = ngx_argv; vm_options.argc = ngx_argc; vm_options.init = 1; + vm_options.file.start = njs_mp_alloc(engine->pool, opts->file.length); + if (vm_options.file.start == NULL) { + return NGX_ERROR; + } + + ngx_memcpy(vm_options.file.start, opts->file.start, opts->file.length); + vm = njs_vm_create(&vm_options); if (vm == NULL) { return NGX_ERROR; @@ -579,7 +585,7 @@ ngx_engine_njs_init(ngx_engine_t *engine, ngx_engine_opts_t *opts) engine->u.njs.vm = vm; - return NJS_OK; + return NGX_OK; } diff --git a/nginx/t/js_import2.t b/nginx/t/js_import2.t index 7fdc624d..c3b4050e 100644 --- a/nginx/t/js_import2.t +++ b/nginx/t/js_import2.t @@ -64,6 +64,11 @@ http { js_content fun; } + location /test_exception { + js_import exception.js; + js_content exception.nonexistent; + } + location /test_var { return 200 $test; } @@ -105,6 +110,11 @@ $t->write_file('fun.js', <write_file('exception.js', <write_file('main.js', <stop(); my $content = $t->read_file('error.log'); my $count = () = $content =~ m/js vm init/g; -ok($count == 4, 'uniq js vm contexts'); +ok($count == 5, 'uniq js vm contexts'); ############################################################################### -- 2.47.3