[Forensics-changes] [yara] 215/415: Fix syntax error handling in hex strings

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:07 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch debian
in repository yara.

commit 5d6a08d579be635e9325e95be898140b66e57f44
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Mon Nov 11 16:34:44 2013 +0000

    Fix syntax error handling in hex strings
---
 libyara/compiler.c    | 78 +---------------------------------------
 libyara/hex_grammar.c | 99 +++++++++++++++++++++++++++++----------------------
 libyara/hex_grammar.h |  2 +-
 libyara/hex_grammar.y | 12 +++++++
 libyara/hex_lexer.c   | 75 +++++++++++++++++++++++++++-----------
 libyara/hex_lexer.h   |  8 +++++
 libyara/hex_lexer.l   | 43 +++++++++++++++++++---
 libyara/parser.c      |  4 ++-
 libyara/yara.h        | 70 +++++++++++++++++-------------------
 9 files changed, 206 insertions(+), 185 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index 1bfaba3..8a9cade 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -691,83 +691,6 @@ char* yr_compiler_get_error_message(
           "duplicate loop identifier \"%s\"",
           compiler->last_error_extra_info);
       break;
-    case ERROR_INVALID_CHAR_IN_HEX_STRING:
-      snprintf(
-          buffer,
-          buffer_size,
-          "invalid char in hex string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_MISMATCHED_BRACKET:
-      snprintf(
-          buffer,
-          buffer_size,
-          "mismatched bracket in string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_SKIP_AT_END:
-      snprintf(
-          buffer,
-          buffer_size,
-          "skip at the end of string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_INVALID_SKIP_VALUE:
-      snprintf(
-          buffer,
-          buffer_size,
-          "invalid skip in string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_UNPAIRED_NIBBLE:
-      snprintf(
-          buffer,
-          buffer_size,
-          "unpaired nibble in string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_CONSECUTIVE_SKIPS:
-      snprintf(
-          buffer,
-          buffer_size,
-          "two consecutive skips in string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_MISPLACED_WILDCARD_OR_SKIP:
-      snprintf(
-          buffer,
-          buffer_size,
-          "misplaced wildcard or skip at string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_MISPLACED_OR_OPERATOR:
-      snprintf(
-          buffer,
-          buffer_size,
-          "misplaced OR (|) operator at string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_NESTED_OR_OPERATION:
-      snprintf(
-          buffer,
-          buffer_size,
-          "nested OR (|) operator at string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_INVALID_OR_OPERATION_SYNTAX:
-      snprintf(
-          buffer,
-          buffer_size,
-          "invalid syntax at hex string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
-    case ERROR_SKIP_INSIDE_OR_OPERATION:
-      snprintf(
-          buffer,
-          buffer_size,
-          "skip inside an OR (|) operation at string \"%s\"",
-          compiler->last_error_extra_info);
-      break;
     case ERROR_UNDEFINED_STRING:
       snprintf(
           buffer,
@@ -802,6 +725,7 @@ char* yr_compiler_get_error_message(
           buffer_size,
           "wrong use of anonymous string");
       break;
+    case ERROR_INVALID_HEX_STRING:
     case ERROR_INVALID_REGULAR_EXPRESSION:
     case ERROR_SYNTAX_ERROR:
       snprintf(
diff --git a/libyara/hex_grammar.c b/libyara/hex_grammar.c
index 4dc5174..4a125bd 100644
--- a/libyara/hex_grammar.c
+++ b/libyara/hex_grammar.c
@@ -139,7 +139,7 @@ yydebug = 1;
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 54 "hex_grammar.y"
+#line 58 "hex_grammar.y"
 {
   int integer;
   RE_NODE *re_node;
@@ -444,8 +444,8 @@ static const yytype_int8 yyrhs[] =
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
 static const yytype_uint8 yyrline[] =
 {
-       0,    67,    67,    75,    79,    87,    91,    95,   103,   116,
-     132,   136,   145,   168
+       0,    71,    71,    79,    83,    91,    95,    99,   107,   120,
+     144,   148,   157,   180
 };
 #endif
 
@@ -574,7 +574,7 @@ do								\
     }								\
   else								\
     {								\
-      yyerror (yyscanner, YY_("syntax error: cannot back up")); \
+      yyerror (yyscanner, lex_env, YY_("syntax error: cannot back up")); \
       YYERROR;							\
     }								\
 while (YYID (0))
@@ -631,7 +631,7 @@ while (YYID (0))
 #ifdef YYLEX_PARAM
 # define YYLEX yylex (&yylval, YYLEX_PARAM)
 #else
-# define YYLEX yylex (&yylval, yyscanner)
+# define YYLEX yylex (&yylval, yyscanner, lex_env)
 #endif
 
 /* Enable debugging if requested.  */
@@ -654,7 +654,7 @@ do {									  \
     {									  \
       YYFPRINTF (stderr, "%s ", Title);					  \
       yy_symbol_print (stderr,						  \
-		  Type, Value, yyscanner); \
+		  Type, Value, yyscanner, lex_env); \
       YYFPRINTF (stderr, "\n");						  \
     }									  \
 } while (YYID (0))
@@ -668,19 +668,21 @@ do {									  \
 #if (defined __STDC__ || defined __C99__FUNC__ \
      || defined __cplusplus || defined _MSC_VER)
 static void
-yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *yyscanner)
+yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *yyscanner, LEX_ENVIRONMENT *lex_env)
 #else
 static void
-yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner)
+yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner, lex_env)
     FILE *yyoutput;
     int yytype;
     YYSTYPE const * const yyvaluep;
     void *yyscanner;
+    LEX_ENVIRONMENT *lex_env;
 #endif
 {
   if (!yyvaluep)
     return;
   YYUSE (yyscanner);
+  YYUSE (lex_env);
 # ifdef YYPRINT
   if (yytype < YYNTOKENS)
     YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
@@ -702,14 +704,15 @@ yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner)
 #if (defined __STDC__ || defined __C99__FUNC__ \
      || defined __cplusplus || defined _MSC_VER)
 static void
-yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *yyscanner)
+yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, void *yyscanner, LEX_ENVIRONMENT *lex_env)
 #else
 static void
-yy_symbol_print (yyoutput, yytype, yyvaluep, yyscanner)
+yy_symbol_print (yyoutput, yytype, yyvaluep, yyscanner, lex_env)
     FILE *yyoutput;
     int yytype;
     YYSTYPE const * const yyvaluep;
     void *yyscanner;
+    LEX_ENVIRONMENT *lex_env;
 #endif
 {
   if (yytype < YYNTOKENS)
@@ -717,7 +720,7 @@ yy_symbol_print (yyoutput, yytype, yyvaluep, yyscanner)
   else
     YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
 
-  yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner);
+  yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner, lex_env);
   YYFPRINTF (yyoutput, ")");
 }
 
@@ -757,13 +760,14 @@ do {								\
 #if (defined __STDC__ || defined __C99__FUNC__ \
      || defined __cplusplus || defined _MSC_VER)
 static void
-yy_reduce_print (YYSTYPE *yyvsp, int yyrule, void *yyscanner)
+yy_reduce_print (YYSTYPE *yyvsp, int yyrule, void *yyscanner, LEX_ENVIRONMENT *lex_env)
 #else
 static void
-yy_reduce_print (yyvsp, yyrule, yyscanner)
+yy_reduce_print (yyvsp, yyrule, yyscanner, lex_env)
     YYSTYPE *yyvsp;
     int yyrule;
     void *yyscanner;
+    LEX_ENVIRONMENT *lex_env;
 #endif
 {
   int yynrhs = yyr2[yyrule];
@@ -777,7 +781,7 @@ yy_reduce_print (yyvsp, yyrule, yyscanner)
       fprintf (stderr, "   $%d = ", yyi + 1);
       yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
 		       &(yyvsp[(yyi + 1) - (yynrhs)])
-		       		       , yyscanner);
+		       		       , yyscanner, lex_env);
       fprintf (stderr, "\n");
     }
 }
