pgsql: Add recursion depth protections to regular expression matching. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Add recursion depth protections to regular expression matching.
Date
Msg-id E1Zi5RP-0000vc-1J@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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.

Branch
------
REL9_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/c5e38b93c62fa360bb054c41a864ab45d9eccea0

Modified Files
--------------
src/backend/regex/regc_nfa.c |   68 +++++++++++++++++++++++++++++++++++++-----
src/backend/regex/regcomp.c  |   34 +++++++++++++++++++--
src/backend/regex/rege_dfa.c |    7 +++++
src/backend/regex/regexec.c  |    3 ++
src/backend/tcop/postgres.c  |   39 +++++++++++++-----------
src/include/miscadmin.h      |    1 +
src/include/regex/regguts.h  |    4 +++
7 files changed, 129 insertions(+), 27 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix potential infinite loop in regular expression execution.
Next
From: Tom Lane
Date:
Subject: pgsql: Add recursion depth protections to regular expression matching.