suricata
detect-ipopts.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 Breno Silva <breno.silva@gmail.com>
22  *
23  * Implements the ipopts keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "flow-var.h"
34 #include "decode-events.h"
35 
36 #include "util-debug.h"
37 
38 #include "detect-ipopts.h"
39 #include "util-unittest.h"
40 
41 #define PARSE_REGEX "\\S[A-z]"
42 
43 static DetectParseRegex parse_regex;
44 
45 static int DetectIpOptsMatch (DetectEngineThreadCtx *, Packet *,
46  const Signature *, const SigMatchCtx *);
47 static int DetectIpOptsSetup (DetectEngineCtx *, Signature *, const char *);
48 #ifdef UNITTESTS
49 static void IpOptsRegisterTests(void);
50 #endif
51 void DetectIpOptsFree(DetectEngineCtx *, void *);
52 
53 /**
54  * \brief Registration function for ipopts: keyword
55  */
57 {
58  sigmatch_table[DETECT_IPOPTS].name = "ipopts";
59  sigmatch_table[DETECT_IPOPTS].desc = "check if a specific IP option is set";
60  sigmatch_table[DETECT_IPOPTS].url = "/rules/header-keywords.html#ipopts";
61  sigmatch_table[DETECT_IPOPTS].Match = DetectIpOptsMatch;
62  sigmatch_table[DETECT_IPOPTS].Setup = DetectIpOptsSetup;
64 #ifdef UNITTESTS
65  sigmatch_table[DETECT_IPOPTS].RegisterTests = IpOptsRegisterTests;
66 #endif
67  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
68 }
69 
70 /**
71  * \struct DetectIpOptss_
72  * DetectIpOptss_ is used to store supported iptops values
73  */
74 
75 struct DetectIpOpts_ {
76  const char *ipopt_name; /**< ip option name */
77  uint16_t code; /**< ip option flag value */
78 } ipopts[] = {
79  { "rr", IPV4_OPT_FLAG_RR, },
80  { "lsrr", IPV4_OPT_FLAG_LSRR, },
81  { "eol", IPV4_OPT_FLAG_EOL, },
82  { "nop", IPV4_OPT_FLAG_NOP, },
83  { "ts", IPV4_OPT_FLAG_TS, },
84  { "sec", IPV4_OPT_FLAG_SEC, },
85  { "ssrr", IPV4_OPT_FLAG_SSRR, },
86  { "satid", IPV4_OPT_FLAG_SID, },
87  { "any", 0xffff, },
88  { NULL, 0 },
89 };
90 
91 /**
92  * \internal
93  * \brief This function is used to match ip option on a packet with those passed via ipopts:
94  *
95  * \param t pointer to thread vars
96  * \param det_ctx pointer to the pattern matcher thread
97  * \param p pointer to the current packet
98  * \param s pointer to the Signature
99  * \param m pointer to the sigmatch
100  *
101  * \retval 0 no match
102  * \retval 1 match
103  */
104 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
105  const Signature *s, const SigMatchCtx *ctx)
106 {
107  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
108 
109  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
110  return 0;
111 
112  if (p->ip4vars.opts_set & de->ipopt) {
113  return 1;
114  }
115 
116  return 0;
117 }
118 
119 /**
120  * \internal
121  * \brief This function is used to parse ipopts options passed via ipopts: keyword
122  *
123  * \param rawstr Pointer to the user provided ipopts options
124  *
125  * \retval de pointer to DetectIpOptsData on success
126  * \retval NULL on failure
127  */
128 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
129 {
130  int i;
131  DetectIpOptsData *de = NULL;
132  int ret = 0, found = 0;
133 
134  ret = DetectParsePcreExec(&parse_regex, rawstr, 0, 0);
135  if (ret < 1) {
136  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
137  goto error;
138  }
139 
140  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
141  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
142  found = 1;
143  break;
144  }
145  }
146 
147  if(found == 0)
148  goto error;
149 
150  de = SCMalloc(sizeof(DetectIpOptsData));
151  if (unlikely(de == NULL))
152  goto error;
153 
154  de->ipopt = ipopts[i].code;
155 
156  return de;
157 
158 error:
159  if (de) SCFree(de);
160  return NULL;
161 }
162 
163 /**
164  * \internal
165  * \brief this function is used to add the parsed ipopts into the current signature
166  *
167  * \param de_ctx pointer to the Detection Engine Context
168  * \param s pointer to the Current Signature
169  * \param rawstr pointer to the user provided ipopts options
170  *
171  * \retval 0 on Success
172  * \retval -1 on Failure
173  */
174 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
175 {
176  DetectIpOptsData *de = NULL;
177  SigMatch *sm = NULL;
178 
179  de = DetectIpOptsParse(rawstr);
180  if (de == NULL)
181  goto error;
182 
183  sm = SigMatchAlloc();
184  if (sm == NULL)
185  goto error;
186 
187  sm->type = DETECT_IPOPTS;
188  sm->ctx = (SigMatchCtx *)de;
189 
192 
193  return 0;
194 
195 error:
196  if (de) SCFree(de);
197  if (sm) SCFree(sm);
198  return -1;
199 }
200 
201 /**
202  * \internal
203  * \brief this function will free memory associated with DetectIpOptsData
204  *
205  * \param de pointer to DetectIpOptsData
206  */
208 {
209  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
210  if(de) SCFree(de);
211 }
212 
213 /*
214  * ONLY TESTS BELOW THIS COMMENT
215  */
216 
217 #ifdef UNITTESTS
218 /**
219  * \test IpOptsTestParse01 is a test for a valid ipopts value
220  */
221 static int IpOptsTestParse01 (void)
222 {
223  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
224 
225  FAIL_IF_NULL(de);
226 
227  DetectIpOptsFree(NULL, de);
228 
229  PASS;
230 }
231 
232 /**
233  * \test IpOptsTestParse02 is a test for an invalid ipopts value
234  */
235 static int IpOptsTestParse02 (void)
236 {
237  DetectIpOptsData *de = DetectIpOptsParse("invalidopt");
238 
240 
241  DetectIpOptsFree(NULL, de);
242 
243  PASS;
244 }
245 
246 /**
247  * \test IpOptsTestParse03 test the match function on a packet that needs to match
248  */
249 static int IpOptsTestParse03 (void)
250 {
251  Packet *p = PacketGetFromAlloc();
252  FAIL_IF_NULL(p);
253  ThreadVars tv;
254  IPV4Hdr ip4h;
255 
256  memset(&tv, 0, sizeof(ThreadVars));
257  memset(&ip4h, 0, sizeof(IPV4Hdr));
258 
259  p->ip4h = &ip4h;
261 
262  DetectIpOptsData *de = DetectIpOptsParse("rr");
263  FAIL_IF_NULL(de);
264 
265  SigMatch *sm = SigMatchAlloc();
266  FAIL_IF_NULL(sm);
267 
268  sm->type = DETECT_IPOPTS;
269  sm->ctx = (SigMatchCtx *)de;
270 
271  FAIL_IF_NOT(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
272 
273  SCFree(de);
274  SCFree(sm);
275  SCFree(p);
276 
277  PASS;
278 }
279 
280 /**
281  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
282  */
283 static int IpOptsTestParse04 (void)
284 {
285  Packet *p = PacketGetFromAlloc();
286  FAIL_IF_NULL(p);
287  ThreadVars tv;
288  IPV4Hdr ip4h;
289 
290  memset(&tv, 0, sizeof(ThreadVars));
291  memset(&ip4h, 0, sizeof(IPV4Hdr));
292 
293  p->ip4h = &ip4h;
295 
296  DetectIpOptsData *de = DetectIpOptsParse("lsrr");
297  FAIL_IF_NULL(de);
298 
299  SigMatch *sm = SigMatchAlloc();
300  FAIL_IF_NULL(sm);
301 
302  sm->type = DETECT_IPOPTS;
303  sm->ctx = (SigMatchCtx *)de;
304 
305  FAIL_IF(DetectIpOptsMatch(NULL, p, NULL, sm->ctx));
306 
307  SCFree(de);
308  SCFree(sm);
309  SCFree(p);
310 
311  PASS;
312 }
313 
314 /**
315  * \brief this function registers unit tests for IpOpts
316  */
317 void IpOptsRegisterTests(void)
318 {
319  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
320  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
321  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
322  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
323 }
324 #endif /* UNITTESTS */
DetectIpOpts_::ipopt_name
const char * ipopt_name
Definition: detect-ipopts.c:76
DetectIpOpts_
Definition: detect-ipopts.c:75
SigTableElmt_::url
const char * url
Definition: detect.h:1248
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
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1235
DetectParseRegex
Definition: detect-parse.h:42
SigTableElmt_::name
const char * name
Definition: detect.h:1245
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1222
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DETECT_IPOPTS
@ DETECT_IPOPTS
Definition: detect-engine-register.h:39
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
Packet_::ip4vars
IPV4Vars ip4vars
Definition: decode.h:538
IPV4_OPT_FLAG_SEC
@ IPV4_OPT_FLAG_SEC
Definition: decode-ipv4.h:165
DetectIpOptsRegister
void DetectIpOptsRegister(void)
Registration function for ipopts: keyword.
Definition: detect-ipopts.c:56
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
ipopts
struct DetectIpOpts_ ipopts[]
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-ipopts.c:41
IPV4_OPT_FLAG_NOP
@ IPV4_OPT_FLAG_NOP
Definition: decode-ipv4.h:158
decode.h
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
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
IPV4_OPT_FLAG_SID
@ IPV4_OPT_FLAG_SID
Definition: decode-ipv4.h:164
DetectEngineThreadCtx_
Definition: detect.h:1034
de
uint8_t de
Definition: app-layer-htp.c:568
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:2598
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:56
IPV4_OPT_FLAG_RR
@ IPV4_OPT_FLAG_RR
Definition: decode-ipv4.h:159
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:80
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:317
Signature_::flags
uint32_t flags
Definition: detect.h:541
Packet_
Definition: decode.h:434
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:532
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1213
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
DetectIpOptsFree
void DetectIpOptsFree(DetectEngineCtx *, void *)
Definition: detect-ipopts.c:207
decode-events.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:309
IPV4Hdr_
Definition: decode-ipv4.h:71
IPV4_OPT_FLAG_TS
@ IPV4_OPT_FLAG_TS
Definition: decode-ipv4.h:160
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:315
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
DetectIpOptsData_
Definition: detect-ipopts.h:38
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:31
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:176
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
SigMatch_
a single match condition for a signature
Definition: detect.h:314
IPV4_OPT_FLAG_SSRR
@ IPV4_OPT_FLAG_SSRR
Definition: decode-ipv4.h:163
detect-ipopts.h
suricata.h
IPV4Vars_::opts_set
uint16_t opts_set
Definition: decode-ipv4.h:176
DetectIpOpts_::code
uint16_t code
Definition: detect-ipopts.c:77
IPV4_OPT_FLAG_LSRR
@ IPV4_OPT_FLAG_LSRR
Definition: decode-ipv4.h:162
flow-var.h
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:253
IPV4_OPT_FLAG_EOL
@ IPV4_OPT_FLAG_EOL
Definition: decode-ipv4.h:157
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:217