From a54229f40e3231eb29a69ca794c72d905a01f4f4 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Fri, 15 Nov 2019 08:55:28 +0000 Subject: [PATCH 1/2] add calls for DUK_USE_EXEC_TIMEOUT_CHECK within extremely long loop within duk__match_regexp to prevent CPU usage in systems which use the timeout check for preventing resource exhaustion --- src-input/duk_regexp_executor.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src-input/duk_regexp_executor.c b/src-input/duk_regexp_executor.c index b26ee0e925..fcd97ab8d0 100644 --- a/src-input/duk_regexp_executor.c +++ b/src-input/duk_regexp_executor.c @@ -162,6 +162,22 @@ DUK_LOCAL const duk_uint8_t *duk__match_regexp(duk_re_matcher_ctx *re_ctx, const } re_ctx->steps_count++; +#if defined(DUK_USE_EXEC_TIMEOUT_CHECK) + /* Ensure that timeouts still operate while parsing a regular expression. + * without this in place a large DUK_RE_EXECUTE_STEPS value will lock up the + * parser for many seconds, even if the user is trying to enforce a much lower + * time limit using an interrupt. + */ + if (DUK_USE_EXEC_TIMEOUT_CHECK(re_ctx->thr->heap->heap_udata)) { + DUK_D(DUK_DPRINT("execution timeout within regexp parsing, throwing a RangeError")); + re_ctx->thr->interrupt_init = 0; + re_ctx->thr->interrupt_counter = 0; + DUK_HEAP_CLEAR_INTERRUPT_RUNNING(re_ctx->thr->heap); + DUK_ERROR_RANGE(re_ctx->thr, "execution timeout"); + DUK_WO_NORETURN(return NULL;); + } +#endif /* DUK_USE_EXEC_TIMEOUT_CHECK */ + /* Opcodes are at most 7 bits now so they encode to one byte. If this * were not the case or 'pc' is invalid here (due to a bug etc) we'll * still fail safely through the switch default case. From db5db90c6913f8884e8c54f2bf0dac67d694bb96 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Fri, 15 Nov 2019 09:05:29 +0000 Subject: [PATCH 2/2] add to contributors as per contributing guide --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 135de3e401..88dbae8312 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -64,6 +64,7 @@ and agreed to irrevocably license their contributions under the Duktape * Luis de Bethencourt (https://github.com/luisbg) * Ian Whyman (https://github.com/v00d00) * Rick Sayre (https://github.com/whorfin) +* Craig Edwards (https://github.com/braindigitalis) Other contributions ===================