suricata
detect-priority.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Implements the priority keyword
25  */
26 
27 #include "suricata-common.h"
28 #include "detect.h"
29 #include "detect-parse.h"
30 #include "detect-priority.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
33 #include "util-error.h"
34 #include "util-debug.h"
35 #include "util-unittest.h"
36 
37 #define PARSE_REGEX "^\\s*(\\d+|\"\\d+\")\\s*$"
38 
39 static DetectParseRegex parse_regex;
40 
41 static int DetectPrioritySetup (DetectEngineCtx *, Signature *, const char *);
42 #ifdef UNITTESTS
43 static void PriorityRegisterTests(void);
44 #endif
45 
46 /**
47  * \brief Registers the handler functions for the "priority" keyword
48  */
50 {
51  sigmatch_table[DETECT_PRIORITY].name = "priority";
52  sigmatch_table[DETECT_PRIORITY].desc = "rules with a higher priority will be examined first";
53  sigmatch_table[DETECT_PRIORITY].url = "/rules/meta.html#priority";
54  sigmatch_table[DETECT_PRIORITY].Setup = DetectPrioritySetup;
55 #ifdef UNITTESTS
56  sigmatch_table[DETECT_PRIORITY].RegisterTests = PriorityRegisterTests;
57 #endif
58  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
59 }
60 
61 static int DetectPrioritySetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
62 {
63  char copy_str[128] = "";
64 
65  int ret = 0;
66  size_t pcre2len;
67 
68  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
69  if (ret < 0) {
70  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Priority in Signature "
71  "- %s", rawstr);
72  return -1;
73  }
74 
75  pcre2len = sizeof(copy_str);
76  ret = pcre2_substring_copy_bynumber(parse_regex.match, 1, (PCRE2_UCHAR8 *)copy_str, &pcre2len);
77  if (ret < 0) {
78  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre2_substring_copy_bynumber failed");
79  return -1;
80  }
81 
82  long prio = 0;
83  char *endptr = NULL;
84  prio = strtol(copy_str, &endptr, 10);
85  if (endptr == NULL || *endptr != '\0') {
86  SCLogError(SC_ERR_INVALID_SIGNATURE, "Saw an invalid character as arg "
87  "to priority keyword");
88  return -1;
89  }
90 
92  SCLogWarning(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicate priority "
93  "keyword. Using highest priority in the rule");
94  s->prio = MIN(s->prio, prio);
95  } else {
96  s->prio = prio;
98  }
99  return 0;
100 }
101 
102 /*------------------------------Unittests-------------------------------------*/
103 
104 #ifdef UNITTESTS
105 
106 static int DetectPriorityTest01(void)
107 {
110 
111  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
112  "(msg:\"Priority test\"; priority:2; sid:1;)");
114 
116 
118  PASS;
119 }
120 
121 static int DetectPriorityTest02(void)
122 {
125 
126  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
127  "(msg:\"Priority test\"; priority:1; sid:1;)");
128  FAIL_IF_NULL(sig);
129  FAIL_IF_NOT(sig->prio == 1);
130 
131  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
132  "(msg:\"Priority test\"; priority:boo; sid:2;)");
133  FAIL_IF_NOT_NULL(sig);
134 
135  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
136  "(msg:\"Priority test\"; priority:10boo; sid:3;)");
137  FAIL_IF_NOT_NULL(sig);
138 
139  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
140  "(msg:\"Priority test\"; priority:b10oo; sid:4;)");
141  FAIL_IF_NOT_NULL(sig);
142 
143  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
144  "(msg:\"Priority test\"; priority:boo10; sid:5;)");
145  FAIL_IF_NOT_NULL(sig);
146 
147  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
148  "(msg:\"Priority test\"; priority:-1; sid:6;)");
149  FAIL_IF_NOT_NULL(sig);
150 
151  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
152  "(msg:\"Priority test\"; sid:7;)");
153  FAIL_IF_NULL(sig);
154  FAIL_IF_NOT(sig->prio == 3);
155 
156  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
157  "(msg:\"Priority test\"; priority:5; priority:4; sid:8;)");
158  FAIL_IF_NULL(sig);
159  FAIL_IF_NOT(sig->prio == 4);
160 
161  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
162  "(msg:\"Priority test\"; priority:5; priority:4; "
163  "priority:1; sid:9;)");
164  FAIL_IF_NULL(sig);
165  FAIL_IF_NOT(sig->prio == 1);
166 
168  PASS;
169 }
170 
171 /**
172  * \brief This function registers unit tests for Classification Config API.
173  */
174 static void PriorityRegisterTests(void)
175 {
176  UtRegisterTest("DetectPriorityTest01", DetectPriorityTest01);
177  UtRegisterTest("DetectPriorityTest02", DetectPriorityTest02);
178 }
179 #endif /* UNITTESTS */
DetectParseRegex::match
pcre2_match_data * match
Definition: detect-parse.h:45
SigTableElmt_::url
const char * url
Definition: detect.h:1248
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1247
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options)
Definition: detect-parse.c:2475
detect-priority.h
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectPriorityRegister
void DetectPriorityRegister(void)
Registers the handler functions for the "priority" keyword.
Definition: detect-priority.c:49
SigInit
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2116
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2445
MIN
#define MIN(x, y)
Definition: suricata-common.h:372
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:503
SC_ERR_PCRE_GET_SUBSTRING
@ SC_ERR_PCRE_GET_SUBSTRING
Definition: util-error.h:34
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
detect-engine-mpm.h
detect.h
DETECT_PRIORITY
@ DETECT_PRIORITY
Definition: detect-engine-register.h:29
SIG_FLAG_INIT_PRIO_EXPLICT
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:258
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:611
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2421
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:791
Signature_::prio
int prio
Definition: detect.h:577
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:242
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2406
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-priority.c:37
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171