]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #595 in SNORT/snort3 from regex_fp_fix to master
authorRuss Combs (rucombs) <rucombs@cisco.com>
Tue, 16 Aug 2016 18:18:40 +0000 (14:18 -0400)
committerRuss Combs (rucombs) <rucombs@cisco.com>
Tue, 16 Aug 2016 18:18:40 +0000 (14:18 -0400)
Squashed commit of the following:

commit 4084e9329da46be546e2d8b2b0fa924d3626663f
Author: Russ Combs <rucombs@cisco.com>
Date:   Mon Aug 15 21:36:32 2016 -0400

    fix regex as fast pattern with hyperscan mpse

src/detection/fp_create.cc
src/detection/pattern_match_data.h
src/framework/mpse.h
src/ips_options/ips_regex.cc
src/search_engines/hyperscan.cc

index a4491d82829b06cf733dec0021c4d66e71c7348d..04b2019f35a5db60c6a19a9a1c6535e1c7d98c41 100644 (file)
@@ -603,7 +603,7 @@ static int fpFinishPortGroupRule(
                 pg->mpse[pmd->pm_type]->set_opt(1);
         }
 
-        Mpse::PatternDescriptor desc(pmd->no_case, pmd->negated, pmd->literal);
+        Mpse::PatternDescriptor desc(pmd->no_case, pmd->negated, pmd->literal, pmd->flags);
         pg->mpse[pmd->pm_type]->add_pattern(sc, (uint8_t*)pattern, pattern_length, desc, pmx);
     }
 
