From: Bruno Haible Date: Thu, 9 Mar 2023 20:23:47 +0000 (+0100) Subject: xgettext: In language PHP, avoid stack overflow. X-Git-Tag: v0.22~126 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=423572741811037a7a6f1127221aaf1fe6842aff;p=thirdparty%2Fgettext.git xgettext: In language PHP, avoid stack overflow. * gettext-tools/src/x-php.c: Include error-progname.h. (MAX_NESTING_DEPTH): New macro. (paren_nesting_depth, bracket_nesting_depth): New variables. (extract_balanced): Increase and check paren_nesting_depth or bracket_nesting_depth before calling extract_balanced recursively. (extract_python): Initialize paren_nesting_depth and bracket_nesting_depth. * gettext-tools/tests/xgettext-php-stackovfl-1: New file. * gettext-tools/tests/xgettext-php-stackovfl-2: New file. * gettext-tools/tests/xgettext-php-stackovfl-3: New file. * gettext-tools/tests/xgettext-php-stackovfl-4: New file. * gettext-tools/tests/Makefile.am (TESTS): Add them. --- diff --git a/gettext-tools/src/x-php.c b/gettext-tools/src/x-php.c index 78cce8e1d..a92cab4ba 100644 --- a/gettext-tools/src/x-php.c +++ b/gettext-tools/src/x-php.c @@ -1,5 +1,5 @@ /* xgettext PHP backend. - Copyright (C) 2001-2003, 2005-2010, 2014, 2018-2020 Free Software Foundation, Inc. + Copyright (C) 2001-2003, 2005-2010, 2014, 2018-2023 Free Software Foundation, Inc. This file was written by Bruno Haible , 2002. @@ -39,6 +39,7 @@ #include "xg-arglist-parser.h" #include "xg-message.h" #include "error.h" +#include "error-progname.h" #include "xalloc.h" #include "gettext.h" @@ -1398,6 +1399,14 @@ x_php_lex (token_ty *tp) static flag_context_list_table_ty *flag_context_list_table; +/* Maximum supported nesting depth. */ +#define MAX_NESTING_DEPTH 1000 + +/* Current nesting depths. */ +static int paren_nesting_depth; +static int bracket_nesting_depth; + + /* The file is broken into tokens. Scan the token stream, looking for a keyword, followed by a left paren, followed by a string. When we see this sequence, we have something to remember. We assume we are @@ -1472,6 +1481,12 @@ extract_balanced (message_list_ty *mlp, continue; case token_type_lparen: + if (++paren_nesting_depth > MAX_NESTING_DEPTH) + { + error_with_progname = false; + error (EXIT_FAILURE, 0, _("%s:%d: error: too many open parentheses"), + logical_file_name, line_number); + } if (extract_balanced (mlp, token_type_rparen, inner_context, next_context_iter, arglist_parser_alloc (mlp, @@ -1480,6 +1495,7 @@ extract_balanced (message_list_ty *mlp, arglist_parser_done (argparser, arg); return true; } + paren_nesting_depth--; next_context_iter = null_context_list_iterator; state = 0; continue; @@ -1505,6 +1521,12 @@ extract_balanced (message_list_ty *mlp, continue; case token_type_lbracket: + if (++bracket_nesting_depth > MAX_NESTING_DEPTH) + { + error_with_progname = false; + error (EXIT_FAILURE, 0, _("%s:%d: error: too many open brackets"), + logical_file_name, line_number); + } if (extract_balanced (mlp, token_type_rbracket, null_context, null_context_list_iterator, arglist_parser_alloc (mlp, NULL))) @@ -1512,6 +1534,7 @@ extract_balanced (message_list_ty *mlp, arglist_parser_done (argparser, arg); return true; } + bracket_nesting_depth--; next_context_iter = null_context_list_iterator; state = 0; continue; @@ -1598,6 +1621,8 @@ extract_php (FILE *f, phase5_last = token_type_eof; flag_context_list_table = flag_table; + paren_nesting_depth = 0; + bracket_nesting_depth = 0; init_keywords (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 9171a2ae6..3911da96d 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -130,6 +130,8 @@ TESTS = gettext-1 gettext-2 \ xgettext-perl-stackovfl-1 xgettext-perl-stackovfl-2 \ xgettext-perl-stackovfl-3 xgettext-perl-stackovfl-4 \ xgettext-php-1 xgettext-php-2 xgettext-php-3 xgettext-php-4 \ + xgettext-php-stackovfl-1 xgettext-php-stackovfl-2 \ + xgettext-php-stackovfl-3 xgettext-php-stackovfl-4 \ xgettext-po-1 xgettext-po-2 \ xgettext-properties-1 xgettext-properties-2 xgettext-properties-3 \ xgettext-properties-4 \ diff --git a/gettext-tools/tests/xgettext-php-stackovfl-1 b/gettext-tools/tests/xgettext-php-stackovfl-1 new file mode 100755 index 000000000..797701074 --- /dev/null +++ b/gettext-tools/tests/xgettext-php-stackovfl-1 @@ -0,0 +1,65 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test PHP support: stack overflow prevented by nesting depth check. + +cat < xg-ph-so-1.php + +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-ph-so-1.tmp xg-ph-so-1.php || Exit 1 +LC_ALL=C tr -d '\r' < xg-ph-so-1.tmp.po > xg-ph-so-1.po || Exit 1 + +cat < xg-ph-so-1.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-ph-so-1.ok xg-ph-so-1.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-php-stackovfl-2 b/gettext-tools/tests/xgettext-php-stackovfl-2 new file mode 100755 index 000000000..28f1c4073 --- /dev/null +++ b/gettext-tools/tests/xgettext-php-stackovfl-2 @@ -0,0 +1,58 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test PHP support: stack overflow prevented by nesting depth check. + +cat < xg-ph-so-2.php + +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-ph-so-2.tmp xg-ph-so-2.php 2>xg-ph-so-2.err +result=$? +cat xg-ph-so-2.err +test $result = 1 || Exit 1 + +exit 0 diff --git a/gettext-tools/tests/xgettext-php-stackovfl-3 b/gettext-tools/tests/xgettext-php-stackovfl-3 new file mode 100755 index 000000000..5072198f5 --- /dev/null +++ b/gettext-tools/tests/xgettext-php-stackovfl-3 @@ -0,0 +1,65 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test PHP support: stack overflow prevented by nesting depth check. + +cat < xg-ph-so-3.php + +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-ph-so-3.tmp xg-ph-so-3.php || Exit 1 +LC_ALL=C tr -d '\r' < xg-ph-so-3.tmp.po > xg-ph-so-3.po || Exit 1 + +cat < xg-ph-so-3.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-ph-so-3.ok xg-ph-so-3.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-php-stackovfl-4 b/gettext-tools/tests/xgettext-php-stackovfl-4 new file mode 100755 index 000000000..f3ae1b238 --- /dev/null +++ b/gettext-tools/tests/xgettext-php-stackovfl-4 @@ -0,0 +1,60 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test PHP support: stack overflow prevented by nesting depth check. + +cat < xg-ph-so-4.php + +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-ph-so-4.tmp xg-ph-so-4.php 2>xg-ph-so-4.err +result=$? +cat xg-ph-so-4.err +test $result = 1 || Exit 1 + +exit 0