suricata
detect-flowbits.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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 Breno Silva <breno.silva@gmail.com>
23  *
24  * Implements the flowbits keyword
25  */
26 
27 #include "suricata-common.h"
28 #include "decode.h"
29 #include "action-globals.h"
30 #include "detect.h"
31 #include "threads.h"
32 #include "flow.h"
33 #include "flow-bit.h"
34 #include "flow-util.h"
35 #include "detect-flowbits.h"
36 #include "util-spm.h"
37 #include "rust.h"
38 
39 #include "app-layer-parser.h"
40 
41 #include "detect-parse.h"
42 #include "detect-engine.h"
43 #include "detect-engine-mpm.h"
44 #include "detect-engine-state.h"
45 #include "detect-engine-build.h"
47 
48 #include "tree.h"
49 
50 #include "util-enum.h"
51 #include "util-var-name.h"
52 #include "util-unittest.h"
53 #include "util-debug.h"
54 #include "util-conf.h"
55 
56 #define PARSE_REGEX "^([a-z]+)(?:,\\s*(.*))?"
57 static DetectParseRegex parse_regex;
58 
59 #define MAX_TOKENS 100
60 
62  const Signature *, const SigMatchCtx *);
63 static int DetectFlowbitSetup (DetectEngineCtx *, Signature *, const char *);
64 static int FlowbitOrAddData(DetectEngineCtx *, DetectFlowbitsData *, char *);
65 void DetectFlowbitFree (DetectEngineCtx *, void *);
66 #ifdef UNITTESTS
67 void FlowBitsRegisterTests(void);
68 #endif
69 static bool PrefilterFlowbitIsPrefilterable(const Signature *s);
70 static int PrefilterSetupFlowbits(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
71 
73 {
74  sigmatch_table[DETECT_FLOWBITS].name = "flowbits";
75  sigmatch_table[DETECT_FLOWBITS].desc = "operate on flow flag";
76  sigmatch_table[DETECT_FLOWBITS].url = "/rules/flow-keywords.html#flowbits";
78  sigmatch_table[DETECT_FLOWBITS].Setup = DetectFlowbitSetup;
80 #ifdef UNITTESTS
82 #endif
83  /* this is compatible to ip-only signatures */
85 
86  sigmatch_table[DETECT_FLOWBITS].SupportsPrefilter = PrefilterFlowbitIsPrefilterable;
87  sigmatch_table[DETECT_FLOWBITS].SetupPrefilter = PrefilterSetupFlowbits;
88  /* all but pre_flow */
92  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex);
93 }
94 
95 static bool DetectFlowbitIsPostmatch(uint8_t cmd)
96 {
98 
99  switch (cmd) {
102  return true;
103  }
104  return false;
105 }
106 
108  { "set", DETECT_FLOWBITS_CMD_SET },
109  { "unset", DETECT_FLOWBITS_CMD_UNSET },
110  { "isnotset", DETECT_FLOWBITS_CMD_ISNOTSET },
111  { "isset", DETECT_FLOWBITS_CMD_ISSET },
112 };
113 
114 static inline int DetectFlowbitValidateDo(
115  Signature *s, uint8_t cmd, uint8_t cmd2, uint32_t idx, bool err)
116 {
117  bool postmatch = DetectFlowbitIsPostmatch(cmd);
118  SigMatch *list = postmatch ? s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]
120 
121  for (SigMatch *sm = list; sm != NULL; sm = sm->next) {
122  if (sm->type != DETECT_FLOWBITS)
123  continue;
124 
125  DetectFlowbitsData *fd = (DetectFlowbitsData *)sm->ctx;
126  if ((fd->idx == idx) && (fd->cmd == cmd)) {
127  if (err) {
128  SCLogError("invalid flowbit command combination in the same signature: isset and "
129  "isnotset");
130  return -1;
131  }
132  SCLogWarning(
133  "inconsequential flowbit command combination in the same signature: %s and %s",
136  return 0;
137  }
138  }
139 
140  /* no invalid or inconsequential command pair was found */
141  return 1;
142 }
143 
144 static int DetectFlowbitValidate(Signature *s, DetectFlowbitsData *fd)
145 {
146  struct DetectFlowbitInvalidCmdMap_ {
147  uint8_t cmd1;
148  uint8_t cmd2;
149  bool err; /* Error out if rule is unsatisfiable at runtime */
150  };
151 
152  struct DetectFlowbitInvalidCmdMap_ icmds_map[] = {
153  /* POSTMATCH, MATCH combinations */
156  /* POSTMATCH, POSTMATCH combinations */
158  /* MATCH, MATCH combinations */
160  };
161 
162  int ret = 0;
163 
164  for (uint8_t i = 0; i < ARRAY_SIZE(icmds_map); i++) {
165  if (fd->cmd == icmds_map[i].cmd1) {
166  ret = DetectFlowbitValidateDo(
167  s, icmds_map[i].cmd2, icmds_map[i].cmd1, fd->idx, icmds_map[i].err);
168  if (ret != 1) {
169  return ret;
170  }
171  } else if (fd->cmd == icmds_map[i].cmd2) {
172  ret = DetectFlowbitValidateDo(
173  s, icmds_map[i].cmd1, icmds_map[i].cmd2, fd->idx, icmds_map[i].err);
174  if (ret != 1) {
175  return ret;
176  }
177  }
178  }
179 
180  return 0;
181 }
182 
183 static int FlowbitOrAddData(DetectEngineCtx *de_ctx, DetectFlowbitsData *cd, char *arrptr)
184 {
185  char *strarr[MAX_TOKENS];
186  char *token;
187  char *saveptr = NULL;
188  uint8_t i = 0;
189 
190  while ((token = strtok_r(arrptr, "|", &saveptr))) {
191  // Check for leading/trailing spaces in the token
192  while(isspace((unsigned char)*token))
193  token++;
194  if (*token == 0)
195  goto next;
196  char *end = token + strlen(token) - 1;
197  while(end > token && isspace((unsigned char)*end))
198  *(end--) = '\0';
199 
200  // Check for spaces in between the flowbit names
201  if (strchr(token, ' ') != NULL) {
202  SCLogError("Spaces are not allowed in flowbit names.");
203  return -1;
204  }
205 
206  if (i == MAX_TOKENS) {
207  SCLogError("Number of flowbits exceeds "
208  "maximum allowed: %d.",
209  MAX_TOKENS);
210  return -1;
211  }
212  strarr[i++] = token;
213  next:
214  arrptr = NULL;
215  }
216  if (i == 0) {
217  SCLogError("No valid flowbits specified");
218  return -1;
219  }
220 
221  cd->or_list_size = i;
222  cd->or_list = SCCalloc(cd->or_list_size, sizeof(uint32_t));
223  if (unlikely(cd->or_list == NULL))
224  return -1;
225  for (uint8_t j = 0; j < cd->or_list_size ; j++) {
226  uint32_t varname_id = VarNameStoreRegister(strarr[j], VAR_TYPE_FLOW_BIT);
227  if (unlikely(varname_id == 0))
228  return -1;
229  cd->or_list[j] = varname_id;
231  }
232 
233  return 1;
234 }
235 
236 static int DetectFlowbitMatchUnset (Packet *p, const DetectFlowbitsData *fd)
237 {
238  if (p->flow == NULL)
239  return 0;
240 
241  FlowBitUnset(p->flow,fd->idx);
242 
243  return 1;
244 }
245 
246 static int DetectFlowbitMatchSet (Packet *p, const DetectFlowbitsData *fd)
247 {
248  if (p->flow == NULL)
249  return -1;
250 
251  int r = FlowBitSet(p->flow, fd->idx);
252  SCLogDebug("set %u", fd->idx);
253  return r;
254 }
255 
256 static int DetectFlowbitMatchIsset (Packet *p, const DetectFlowbitsData *fd)
257 {
258  if (p->flow == NULL)
259  return 0;
260  if (fd->or_list_size > 0) {
261  for (uint8_t i = 0; i < fd->or_list_size; i++) {
262  if (FlowBitIsset(p->flow, fd->or_list[i]) == 1)
263  return 1;
264  }
265  return 0;
266  }
267 
268  return FlowBitIsset(p->flow,fd->idx);
269 }
270 
271 static int DetectFlowbitMatchIsnotset (Packet *p, const DetectFlowbitsData *fd)
272 {
273  if (p->flow == NULL)
274  return 0;
275  if (fd->or_list_size > 0) {
276  for (uint8_t i = 0; i < fd->or_list_size; i++) {
277  if (FlowBitIsnotset(p->flow, fd->or_list[i]) == 1)
278  return 1;
279  }
280  return 0;
281  }
282  return FlowBitIsnotset(p->flow,fd->idx);
283 }
284 
285 /*
286  * returns 0: no match (or error)
287  * 1: match
288  */
289 
291  const Signature *s, const SigMatchCtx *ctx)
292 {
293  const DetectFlowbitsData *fd = (const DetectFlowbitsData *)ctx;
294  if (fd == NULL)
295  return 0;
296 
297  switch (fd->cmd) {
299  return DetectFlowbitMatchIsset(p,fd);
301  return DetectFlowbitMatchIsnotset(p,fd);
303  int r = DetectFlowbitMatchSet(p, fd);
304  /* only on a new "set" invoke the prefilter */
305  if (r == 1 && fd->post_rule_match_prefilter) {
306  SCLogDebug("flowbit set, appending to work queue");
308  }
309  return (r != -1);
310  }
312  return DetectFlowbitMatchUnset(p, fd);
313  default:
314  SCLogError("unknown cmd %" PRIu32 "", fd->cmd);
315  return 0;
316  }
317 
318  return 0;
319 }
320 
321 static int DetectFlowbitParse(const char *str, char *cmd, int cmd_len, char *name,
322  int name_len)
323 {
324  int rc;
325  size_t pcre2len;
326  pcre2_match_data *match = NULL;
327 
328  int count = DetectParsePcreExec(&parse_regex, &match, str, 0, 0);
329  if (count != 2 && count != 3) {
330  SCLogError("\"%s\" is not a valid setting for flowbits.", str);
331  goto error;
332  }
333 
334  pcre2len = cmd_len;
335  rc = pcre2_substring_copy_bynumber(match, 1, (PCRE2_UCHAR8 *)cmd, &pcre2len);
336  if (rc < 0) {
337  SCLogError("pcre2_substring_copy_bynumber failed");
338  goto error;
339  }
340 
341  if (count == 3) {
342  pcre2len = name_len;
343  rc = pcre2_substring_copy_bynumber(match, 2, (PCRE2_UCHAR8 *)name, &pcre2len);
344  if (rc < 0) {
345  SCLogError("pcre2_substring_copy_bynumber failed");
346  goto error;
347  }
348 
349  /* Trim trailing whitespace. */
350  while (strlen(name) > 0 && isblank(name[strlen(name) - 1])) {
351  name[strlen(name) - 1] = '\0';
352  }
353 
354  if (strchr(name, '|') == NULL) {
355  /* Validate name, spaces are not allowed. */
356  for (size_t i = 0; i < strlen(name); i++) {
357  if (isblank(name[i])) {
358  SCLogError("spaces not allowed in flowbit names");
359  goto error;
360  }
361  }
362  }
363  }
364 
365  pcre2_match_data_free(match);
366  return 1;
367 
368 error:
369  if (match) {
370  pcre2_match_data_free(match);
371  }
372  return 0;
373 }
374 
375 int DetectFlowbitSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
376 {
377  DetectFlowbitsData *cd = NULL;
378  uint8_t fb_cmd = 0;
379  char fb_cmd_str[16] = "", fb_name[256] = "";
380 
381  if (!DetectFlowbitParse(rawstr, fb_cmd_str, sizeof(fb_cmd_str), fb_name,
382  sizeof(fb_name))) {
383  return -1;
384  }
385 
386  if (strcmp(fb_cmd_str,"noalert") == 0) {
387  if (strlen(fb_name) != 0)
388  goto error;
389  s->action &= ~ACTION_ALERT;
390  return 0;
391  } else if (strcmp(fb_cmd_str,"isset") == 0) {
392  fb_cmd = DETECT_FLOWBITS_CMD_ISSET;
393  } else if (strcmp(fb_cmd_str,"isnotset") == 0) {
395  } else if (strcmp(fb_cmd_str,"set") == 0) {
396  fb_cmd = DETECT_FLOWBITS_CMD_SET;
397  } else if (strcmp(fb_cmd_str,"unset") == 0) {
398  fb_cmd = DETECT_FLOWBITS_CMD_UNSET;
399  } else {
400  SCLogError("ERROR: flowbits action \"%s\" is not supported.", fb_cmd_str);
401  goto error;
402  }
403 
404  switch (fb_cmd) {
409  default:
410  if (strlen(fb_name) == 0)
411  goto error;
412  break;
413  }
414 
415  cd = SCCalloc(1, sizeof(DetectFlowbitsData));
416  if (unlikely(cd == NULL))
417  goto error;
418  if (strchr(fb_name, '|') != NULL) {
419  int retval = FlowbitOrAddData(de_ctx, cd, fb_name);
420  if (retval == -1) {
421  goto error;
422  }
423  cd->cmd = fb_cmd;
424  } else {
425  uint32_t varname_id = VarNameStoreRegister(fb_name, VAR_TYPE_FLOW_BIT);
426  if (unlikely(varname_id == 0))
427  goto error;
428  cd->idx = varname_id;
430  cd->cmd = fb_cmd;
431  cd->or_list_size = 0;
432  cd->or_list = NULL;
433  SCLogDebug("idx %" PRIu32 ", cmd %s, name %s",
434  cd->idx, fb_cmd_str, strlen(fb_name) ? fb_name : "(none)");
435  }
436 
437  if (DetectFlowbitValidate(s, cd) != 0) {
438  goto error;
439  }
440 
441  /* Okay so far so good, lets get this into a SigMatch
442  * and put it in the Signature. */
443 
444  switch (fb_cmd) {
445  /* noalert can't happen here */
448  /* checks, so packet list */
450  DETECT_SM_LIST_MATCH) == NULL) {
451  goto error;
452  }
453  break;
454 
457  /* modifiers, only run when entire sig has matched */
459  DETECT_SM_LIST_POSTMATCH) == NULL) {
460  goto error;
461  }
462  break;
463 
464  // suppress coverity warning as scan-build-7 warns w/o this.
465  // coverity[deadcode : FALSE]
466  default:
467  goto error;
468  }
469 
470  return 0;
471 
472 error:
473  if (cd != NULL)
475  return -1;
476 }
477 
479 {
481  if (fd == NULL)
482  return;
484  if (fd->or_list != NULL) {
485  for (uint8_t i = 0; i < fd->or_list_size; i++) {
487  }
488  SCFree(fd->or_list);
489  }
490  SCFree(fd);
491 }
492 
493 struct FBAnalyzer {
494  struct FBAnalyze *array;
495  uint32_t array_size;
496 };
497 
498 struct FBAnalyze {
501 
502  uint32_t *set_sids;
503  uint32_t set_sids_idx;
504  uint32_t set_sids_size;
505 
506  uint32_t *isset_sids;
507  uint32_t isset_sids_idx;
508  uint32_t isset_sids_size;
509 
510  uint32_t *isnotset_sids;
513 
514  uint32_t *unset_sids;
515  uint32_t unset_sids_idx;
516  uint32_t unset_sids_size;
517 };
518 
519 extern bool rule_engine_analysis_set;
520 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
521  struct FBAnalyze *array, uint32_t elements);
522 
523 static void FBAnalyzerArrayFree(struct FBAnalyze *array, const uint32_t array_size)
524 {
525  if (array) {
526  for (uint32_t i = 0; i < array_size; i++) {
527  SCFree(array[i].set_sids);
528  SCFree(array[i].unset_sids);
529  SCFree(array[i].isset_sids);
530  SCFree(array[i].isnotset_sids);
531  }
532  SCFree(array);
533  }
534 }
535 
536 static void FBAnalyzerFree(struct FBAnalyzer *fba)
537 {
538  if (fba && fba->array) {
539  FBAnalyzerArrayFree(fba->array, fba->array_size);
540  fba->array = NULL;
541  fba->array_size = 0;
542  }
543 }
544 
545 #define MAX_SIDS 8
546 static bool CheckExpand(const uint32_t sids_idx, uint32_t **sids, uint32_t *sids_size)
547 {
548  if (sids_idx >= *sids_size) {
549  const uint32_t old_size = *sids_size;
550  const uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
551 
552  void *ptr = SCRealloc(*sids, new_size * sizeof(uint32_t));
553  if (ptr == NULL)
554  return false;
555  *sids_size = new_size;
556  *sids = ptr;
557  }
558  return true;
559 }
560 
561 static int DetectFlowbitsAnalyzeSignature(const Signature *s, struct FBAnalyzer *fba)
562 {
563  struct FBAnalyze *array = fba->array;
564  if (array == NULL)
565  return -1;
566 
567  /* see if the signature uses stateful matching TODO is there not a flag? */
568  bool has_state = (s->init_data->buffer_index != 0);
569 
570  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL;
571  sm = sm->next) {
572  if (sm->type != DETECT_FLOWBITS)
573  continue;
574  /* figure out the flowbit action */
575  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
576  // Handle flowbit array in case of ORed flowbits
577  for (uint8_t k = 0; k < fb->or_list_size; k++) {
578  struct FBAnalyze *fa = &array[fb->or_list[k]];
579  fa->cnts[fb->cmd]++;
580  fa->state_cnts[fb->cmd] += has_state;
581 
582  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
583  if (!CheckExpand(fa->isset_sids_idx, &fa->isset_sids, &fa->isset_sids_size))
584  return -1;
585  fa->isset_sids[fa->isset_sids_idx] = s->iid;
586  fa->isset_sids_idx++;
587  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
588  if (!CheckExpand(
590  return -1;
591  fa->isnotset_sids[fa->isnotset_sids_idx] = s->iid;
592  fa->isnotset_sids_idx++;
593  }
594  }
595  if (fb->or_list_size == 0) {
596  struct FBAnalyze *fa = &array[fb->idx];
597  fa->cnts[fb->cmd]++;
598  fa->state_cnts[fb->cmd] += has_state;
599 
600  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
601  if (!CheckExpand(fa->isset_sids_idx, &fa->isset_sids, &fa->isset_sids_size))
602  return -1;
603  fa->isset_sids[fa->isset_sids_idx] = s->iid;
604  fa->isset_sids_idx++;
605  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
606  if (!CheckExpand(
608  return -1;
609  fa->isnotset_sids[fa->isnotset_sids_idx] = s->iid;
610  fa->isnotset_sids_idx++;
611  }
612  }
613  }
614  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]; sm != NULL;
615  sm = sm->next) {
616  if (sm->type != DETECT_FLOWBITS)
617  continue;
618  /* figure out what flowbit action */
619  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
620  struct FBAnalyze *fa = &array[fb->idx];
621  fa->cnts[fb->cmd]++;
622  fa->state_cnts[fb->cmd] += has_state;
623 
624  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
625  if (!CheckExpand(fa->set_sids_idx, &fa->set_sids, &fa->set_sids_size))
626  return -1;
627  fa->set_sids[fa->set_sids_idx] = s->iid;
628  fa->set_sids_idx++;
629  } else if (fb->cmd == DETECT_FLOWBITS_CMD_UNSET) {
630  if (!CheckExpand(fa->unset_sids_idx, &fa->unset_sids, &fa->unset_sids_size))
631  return -1;
632  fa->unset_sids[fa->unset_sids_idx] = s->iid;
633  fa->unset_sids_idx++;
634  }
635  }
636  return 0;
637 }
638 
640 {
641  const uint32_t max_fb_id = de_ctx->max_fb_id;
642  if (max_fb_id == 0)
643  return 0;
644 
645  struct FBAnalyzer fba = { .array = NULL, .array_size = 0 };
646  const uint32_t array_size = max_fb_id + 1;
647  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
648  if (array == NULL) {
649  SCLogError("Unable to allocate flowbit analyze array");
650  return -1;
651  }
652  fba.array = array;
653  fba.array_size = array_size;
654 
655  SCLogDebug("fb analyzer array size: %"PRIu64,
656  (uint64_t)(array_size * sizeof(struct FBAnalyze)));
657 
658  /* fill flowbit array, updating counters per sig */
659  for (uint32_t i = 0; i < de_ctx->sig_array_len; i++) {
660  const Signature *s = de_ctx->sig_array[i];
661 
662  int r = DetectFlowbitsAnalyzeSignature(s, &fba);
663  if (r < 0) {
664  FBAnalyzerFree(&fba);
665  return -1;
666  }
667  }
668 
669  /* walk array to see if all bits make sense */
670  for (uint32_t i = 0; i < array_size; i++) {
671  const char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
672  if (varname == NULL)
673  continue;
674 
675  bool to_state = false;
676 
677  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] &&
678  array[i].cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
679 
680  const Signature *s = de_ctx->sig_array[array[i].isset_sids[0]];
681  SCLogWarning("flowbit '%s' is checked but not "
682  "set. Checked in %u and %u other sigs",
683  varname, s->id, array[i].isset_sids_idx - 1);
684  }
685  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
686  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0)
687  {
688  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
689  }
690 
691  /* if signature depends on 'stateful' flowbits, then turn the
692  * sig into a stateful sig itself */
693  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
694  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
696  {
697  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
698  to_state = true;
699  }
700 
701  SCLogDebug("ALL flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
704  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
705  SCLogDebug("STATE flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
710  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
711  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
712  de_ctx->sig_array[array[i].set_sids[x]]->id);
713  }
714  if (to_state) {
715  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
716  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
717  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
718 
721 
722  const uint32_t sids_array_size = array[i].set_sids_idx;
723  if (sids_array_size == 0)
724  continue;
725 
726  // save information about flowbits that affect this rule's state
727  if (s->init_data->rule_state_dependant_sids_array == NULL) {
729  SCCalloc(sids_array_size, sizeof(uint32_t));
730  if (s->init_data->rule_state_dependant_sids_array == NULL) {
731  SCLogError("Failed to allocate memory for rule_state_dependant_ids");
732  goto error;
733  }
736  SCCalloc(s->init_data->rule_state_flowbits_ids_size, sizeof(uint32_t));
737  if (s->init_data->rule_state_flowbits_ids_array == NULL) {
738  SCLogError("Failed to allocate memory for rule_state_variable_idx");
739  goto error;
740  }
741  s->init_data->rule_state_dependant_sids_size = sids_array_size;
742  SCLogDebug("alloc'ed array for rule dependency and fbs idx array, sid %u, "
743  "sizes are %u and %u",
746  } else {
747  uint32_t new_array_size =
748  s->init_data->rule_state_dependant_sids_size + sids_array_size;
750  new_array_size * sizeof(uint32_t));
751  if (tmp_ptr == NULL) {
752  SCLogError("Failed to allocate memory for rule_state_variable_idx");
753  goto error;
754  }
756  s->init_data->rule_state_dependant_sids_size = new_array_size;
757  SCLogDebug("realloc'ed array for rule dependency, sid %u, new size is %u",
759  uint32_t new_fb_array_size = s->init_data->rule_state_flowbits_ids_size + 1;
760  void *tmp_fb_ptr = SCRealloc(s->init_data->rule_state_flowbits_ids_array,
761  new_fb_array_size * sizeof(uint32_t));
762  if (tmp_fb_ptr == NULL) {
763  SCLogError("Failed to reallocate memory for rule_state_variable_idx");
764  goto error;
765  }
766  s->init_data->rule_state_flowbits_ids_array = tmp_fb_ptr;
767  SCLogDebug(
768  "realloc'ed array for flowbits ids, new size is %u", new_fb_array_size);
769  s->init_data->rule_state_dependant_sids_size = new_array_size;
770  s->init_data->rule_state_flowbits_ids_size = new_fb_array_size;
771  }
772  for (uint32_t idx = 0; idx < s->init_data->rule_state_dependant_sids_size; idx++) {
773  if (idx < array[i].set_sids_idx) {
776  de_ctx->sig_array[array[i].set_sids[idx]]->id;
778  }
779  }
780  s->init_data
782  1] = i;
784  // flowbit info saving for rule made stateful rule work finished
785 
786  SCLogDebug("made SID %u stateful because it depends on "
787  "stateful rules that set flowbit %s", s->id, varname);
788  }
789  }
790  }
791 
793  DetectFlowbitsAnalyzeDump(de_ctx, array, array_size);
794  }
795 
796  FBAnalyzerFree(&fba);
797  return 0;
798 error:
799  FBAnalyzerFree(&fba);
800  return -1;
801 }
802 
803 // TODO misses IPOnly rules. IPOnly flowbit rules are set only though.
804 static struct FBAnalyzer DetectFlowbitsAnalyzeForGroup(
805  const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
806 {
807  struct FBAnalyzer fba = { .array = NULL, .array_size = 0 };
808 
809  const uint32_t max_fb_id = de_ctx->max_fb_id;
810  if (max_fb_id == 0)
811  return fba;
812 
813  uint32_t array_size = max_fb_id + 1;
814  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
815  if (array == NULL) {
816  SCLogError("Unable to allocate flowbit analyze array");
817  return fba;
818  }
819  SCLogDebug(
820  "fb analyzer array size: %" PRIu64, (uint64_t)(array_size * sizeof(struct FBAnalyze)));
821  fba.array = array;
822  fba.array_size = array_size;
823 
824  /* fill flowbit array, updating counters per sig */
825  for (uint32_t i = 0; i < sgh->init->sig_cnt; i++) {
826  const Signature *s = sgh->init->match_array[i];
827  SCLogDebug("sgh %p: s->id %u", sgh, s->id);
828 
829  int r = DetectFlowbitsAnalyzeSignature(s, &fba);
830  if (r < 0) {
831  FBAnalyzerFree(&fba);
832  return fba;
833  }
834  }
835 
836  /* walk array to see if all bits make sense */
837  for (uint32_t i = 0; i < array_size; i++) {
838  const char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
839  if (varname == NULL)
840  continue;
841 
842  bool to_state = false;
843  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
844  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
845  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
846  }
847 
848  /* if signature depends on 'stateful' flowbits, then turn the
849  * sig into a stateful sig itself */
850  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
851  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
853  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
854  to_state = true;
855  }
856 
857  SCLogDebug("ALL flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
860  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
861  SCLogDebug("STATE flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
866  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
867  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
868  de_ctx->sig_array[array[i].set_sids[x]]->id);
869  }
870  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
871  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
872  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
873 
874  if (to_state) {
876  SCLogDebug("made SID %u stateful because it depends on "
877  "stateful rules that set flowbit %s",
878  s->id, varname);
879  }
880  }
881  }
882 
883  return fba;
884 }
885 
887 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
888  struct FBAnalyze *array, uint32_t elements)
889 {
890  SCJsonBuilder *js = SCJbNewObject();
891  if (js == NULL)
892  return;
893 
894  SCJbOpenArray(js, "flowbits");
895  for (uint32_t x = 0; x < elements; x++) {
896  const char *varname = VarNameStoreSetupLookup(x, VAR_TYPE_FLOW_BIT);
897  if (varname == NULL)
898  continue;
899 
900  const struct FBAnalyze *e = &array[x];
901 
902  SCJbStartObject(js);
903  SCJbSetString(js, "name", varname);
904  SCJbSetUint(js, "internal_id", x);
905  SCJbSetUint(js, "set_cnt", e->cnts[DETECT_FLOWBITS_CMD_SET]);
906  SCJbSetUint(js, "unset_cnt", e->cnts[DETECT_FLOWBITS_CMD_UNSET]);
907  SCJbSetUint(js, "isset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISSET]);
908  SCJbSetUint(js, "isnotset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISNOTSET]);
909 
910  // sets
911  if (e->cnts[DETECT_FLOWBITS_CMD_SET]) {
912  SCJbOpenArray(js, "sets");
913  for (uint32_t i = 0; i < e->set_sids_idx; i++) {
914  const Signature *s = de_ctx->sig_array[e->set_sids[i]];
915  SCJbAppendUint(js, s->id);
916  }
917  SCJbClose(js);
918  }
919  // gets
921  SCJbOpenArray(js, "isset");
922  for (uint32_t i = 0; i < e->isset_sids_idx; i++) {
923  const Signature *s = de_ctx->sig_array[e->isset_sids[i]];
924  SCJbAppendUint(js, s->id);
925  }
926  SCJbClose(js);
927  }
928  // isnotset
930  SCJbOpenArray(js, "isnotset");
931  for (uint32_t i = 0; i < e->isnotset_sids_idx; i++) {
932  const Signature *s = de_ctx->sig_array[e->isnotset_sids[i]];
933  SCJbAppendUint(js, s->id);
934  }
935  SCJbClose(js);
936  }
937  // unset
939  SCJbOpenArray(js, "unset");
940  for (uint32_t i = 0; i < e->unset_sids_idx; i++) {
941  const Signature *s = de_ctx->sig_array[e->unset_sids[i]];
942  SCJbAppendUint(js, s->id);
943  }
944  SCJbClose(js);
945  }
946  SCJbClose(js);
947  }
948  SCJbClose(js); // array
949  SCJbClose(js); // object
950 
951  const char *filename = "flowbits.json";
952  const char *log_dir = SCConfigGetLogDirectory();
953  char log_path[PATH_MAX] = "";
954  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
955 
957  FILE *fp = fopen(log_path, "w");
958  if (fp != NULL) {
959  fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
960  fprintf(fp, "\n");
961  fclose(fp);
962  }
964 
965  SCJbFree(js);
966 }
967 
968 static bool PrefilterFlowbitIsPrefilterable(const Signature *s)
969 {
970  SCLogDebug("sid:%u: checking", s->id);
971 
972  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL;
973  sm = sm->next) {
974  switch (sm->type) {
975  case DETECT_FLOWBITS: {
976  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
977  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
978  SCLogDebug("sid:%u: FLOWBITS ISSET can prefilter", s->id);
979  return true;
980  }
981  break;
982  }
983  }
984  }
985  SCLogDebug("sid:%u: no flowbit prefilter", s->id);
986  return false;
987 }
988 
989 /** core flowbit data structure: map a flowbit id to the signatures that need inspecting after it is
990  * found. Part of a rb-tree. */
991 typedef struct PrefilterFlowbit {
992  uint32_t *rule_id; /**< array of signature iid that are part of this prefilter */
993  RB_ENTRY(PrefilterFlowbit) __attribute__((__packed__)) rb;
994  uint32_t id; /**< flowbit id */
995  uint32_t rule_id_size; /**< size in elements of `rule_id` */
996  uint32_t rule_id_cnt; /**< usage in elements of `rule_id` */
997 } __attribute__((__packed__)) PrefilterFlowbit;
998 
999 static int PrefilterFlowbitCompare(const PrefilterFlowbit *a, const PrefilterFlowbit *b)
1000 {
1001  if (a->id > b->id)
1002  return 1;
1003  else if (a->id < b->id)
1004  return -1;
1005  else
1006  return 0;
1007 }
1008 
1009 /** red-black tree prototype for PFB (Prefilter Flow Bits) */
1011 RB_PROTOTYPE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare);
1012 RB_GENERATE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare);
1013 
1015  struct PFB fb_tree;
1016 };
1017 
1018 static void PrefilterFlowbitFree(void *vctx)
1019 {
1020  struct PrefilterEngineFlowbits *ctx = vctx;
1021  struct PrefilterFlowbit *rec, *safe = NULL;
1022  RB_FOREACH_SAFE (rec, PFB, &ctx->fb_tree, safe) {
1023  PFB_RB_REMOVE(&ctx->fb_tree, rec);
1024  SCFree(rec->rule_id);
1025  SCFree(rec);
1026  }
1027 
1028  SCFree(ctx);
1029 }
1030 
1031 static void PrefilterFlowbitMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
1032 {
1033  struct PrefilterEngineFlowbits *ctx = (struct PrefilterEngineFlowbits *)pectx;
1034  SCLogDebug("%" PRIu64 ": ctx %p", PcapPacketCntGet(p), ctx);
1035 
1036  if (p->flow == NULL) {
1037  SCReturn;
1038  }
1039 
1040  for (GenericVar *gv = p->flow->flowvar; gv != NULL; gv = gv->next) {
1041  if (gv->type != DETECT_FLOWBITS)
1042  continue;
1043 
1044  PrefilterFlowbit lookup;
1045  memset(&lookup, 0, sizeof(lookup));
1046  lookup.id = gv->idx;
1047  SCLogDebug("flowbit %u", gv->idx);
1048 
1049  PrefilterFlowbit *b = PFB_RB_FIND(&ctx->fb_tree, &lookup);
1050  if (b == NULL) {
1051  SCLogDebug("flowbit %u not in the tree", lookup.id);
1052  } else {
1053  SCLogDebug("flowbit %u found in the tree: %u", lookup.id, b->id);
1054 
1055  PrefilterAddSids(&det_ctx->pmq, b->rule_id, b->rule_id_cnt);
1056 #ifdef DEBUG
1057  for (uint32_t x = 0; x < b->rule_id_cnt; x++) {
1058  const Signature *s = det_ctx->de_ctx->sig_array[b->rule_id[x]];
1059  SCLogDebug("flowbit %u -> sig %u", gv->idx, s->id);
1060  }
1061 #endif
1062  }
1063  }
1064 }
1065 
1066 static void PrefilterFlowbitPostRuleMatch(
1067  DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f)
1068 {
1069  struct PrefilterEngineFlowbits *ctx = (struct PrefilterEngineFlowbits *)pectx;
1070  SCLogDebug("%" PRIu64 ": ctx %p", PcapPacketCntGet(p), ctx);
1071 
1072  if (p->flow == NULL) {
1073  SCReturn;
1074  }
1075 
1076  for (uint32_t i = 0; i < det_ctx->post_rule_work_queue.len; i++) {
1077  const PostRuleMatchWorkQueueItem *w = &det_ctx->post_rule_work_queue.q[i];
1078  if (w->sm_type != DETECT_FLOWBITS)
1079  continue;
1080 
1081  PrefilterFlowbit lookup;
1082  memset(&lookup, 0, sizeof(lookup));
1083  lookup.id = w->value;
1084 
1085  PrefilterFlowbit *b = PFB_RB_FIND(&ctx->fb_tree, &lookup);
1086  if (b == NULL) {
1087  SCLogDebug("flowbit %u not in the tree", lookup.id);
1088  } else {
1089  SCLogDebug("flowbit %u found in the tree: %u. Adding %u sids", lookup.id, b->id,
1090  b->rule_id_cnt);
1091  PrefilterAddSids(&det_ctx->pmq, b->rule_id, b->rule_id_cnt);
1092 #ifdef DEBUG
1093  // SCLogDebug("b %u", b->rule_id_cnt);
1094  for (uint32_t x = 0; x < b->rule_id_cnt; x++) {
1095  Signature *s = det_ctx->de_ctx->sig_array[b->rule_id[x]];
1096  SCLogDebug("flowbit %u -> sig %u (triggered by %u)", w->value, s->id,
1097  det_ctx->de_ctx->sig_array[w->id]->id);
1098  }
1099 #endif
1100  }
1101  }
1102 }
1103 
1104 #define BLOCK_SIZE 8
1106 static int AddBitAndSid(
1107  struct PrefilterEngineFlowbits *ctx, const Signature *s, const uint32_t flowbit_id)
1108 {
1109  PrefilterFlowbit x;
1110  memset(&x, 0, sizeof(x));
1111  x.id = flowbit_id;
1112 
1113  PrefilterFlowbit *pfb = PFB_RB_FIND(&ctx->fb_tree, &x);
1114  if (pfb == NULL) {
1115  PrefilterFlowbit *add = SCCalloc(1, sizeof(*add));
1116  if (add == NULL)
1117  return -1;
1118 
1119  add->id = flowbit_id;
1120  add->rule_id = SCCalloc(1, BLOCK_SIZE * sizeof(uint32_t));
1121  if (add->rule_id == NULL) {
1122  SCFree(add);
1123  return -1;
1124  }
1125  add->rule_id_size = BLOCK_SIZE;
1126  add->rule_id_cnt = 1;
1127  add->rule_id[0] = s->iid;
1128 
1129  PrefilterFlowbit *res = PFB_RB_INSERT(&ctx->fb_tree, add);
1130  SCLogDebug("not found, so added (res %p)", res);
1131  if (res != NULL) {
1132  // duplicate, shouldn't be possible after the FIND above
1133  BUG_ON(1);
1134  return -1;
1135  }
1136  } else {
1137  SCLogDebug("found! pfb %p id %u", pfb, pfb->id);
1138 
1139  if (pfb->rule_id_cnt < pfb->rule_id_size) {
1140  pfb->rule_id[pfb->rule_id_cnt++] = s->iid;
1141  } else {
1142  uint32_t *ptr =
1143  SCRealloc(pfb->rule_id, (pfb->rule_id_size + BLOCK_SIZE) * sizeof(uint32_t));
1144  if (ptr == NULL) {
1145  // memory stays in the tree
1146  return -1;
1147  }
1148  pfb->rule_id = ptr;
1149  pfb->rule_id_size += BLOCK_SIZE;
1150  pfb->rule_id[pfb->rule_id_cnt++] = s->iid;
1151  }
1152  }
1153  return 0;
1154 }
1155 
1156 static int AddBitsAndSid(const DetectEngineCtx *de_ctx, struct PrefilterEngineFlowbits *ctx,
1157  const DetectFlowbitsData *fb, const Signature *s)
1158 {
1159  if (fb->or_list_size == 0) {
1160  if (AddBitAndSid(ctx, s, fb->idx) < 0) {
1161  return -1;
1162  }
1163  } else {
1164  for (uint8_t i = 0; i < fb->or_list_size; i++) {
1165  SCLogDebug("flowbit OR: bit %u", fb->or_list[i]);
1166  if (AddBitAndSid(ctx, s, fb->or_list[i]) < 0) {
1167  return -1;
1168  }
1169  }
1170  }
1171  return 0;
1172 }
1173 
1174 static uint32_t NextMultiple(const uint32_t v, const uint32_t m)
1175 {
1176  return v + (m - v % m);
1177 }
1178 
1179 /** \internal
1180  * \brief adds sids for 'isset' prefilter flowbits
1181  * \retval int 1 if we added sid(s), 0 if we didn't, -1 on error */
1182 // TODO skip sids that aren't set by this sgh
1183 // TODO skip sids that doesn't have a isset in the same direction
1184 static int AddIssetSidsForBit(const DetectEngineCtx *de_ctx, const struct FBAnalyzer *fba,
1185  const DetectFlowbitsData *fb, PrefilterFlowbit *add)
1186 {
1187  int added = 0;
1188  for (uint32_t i = 0; i < fba->array[fb->idx].isset_sids_idx; i++) {
1189  const uint32_t sig_iid = fba->array[fb->idx].isset_sids[i];
1190  const Signature *s = de_ctx->sig_array[sig_iid];
1191  SCLogDebug("flowbit: %u => considering sid %u (iid:%u)", fb->idx, s->id, s->iid);
1192 
1193  /* Skip sids that aren't prefilter. These would just run all the time. */
1194  if (s->init_data->prefilter_sm == NULL ||
1196 #ifdef DEBUG
1197  const char *name = s->init_data->prefilter_sm
1199  : "none";
1200  SCLogDebug("flowbit: %u => rejected sid %u (iid:%u). No prefilter or prefilter not "
1201  "flowbits (%p, %s, %d)",
1202  fb->idx, s->id, sig_iid, s->init_data->prefilter_sm, name,
1204 #endif
1205  continue;
1206  }
1207 
1208  /* only add sids that match our bit */
1209  const DetectFlowbitsData *fs_fb =
1211  if (fs_fb->idx != fb->idx) {
1212  SCLogDebug(
1213  "flowbit: %u => rejected sid %u (iid:%u). Sig prefilters on different bit %u",
1214  fb->idx, s->id, sig_iid, fs_fb->idx);
1215  continue;
1216  }
1217 
1218  bool dup = false;
1219  for (uint32_t x = 0; x < add->rule_id_cnt; x++) {
1220  if (add->rule_id[x] == sig_iid) {
1221  dup = true;
1222  }
1223  }
1224 
1225  if (!dup) {
1226  if (add->rule_id_cnt < add->rule_id_size) {
1227  add->rule_id[add->rule_id_cnt++] = sig_iid;
1228  } else {
1229  uint32_t *ptr = SCRealloc(
1230  add->rule_id, (add->rule_id_size + BLOCK_SIZE) * sizeof(uint32_t));
1231  if (ptr == NULL) {
1232  return -1;
1233  }
1234  add->rule_id = ptr;
1235  add->rule_id_size += BLOCK_SIZE;
1236  add->rule_id[add->rule_id_cnt++] = sig_iid;
1237  }
1238  added = 1;
1239  SCLogDebug("flowbit: %u => accepted sid %u (iid:%u)", fb->idx, s->id, sig_iid);
1240  }
1241  }
1242  return added;
1243 }
1244 
1245 /* TODO shouldn't add sids for which Signature::num is < our num. Is this possible after sorting? */
1246 
1247 /** \brief For set flowbits, build "set" post-rule-match engine
1248  *
1249  * For set flowbits, a special post-rule-match engine is constructed
1250  * to update the running match array during rule matching.
1251  */
1252 static int AddBitSet(const DetectEngineCtx *de_ctx, struct FBAnalyzer *fba,
1253  struct PrefilterEngineFlowbits *ctx, const DetectFlowbitsData *fb, const Signature *s)
1254 {
1255  PrefilterFlowbit x;
1256  memset(&x, 0, sizeof(x));
1257  x.id = fb->idx;
1258  PrefilterFlowbit *pfb = PFB_RB_FIND(&ctx->fb_tree, &x);
1259  if (pfb == NULL) {
1260  PrefilterFlowbit *add = SCCalloc(1, sizeof(*add));
1261  if (add == NULL)
1262  return -1;
1263 
1264  add->id = fb->idx;
1265  add->rule_id_size = NextMultiple(fba->array[fb->idx].isset_sids_idx, BLOCK_SIZE);
1266  add->rule_id = SCCalloc(1, add->rule_id_size * sizeof(uint32_t));
1267  if (add->rule_id == NULL) {
1268  SCFree(add);
1269  return -1;
1270  }
1271 
1272  if (AddIssetSidsForBit(de_ctx, fba, fb, add) != 1) {
1273  SCLogDebug("no sids added");
1274  SCFree(add->rule_id);
1275  SCFree(add);
1276  return 0;
1277  }
1278  PrefilterFlowbit *res = PFB_RB_INSERT(&ctx->fb_tree, add);
1279  SCLogDebug("not found, so added (res %p)", res);
1280  BUG_ON(res != NULL); // TODO if res != NULL we have a duplicate which should be impossible
1281  } else {
1282  SCLogDebug("found! pfb %p id %u", pfb, pfb->id);
1283 
1284  int r = AddIssetSidsForBit(de_ctx, fba, fb, pfb);
1285  if (r < 0) {
1286  return -1;
1287  } else if (r == 0) {
1288  SCLogDebug("no sids added");
1289  return 0;
1290  }
1291  }
1292  return 1;
1293 }
1294 
1295 /** \brief build flowbit prefilter state(s)
1296  *
1297  * Build "set" and "isset" states.
1298  *
1299  * For each flowbit "isset" in the sgh, we need to check:
1300  * 1. is it supported
1301  * 2. is prefilter enabled
1302  * 3. does it match in the same dir or only opposing dir
1303  */
1304 static int PrefilterSetupFlowbits(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
1305 {
1306  if (sgh == NULL)
1307  return 0;
1308 
1309  SCLogDebug("sgh %p: setting up prefilter", sgh);
1310  struct PrefilterEngineFlowbits *isset_ctx = NULL;
1311  struct PrefilterEngineFlowbits *set_ctx = NULL;
1312 
1313  struct FBAnalyzer fb_analysis = DetectFlowbitsAnalyzeForGroup(de_ctx, sgh);
1314  if (fb_analysis.array == NULL)
1315  goto error;
1316 
1317  for (uint32_t i = 0; i < sgh->init->sig_cnt; i++) {
1318  Signature *s = sgh->init->match_array[i];
1319  if (s == NULL)
1320  continue;
1321 
1322  SCLogDebug("checking sid %u", s->id);
1323 
1324  /* first build the 'set' state */
1325  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]; sm != NULL;
1326  sm = sm->next) {
1327  if (sm->type != DETECT_FLOWBITS) {
1328  SCLogDebug("skip non flowbits sm");
1329  continue;
1330  }
1331 
1332  DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
1333  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
1334  SCLogDebug(
1335  "DETECT_SM_LIST_POSTMATCH: sid %u DETECT_FLOWBITS set %u", s->id, fb->idx);
1336  } else {
1337  SCLogDebug("unsupported flowbits setting");
1338  continue;
1339  }
1340 
1341  if (fb_analysis.array[fb->idx].isnotset_sids_idx ||
1342  fb_analysis.array[fb->idx].unset_sids_idx) {
1343  SCLogDebug("flowbit %u not supported: unset in use", fb->idx);
1344  continue;
1345  }
1346 
1347  if (set_ctx == NULL) {
1348  set_ctx = SCCalloc(1, sizeof(*set_ctx));
1349  if (set_ctx == NULL)
1350  goto error;
1351  }
1352 
1353  SCLogDebug("setting up sets for sid %u", s->id);
1354  if (AddBitSet(de_ctx, &fb_analysis, set_ctx, fb, s) == 1) {
1355  // flag the set to trigger the post-rule match logic
1356  SCLogDebug("set up sets for sid %u", s->id);
1357  fb->post_rule_match_prefilter = true;
1358  }
1359 
1360  // TODO don't add for sigs that don't have isset in this sgh. Reasoning:
1361  // prefilter post match logic only makes sense in the same dir as otherwise
1362  // the regular 'isset' logic can simply run with the regular prefilters
1363  // before the rule loop
1364  }
1365 
1366  /* next, build the 'isset' state */
1367  if (s->init_data->prefilter_sm == NULL ||
1369  SCLogDebug("no prefilter or prefilter not flowbits");
1370  continue;
1371  }
1372 
1374  if (fb_analysis.array[fb->idx].isnotset_sids_idx ||
1375  fb_analysis.array[fb->idx].unset_sids_idx) {
1376  SCLogDebug("flowbit %u not supported: unset in use", fb->idx);
1377  s->init_data->prefilter_sm = NULL;
1378  s->flags &= ~SIG_FLAG_PREFILTER;
1379  continue;
1380  }
1381 
1382  SCLogDebug("isset: adding sid %u, flowbit %u", s->id, fb->idx);
1383 
1384  if (isset_ctx == NULL) {
1385  isset_ctx = SCCalloc(1, sizeof(*isset_ctx));
1386  if (isset_ctx == NULL)
1387  goto error;
1388  }
1389  if (AddBitsAndSid(de_ctx, isset_ctx, fb, s) < 0) {
1390  goto error;
1391  }
1392  }
1393 
1394  /* finally, register the states with their engines */
1395  static const char *g_prefilter_flowbits_isset = "flowbits:isset";
1396  if (isset_ctx != NULL) {
1397  enum SignatureHookPkt hook = SIGNATURE_HOOK_PKT_NOT_SET; // TODO review
1398  PrefilterAppendEngine(de_ctx, sgh, PrefilterFlowbitMatch, SIG_MASK_REQUIRE_FLOW, hook,
1399  isset_ctx, PrefilterFlowbitFree, g_prefilter_flowbits_isset);
1400  SCLogDebug("isset: added prefilter engine");
1401 
1402  if (set_ctx != NULL && !RB_EMPTY(&set_ctx->fb_tree)) {
1403  static const char *g_prefilter_flowbits_set = "flowbits:set";
1404  PrefilterAppendPostRuleEngine(de_ctx, sgh, PrefilterFlowbitPostRuleMatch, set_ctx,
1405  PrefilterFlowbitFree, g_prefilter_flowbits_set);
1406  SCLogDebug("set: added prefilter engine");
1407  } else {
1408  if (set_ctx) {
1409  PrefilterFlowbitFree(set_ctx);
1410  }
1411  SCLogDebug("set: NO prefilter engine added");
1412  }
1413  } else if (set_ctx != NULL) {
1414  PrefilterFlowbitFree(set_ctx);
1415  }
1416  FBAnalyzerFree(&fb_analysis);
1417  return 0;
1418 
1419 error:
1420  if (set_ctx) {
1421  PrefilterFlowbitFree(set_ctx);
1422  }
1423  if (isset_ctx) {
1424  PrefilterFlowbitFree(isset_ctx);
1425  }
1426  FBAnalyzerFree(&fb_analysis);
1427  return -1;
1428 }
1429 
1430 #ifdef UNITTESTS
1431 
1432 static int FlowBitsTestParse01(void)
1433 {
1434  char command[16] = "", name[16] = "";
1435 
1436  /* Single argument version. */
1437  FAIL_IF(!DetectFlowbitParse("noalert", command, sizeof(command), name,
1438  sizeof(name)));
1439  FAIL_IF(strcmp(command, "noalert") != 0);
1440 
1441  /* No leading or trailing spaces. */
1442  FAIL_IF(!DetectFlowbitParse("set,flowbit", command, sizeof(command), name,
1443  sizeof(name)));
1444  FAIL_IF(strcmp(command, "set") != 0);
1445  FAIL_IF(strcmp(name, "flowbit") != 0);
1446 
1447  /* Leading space. */
1448  FAIL_IF(!DetectFlowbitParse("set, flowbit", command, sizeof(command), name,
1449  sizeof(name)));
1450  FAIL_IF(strcmp(command, "set") != 0);
1451  FAIL_IF(strcmp(name, "flowbit") != 0);
1452 
1453  /* Trailing space. */
1454  FAIL_IF(!DetectFlowbitParse("set,flowbit ", command, sizeof(command), name,
1455  sizeof(name)));
1456  FAIL_IF(strcmp(command, "set") != 0);
1457  FAIL_IF(strcmp(name, "flowbit") != 0);
1458 
1459  /* Leading and trailing space. */
1460  FAIL_IF(!DetectFlowbitParse("set, flowbit ", command, sizeof(command), name,
1461  sizeof(name)));
1462  FAIL_IF(strcmp(command, "set") != 0);
1463  FAIL_IF(strcmp(name, "flowbit") != 0);
1464 
1465  /* Spaces are not allowed in the name. */
1466  FAIL_IF(DetectFlowbitParse("set,namewith space", command, sizeof(command),
1467  name, sizeof(name)));
1468 
1469  PASS;
1470 }
1471 
1472 /**
1473  * \test FlowBitsTestSig01 is a test for a valid noalert flowbits option
1474  *
1475  * \retval 1 on success
1476  * \retval 0 on failure
1477  */
1478 
1479 static int FlowBitsTestSig01(void)
1480 {
1481  Signature *s = NULL;
1482  DetectEngineCtx *de_ctx = NULL;
1483 
1486 
1487  de_ctx->flags |= DE_QUIET;
1488 
1489  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert,wrongusage; content:\"GET \"; sid:1;)");
1490  FAIL_IF_NOT_NULL(s);
1491 
1494  PASS;
1495 }
1496 
1497 /**
1498  * \test FlowBitsTestSig02 is a test for a valid isset,set,isnotset,unset flowbits options
1499  *
1500  * \retval 1 on success
1501  * \retval 0 on failure
1502  */
1503 
1504 static int FlowBitsTestSig02(void)
1505 {
1506  Signature *s = NULL;
1507  ThreadVars th_v;
1508  DetectEngineCtx *de_ctx = NULL;
1509 
1510  memset(&th_v, 0, sizeof(th_v));
1511 
1514 
1515  de_ctx->flags |= DE_QUIET;
1516 
1517  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset rule need an option\"; flowbits:isset; content:\"GET \"; sid:1;)");
1518  FAIL_IF_NOT_NULL(s);
1519 
1520  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isnotset rule need an option\"; flowbits:isnotset; content:\"GET \"; sid:2;)");
1521  FAIL_IF_NOT_NULL(s);
1522 
1523  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"set rule need an option\"; flowbits:set; content:\"GET \"; sid:3;)");
1524  FAIL_IF_NOT_NULL(s);
1525 
1526  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"unset rule need an option\"; flowbits:unset; content:\"GET \"; sid:4;)");
1527  FAIL_IF_NOT_NULL(s);
1528 
1529  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"!set is not an option\"; flowbits:!set,myerr; content:\"GET \"; sid:6;)");
1530  FAIL_IF_NOT_NULL(s);
1531 
1534 
1535  PASS;
1536 }
1537 
1538 /**
1539  * \test FlowBitsTestSig03 is a test for a invalid flowbits option
1540  *
1541  * \retval 1 on success
1542  * \retval 0 on failure
1543  */
1544 
1545 static int FlowBitsTestSig03(void)
1546 {
1547  Signature *s = NULL;
1548  DetectEngineCtx *de_ctx = NULL;
1549 
1552 
1553  de_ctx->flags |= DE_QUIET;
1554 
1555  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Unknown cmd\"; flowbits:wrongcmd; content:\"GET \"; sid:1;)");
1556  FAIL_IF_NOT_NULL(s);
1557 
1560  PASS;
1561 }
1562 
1563 /**
1564  * \test FlowBitsTestSig04 is a test check idx value
1565  *
1566  * \retval 1 on success
1567  * \retval 0 on failure
1568  */
1569 
1570 static int FlowBitsTestSig04(void)
1571 {
1572  Signature *s = NULL;
1573  DetectEngineCtx *de_ctx = NULL;
1574  int idx = 0;
1577 
1578  de_ctx->flags |= DE_QUIET;
1579 
1580  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset option\"; flowbits:isset,fbt; content:\"GET \"; sid:1;)");
1581  FAIL_IF_NULL(s);
1582 
1584  FAIL_IF(idx == 0);
1585 
1588  PASS;
1589 }
1590 
1591 /**
1592  * \test FlowBitsTestSig05 is a test check noalert flag
1593  *
1594  * \retval 1 on success
1595  * \retval 0 on failure
1596  */
1597 
1598 static int FlowBitsTestSig05(void)
1599 {
1600  Signature *s = NULL;
1601  DetectEngineCtx *de_ctx = NULL;
1602 
1605 
1606  de_ctx->flags |= DE_QUIET;
1607 
1608  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert; content:\"GET \"; sid:1;)");
1609  FAIL_IF_NULL(s);
1610  FAIL_IF((s->action & ACTION_ALERT) != 0);
1611 
1614  PASS;
1615 }
1616 
1617 /**
1618  * \test FlowBitsTestSig06 is a test set flowbits option
1619  *
1620  * \retval 1 on success
1621  * \retval 0 on failure
1622  */
1623 
1624 static int FlowBitsTestSig06(void)
1625 {
1626  uint8_t *buf = (uint8_t *)
1627  "GET /one/ HTTP/1.1\r\n"
1628  "Host: one.example.org\r\n"
1629  "\r\n";
1630  uint16_t buflen = strlen((char *)buf);
1632  FAIL_IF_NULL(p);
1633  Signature *s = NULL;
1634  ThreadVars th_v;
1635  DetectEngineThreadCtx *det_ctx = NULL;
1636  DetectEngineCtx *de_ctx = NULL;
1637  Flow f;
1638  GenericVar flowvar, *gv = NULL;
1639  int result = 0;
1640  uint32_t idx = 0;
1641 
1642  memset(&th_v, 0, sizeof(th_v));
1644  memset(&f, 0, sizeof(Flow));
1645  memset(&flowvar, 0, sizeof(GenericVar));
1646 
1647  FLOW_INITIALIZE(&f);
1648  p->flow = &f;
1649  p->flow->flowvar = &flowvar;
1650 
1651  p->src.family = AF_INET;
1652  p->dst.family = AF_INET;
1653  p->payload = buf;
1654  p->payload_len = buflen;
1655  p->proto = IPPROTO_TCP;
1656  p->flags |= PKT_HAS_FLOW;
1658 
1661 
1662  de_ctx->flags |= DE_QUIET;
1663 
1664  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow; sid:10;)");
1665  FAIL_IF_NULL(s);
1666 
1667  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1668  FAIL_IF_NOT(idx);
1670  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1671 
1672  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1673 
1674  gv = p->flow->flowvar;
1675  FAIL_IF_NULL(gv);
1676  for ( ; gv != NULL; gv = gv->next) {
1677  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1678  result = 1;
1679  }
1680  }
1681  FAIL_IF_NOT(result);
1682 
1683  PacketFree(p);
1684  FLOW_DESTROY(&f);
1685 
1686  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1689  PASS;
1690 }
1691 
1692 /**
1693  * \test FlowBitsTestSig07 is a test unset flowbits option
1694  *
1695  * \retval 1 on success
1696  * \retval 0 on failure
1697  */
1698 
1699 static int FlowBitsTestSig07(void)
1700 {
1701  uint8_t *buf = (uint8_t *)
1702  "GET /one/ HTTP/1.1\r\n"
1703  "Host: one.example.org\r\n"
1704  "\r\n";
1705  uint16_t buflen = strlen((char *)buf);
1707  FAIL_IF_NULL(p);
1708  Signature *s = NULL;
1709  ThreadVars th_v;
1710  DetectEngineThreadCtx *det_ctx = NULL;
1711  DetectEngineCtx *de_ctx = NULL;
1712  Flow f;
1713  GenericVar flowvar, *gv = NULL;
1714  int result = 0;
1715  uint32_t idx = 0;
1716 
1717  memset(&th_v, 0, sizeof(th_v));
1719  memset(&f, 0, sizeof(Flow));
1720  memset(&flowvar, 0, sizeof(GenericVar));
1721 
1722  FLOW_INITIALIZE(&f);
1723  p->flow = &f;
1724  p->flow->flowvar = &flowvar;
1725 
1726  p->src.family = AF_INET;
1727  p->dst.family = AF_INET;
1728  p->payload = buf;
1729  p->payload_len = buflen;
1730  p->proto = IPPROTO_TCP;
1731 
1734 
1735  de_ctx->flags |= DE_QUIET;
1736 
1737  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1738  FAIL_IF_NULL(s);
1739 
1740  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)");
1741  FAIL_IF_NULL(s);
1742 
1743  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1744  FAIL_IF_NOT(idx);
1746  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1747 
1748  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1749 
1750  gv = p->flow->flowvar;
1751  FAIL_IF_NULL(gv);
1752 
1753  for ( ; gv != NULL; gv = gv->next) {
1754  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1755  result = 1;
1756  }
1757  }
1758  FAIL_IF(result);
1759 
1760  PacketFree(p);
1761  FLOW_DESTROY(&f);
1762  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1765  PASS;
1766 }
1767 
1768 /**
1769  * \brief this function registers unit tests for FlowBits
1770  */
1772 {
1773  UtRegisterTest("FlowBitsTestParse01", FlowBitsTestParse01);
1774  UtRegisterTest("FlowBitsTestSig01", FlowBitsTestSig01);
1775  UtRegisterTest("FlowBitsTestSig02", FlowBitsTestSig02);
1776  UtRegisterTest("FlowBitsTestSig03", FlowBitsTestSig03);
1777  UtRegisterTest("FlowBitsTestSig04", FlowBitsTestSig04);
1778  UtRegisterTest("FlowBitsTestSig05", FlowBitsTestSig05);
1779  UtRegisterTest("FlowBitsTestSig06", FlowBitsTestSig06);
1780  UtRegisterTest("FlowBitsTestSig07", FlowBitsTestSig07);
1781 }
1782 #endif /* UNITTESTS */
FBAnalyze::cnts
uint16_t cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:499
SignatureInitData_::rule_state_dependant_sids_idx
uint32_t rule_state_dependant_sids_idx
Definition: detect.h:666
FBAnalyze::isset_sids
uint32_t * isset_sids
Definition: detect-flowbits.c:506
SigTableElmt_::url
const char * url
Definition: detect.h:1512
Packet_::proto
uint8_t proto
Definition: decode.h:537
FBAnalyze::isnotset_sids_idx
uint32_t isnotset_sids_idx
Definition: detect-flowbits.c:511
g_flowbits_dump_write_m
SCMutex g_flowbits_dump_write_m
Definition: detect-flowbits.c:886
DetectFlowbitsData_::or_list_size
uint8_t or_list_size
Definition: detect-flowbits.h:37
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:649
DETECT_FLOWBITS_CMD_MAX
#define DETECT_FLOWBITS_CMD_MAX
Definition: detect-flowbits.h:32
SigTableElmt_::desc
const char * desc
Definition: detect.h:1511
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1310
PostRuleMatchWorkQueueAppend
void PostRuleMatchWorkQueueAppend(DetectEngineThreadCtx *det_ctx, const Signature *s, const int type, const uint32_t value)
Definition: detect-engine-prefilter.c:1774
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1496
flow-util.h
DetectParseRegex
Definition: detect-parse.h:94
PostRuleMatchWorkQueue::len
uint32_t len
Definition: detect.h:1274
PostRuleMatchWorkQueueItem::sm_type
int sm_type
Definition: detect.h:1263
SigTableElmt_::name
const char * name
Definition: detect.h:1509
DETECT_TABLE_PACKET_FILTER_FLAG
#define DETECT_TABLE_PACKET_FILTER_FLAG
Definition: detect.h:567
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1679
FBAnalyze::isnotset_sids
uint32_t * isnotset_sids
Definition: detect-flowbits.c:510
FlowBitsRegisterTests
void FlowBitsRegisterTests(void)
this function registers unit tests for FlowBits
Definition: detect-flowbits.c:1771
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
PostRuleMatchWorkQueueItem
Definition: detect.h:1262
MAX_SIDS
#define MAX_SIDS
Definition: detect-flowbits.c:545
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
FBAnalyze::unset_sids_size
uint32_t unset_sids_size
Definition: detect-flowbits.c:516
MAX_TOKENS
#define MAX_TOKENS
Definition: detect-flowbits.c:59
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1500
SignatureInitData_::prefilter_sm
SigMatch * prefilter_sm
Definition: detect.h:629
PcapPacketCntGet
uint64_t PcapPacketCntGet(const Packet *p)
Definition: decode.c:1180
SignatureInitData_::is_rule_state_dependant
bool is_rule_state_dependant
Definition: detect.h:663
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PrefilterEngineFlowbits::fb_tree
struct PFB fb_tree
Definition: detect-flowbits.c:1015
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
name
const char * name
Definition: detect-engine-proto.c:48
DETECT_FLOWBITS_CMD_ISNOTSET
#define DETECT_FLOWBITS_CMD_ISNOTSET
Definition: detect-flowbits.h:30
Packet_::payload
uint8_t * payload
Definition: decode.h:619
action-globals.h
PrefilterAppendEngine
int PrefilterAppendEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterPktFn PrefilterFunc, SignatureMask mask, enum SignatureHookPkt hook, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:285
Packet_::flags
uint32_t flags
Definition: decode.h:561
flowbit_cmds
SCEnumCharMap flowbit_cmds[]
Definition: detect-flowbits.c:107
threads.h
Flow_
Flow data structure.
Definition: flow.h:354
DetectFlowbitsAnalyze
int DetectFlowbitsAnalyze(DetectEngineCtx *de_ctx)
Definition: detect-flowbits.c:639
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1399
ctx
struct Thresholds ctx
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
DetectFlowbitsData_::cmd
uint8_t cmd
Definition: detect-flowbits.h:36
SIGNATURE_HOOK_PKT_NOT_SET
@ SIGNATURE_HOOK_PKT_NOT_SET
Definition: detect.h:540
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2760
flow-bit.h
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:231
util-var-name.h
rust.h
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
VarNameStoreSetupLookup
const char * VarNameStoreSetupLookup(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:192
DETECT_TABLE_APP_TD_FLAG
#define DETECT_TABLE_APP_TD_FLAG
Definition: detect.h:570
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2971
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:612
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:3649
m
SCMutex m
Definition: flow-hash.h:6
DETECT_FLOWBITS_CMD_ISSET
#define DETECT_FLOWBITS_CMD_ISSET
Definition: detect-flowbits.h:31
p
Packet * p
Definition: fuzz_iprep.c:21
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:156
__attribute__
struct PrefilterEngineFlowbits __attribute__
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:546
MAX
#define MAX(x, y)
Definition: suricata-common.h:420
DETECT_TABLE_APP_FILTER_FLAG
#define DETECT_TABLE_APP_FILTER_FLAG
Definition: detect.h:569
RB_HEAD
RB_HEAD(PFB, PrefilterFlowbit)
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:620
detect-engine-prefilter.h
util-unittest.h
FlowBitSet
int FlowBitSet(Flow *f, uint32_t idx)
add a flowbit to the flow
Definition: flow-bit.c:94
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DETECT_TABLE_PACKET_TD_FLAG
#define DETECT_TABLE_PACKET_TD_FLAG
Definition: detect.h:568
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1494
Signature_::next
struct Signature_ * next
Definition: detect.h:757
FlowBitUnset
void FlowBitUnset(Flow *f, uint32_t idx)
Definition: flow-bit.c:99
DetectFlowbitsData_
Definition: detect-flowbits.h:34
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:127
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:38
decode.h
RB_EMPTY
#define RB_EMPTY(head)
Definition: tree.h:327
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
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:57
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1672
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
PARSE_REGEX
#define PARSE_REGEX
Definition: detect-flowbits.c:56
DetectEngineThreadCtx_
Definition: detect.h:1291
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
SIG_MASK_REQUIRE_FLOW
#define SIG_MASK_REQUIRE_FLOW
Definition: detect.h:312
FBAnalyzer::array
struct FBAnalyze * array
Definition: detect-flowbits.c:494
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:3775
RB_FOREACH_SAFE
#define RB_FOREACH_SAFE(x, name, head, y)
Definition: tree.h:791
detect-engine-mpm.h
SigTableElmt_::tables
uint8_t tables
Definition: detect.h:1504
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:387
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SignatureInitData_::rule_state_flowbits_ids_size
uint32_t rule_state_flowbits_ids_size
Definition: detect.h:668
SignatureHookPkt
SignatureHookPkt
Definition: detect.h:539
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3506
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:205
PrefilterFlowbit::rule_id
uint32_t * rule_id
Definition: detect-flowbits.c:992
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
PacketFree
void PacketFree(Packet *p)
Return a malloced packet.
Definition: decode.c:222
FBAnalyzer
Definition: detect-flowbits.c:493
FBAnalyze::set_sids_size
uint32_t set_sids_size
Definition: detect-flowbits.c:504
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:3248
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1696
app-layer-parser.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
GenericVar_::idx
uint32_t idx
Definition: util-var.h:56
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
FBAnalyze::unset_sids
uint32_t * unset_sids
Definition: detect-flowbits.c:514
DetectFlowbitFree
void DetectFlowbitFree(DetectEngineCtx *, void *)
Definition: detect-flowbits.c:478
FBAnalyzer::array_size
uint32_t array_size
Definition: detect-flowbits.c:495
Signature_::action
uint8_t action
Definition: detect.h:690
DetectFlowbitsRegister
void DetectFlowbitsRegister(void)
Definition: detect-flowbits.c:72
SCReturn
#define SCReturn
Definition: util-debug.h:286
Signature_::flags
uint32_t flags
Definition: detect.h:676
FBAnalyze::set_sids
uint32_t * set_sids
Definition: detect-flowbits.c:502
DetectEngineCtx_::max_fb_id
uint32_t max_fb_id
Definition: detect.h:1038
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:515
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect-engine-register.h:313
detect-engine-build.h
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:38
PrefilterFlowbit
Definition: detect-flowbits.c:991
DetectFlowbitsData_::idx
uint32_t idx
Definition: detect-flowbits.h:35
RB_GENERATE
RB_GENERATE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare)
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:754
SignatureInitData_::rule_state_dependant_sids_array
uint32_t * rule_state_dependant_sids_array
Definition: detect.h:664
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1471
SignatureInitData_::rule_state_dependant_sids_size
uint32_t rule_state_dependant_sids_size
Definition: detect.h:665
FBAnalyze::isset_sids_idx
uint32_t isset_sids_idx
Definition: detect-flowbits.c:507
detect-flowbits.h
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:489
tree.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2295
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1333
FBAnalyze::unset_sids_idx
uint32_t unset_sids_idx
Definition: detect-flowbits.c:515
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:296
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition: suricata-common.h:569
util-conf.h
SCMapEnumValueToName
const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
Maps an enum value to a string name, from the supplied table.
Definition: util-enum.c:68
Packet_::flow
struct Flow_ * flow
Definition: decode.h:563
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
VAR_TYPE_FLOW_BIT
@ VAR_TYPE_FLOW_BIT
Definition: util-var.h:36
FBAnalyze::state_cnts
uint16_t state_cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:500
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
GenericVar_
Definition: util-var.h:53
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1675
SCEnumCharMap_
Definition: util-enum.h:27
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3751
util-spm.h
PostRuleMatchWorkQueue::q
PostRuleMatchWorkQueueItem * q
Definition: detect.h:1273
FBAnalyze::set_sids_idx
uint32_t set_sids_idx
Definition: detect-flowbits.c:503
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:982
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:66
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:261
PrefilterAppendPostRuleEngine
int PrefilterAppendPostRuleEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterPostRuleFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f), void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:428
FlowBitIsset
int FlowBitIsset(Flow *f, uint32_t idx)
Definition: flow-bit.c:104
str
#define str(s)
Definition: suricata-common.h:316
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
FBAnalyze::isset_sids_size
uint32_t isset_sids_size
Definition: detect-flowbits.c:508
Signature_::iid
SigIntId iid
Definition: detect.h:687
DETECT_TABLE_PACKET_PRE_STREAM_FLAG
#define DETECT_TABLE_PACKET_PRE_STREAM_FLAG
Definition: detect.h:566
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DetectFlowbitsData_::or_list
uint32_t * or_list
Definition: detect-flowbits.h:41
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1493
Signature_::id
uint32_t id
Definition: detect.h:720
DETECT_FLOWBITS_CMD_UNSET
#define DETECT_FLOWBITS_CMD_UNSET
Definition: detect-flowbits.h:29
detect-parse.h
Signature_
Signature container.
Definition: detect.h:675
SigMatch_
a single match condition for a signature
Definition: detect.h:356
FBAnalyze::isnotset_sids_size
uint32_t isnotset_sids_size
Definition: detect-flowbits.c:512
PostRuleMatchWorkQueueItem::value
uint32_t value
Definition: detect.h:1264
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2721
GenericVar_::type
uint16_t type
Definition: util-var.h:54
DetectFlowbitMatch
int DetectFlowbitMatch(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect-flowbits.c:290
PrefilterEngineFlowbits
Definition: detect-flowbits.c:1014
RB_ENTRY
#define RB_ENTRY(type)
Definition: tree.h:314
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:339
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1414
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:991
Address_::family
char family
Definition: decode.h:114
Packet_::dst
Address dst
Definition: decode.h:520
SignatureInitData_::rule_state_flowbits_ids_array
uint32_t * rule_state_flowbits_ids_array
Definition: detect.h:667
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:975
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:992
util-enum.h
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
FBAnalyze
Definition: detect-flowbits.c:498
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:655
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1429
FLOW_PKT_TOSERVER_FIRST
#define FLOW_PKT_TOSERVER_FIRST
Definition: flow.h:234
SCMutex
#define SCMutex
Definition: threads-debug.h:114
rule_engine_analysis_set
bool rule_engine_analysis_set
Definition: detect-engine-loader.c:58
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
SIG_FLAG_PREFILTER
#define SIG_FLAG_PREFILTER
Definition: detect.h:277
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:119
Packet_::src
Address src
Definition: decode.h:519
DetectFlowbitsData_::post_rule_match_prefilter
bool post_rule_match_prefilter
Definition: detect-flowbits.h:39
BLOCK_SIZE
#define BLOCK_SIZE
Definition: detect-flowbits.c:1104
RB_PROTOTYPE
RB_PROTOTYPE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare)
DetectEngineThreadCtx_::post_rule_work_queue
PostRuleMatchWorkQueue post_rule_work_queue
Definition: detect.h:1397
FlowBitIsnotset
int FlowBitIsnotset(Flow *f, uint32_t idx)
Definition: flow-bit.c:116
DETECT_FLOWBITS_CMD_SET
#define DETECT_FLOWBITS_CMD_SET
Definition: detect-flowbits.h:28
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1498