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  int ov[MAX_SUBSTRINGS];
67 
68  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, 30);
69  if (ret < 0) {
70  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Priority in Signature "
71  "- %s", rawstr);
72  return -1;
73  }
74 
75  ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, copy_str, sizeof(copy_str));
76  if (ret < 0) {
77  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
78  return -1;
79  }
80 
81  long prio = 0;
82  char *endptr = NULL;
83  prio = strtol(copy_str, &endptr, 10);
84  if (endptr == NULL || *endptr != '\0') {
85  SCLogError(SC_ERR_INVALID_SIGNATURE, "Saw an invalid character as arg "
86  "to priority keyword");
87  return -1;
88  }
89 
91  SCLogWarning(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicate priority "
92  "keyword. Using highest priority in the rule");
93  s->prio = MIN(s->prio, prio);
94  } else {
95  s->prio = prio;
97  }
98  return 0;
99 }
100 
101 /*------------------------------Unittests-------------------------------------*/
102 
103 #ifdef UNITTESTS
104 
105 static int DetectPriorityTest01(void)
106 {
109 
110  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
111  "(msg:\"Priority test\"; priority:2; sid:1;)");
113 
115 
117  PASS;
118 }
119 
120 static int DetectPriorityTest02(void)
121 {
124 
125  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
126  "(msg:\"Priority test\"; priority:1; sid:1;)");
127  FAIL_IF_NULL(sig);
128  FAIL_IF_NOT(sig->prio == 1);
129 
130  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
131  "(msg:\"Priority test\"; priority:boo; sid:2;)");
132  FAIL_IF_NOT_NULL(sig);
133 
134  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
135  "(msg:\"Priority test\"; priority:10boo; sid:3;)");
136  FAIL_IF_NOT_NULL(sig);
137 
138  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
139  "(msg:\"Priority test\"; priority:b10oo; sid:4;)");
140  FAIL_IF_NOT_NULL(sig);
141 
142  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
143  "(msg:\"Priority test\"; priority:boo10; sid:5;)");
144  FAIL_IF_NOT_NULL(sig);
145 
146  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
147  "(msg:\"Priority test\"; priority:-1; sid:6;)");
148  FAIL_IF_NOT_NULL(sig);
149 
150  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
151  "(msg:\"Priority test\"; sid:7;)");
152  FAIL_IF_NULL(sig);
153  FAIL_IF_NOT(sig->prio == 3);
154 
155  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
156  "(msg:\"Priority test\"; priority:5; priority:4; sid:8;)");
157  FAIL_IF_NULL(sig);
158  FAIL_IF_NOT(sig->prio == 4);
159 
160  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
161  "(msg:\"Priority test\"; priority:5; priority:4; "
162  "priority:1; sid:9;)");
163  FAIL_IF_NULL(sig);
164  FAIL_IF_NOT(sig->prio == 1);
165 
167  PASS;
168 }
169 
170 /**
171  * \brief This function registers unit tests for Classification Config API.
172  */
173 static void PriorityRegisterTests(void)
174 {
175  UtRegisterTest("DetectPriorityTest01", DetectPriorityTest01);
176  UtRegisterTest("DetectPriorityTest02", DetectPriorityTest02);
177 }
178 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
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:1213
detect-priority.h
SigTableElmt_::name
const char * name
Definition: detect.h:1211
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS
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:2056
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
MIN
#define MIN(x, y)
Definition: suricata-common.h:377
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:493
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:1196
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression to true.
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:2493
detect-engine-mpm.h
detect.h
DETECT_PRIORITY
@ DETECT_PRIORITY
Definition: detect-engine-register.h:29
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, const char *str, int start_offset, int options, int *ovector, int ovector_size)
Definition: detect-parse.c:2423
SIG_FLAG_INIT_PRIO_EXPLICT
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:264
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:597
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:2361
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectParseRegex_
Definition: detect-parse.h:42
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:773
Signature_::prio
int prio
Definition: detect.h:564
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-priority.c:37
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171