06de9c
From 6eb257e5c731c691eb137fca94e916ca73941a5a Mon Sep 17 00:00:00 2001
06de9c
From: Michael Catanzaro <mcatanzaro@gnome.org>
06de9c
Date: Fri, 31 Jul 2020 15:21:53 -0500
06de9c
Subject: [PATCH] parser: limit recursion in block and any productions
06de9c
06de9c
If we don't have any limits, we can recurse forever and overflow the
06de9c
stack.
06de9c
06de9c
Fixes #8
06de9c
---
06de9c
 src/cr-parser.c | 44 +++++++++++++++++++++++++++++---------------
06de9c
 1 file changed, 29 insertions(+), 15 deletions(-)
06de9c
06de9c
diff --git a/src/cr-parser.c b/src/cr-parser.c
06de9c
index 18c9a01..f4a62e3 100644
06de9c
--- a/src/cr-parser.c
06de9c
+++ b/src/cr-parser.c
06de9c
@@ -136,6 +136,8 @@ struct _CRParserPriv {
06de9c
 
06de9c
 #define CHARS_TAB_SIZE 12
06de9c
 
06de9c
+#define RECURSIVE_CALLERS_LIMIT 100
06de9c
+
06de9c
 /**
06de9c
  * IS_NUM:
06de9c
  *@a_char: the char to test.
06de9c
@@ -344,9 +346,11 @@ static enum CRStatus cr_parser_parse_selector_core (CRParser * a_this);
06de9c
 
06de9c
 static enum CRStatus cr_parser_parse_declaration_core (CRParser * a_this);
06de9c
 
06de9c
-static enum CRStatus cr_parser_parse_any_core (CRParser * a_this);
06de9c
+static enum CRStatus cr_parser_parse_any_core (CRParser * a_this,
06de9c
+                                               guint      n_calls);
06de9c
 
06de9c
-static enum CRStatus cr_parser_parse_block_core (CRParser * a_this);
06de9c
+static enum CRStatus cr_parser_parse_block_core (CRParser * a_this,
06de9c
+                                                 guint      n_calls);
06de9c
 
06de9c
 static enum CRStatus cr_parser_parse_value_core (CRParser * a_this);
06de9c
 
06de9c
@@ -784,7 +788,7 @@ cr_parser_parse_atrule_core (CRParser * a_this)
06de9c
         cr_parser_try_to_skip_spaces_and_comments (a_this);
06de9c
 
06de9c
         do {
06de9c
-                status = cr_parser_parse_any_core (a_this);
06de9c
+                status = cr_parser_parse_any_core (a_this, 0);
06de9c
         } while (status == CR_OK);
06de9c
 
06de9c
         status = cr_tknzr_get_next_token (PRIVATE (a_this)->tknzr,
06de9c
@@ -795,7 +799,7 @@ cr_parser_parse_atrule_core (CRParser * a_this)
06de9c
                 cr_tknzr_unget_token (PRIVATE (a_this)->tknzr, 
06de9c
                                       token);
06de9c
                 token = NULL;
06de9c
-                status = cr_parser_parse_block_core (a_this);
06de9c
+                status = cr_parser_parse_block_core (a_this, 0);
06de9c
                 CHECK_PARSING_STATUS (status,
06de9c
                                       FALSE);
06de9c
                 goto done;
06de9c
@@ -930,11 +934,11 @@ cr_parser_parse_selector_core (CRParser * a_this)
06de9c
 
06de9c
         RECORD_INITIAL_POS (a_this, &init_pos);
06de9c
 
06de9c
-        status = cr_parser_parse_any_core (a_this);
06de9c
+        status = cr_parser_parse_any_core (a_this, 0);
06de9c
         CHECK_PARSING_STATUS (status, FALSE);
06de9c
 
06de9c
         do {
06de9c
-                status = cr_parser_parse_any_core (a_this);
06de9c
+                status = cr_parser_parse_any_core (a_this, 0);
06de9c
 
06de9c
         } while (status == CR_OK);
06de9c
 
06de9c
@@ -956,10 +960,12 @@ cr_parser_parse_selector_core (CRParser * a_this)
06de9c
  *in chapter 4.1 of the css2 spec.
06de9c
  *block ::= '{' S* [ any | block | ATKEYWORD S* | ';' ]* '}' S*;
06de9c
  *@param a_this the current instance of #CRParser.
06de9c
+ *@param n_calls used to limit recursion depth
06de9c
  *FIXME: code this function.
06de9c
  */
06de9c
 static enum CRStatus
06de9c
-cr_parser_parse_block_core (CRParser * a_this)
06de9c
+cr_parser_parse_block_core (CRParser * a_this,
06de9c
+                            guint      n_calls)
06de9c
 {
06de9c
         CRToken *token = NULL;
06de9c
         CRInputPos init_pos;
06de9c
@@ -967,6 +973,9 @@ cr_parser_parse_block_core (CRParser * a_this)
06de9c
 
06de9c
         g_return_val_if_fail (a_this && PRIVATE (a_this), CR_BAD_PARAM_ERROR);
06de9c
 
06de9c
+        if (n_calls > RECURSIVE_CALLERS_LIMIT)
06de9c
+                return CR_ERROR;
06de9c
+
06de9c
         RECORD_INITIAL_POS (a_this, &init_pos);
06de9c
 
06de9c
         status = cr_tknzr_get_next_token (PRIVATE (a_this)->tknzr, &token);
06de9c
@@ -996,13 +1005,13 @@ cr_parser_parse_block_core (CRParser * a_this)
06de9c
         } else if (token->type == CBO_TK) {
06de9c
                 cr_tknzr_unget_token (PRIVATE (a_this)->tknzr, token);
06de9c
                 token = NULL;
06de9c
-                status = cr_parser_parse_block_core (a_this);
06de9c
+                status = cr_parser_parse_block_core (a_this, n_calls + 1);
06de9c
                 CHECK_PARSING_STATUS (status, FALSE);
06de9c
                 goto parse_block_content;
06de9c
         } else {
06de9c
                 cr_tknzr_unget_token (PRIVATE (a_this)->tknzr, token);
06de9c
                 token = NULL;
06de9c
-                status = cr_parser_parse_any_core (a_this);
06de9c
+                status = cr_parser_parse_any_core (a_this, n_calls + 1);
06de9c
                 CHECK_PARSING_STATUS (status, FALSE);
06de9c
                 goto parse_block_content;
06de9c
         }
06de9c
@@ -1109,7 +1118,7 @@ cr_parser_parse_value_core (CRParser * a_this)
06de9c
                 status = cr_tknzr_unget_token (PRIVATE (a_this)->tknzr,
06de9c
                                                token);
06de9c
                 token = NULL;
06de9c
-                status = cr_parser_parse_block_core (a_this);
06de9c
+                status = cr_parser_parse_block_core (a_this, 0);
06de9c
                 CHECK_PARSING_STATUS (status, FALSE);
06de9c
                 ref++;
06de9c
                 goto continue_parsing;
06de9c
@@ -1123,7 +1132,7 @@ cr_parser_parse_value_core (CRParser * a_this)
06de9c
                 status = cr_tknzr_unget_token (PRIVATE (a_this)->tknzr,
06de9c
                                                token);
06de9c
                 token = NULL;
06de9c
-                status = cr_parser_parse_any_core (a_this);
06de9c
+                status = cr_parser_parse_any_core (a_this, 0);
06de9c
                 if (status == CR_OK) {
06de9c
                         ref++;
06de9c
                         goto continue_parsing;
06de9c
@@ -1162,10 +1171,12 @@ cr_parser_parse_value_core (CRParser * a_this)
06de9c
  *        | FUNCTION | DASHMATCH | '(' any* ')' | '[' any* ']' ] S*;
06de9c
  *
06de9c
  *@param a_this the current instance of #CRParser.
06de9c
+ *@param n_calls used to limit recursion depth
06de9c
  *@return CR_OK upon successfull completion, an error code otherwise.
06de9c
  */
06de9c
 static enum CRStatus
06de9c
-cr_parser_parse_any_core (CRParser * a_this)
06de9c
+cr_parser_parse_any_core (CRParser * a_this,
06de9c
+                          guint      n_calls)
06de9c
 {
06de9c
         CRToken *token1 = NULL,
06de9c
                 *token2 = NULL;
06de9c
@@ -1174,6 +1185,9 @@ cr_parser_parse_any_core (CRParser * a_this)
06de9c
 
06de9c
         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
06de9c
 
06de9c
+        if (n_calls > RECURSIVE_CALLERS_LIMIT)
06de9c
+                return CR_ERROR;
06de9c
+
06de9c
         RECORD_INITIAL_POS (a_this, &init_pos);
06de9c
 
06de9c
         status = cr_tknzr_get_next_token (PRIVATE (a_this)->tknzr, &token1);
06de9c
@@ -1212,7 +1226,7 @@ cr_parser_parse_any_core (CRParser * a_this)
06de9c
                  *We consider parameter as being an "any*" production.
06de9c
                  */
06de9c
                 do {
06de9c
-                        status = cr_parser_parse_any_core (a_this);
06de9c
+                        status = cr_parser_parse_any_core (a_this, n_calls + 1);
06de9c
                 } while (status == CR_OK);
06de9c
 
06de9c
                 ENSURE_PARSING_COND (status == CR_PARSING_ERROR);
06de9c
@@ -1237,7 +1251,7 @@ cr_parser_parse_any_core (CRParser * a_this)
06de9c
                 }
06de9c
 
06de9c
                 do {
06de9c
-                        status = cr_parser_parse_any_core (a_this);
06de9c
+                        status = cr_parser_parse_any_core (a_this, n_calls + 1);
06de9c
                 } while (status == CR_OK);
06de9c
 
06de9c
                 ENSURE_PARSING_COND (status == CR_PARSING_ERROR);
06de9c
@@ -1265,7 +1279,7 @@ cr_parser_parse_any_core (CRParser * a_this)
06de9c
                 }
06de9c
 
06de9c
                 do {
06de9c
-                        status = cr_parser_parse_any_core (a_this);
06de9c
+                        status = cr_parser_parse_any_core (a_this, n_calls + 1);
06de9c
                 } while (status == CR_OK);
06de9c
 
06de9c
                 ENSURE_PARSING_COND (status == CR_PARSING_ERROR);
06de9c
-- 
06de9c
GitLab
06de9c