[Forensics-changes] [yara] 81/415: Fix segfault when compiling invalid regex with RE2. regex_compile() now takes a buffer + len to use for errors. Remove duplicate copies of regex-pcre.c and regex-re2.cc. Add bootstrap.sh to rerun libtool + autoconf/make programs.

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:42:47 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 0f1fa5407ccb816843e6cb41c11210e2e9d9c58d
Author: Mike Wiacek <mjwiacek at google.com>
Date:   Wed Mar 9 02:05:16 2011 +0000

    Fix segfault when compiling invalid regex with RE2.
    regex_compile() now takes a buffer + len to use for errors.
    Remove duplicate copies of regex-pcre.c and regex-re2.cc.
    Add bootstrap.sh to rerun libtool + autoconf/make programs.
---
 bootstrap.sh               |  8 ++++
 libyara/ast.c              |  8 ++--
 libyara/grammar.c          | 17 ++++++---
 libyara/grammar.y          |  9 ++++-
 libyara/regex-pcre.c       | 94 ----------------------------------------------
 libyara/regex-re2.cc       | 89 -------------------------------------------
 libyara/regex.h            |  3 +-
 libyara/regex/regex-pcre.c | 13 +++++--
 libyara/regex/regex-re2.cc | 10 ++++-
 9 files changed, 50 insertions(+), 201 deletions(-)

diff --git a/bootstrap.sh b/bootstrap.sh
new file mode 100755
index 0000000..f0043f8
--- /dev/null
+++ b/bootstrap.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# Quick and dirty script to reset everything
+# regarding libtool, autoconf, automake, etc.
+
+make distclean
+libtoolize
+autoreconf --force && cd libyara/ && autoreconf --force
diff --git a/libyara/ast.c b/libyara/ast.c
index 2c0bc9f..576f2e7 100644
--- a/libyara/ast.c
+++ b/libyara/ast.c
@@ -542,7 +542,7 @@ int new_text_string(    YARA_CONTEXT* context,
                         REGEXP* re,
                         unsigned int* length)
 {
-    const char *error;
+    char *error;
     int erroffset;
     int options;
     int result = ERROR_SUCCESS;
@@ -565,11 +565,11 @@ int new_text_string(    YARA_CONTEXT* context,
                           charstr->c_string,  // Regex pattern
                           TRUE,  // Anchor the pattern to the first character when evaluating
                           flags & STRING_FLAGS_NO_CASE,  // If TRUE then case insensitive search
-                          &error,  // Error message
+                          context->last_error_extra_info,  // Error message
+                          sizeof(context->last_error_extra_info), // Size of error buffer
                           &erroffset) <= 0) // Offset into regex pattern if error detected
         {
-            strncpy(context->last_error_extra_info, error, sizeof(context->last_error_extra_info));
-            result = ERROR_INVALID_REGULAR_EXPRESSION;
+             result = ERROR_INVALID_REGULAR_EXPRESSION;
         }
     }
     else
diff --git a/libyara/grammar.c b/libyara/grammar.c
index 52c44ae..4c18707 100644
--- a/libyara/grammar.c
+++ b/libyara/grammar.c
@@ -87,7 +87,7 @@
 
 
 /* Line 189 of yacc.c  */
-#line 91 "grammar.c"
+#line 91 "y.tab.c"
 
 /* Enabling traces.  */
 #ifndef YYDEBUG
@@ -259,7 +259,7 @@ typedef union YYSTYPE
 
 
 /* Line 214 of yacc.c  */
-#line 263 "grammar.c"
+#line 263 "y.tab.c"
 } YYSTYPE;
 # define YYSTYPE_IS_TRIVIAL 1
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
@@ -362,7 +362,7 @@ int count_strings(TERM_STRING* st);
 
 
 /* Line 264 of yacc.c  */
-#line 366 "grammar.c"
+#line 366 "y.tab.c"
 
 #ifdef short
 # undef short
@@ -2734,7 +2734,7 @@ yyreduce:
 
 
 /* Line 1455 of yacc.c  */
-#line 2738 "grammar.c"
+#line 2738 "y.tab.c"
       default: break;
     }
   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
