From 20c627707c44deaed92c5d67b350f27e06e24228 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 2 Oct 2015 14:51:58 -0400 Subject: Add recursion depth protections to regular expression matching. Some of the functions in regex compilation and execution recurse, and therefore could in principle be driven to stack overflow. The Tcl crew has seen this happen in practice in duptraverse(), though their fix was to put in a hard-wired limit on the number of recursive levels, which is not too appetizing --- fortunately, we have enough infrastructure to check the actually available stack. Greg Stark has also seen it in other places while fuzz testing on a machine with limited stack space. Let's put guards in to prevent crashes in all these places. Since the regex code would leak memory if we simply threw elog(ERROR), we have to introduce an API that checks for stack depth without throwing such an error. Fortunately that's not difficult. --- src/backend/regex/regexec.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/backend/regex/regexec.c') diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c index 66847d4adfa..45c3a579c75 100644 --- a/src/backend/regex/regexec.c +++ b/src/backend/regex/regexec.c @@ -624,6 +624,9 @@ cdissect(struct vars * v, /* handy place to check for operation cancel */ if (CANCEL_REQUESTED(v->re)) return REG_CANCEL; + /* ... and stack overrun */ + if (STACK_TOO_DEEP(v->re)) + return REG_ETOOBIG; switch (t->op) { -- cgit v1.2.3