From 20f83d963b214e3bd7a226fdc2e3e6e70388c58a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Tue, 28 Jul 2020 10:47:37 +0200 Subject: [PATCH] Fix a use-after-free in RegExp.prototype.compile (#4068) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #4056. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai daniel.batyai@h-lab.eu --- .../ecma-builtin-regexp-prototype.c | 9 ++++--- tests/jerry/regression-test-issue-4056.js | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 tests/jerry/regression-test-issue-4056.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c index d01fca15f..eb81b00c9 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c @@ -301,6 +301,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this */ } JERRY_ASSERT (ecma_is_value_true (status)); + ecma_value_t ret_value; if (ecma_object_is_regexp_object (pattern_arg)) { @@ -314,13 +315,13 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this */ pattern_obj_p->u.class_prop.u.value); ecma_ref_object (this_obj_p); - /* ecma_op_create_regexp_from_bytecode will never throw an error while re-initalizing the regexp object, so we - * can deref the old bytecode without leaving a dangling pointer. */ + ret_value = ecma_op_create_regexp_from_bytecode (this_obj_p, bc_p); + ecma_bytecode_deref ((ecma_compiled_code_t *) old_bc_p); - return ecma_op_create_regexp_from_bytecode (this_obj_p, bc_p); + return ret_value; } - ecma_value_t ret_value = ecma_op_create_regexp_from_pattern (this_obj_p, pattern_arg, flags_arg); + ret_value = ecma_op_create_regexp_from_pattern (this_obj_p, pattern_arg, flags_arg); if (!ECMA_IS_VALUE_ERROR (ret_value)) { diff --git a/tests/jerry/regression-test-issue-4056.js b/tests/jerry/regression-test-issue-4056.js new file mode 100644 index 000000000..e26bd2704 --- /dev/null +++ b/tests/jerry/regression-test-issue-4056.js @@ -0,0 +1,26 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var r = new RegExp('a'); + +new RegExp('1'); +new RegExp('2'); +new RegExp('3'); +new RegExp('4'); +new RegExp('5'); +new RegExp('6'); +new RegExp('7'); +new RegExp('8'); + +r.compile(r);