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 void SCPriorityRegisterTests(void);
43 
44 /**
45  * \brief Registers the handler functions for the "priority" keyword
46  */
48 {
49  sigmatch_table[DETECT_PRIORITY].name = "priority";
50  sigmatch_table[DETECT_PRIORITY].desc = "rules with a higher priority will be examined first";
51  sigmatch_table[DETECT_PRIORITY].url = "/rules/meta.html#priority";
52  sigmatch_table[DETECT_PRIORITY].Setup = DetectPrioritySetup;
54 
55  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
56 }
57 
58 static int DetectPrioritySetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
59 {
60  char copy_str[128] = "";
61 
62  int ret = 0;
63  int ov[MAX_SUBSTRINGS];
64 
65  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0, ov, 30);
66  if (ret < 0) {
67  SCLogError(SC_ERR_PCRE_MATCH, "Invalid Priority in Signature "
68  "- %s", rawstr);
69  return -1;
70  }
71 
72  ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, copy_str, sizeof(copy_str));
73  if (ret < 0) {
74  SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring failed");
75  return -1;
76  }
77 
78  long prio = 0;
79  char *endptr = NULL;
80  prio = strtol(copy_str, &endptr, 10);
81  if (endptr == NULL || *endptr != '\0') {
82  SCLogError(SC_ERR_INVALID_SIGNATURE, "Saw an invalid character as arg "
83  "to priority keyword");
84  return -1;
85  }
86 
88  SCLogWarning(SC_ERR_CONFLICTING_RULE_KEYWORDS, "duplicate priority "
89  "keyword. Using highest priority in the rule");
90  s->prio = MIN(s->prio, prio);
91  } else {
92  s->prio = prio;
94  }
95  return 0;
96 }
97 
98 /*------------------------------Unittests-------------------------------------*/
99 
100 #ifdef UNITTESTS
101 
102 static int DetectPriorityTest01(void)
103 {
106 
107  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
108  "(msg:\"Priority test\"; priority:2; sid:1;)");
110 
112 
114  PASS;
115 }
116 
117 static int DetectPriorityTest02(void)
118 {
121 
122  Signature *sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
123  "(msg:\"Priority test\"; priority:1; sid:1;)");
124  FAIL_IF_NULL(sig);
125  FAIL_IF_NOT(sig->prio == 1);
126 
127  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
128  "(msg:\"Priority test\"; priority:boo; sid:2;)");
129  FAIL_IF_NOT_NULL(sig);
130 
131  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
132  "(msg:\"Priority test\"; priority:10boo; sid:3;)");
133  FAIL_IF_NOT_NULL(sig);
134 
135  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
136  "(msg:\"Priority test\"; priority:b10oo; sid:4;)");
137  FAIL_IF_NOT_NULL(sig);
138 
139  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
140  "(msg:\"Priority test\"; priority:boo10; sid:5;)");
141  FAIL_IF_NOT_NULL(sig);
142 
143  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
144  "(msg:\"Priority test\"; priority:-1; sid:6;)");
145  FAIL_IF_NOT_NULL(sig);
146 
147  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
148  "(msg:\"Priority test\"; sid:7;)");
149  FAIL_IF_NULL(sig);
150  FAIL_IF_NOT(sig->prio == 3);
151 
152  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
153  "(msg:\"Priority test\"; priority:5; priority:4; sid:8;)");
154  FAIL_IF_NULL(sig);
155  FAIL_IF_NOT(sig->prio == 4);
156 
157  sig = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
158  "(msg:\"Priority test\"; priority:5; priority:4; "
159  "priority:1; sid:9;)");
160  FAIL_IF_NULL(sig);
161  FAIL_IF_NOT(sig->prio == 1);
162 
164  PASS;
165 }
166 #endif /* UNITTESTS */
167 
168 /**
169  * \brief This function registers unit tests for Classification Config API.
170  */
172 {
173 #ifdef UNITTESTS
174  UtRegisterTest("DetectPriorityTest01", DetectPriorityTest01);
175  UtRegisterTest("DetectPriorityTest02", DetectPriorityTest02);
176 #endif /* UNITTESTS */
177 }
SigTableElmt_::url
const char * url
Definition: detect.h:1212
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:1211
detect-priority.h
SigTableElmt_::name
const char * name
Definition: detect.h:1209
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:47
SigInit
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2033
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2089
MIN
#define MIN(x, y)
Definition: suricata-common.h:377
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:492
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:1195
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:2456
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:2388
SIG_FLAG_INIT_PRIO_EXPLICT
#define SIG_FLAG_INIT_PRIO_EXPLICT
Definition: detect.h:264
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:596
SCPriorityRegisterTests
void SCPriorityRegisterTests(void)
This function registers unit tests for Classification Config API.
Definition: detect-priority.c:171
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:2326
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:772
Signature_::prio
int prio
Definition: detect.h:563
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:527
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2044
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-priority.c:37
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201
SC_ERR_CONFLICTING_RULE_KEYWORDS
@ SC_ERR_CONFLICTING_RULE_KEYWORDS
Definition: util-error.h:171