Skip to content

Commit c04a8ee

Browse files
committed
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).
1 parent 88d23cb commit c04a8ee

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

nginx/ngx_js.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,17 @@ ngx_engine_njs_init(ngx_engine_t *engine, ngx_engine_opts_t *opts)
557557
vm_options.backtrace = 1;
558558
vm_options.addons = opts->u.njs.addons;
559559
vm_options.metas = opts->u.njs.metas;
560-
vm_options.file = opts->file;
561560
vm_options.argv = ngx_argv;
562561
vm_options.argc = ngx_argc;
563562
vm_options.init = 1;
564563

564+
vm_options.file.start = njs_mp_alloc(engine->pool, opts->file.length);
565+
if (vm_options.file.start == NULL) {
566+
return NGX_ERROR;
567+
}
568+
569+
ngx_memcpy(vm_options.file.start, opts->file.start, opts->file.length);
570+
565571
vm = njs_vm_create(&vm_options);
566572
if (vm == NULL) {
567573
return NGX_ERROR;

nginx/t/js_import2.t

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ http {
6464
js_content fun;
6565
}
6666
67+
location /test_exception {
68+
js_import exception.js;
69+
js_content exception.nonexistent;
70+
}
71+
6772
location /test_var {
6873
return 200 $test;
6974
}
@@ -105,6 +110,11 @@ $t->write_file('fun.js', <<EOF);
105110
106111
EOF
107112

113+
$t->write_file('exception.js', <<EOF);
114+
export default {nonexistent};
115+
116+
EOF
117+
108118
$t->write_file('main.js', <<EOF);
109119
function version(r) {
110120
r.return(200, njs.version);
@@ -127,11 +137,13 @@ like(http_get('/test_lib'), qr/LIB-TEST/s, 'lib.test');
127137
like(http_get('/test_fun'), qr/FUN-TEST/s, 'fun');
128138
like(http_get('/proxy/test_fun'), qr/FUN-TEST/s, 'proxy fun');
129139
like(http_get('/test_var'), qr/P-TEST/s, 'foo.bar.p');
140+
http_get('/test_exception');
141+
http_get('/test_exception');
130142

131143
$t->stop();
132144

133145
my $content = $t->read_file('error.log');
134146
my $count = () = $content =~ m/js vm init/g;
135-
ok($count == 4, 'uniq js vm contexts');
147+
ok($count == 5, 'uniq js vm contexts');
136148

137149
###############################################################################

0 commit comments

Comments
 (0)