@@ -3445,11 +3445,16 @@ TERM* reduce_external_string_operation( yyscan_t yyscanner,
                 
                 if (type == TERM_TYPE_EXTERNAL_STRING_MATCH)
                 {
-                    if (regex_compile(&(term->re), string->c_string, FALSE, FALSE, &error, &erroffset) <= 0)
+                    if (regex_compile(&(term->re),
+                                      string->c_string,
+                                      FALSE,
+                                      FALSE,
+                                      context->last_error_extra_info,
+                                      sizeof(context->last_error_extra_info),
+                                      &erroffset) <= 0)
                     {
                         yr_free(term);
                         term = NULL;
-                        strncpy(context->last_error_extra_info, error, sizeof(context->last_error_extra_info));
                         context->last_result = ERROR_INVALID_REGULAR_EXPRESSION;
                     }
                 }
diff --git a/libyara/grammar.y b/libyara/grammar.y
index 057bbcf..94e5e94 100644
--- a/libyara/grammar.y
+++ b/libyara/grammar.y
@@ -1203,11 +1203,16 @@ TERM* reduce_external_string_operation( yyscan_t yyscanner,
                 
                 if (type == TERM_TYPE_EXTERNAL_STRING_MATCH)
                 {
-                    if (regex_compile(&(term->re), string->c_string, FALSE, FALSE, &error, &erroffset) <= 0)
+                    if (regex_compile(&(term->re),
+                                      string->c_string,
+                                      FALSE,
+                                      FALSE,
+                                      context->last_error_extra_info,
+                                      sizeof(context->last_error_extra_info),
+                                      &erroffset) <= 0)
                     {
                         yr_free(term);
                         term = NULL;
-                        strncpy(context->last_error_extra_info, error, sizeof(context->last_error_extra_info));
                         context->last_result = ERROR_INVALID_REGULAR_EXPRESSION;
                     }
                 }
diff --git a/libyara/regex-pcre.c b/libyara/regex-pcre.c
deleted file mode 100644
index d310007..0000000
--- a/libyara/regex-pcre.c
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
-
-Copyright(c) 2011, Google, Inc. [mjwiacek at google.com].
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-*/
-
-#include "regex.h"
-#include <pcre.h>
-#include <string.h>
-#include "yara.h"
-
-
-int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size) {
-  if (!regex || buffer_size == 0)
-    return 0;
-
-  int ovector[3];
-  int result = -1;
-  char *s;
-
-  result = pcre_exec((pcre*)regex->regexp,       /* the compiled pattern */
-                     (pcre_extra*)regex->extra,  /* extra data */
-                     (char*) buffer,    /* the subject string */
-                     buffer_size,       /* the length of the subject */
-                     0,                 /* start at offset 0 in the subject */
-                     0,                 /* default options */
-                     ovector,           /* output vector for substring information */
-                     sizeof(ovector));  /* number of elements in the output vector */
-  if (result >= 0) {
-    result = pcre_get_substring(
-        (char*) buffer, ovector, 1, 0, (const char**) &s);
-    if (result != PCRE_ERROR_NOMEMORY && result != PCRE_ERROR_NOSUBSTRING) {
-      pcre_free_substring(s);
-      return result;
-    }
-  }
-  return -1;
-}
-
-
-void regex_free(REGEXP* regex) {
-  if (!regex)
-    return;
-
-  if (regex->regexp) {
-    pcre_free((pcre*)regex->regexp);
-    regex->regexp = NULL;
-  }
-
-  if (regex->extra) {
-    pcre_free((pcre_extra*)regex->extra);
-    regex->extra = NULL;
-  }
-}
-
-
-int regex_compile(REGEXP* output,
-                  const char* pattern,
-                  int anchored,
-                  int case_insensitive,
-                  const char** error_message,
-                  int* error_offset) {
-  if (!output || !pattern)
-    return 0;
-
-  memset(output, '\0', sizeof(REGEXP));
-
-  int pcre_options = 0;
-  if (anchored)
-    pcre_options |= PCRE_ANCHORED;
-  if (case_insensitive)
-    pcre_options |= PCRE_CASELESS;
-
-  output->regexp = (pcre*) pcre_compile(
-      pattern, pcre_options, error_message, error_offset, NULL);
-  if (output->regexp != NULL) {
-    output->extra = (pcre_extra *)pcre_study(output->regexp, 0, error_message);
-  } else {
-    // TODO: Handle fatal error here, consistently with how yara would.
-    return 0;
-  }
-
-  return 1;
-}
diff --git a/libyara/regex-re2.cc b/libyara/regex-re2.cc
deleted file mode 100644
index b43aacc..0000000
--- a/libyara/regex-re2.cc
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
-
-Copyright(c) 2011, Google, Inc. [mjwiacek at google.com].
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-*/
-
-#include "regex.h"
-#include <string.h>
-#include <re2/re2.h>
-#include <re2/stringpiece.h>
-#include "yara.h"
-
-
-int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size) {
-  if (!regex || buffer_size == 0)
-    return 0;
-
-  re2::StringPiece data(buffer, buffer_size);
-  re2::StringPiece substring;
-  re2::RE2::Anchor anchor = re2::RE2::UNANCHORED;
-  if (regex->re2_anchored)
-    anchor = re2::RE2::ANCHOR_START;
-
-  re2::RE2* re_ptr = (re2::RE2*) regex->regexp;
-
-  if (re_ptr->Match(data, 0, data.size()-1, anchor, &substring, 1)) {
-    return substring.size();
-  }
-  return -1;
-}
-
-
-void regex_free(REGEXP* regex) {
-  if (!regex)
-    return;
-
-  if (regex->regexp) {
-    delete (re2::RE2*) regex->regexp;
-    regex->regexp = NULL;
-  }
-
-}
-
-
-int regex_compile(REGEXP* output,
-                  const char* pattern,
-                  int anchored,
-                  int case_insensitive,
-                  const char** error_message,
-                  int* error_offset) {
-  if (!output || !pattern)
-    return 0;
-
-  memset(output, '\0', sizeof(REGEXP));
-
-  RE2::Options options;
-  options.set_log_errors(false);
-
-  if (case_insensitive)
-    options.set_case_sensitive(false);
-  if (anchored)
-    output->re2_anchored = anchored;
-
-  re2::StringPiece string_piece_pattern(pattern);
-  output->regexp = (void *)new RE2(string_piece_pattern, options);
-  if (output->regexp == NULL) {
-    // TODO: Handle fatal error here, consistently with how yara would.
-    return 0;
-  }
-
-  re2::RE2* re_ptr = (re2::RE2*)output->regexp;
-  if (!re_ptr->ok()) {
-    *error_message = re_ptr->error().c_str();
-    *error_offset = re_ptr->error().find(pattern);
-    delete re_ptr;
-    output->regexp = NULL;
-  }
-  return 1;
-}
diff --git a/libyara/regex.h b/libyara/regex.h
index 931425e..a02c3dd 100644
--- a/libyara/regex.h
+++ b/libyara/regex.h
@@ -27,7 +27,8 @@ int regex_compile(REGEXP* output,
                   const char* pattern,
                   int anchored,
                   int case_insensitive,
-                  const char** error_message,
+                  char* error_message,
+                  size_t error_message_size,
                   int* error_offset);
 
 #ifdef __cplusplus