@@ -684,10 +684,11 @@ static int fpAddPortGroupRule(
     OptFpList* next = nullptr;
     pmd = get_fp_content(otn, next);
 
-    if ( pmd && pmd->fp)
+    if ( pmd && pmd->fp )
     {
         if (
-            pmd->fp && !pmd->relative && !pmd->negated && pmd->fp_only >= 0 &&
+            !pmd->relative && !pmd->negated && pmd->fp_only >= 0 &&
+            // FIXIT-L no_case consideration is mpse specific, delegate
             !pmd->offset && !pmd->depth && pmd->no_case )
         {
             if ( !next || !next->ips_opt || !next->ips_opt->is_relative() )
index 02cb45f4d49aee54f67ff9df3cdf8276a276f30d..50f86ee039be315bb7719f913f85a6236f78f241 100644 (file)
@@ -37,10 +37,11 @@ struct PmdLastCheck
 struct PatternMatchData
 {
     // used by both
-    bool negated;        // search for "not this pattern"
-    bool fp;             // For fast_pattern arguments
-    bool no_case;        // Toggle case sensitivity
-    bool relative;       // do relative pattern searching
+    bool negated;            // search for "not this pattern"
+    bool fp;                 // for fast_pattern arguments
+    bool no_case;            // toggle case sensitivity
+    bool relative;           // do relative pattern searching
+    bool literal;            // true for plain contents only
 
     uint16_t fp_offset;
     uint16_t fp_length;
@@ -48,11 +49,10 @@ struct PatternMatchData
     int offset;              // pattern search start offset
     int depth;               // pattern search depth
 
+    unsigned flags;          // hyperscan only FIXIT-L need to generalize
     unsigned pattern_size;   // size of app layer pattern
     const char* pattern_buf; // app layer pattern to match on
 
-    bool literal;            // set to plain contents
-
     // not used by ips_content
     int8_t fp_only;
     uint8_t pm_type;
index 77a8cdc51d00fd96fdc9b09dd336379e5fb4c7bc..9e7e14bb347c3dedecfd93f5fa56b50438d04d06 100644 (file)
@@ -56,9 +56,11 @@ public:
         bool no_case;
         bool negated;
         bool literal;
+        unsigned flags;
 
-        PatternDescriptor(bool noc = false, bool neg = false, bool lit = false)
-        { no_case = noc; negated = neg; literal = lit; }
+        PatternDescriptor(
+            bool noc = false, bool neg = false, bool lit = false, unsigned f = 0)
+        { no_case = noc; negated = neg; literal = lit; flags = f; }
     };
 
     virtual int add_pattern(
index 5285786feeef516466af82129dfdd18cf523f1bb..87f6f63b70732b5b2f2a0856672b6838319e5c89 100644 (file)
@@ -49,8 +49,6 @@ struct RegexConfig
     PatternMatchData pmd;
     std::string re;
     hs_database_t* db;
-    unsigned flags;
-    bool relative;
 
     RegexConfig()
     { reset(); }
@@ -60,7 +58,6 @@ struct RegexConfig
         memset(&pmd, 0, sizeof(pmd));
         re.clear();
         db = nullptr;
-        flags = 0;
     }
 };
 
@@ -133,7 +130,7 @@ RegexOption::~RegexOption()
 
 uint32_t RegexOption::hash() const
 {
-    uint32_t a = config.flags, b = config.pmd.relative, c = 0;
+    uint32_t a = config.pmd.flags, b = config.pmd.relative, c = 0;
     mix_str(a, b, c, config.re.c_str());
     mix_str(a, b, c, get_name());
     finalize(a, b, c);
@@ -150,7 +147,7 @@ bool RegexOption::operator==(const IpsOption& ips) const
     RegexOption& rhs = (RegexOption&)ips;
 
     if ( config.re == rhs.config.re and 
-         config.flags == rhs.config.flags and
+         config.pmd.flags == rhs.config.pmd.flags and
          config.pmd.relative == rhs.config.pmd.relative )
         return true;
 
@@ -273,13 +270,15 @@ bool RegexModule::set(const char*, Value& v, SnortConfig*)
     }
 
     else if ( v.is("nocase") )
-        config.flags |= HS_FLAG_CASELESS;
-
+    {
+        config.pmd.flags |= HS_FLAG_CASELESS;
+        config.pmd.no_case = true;
+    }
     else if ( v.is("dotall") )
-        config.flags |= HS_FLAG_DOTALL;
+        config.pmd.flags |= HS_FLAG_DOTALL;
 
     else if ( v.is("multiline") )
-        config.flags |= HS_FLAG_MULTILINE;
+        config.pmd.flags |= HS_FLAG_MULTILINE;
 
     else if ( v.is("relative") )
         config.pmd.relative = true;
@@ -294,7 +293,7 @@ bool RegexModule::end(const char*, int, SnortConfig*)
 {
     hs_compile_error_t* err = nullptr;
 
-    if ( hs_compile(config.re.c_str(), config.flags, HS_MODE_BLOCK, nullptr, &config.db, &err)
+    if ( hs_compile(config.re.c_str(), config.pmd.flags, HS_MODE_BLOCK, nullptr, &config.db, &err)
         or !config.db )
     {
         ParseError("can't compile regex '%s'", config.re.c_str());
index b2a035c423a0c6b363ab7ea5f77fd28432c5fd2a..724756f36e3f339ca97a0c05c481758d7d74d4fc 100644 (file)
@@ -39,6 +39,7 @@ struct Pattern
 {
     std::string pat;
     unsigned len;
+    unsigned flags;
     bool no_case;
     bool negate;
 
@@ -58,6 +59,7 @@ Pattern::Pattern(
     len = n;
     no_case = d.no_case;
     negate = d.negated;
+    flags = d.flags;
     user = u;
     user_tree = user_list = nullptr;
 }
@@ -205,7 +207,7 @@ int HyperscanMpse::prep_patterns(SnortConfig* sc)
     for ( auto& p : pvector )
     {
         pats.push_back(p.pat.c_str());
-        flags.push_back(p.no_case ? HS_FLAG_CASELESS : 0);
+        flags.push_back(p.flags);
         ids.push_back(id++);
     }