@@ -785,7 +789,7 @@ yy_reduce_print (yyvsp, yyrule, yyscanner)
 # define YY_REDUCE_PRINT(Rule)		\
 do {					\
   if (yydebug)				\
-    yy_reduce_print (yyvsp, Rule, yyscanner); \
+    yy_reduce_print (yyvsp, Rule, yyscanner, lex_env); \
 } while (YYID (0))
 
 /* Nonzero means print parse trace.  It is left uninitialized so that
@@ -1036,18 +1040,20 @@ yysyntax_error (char *yyresult, int yystate, int yychar)
 #if (defined __STDC__ || defined __C99__FUNC__ \
      || defined __cplusplus || defined _MSC_VER)
 static void
-yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, void *yyscanner)
+yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, void *yyscanner, LEX_ENVIRONMENT *lex_env)
 #else
 static void
-yydestruct (yymsg, yytype, yyvaluep, yyscanner)
+yydestruct (yymsg, yytype, yyvaluep, yyscanner, lex_env)
     const char *yymsg;
     int yytype;
     YYSTYPE *yyvaluep;
     void *yyscanner;
+    LEX_ENVIRONMENT *lex_env;
 #endif
 {
   YYUSE (yyvaluep);
   YYUSE (yyscanner);
+  YYUSE (lex_env);
 
   if (!yymsg)
     yymsg = "Deleting";
@@ -1072,7 +1078,7 @@ int yyparse ();
 #endif
 #else /* ! YYPARSE_PARAM */
 #if defined __STDC__ || defined __cplusplus