diff --git a/libyara/regex/regex-pcre.c b/libyara/regex/regex-pcre.c
index d310007..bd30706 100644
--- a/libyara/regex/regex-pcre.c
+++ b/libyara/regex/regex-pcre.c
@@ -68,7 +68,8 @@ int regex_compile(REGEXP* output,
                   const char* pattern,
                   int anchored,
                   int case_insensitive,
-                  const char** error_message,
+                  char* error_message,
+                  size_t error_message_size,
                   int* error_offset) {
   if (!output || !pattern)
     return 0;
@@ -81,11 +82,17 @@ int regex_compile(REGEXP* output,
   if (case_insensitive)
     pcre_options |= PCRE_CASELESS;
 
+  char *pcre_error = NULL;
   output->regexp = (pcre*) pcre_compile(
-      pattern, pcre_options, error_message, error_offset, NULL);
+      pattern, pcre_options, (const char **)&pcre_error, error_offset, NULL);
   if (output->regexp != NULL) {
-    output->extra = (pcre_extra *)pcre_study(output->regexp, 0, error_message);
+    output->extra = (pcre_extra *)pcre_study(
+        output->regexp, 0, (const char **)error_message);
   } else {
+    if (error_message && error_message_size) {
+      strncpy(error_message, pcre_error, error_message_size - 1);
+      error_message[error_message_size - 1] = '\0';
+    }
     // TODO: Handle fatal error here, consistently with how yara would.
     return 0;
   }
diff --git a/libyara/regex/regex-re2.cc b/libyara/regex/regex-re2.cc
index 84c7427..f1f8ae8 100644
--- a/libyara/regex/regex-re2.cc
+++ b/libyara/regex/regex-re2.cc
@@ -18,6 +18,7 @@ GNU General Public License for more details.
 #include <string.h>
 #include <re2/re2.h>
 #include <re2/stringpiece.h>
+#include "yara.h"
 
 
 int regex_exec(REGEXP* regex, const char *buffer, size_t buffer_size) {
@@ -55,7 +56,8 @@ int regex_compile(REGEXP* output,
                   const char* pattern,
                   int anchored,
                   int case_insensitive,
-                  const char** error_message,
+                  char* error_message,
+                  size_t error_message_size,
                   int* error_offset) {
   if (!output || !pattern)
     return 0;
@@ -79,10 +81,14 @@ int regex_compile(REGEXP* output,
 
   re2::RE2* re_ptr = (re2::RE2*)output->regexp;
   if (!re_ptr->ok()) {
-    *error_message = re_ptr->error().c_str();
+    if (error_message && error_message_size) {
+      strncpy(error_message, re_ptr->error().c_str(), error_message_size - 1);
+      error_message[error_message_size - 1] = '\0';
+    }
     *error_offset = re_ptr->error().find(pattern);
     delete re_ptr;
     output->regexp = NULL;
+    return 0;
   }
   return 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