-int yyparse (void *yyscanner);
+int yyparse (void *yyscanner, LEX_ENVIRONMENT *lex_env);
 #else
 int yyparse ();
 #endif
@@ -1101,11 +1107,12 @@ yyparse (YYPARSE_PARAM)
 #if (defined __STDC__ || defined __C99__FUNC__ \
      || defined __cplusplus || defined _MSC_VER)
 int
-yyparse (void *yyscanner)
+yyparse (void *yyscanner, LEX_ENVIRONMENT *lex_env)
 #else
 int
-yyparse (yyscanner)
+yyparse (yyscanner, lex_env)
     void *yyscanner;
+    LEX_ENVIRONMENT *lex_env;
 #endif
 #endif
 {
@@ -1362,7 +1369,7 @@ yyreduce:
   switch (yyn)
     {
         case 2:
-#line 68 "hex_grammar.y"
+#line 72 "hex_grammar.y"
     {
                 RE* re = yyget_extra(yyscanner);
                 re->root_node = (yyvsp[(2) - (3)].re_node);
@@ -1370,14 +1377,14 @@ yyreduce:
     break;
 
   case 3:
-#line 76 "hex_grammar.y"
+#line 80 "hex_grammar.y"
     {
             (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
          }
     break;
 
   case 4:
-#line 80 "hex_grammar.y"
+#line 84 "hex_grammar.y"
     {
             (yyval.re_node) = yr_re_node_create(RE_NODE_CONCAT, (yyvsp[(1) - (2)].re_node), (yyvsp[(2) - (2)].re_node));
             ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
@@ -1385,21 +1392,21 @@ yyreduce:
     break;
 
   case 5:
-#line 88 "hex_grammar.y"
+#line 92 "hex_grammar.y"
     {
           (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
         }
     break;
 
   case 6:
-#line 92 "hex_grammar.y"
+#line 96 "hex_grammar.y"
     {
           (yyval.re_node) = (yyvsp[(2) - (3)].re_node);
         }
     break;
 
   case 7:
-#line 96 "hex_grammar.y"
+#line 100 "hex_grammar.y"
     {
           mark_as_not_literal();
           (yyval.re_node) = (yyvsp[(2) - (3)].re_node);
@@ -1407,7 +1414,7 @@ yyreduce:
     break;
 
   case 8:
-#line 104 "hex_grammar.y"
+#line 108 "hex_grammar.y"
     {
           RE_NODE* re_any;
 
@@ -1423,10 +1430,18 @@ yyreduce:
     break;
 
   case 9:
-#line 117 "hex_grammar.y"
+#line 121 "hex_grammar.y"
     {
           RE_NODE* re_any;
 
+          if ((yyvsp[(1) - (3)].integer) > (yyvsp[(3) - (3)].integer))
+          {
+            RE* re = yyget_extra(yyscanner);
+            re->error_code = ERROR_INVALID_HEX_STRING;
+            re->error_message = yr_strdup("invalid range");
+            YYABORT;
+          }
+
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
@@ -1439,14 +1454,14 @@ yyreduce:
     break;
 
   case 10:
-#line 133 "hex_grammar.y"
+#line 145 "hex_grammar.y"
     {
                   (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
                }
     break;
 
   case 11:
-#line 137 "hex_grammar.y"
+#line 149 "hex_grammar.y"
     {
                   mark_as_not_literal();
                   (yyval.re_node) = yr_re_node_create(RE_NODE_ALT, (yyvsp[(1) - (3)].re_node), (yyvsp[(3) - (3)].re_node));
@@ -1456,7 +1471,7 @@ yyreduce:
     break;
 
   case 12:
-#line 146 "hex_grammar.y"
+#line 158 "hex_grammar.y"
     {
           RE* re = yyget_extra(yyscanner);
 
@@ -1482,7 +1497,7 @@ yyreduce:
     break;
 
   case 13:
-#line 169 "hex_grammar.y"
+#line 181 "hex_grammar.y"
     {
           uint8_t mask = (yyvsp[(1) - (1)].integer) >> 8;
 
@@ -1508,7 +1523,7 @@ yyreduce:
 
 
 /* Line 1267 of yacc.c.  */
-#line 1512 "hex_grammar.c"
+#line 1527 "hex_grammar.c"
       default: break;
     }
   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
@@ -1544,7 +1559,7 @@ yyerrlab:
     {
       ++yynerrs;
 #if ! YYERROR_VERBOSE
-      yyerror (yyscanner, YY_("syntax error"));
+      yyerror (yyscanner, lex_env, YY_("syntax error"));
 #else
       {
 	YYSIZE_T yysize = yysyntax_error (0, yystate, yychar);
@@ -1568,11 +1583,11 @@ yyerrlab:
 	if (0 < yysize && yysize <= yymsg_alloc)
 	  {
 	    (void) yysyntax_error (yymsg, yystate, yychar);
-	    yyerror (yyscanner, yymsg);
+	    yyerror (yyscanner, lex_env, yymsg);
 	  }
 	else
 	  {
-	    yyerror (yyscanner, YY_("syntax error"));
+	    yyerror (yyscanner, lex_env, YY_("syntax error"));
 	    if (yysize != 0)
 	      goto yyexhaustedlab;
 	  }
@@ -1596,7 +1611,7 @@ yyerrlab:
       else
 	{
 	  yydestruct ("Error: discarding",
-		      yytoken, &yylval, yyscanner);
+		      yytoken, &yylval, yyscanner, lex_env);
 	  yychar = YYEMPTY;
 	}
     }
@@ -1652,7 +1667,7 @@ yyerrlab1:
 
 
       yydestruct ("Error: popping",
-		  yystos[yystate], yyvsp, yyscanner);
+		  yystos[yystate], yyvsp, yyscanner, lex_env);
       YYPOPSTACK (1);
       yystate = *yyssp;
       YY_STACK_PRINT (yyss, yyssp);
@@ -1690,7 +1705,7 @@ yyabortlab:
 | yyexhaustedlab -- memory exhaustion comes here.  |
 `-------------------------------------------------*/
 yyexhaustedlab:
-  yyerror (yyscanner, YY_("memory exhausted"));
+  yyerror (yyscanner, lex_env, YY_("memory exhausted"));
   yyresult = 2;
   /* Fall through.  */
 #endif
@@ -1698,7 +1713,7 @@ yyexhaustedlab:
 yyreturn:
   if (yychar != YYEOF && yychar != YYEMPTY)
      yydestruct ("Cleanup: discarding lookahead",
-		 yytoken, &yylval, yyscanner);
+		 yytoken, &yylval, yyscanner, lex_env);
   /* Do not reclaim the symbols of the rule which action triggered
      this YYABORT or YYACCEPT.  */
   YYPOPSTACK (yylen);
@@ -1706,7 +1721,7 @@ yyreturn:
   while (yyssp != yyss)
     {
       yydestruct ("Cleanup: popping",
-		  yystos[*yyssp], yyvsp, yyscanner);
+		  yystos[*yyssp], yyvsp, yyscanner, lex_env);
       YYPOPSTACK (1);
     }
 #ifndef yyoverflow
@@ -1722,7 +1737,7 @@ yyreturn:
 }
 
 
-#line 192 "hex_grammar.y"
+#line 204 "hex_grammar.y"
 
 
 
diff --git a/libyara/hex_grammar.h b/libyara/hex_grammar.h
index c38000e..06ad3f0 100644
--- a/libyara/hex_grammar.h
+++ b/libyara/hex_grammar.h
@@ -54,7 +54,7 @@
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 54 "hex_grammar.y"
+#line 58 "hex_grammar.y"
 {
   int integer;
   RE_NODE *re_node;
diff --git a/libyara/hex_grammar.y b/libyara/hex_grammar.y
index d6e029d..c487a5c 100644
--- a/libyara/hex_grammar.y
+++ b/libyara/hex_grammar.y
@@ -48,8 +48,12 @@ yydebug = 1;
 
 %name-prefix="hex_yy"
 %pure-parser
+
 %parse-param {void *yyscanner}
+%parse-param {LEX_ENVIRONMENT *lex_env}
+
 %lex-param {yyscan_t yyscanner}
+%lex-param {LEX_ENVIRONMENT *lex_env}
 
 %union {
   int integer;
@@ -117,6 +121,14 @@ range : _NUMBER_
         {
           RE_NODE* re_any;
 
+          if ($1 > $3)
+          {
+            RE* re = yyget_extra(yyscanner);
+            re->error_code = ERROR_INVALID_HEX_STRING;
+            re->error_message = yr_strdup("invalid range");
+            YYABORT;
+          }
+
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           $$ = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
diff --git a/libyara/hex_lexer.c b/libyara/hex_lexer.c
index 605e4c0..1320661 100644
--- a/libyara/hex_lexer.c
+++ b/libyara/hex_lexer.c
@@ -478,6 +478,7 @@ limitations under the License.
 
 #include "yara.h"
 #include "atoms.h"
+#include "mem.h"
 #include "re.h"
 #include "hex_grammar.h"
 #include "hex_lexer.h"
@@ -488,9 +489,23 @@ limitations under the License.
 #define snprintf _snprintf
 #endif
 
+
+#define YY_DECL int hex_yylex \
+    (YYSTYPE * yylval_param , yyscan_t yyscanner, LEX_ENVIRONMENT* lex_env)
+
+#define LEX_ENV  ((LEX_ENVIRONMENT*) lex_env)
+
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = hex_yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
+
 #define YY_NO_UNISTD_H 1
 
-#line 494 "hex_lexer.c"
+#line 509 "hex_lexer.c"
 
 #define INITIAL 0
 #define range 1
@@ -724,11 +739,11 @@ YY_DECL
 	register int yy_act;
     struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
 
-#line 52 "hex_lexer.l"
+#line 67 "hex_lexer.l"
 
 
 
-#line 732 "hex_lexer.c"
+#line 747 "hex_lexer.c"
 
     yylval = yylval_param;
 
@@ -827,7 +842,7 @@ do_action:	/* This label is used only to access EOF actions. */
 
 case 1:
 YY_RULE_SETUP
-#line 55 "hex_lexer.l"
+#line 70 "hex_lexer.l"
 {
 
   yylval->integer = xtoi(yytext);
@@ -836,7 +851,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 2:
 YY_RULE_SETUP
-#line 61 "hex_lexer.l"
+#line 76 "hex_lexer.l"
 {
 
   yytext[1] = '0'; // replace ? by 0
@@ -846,7 +861,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 3:
 YY_RULE_SETUP
-#line 68 "hex_lexer.l"
+#line 83 "hex_lexer.l"
 {
 
   yytext[0] = '0'; // replace ? by 0
@@ -856,7 +871,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 4:
 YY_RULE_SETUP
-#line 75 "hex_lexer.l"
+#line 90 "hex_lexer.l"
 {
 
   yylval->integer = 0x0000;
@@ -865,7 +880,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 5:
 YY_RULE_SETUP
-#line 81 "hex_lexer.l"
+#line 96 "hex_lexer.l"
 {
 
   BEGIN(range);
@@ -874,23 +889,30 @@ YY_RULE_SETUP
 	YY_BREAK
 case 6:
 YY_RULE_SETUP
-#line 87 "hex_lexer.l"
+#line 102 "hex_lexer.l"
 {
   return yytext[0];
 }
 	YY_BREAK
 case 7:
 YY_RULE_SETUP
-#line 91 "hex_lexer.l"
+#line 106 "hex_lexer.l"
 {
 
   yylval->integer = atoi(yytext);
+
+  if (yylval->integer > INT16_MAX)
+  {
+    yyerror(yyscanner, lex_env, "range value too large");
+    yyterminate();
+  }
+
   return _NUMBER_;
 }
 	YY_BREAK
 case 8:
 YY_RULE_SETUP
-#line 97 "hex_lexer.l"
+#line 119 "hex_lexer.l"
 {
 
   BEGIN(INITIAL);
@@ -900,12 +922,12 @@ YY_RULE_SETUP
 case 9:
 /* rule 9 can match eol */
 YY_RULE_SETUP
-#line 104 "hex_lexer.l"
+#line 126 "hex_lexer.l"
 // skip whitespace
 	YY_BREAK
 case 10:
 YY_RULE_SETUP
-#line 107 "hex_lexer.l"
+#line 129 "hex_lexer.l"
 {
 
   if (yytext[0] >= 32 && yytext[0] < 127)
@@ -914,17 +936,17 @@ YY_RULE_SETUP
   }
   else
   {
-    yyerror(yyscanner, "non-ascii character");
+    yyerror(yyscanner, lex_env, "non-ascii character");
     yyterminate();
   }
 }
 	YY_BREAK
 case 11:
 YY_RULE_SETUP
-#line 120 "hex_lexer.l"
+#line 142 "hex_lexer.l"
 ECHO;
 	YY_BREAK
-#line 928 "hex_lexer.c"
+#line 950 "hex_lexer.c"
 case YY_STATE_EOF(INITIAL):
 case YY_STATE_EOF(range):
 	yyterminate();
@@ -2102,24 +2124,29 @@ void hex_yyfree (void * ptr , yyscan_t yyscanner)
 
 #define YYTABLES_NAME "yytables"
 
-#line 120 "hex_lexer.l"
+#line 142 "hex_lexer.l"
 
 
 
 void yyerror(
     yyscan_t yyscanner,
+    LEX_ENVIRONMENT* lex_env,
     const char *error_message)
 {
-  printf("%s", error_message);
-
+  if (lex_env->last_error_message == NULL)
+  {
+    lex_env->last_error_message = yr_strdup(error_message);
+  }
 }
 
-
 int yr_parse_hex_string(
   const char* hex_string,
   RE** re)
 {
   yyscan_t yyscanner;
+  LEX_ENVIRONMENT lex_env;
+
+  lex_env.last_error_message = NULL;
 
   FAIL_ON_ERROR(yr_re_create(re));
 
@@ -2134,9 +2161,15 @@ int yr_parse_hex_string(
   hex_yylex_init(&yyscanner);
   hex_yyset_extra(*re,yyscanner);
   hex_yy_scan_string(hex_string,yyscanner);
-  yyparse(yyscanner);
+  yyparse(yyscanner, &lex_env);
   hex_yylex_destroy(yyscanner);
 
+  if (lex_env.last_error_message != NULL)
+  {
+    (*re)->error_message = lex_env.last_error_message;
+    return ERROR_INVALID_HEX_STRING;
+  }
+
   return (*re)->error_code;
 }
 
diff --git a/libyara/hex_lexer.h b/libyara/hex_lexer.h
index 1830eeb..412b7bb 100644
--- a/libyara/hex_lexer.h
+++ b/libyara/hex_lexer.h
@@ -36,11 +36,19 @@ typedef void* yyscan_t;
 #define YY_USE_CONST
 
 
+typedef struct _LEX_ENVIRONMENT 
+{
+  const char* last_error_message;
+
+} LEX_ENVIRONMENT;
+
+
 YY_EXTRA_TYPE yyget_extra(
     yyscan_t yyscanner);
 
 void yyerror(
     yyscan_t yyscanner,
+    LEX_ENVIRONMENT* lex_env,
     const char *error_message);
 
 int yr_parse_hex_string(
diff --git a/libyara/hex_lexer.l b/libyara/hex_lexer.l
index 39cc6a7..e10c81d 100644
--- a/libyara/hex_lexer.l
+++ b/libyara/hex_lexer.l
@@ -20,6 +20,7 @@ limitations under the License.
 
 #include "yara.h"
 #include "atoms.h"
+#include "mem.h"
 #include "re.h"
 #include "hex_grammar.h"
 #include "hex_lexer.h"
@@ -30,6 +31,20 @@ limitations under the License.
 #define snprintf _snprintf
 #endif
 
+
+#define YY_DECL int hex_yylex \
+    (YYSTYPE * yylval_param , yyscan_t yyscanner, LEX_ENVIRONMENT* lex_env)
+
+#define LEX_ENV  ((LEX_ENVIRONMENT*) lex_env)
+
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
+
 %}
 
 %option reentrant bison-bridge
@@ -91,6 +106,13 @@ hexdigit      [a-fA-F0-9]
 <range>{digit}+ {
 
   yylval->integer = atoi(yytext);
+
+  if (yylval->integer > INT16_MAX)
+  {
+    yyerror(yyscanner, lex_env, "range value too large");
+    yyterminate();
+  }
+
   return _NUMBER_;
 }
 
@@ -112,7 +134,7 @@ hexdigit      [a-fA-F0-9]
   }
   else
   {
-    yyerror(yyscanner, "non-ascii character");
+    yyerror(yyscanner, lex_env, "non-ascii character");
     yyterminate();
   }
 }
@@ -121,18 +143,23 @@ hexdigit      [a-fA-F0-9]
 
 void yyerror(
     yyscan_t yyscanner,
+    LEX_ENVIRONMENT* lex_env,
     const char *error_message)
 {
-  printf("%s", error_message);
-
+  if (lex_env->last_error_message == NULL)
+  {
+    lex_env->last_error_message = yr_strdup(error_message);
+  }
 }
 
-
 int yr_parse_hex_string(
   const char* hex_string,
   RE** re)
 {
   yyscan_t yyscanner;
+  LEX_ENVIRONMENT lex_env;
+
+  lex_env.last_error_message = NULL;
 
   FAIL_ON_ERROR(yr_re_create(re));
 
@@ -147,9 +174,15 @@ int yr_parse_hex_string(
   yylex_init(&yyscanner);
   yyset_extra(*re, yyscanner);
   yy_scan_string(hex_string, yyscanner);
-  yyparse(yyscanner);
+  yyparse(yyscanner, &lex_env);
   yylex_destroy(yyscanner);
 
+  if (lex_env.last_error_message != NULL)
+  {
+    (*re)->error_message = lex_env.last_error_message;
+    return ERROR_INVALID_HEX_STRING;
+  }
+
   return (*re)->error_code;
 }
 
diff --git a/libyara/parser.c b/libyara/parser.c
index 4e8a130..e086374 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -279,7 +279,9 @@ STRING* yr_parser_reduce_string_declaration(
       snprintf(
           message, 
           sizeof(message), 
-          "invalid regular expression in string \"%s\": %s", 
+          "invalid %s in string \"%s\": %s", 
+          (flags & STRING_GFLAGS_HEXADECIMAL) ? 
+              "hex string" : "regular expression",
           identifier, 
           re->error_message);
 
diff --git a/libyara/yara.h b/libyara/yara.h
index 34a4d65..5945e98 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -56,44 +56,38 @@ typedef pthread_mutex_t mutex_t;
 
 #define ERROR_INSUFICIENT_MEMORY                1
 #define ERROR_DUPLICATE_RULE_IDENTIFIER         2
-#define ERROR_INVALID_CHAR_IN_HEX_STRING        3
-#define ERROR_MISMATCHED_BRACKET                4
-#define ERROR_SKIP_AT_END                       5
-#define ERROR_INVALID_SKIP_VALUE                6
-#define ERROR_UNPAIRED_NIBBLE                   7
-#define ERROR_CONSECUTIVE_SKIPS                 8
-#define ERROR_MISPLACED_WILDCARD_OR_SKIP        9
-#define ERROR_UNDEFINED_STRING                  10
-#define ERROR_UNDEFINED_IDENTIFIER              11
-#define ERROR_COULD_NOT_OPEN_FILE               12
-#define ERROR_INVALID_REGULAR_EXPRESSION        13
-#define ERROR_SYNTAX_ERROR                      14
-#define ERROR_DUPLICATE_TAG_IDENTIFIER          15
-#define ERROR_UNREFERENCED_STRING               16
-#define ERROR_DUPLICATE_STRING_IDENTIFIER       17
-#define ERROR_CALLBACK_ERROR                    18
-#define ERROR_MISPLACED_OR_OPERATOR             19
-#define ERROR_INVALID_OR_OPERATION_SYNTAX       20
-#define ERROR_SKIP_INSIDE_OR_OPERATION          21
-#define ERROR_NESTED_OR_OPERATION               22
-#define ERROR_MISPLACED_ANONYMOUS_STRING        23
-#define ERROR_COULD_NOT_MAP_FILE                24
-#define ERROR_ZERO_LENGTH_FILE                  25
-#define ERROR_INVALID_ARGUMENT                  26
-#define ERROR_DUPLICATE_META_IDENTIFIER         27
-#define ERROR_INCLUDES_CIRCULAR_REFERENCE       28
-#define ERROR_INCORRECT_VARIABLE_TYPE           29
-#define ERROR_COULD_NOT_ATTACH_TO_PROCESS       30
-#define ERROR_VECTOR_TOO_LONG                   31
-#define ERROR_INCLUDE_DEPTH_EXCEEDED            32
-#define ERROR_INVALID_FILE                      33
-#define ERROR_CORRUPT_FILE                      34
-#define ERROR_UNSUPPORTED_FILE_VERSION          35
-#define ERROR_EXEC_STACK_OVERFLOW               36
-#define ERROR_SCAN_TIMEOUT                      37
-#define ERROR_LOOP_NESTING_LIMIT_EXCEEDED       38
-#define ERROR_DUPLICATE_LOOP_IDENTIFIER         39
-#define ERROR_TOO_MANY_SCAN_THREADS             40
+#define ERROR_INVALID_HEX_STRING                3
+#define ERROR_UNDEFINED_STRING                  4
+#define ERROR_UNDEFINED_IDENTIFIER              5
+#define ERROR_COULD_NOT_OPEN_FILE               6
+#define ERROR_INVALID_REGULAR_EXPRESSION        7
+#define ERROR_SYNTAX_ERROR                      8
+#define ERROR_DUPLICATE_TAG_IDENTIFIER          9
+#define ERROR_UNREFERENCED_STRING               10
+#define ERROR_DUPLICATE_STRING_IDENTIFIER       11
+#define ERROR_CALLBACK_ERROR                    12
+#define ERROR_MISPLACED_OR_OPERATOR             13
+#define ERROR_INVALID_OR_OPERATION_SYNTAX       14
+#define ERROR_SKIP_INSIDE_OR_OPERATION          15
+#define ERROR_NESTED_OR_OPERATION               16
+#define ERROR_MISPLACED_ANONYMOUS_STRING        17
+#define ERROR_COULD_NOT_MAP_FILE                18
+#define ERROR_ZERO_LENGTH_FILE                  19
+#define ERROR_INVALID_ARGUMENT                  20
+#define ERROR_DUPLICATE_META_IDENTIFIER         21
+#define ERROR_INCLUDES_CIRCULAR_REFERENCE       22
+#define ERROR_INCORRECT_VARIABLE_TYPE           23
+#define ERROR_COULD_NOT_ATTACH_TO_PROCESS       24
+#define ERROR_VECTOR_TOO_LONG                   25
+#define ERROR_INCLUDE_DEPTH_EXCEEDED            26
+#define ERROR_INVALID_FILE                      27
+#define ERROR_CORRUPT_FILE                      28
+#define ERROR_UNSUPPORTED_FILE_VERSION          29
+#define ERROR_EXEC_STACK_OVERFLOW               30
+#define ERROR_SCAN_TIMEOUT                      31
+#define ERROR_LOOP_NESTING_LIMIT_EXCEEDED       32
+#define ERROR_DUPLICATE_LOOP_IDENTIFIER         33
+#define ERROR_TOO_MANY_SCAN_THREADS             34
 
 
 #define CALLBACK_MSG_RULE_MATCHING            1

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list