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  }
454  break;
455 
458  /* modifiers, only run when entire sig has matched */
460  DETECT_SM_LIST_POSTMATCH) == NULL) {
461  goto error;
462  }
464  break;
465 
466  // suppress coverity warning as scan-build-7 warns w/o this.
467  // coverity[deadcode : FALSE]
468  default:
469  goto error;
470  }
471 
472  return 0;
473 
474 error:
475  if (cd != NULL)
477  return -1;
478 }
479 
481 {
483  if (fd == NULL)
484  return;
486  if (fd->or_list != NULL) {
487  for (uint8_t i = 0; i < fd->or_list_size; i++) {
489  }
490  SCFree(fd->or_list);
491  }
492  SCFree(fd);
493 }
494 
495 struct FBAnalyzer {
496  struct FBAnalyze *array;
497  uint32_t array_size;
498 };
499 
500 struct FBAnalyze {
503 
504  uint32_t *set_sids;
505  uint32_t set_sids_idx;
506  uint32_t set_sids_size;
507 
508  uint32_t *isset_sids;
509  uint32_t isset_sids_idx;
510  uint32_t isset_sids_size;
511 
512  uint32_t *isnotset_sids;
515 
516  uint32_t *unset_sids;
517  uint32_t unset_sids_idx;
518  uint32_t unset_sids_size;
519 };
520 
521 extern bool rule_engine_analysis_set;
522 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
523  struct FBAnalyze *array, uint32_t elements);
524 
525 static void FBAnalyzerArrayFree(struct FBAnalyze *array, const uint32_t array_size)
526 {
527  if (array) {
528  for (uint32_t i = 0; i < array_size; i++) {
529  SCFree(array[i].set_sids);
530  SCFree(array[i].unset_sids);
531  SCFree(array[i].isset_sids);
532  SCFree(array[i].isnotset_sids);
533  }
534  SCFree(array);
535  }
536 }
537 
538 static void FBAnalyzerFree(struct FBAnalyzer *fba)
539 {
540  if (fba && fba->array) {
541  FBAnalyzerArrayFree(fba->array, fba->array_size);
542  fba->array = NULL;
543  fba->array_size = 0;
544  }
545 }
546 
547 #define MAX_SIDS 8
548 static bool CheckExpand(const uint32_t sids_idx, uint32_t **sids, uint32_t *sids_size)
549 {
550  if (sids_idx >= *sids_size) {
551  const uint32_t old_size = *sids_size;
552  const uint32_t new_size = MAX(2 * old_size, MAX_SIDS);
553 
554  void *ptr = SCRealloc(*sids, new_size * sizeof(uint32_t));
555  if (ptr == NULL)
556  return false;
557  *sids_size = new_size;
558  *sids = ptr;
559  }
560  return true;
561 }
562 
563 static int DetectFlowbitsAnalyzeSignature(const Signature *s, struct FBAnalyzer *fba)
564 {
565  struct FBAnalyze *array = fba->array;
566  if (array == NULL)
567  return -1;
568 
569  /* see if the signature uses stateful matching TODO is there not a flag? */
570  bool has_state = (s->init_data->buffer_index != 0);
571 
572  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL;
573  sm = sm->next) {
574  if (sm->type != DETECT_FLOWBITS)
575  continue;
576  /* figure out the flowbit action */
577  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
578  // Handle flowbit array in case of ORed flowbits
579  for (uint8_t k = 0; k < fb->or_list_size; k++) {
580  struct FBAnalyze *fa = &array[fb->or_list[k]];
581  fa->cnts[fb->cmd]++;
582  fa->state_cnts[fb->cmd] += has_state;
583 
584  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
585  if (!CheckExpand(fa->isset_sids_idx, &fa->isset_sids, &fa->isset_sids_size))
586  return -1;
587  fa->isset_sids[fa->isset_sids_idx] = s->iid;
588  fa->isset_sids_idx++;
589  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
590  if (!CheckExpand(
592  return -1;
593  fa->isnotset_sids[fa->isnotset_sids_idx] = s->iid;
594  fa->isnotset_sids_idx++;
595  }
596  }
597  if (fb->or_list_size == 0) {
598  struct FBAnalyze *fa = &array[fb->idx];
599  fa->cnts[fb->cmd]++;
600  fa->state_cnts[fb->cmd] += has_state;
601 
602  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
603  if (!CheckExpand(fa->isset_sids_idx, &fa->isset_sids, &fa->isset_sids_size))
604  return -1;
605  fa->isset_sids[fa->isset_sids_idx] = s->iid;
606  fa->isset_sids_idx++;
607  } else if (fb->cmd == DETECT_FLOWBITS_CMD_ISNOTSET) {
608  if (!CheckExpand(
610  return -1;
611  fa->isnotset_sids[fa->isnotset_sids_idx] = s->iid;
612  fa->isnotset_sids_idx++;
613  }
614  }
615  }
616  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]; sm != NULL;
617  sm = sm->next) {
618  if (sm->type != DETECT_FLOWBITS)
619  continue;
620  /* figure out what flowbit action */
621  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
622  struct FBAnalyze *fa = &array[fb->idx];
623  fa->cnts[fb->cmd]++;
624  fa->state_cnts[fb->cmd] += has_state;
625 
626  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
627  if (!CheckExpand(fa->set_sids_idx, &fa->set_sids, &fa->set_sids_size))
628  return -1;
629  fa->set_sids[fa->set_sids_idx] = s->iid;
630  fa->set_sids_idx++;
631  } else if (fb->cmd == DETECT_FLOWBITS_CMD_UNSET) {
632  if (!CheckExpand(fa->unset_sids_idx, &fa->unset_sids, &fa->unset_sids_size))
633  return -1;
634  fa->unset_sids[fa->unset_sids_idx] = s->iid;
635  fa->unset_sids_idx++;
636  }
637  }
638  return 0;
639 }
640 
642 {
643  const uint32_t max_fb_id = de_ctx->max_fb_id;
644  if (max_fb_id == 0)
645  return 0;
646 
647  struct FBAnalyzer fba = { .array = NULL, .array_size = 0 };
648  const uint32_t array_size = max_fb_id + 1;
649  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
650  if (array == NULL) {
651  SCLogError("Unable to allocate flowbit analyze array");
652  return -1;
653  }
654  fba.array = array;
655  fba.array_size = array_size;
656 
657  SCLogDebug("fb analyzer array size: %"PRIu64,
658  (uint64_t)(array_size * sizeof(struct FBAnalyze)));
659 
660  /* fill flowbit array, updating counters per sig */
661  for (uint32_t i = 0; i < de_ctx->sig_array_len; i++) {
662  const Signature *s = de_ctx->sig_array[i];
663 
664  int r = DetectFlowbitsAnalyzeSignature(s, &fba);
665  if (r < 0) {
666  FBAnalyzerFree(&fba);
667  return -1;
668  }
669  }
670 
671  /* walk array to see if all bits make sense */
672  for (uint32_t i = 0; i < array_size; i++) {
673  const char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
674  if (varname == NULL)
675  continue;
676 
677  bool to_state = false;
678 
679  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] &&
680  array[i].cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
681 
682  const Signature *s = de_ctx->sig_array[array[i].isset_sids[0]];
683  SCLogWarning("flowbit '%s' is checked but not "
684  "set. Checked in %u and %u other sigs",
685  varname, s->id, array[i].isset_sids_idx - 1);
686  }
687  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
688  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0)
689  {
690  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
691  }
692 
693  /* if signature depends on 'stateful' flowbits, then turn the
694  * sig into a stateful sig itself */
695  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
696  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
698  {
699  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
700  to_state = true;
701  }
702 
703  SCLogDebug("ALL flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
706  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
707  SCLogDebug("STATE flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
712  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
713  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
714  de_ctx->sig_array[array[i].set_sids[x]]->id);
715  }
716  if (to_state) {
717  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
718  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
719  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
720 
723 
724  const uint32_t sids_array_size = array[i].set_sids_idx;
725  if (sids_array_size == 0)
726  continue;
727 
728  // save information about flowbits that affect this rule's state
729  if (s->init_data->rule_state_dependant_sids_array == NULL) {
731  SCCalloc(sids_array_size, sizeof(uint32_t));
732  if (s->init_data->rule_state_dependant_sids_array == NULL) {
733  SCLogError("Failed to allocate memory for rule_state_dependant_ids");
734  goto error;
735  }
738  SCCalloc(s->init_data->rule_state_flowbits_ids_size, sizeof(uint32_t));
739  if (s->init_data->rule_state_flowbits_ids_array == NULL) {
740  SCLogError("Failed to allocate memory for rule_state_variable_idx");
741  goto error;
742  }
743  s->init_data->rule_state_dependant_sids_size = sids_array_size;
744  SCLogDebug("alloc'ed array for rule dependency and fbs idx array, sid %u, "
745  "sizes are %u and %u",
748  } else {
749  uint32_t new_array_size =
750  s->init_data->rule_state_dependant_sids_size + sids_array_size;
752  new_array_size * sizeof(uint32_t));
753  if (tmp_ptr == NULL) {
754  SCLogError("Failed to allocate memory for rule_state_variable_idx");
755  goto error;
756  }
758  s->init_data->rule_state_dependant_sids_size = new_array_size;
759  SCLogDebug("realloc'ed array for rule dependency, sid %u, new size is %u",
761  uint32_t new_fb_array_size = s->init_data->rule_state_flowbits_ids_size + 1;
762  void *tmp_fb_ptr = SCRealloc(s->init_data->rule_state_flowbits_ids_array,
763  new_fb_array_size * sizeof(uint32_t));
764  if (tmp_fb_ptr == NULL) {
765  SCLogError("Failed to reallocate memory for rule_state_variable_idx");
766  goto error;
767  }
768  s->init_data->rule_state_flowbits_ids_array = tmp_fb_ptr;
769  SCLogDebug(
770  "realloc'ed array for flowbits ids, new size is %u", new_fb_array_size);
771  s->init_data->rule_state_dependant_sids_size = new_array_size;
772  s->init_data->rule_state_flowbits_ids_size = new_fb_array_size;
773  }
774  for (uint32_t idx = 0; idx < s->init_data->rule_state_dependant_sids_size; idx++) {
775  if (idx < array[i].set_sids_idx) {
778  de_ctx->sig_array[array[i].set_sids[idx]]->id;
780  }
781  }
782  s->init_data
784  1] = i;
786  // flowbit info saving for rule made stateful rule work finished
787 
788  SCLogDebug("made SID %u stateful because it depends on "
789  "stateful rules that set flowbit %s", s->id, varname);
790  }
791  }
792  }
793 
795  DetectFlowbitsAnalyzeDump(de_ctx, array, array_size);
796  }
797 
798  FBAnalyzerFree(&fba);
799  return 0;
800 error:
801  FBAnalyzerFree(&fba);
802  return -1;
803 }
804 
805 // TODO misses IPOnly rules. IPOnly flowbit rules are set only though.
806 static struct FBAnalyzer DetectFlowbitsAnalyzeForGroup(
807  const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
808 {
809  struct FBAnalyzer fba = { .array = NULL, .array_size = 0 };
810 
811  const uint32_t max_fb_id = de_ctx->max_fb_id;
812  if (max_fb_id == 0)
813  return fba;
814 
815  uint32_t array_size = max_fb_id + 1;
816  struct FBAnalyze *array = SCCalloc(array_size, sizeof(struct FBAnalyze));
817  if (array == NULL) {
818  SCLogError("Unable to allocate flowbit analyze array");
819  return fba;
820  }
821  SCLogDebug(
822  "fb analyzer array size: %" PRIu64, (uint64_t)(array_size * sizeof(struct FBAnalyze)));
823  fba.array = array;
824  fba.array_size = array_size;
825 
826  /* fill flowbit array, updating counters per sig */
827  for (uint32_t i = 0; i < sgh->init->sig_cnt; i++) {
828  const Signature *s = sgh->init->match_array[i];
829  SCLogDebug("sgh %p: s->id %u", sgh, s->id);
830 
831  int r = DetectFlowbitsAnalyzeSignature(s, &fba);
832  if (r < 0) {
833  FBAnalyzerFree(&fba);
834  return fba;
835  }
836  }
837 
838  /* walk array to see if all bits make sense */
839  for (uint32_t i = 0; i < array_size; i++) {
840  const char *varname = VarNameStoreSetupLookup(i, VAR_TYPE_FLOW_BIT);
841  if (varname == NULL)
842  continue;
843 
844  bool to_state = false;
845  if (array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] &&
846  array[i].state_cnts[DETECT_FLOWBITS_CMD_SET] == 0) {
847  SCLogDebug("flowbit %s/%u: isset in state, set not in state", varname, i);
848  }
849 
850  /* if signature depends on 'stateful' flowbits, then turn the
851  * sig into a stateful sig itself */
852  if (array[i].cnts[DETECT_FLOWBITS_CMD_ISSET] > 0 &&
853  array[i].state_cnts[DETECT_FLOWBITS_CMD_ISSET] == 0 &&
855  SCLogDebug("flowbit %s/%u: isset not in state, set in state", varname, i);
856  to_state = true;
857  }
858 
859  SCLogDebug("ALL flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
862  array[i].cnts[DETECT_FLOWBITS_CMD_ISSET]);
863  SCLogDebug("STATE flowbit %s/%u: sets %u unsets %u isnotsets %u issets %u", varname, i,
868  for (uint32_t x = 0; x < array[i].set_sids_idx; x++) {
869  SCLogDebug("SET flowbit %s/%u: SID %u", varname, i,
870  de_ctx->sig_array[array[i].set_sids[x]]->id);
871  }
872  for (uint32_t x = 0; x < array[i].isset_sids_idx; x++) {
873  Signature *s = de_ctx->sig_array[array[i].isset_sids[x]];
874  SCLogDebug("GET flowbit %s/%u: SID %u", varname, i, s->id);
875 
876  if (to_state) {
878  SCLogDebug("made SID %u stateful because it depends on "
879  "stateful rules that set flowbit %s",
880  s->id, varname);
881  }
882  }
883  }
884 
885  return fba;
886 }
887 
889 static void DetectFlowbitsAnalyzeDump(const DetectEngineCtx *de_ctx,
890  struct FBAnalyze *array, uint32_t elements)
891 {
892  SCJsonBuilder *js = SCJbNewObject();
893  if (js == NULL)
894  return;
895 
896  SCJbOpenArray(js, "flowbits");
897  for (uint32_t x = 0; x < elements; x++) {
898  const char *varname = VarNameStoreSetupLookup(x, VAR_TYPE_FLOW_BIT);
899  if (varname == NULL)
900  continue;
901 
902  const struct FBAnalyze *e = &array[x];
903 
904  SCJbStartObject(js);
905  SCJbSetString(js, "name", varname);
906  SCJbSetUint(js, "internal_id", x);
907  SCJbSetUint(js, "set_cnt", e->cnts[DETECT_FLOWBITS_CMD_SET]);
908  SCJbSetUint(js, "unset_cnt", e->cnts[DETECT_FLOWBITS_CMD_UNSET]);
909  SCJbSetUint(js, "isset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISSET]);
910  SCJbSetUint(js, "isnotset_cnt", e->cnts[DETECT_FLOWBITS_CMD_ISNOTSET]);
911 
912  // sets
913  if (e->cnts[DETECT_FLOWBITS_CMD_SET]) {
914  SCJbOpenArray(js, "sets");
915  for (uint32_t i = 0; i < e->set_sids_idx; i++) {
916  const Signature *s = de_ctx->sig_array[e->set_sids[i]];
917  SCJbAppendUint(js, s->id);
918  }
919  SCJbClose(js);
920  }
921  // gets
923  SCJbOpenArray(js, "isset");
924  for (uint32_t i = 0; i < e->isset_sids_idx; i++) {
925  const Signature *s = de_ctx->sig_array[e->isset_sids[i]];
926  SCJbAppendUint(js, s->id);
927  }
928  SCJbClose(js);
929  }
930  // isnotset
932  SCJbOpenArray(js, "isnotset");
933  for (uint32_t i = 0; i < e->isnotset_sids_idx; i++) {
934  const Signature *s = de_ctx->sig_array[e->isnotset_sids[i]];
935  SCJbAppendUint(js, s->id);
936  }
937  SCJbClose(js);
938  }
939  // unset
941  SCJbOpenArray(js, "unset");
942  for (uint32_t i = 0; i < e->unset_sids_idx; i++) {
943  const Signature *s = de_ctx->sig_array[e->unset_sids[i]];
944  SCJbAppendUint(js, s->id);
945  }
946  SCJbClose(js);
947  }
948  SCJbClose(js);
949  }
950  SCJbClose(js); // array
951  SCJbClose(js); // object
952 
953  const char *filename = "flowbits.json";
954  const char *log_dir = SCConfigGetLogDirectory();
955  char log_path[PATH_MAX] = "";
956  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
957 
959  FILE *fp = fopen(log_path, "w");
960  if (fp != NULL) {
961  fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
962  fprintf(fp, "\n");
963  fclose(fp);
964  }
966 
967  SCJbFree(js);
968 }
969 
970 static bool PrefilterFlowbitIsPrefilterable(const Signature *s)
971 {
972  SCLogDebug("sid:%u: checking", s->id);
973 
974  for (const SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL;
975  sm = sm->next) {
976  switch (sm->type) {
977  case DETECT_FLOWBITS: {
978  const DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
979  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
980  SCLogDebug("sid:%u: FLOWBITS ISSET can prefilter", s->id);
981  return true;
982  }
983  break;
984  }
985  }
986  }
987  SCLogDebug("sid:%u: no flowbit prefilter", s->id);
988  return false;
989 }
990 
991 /** core flowbit data structure: map a flowbit id to the signatures that need inspecting after it is
992  * found. Part of a rb-tree. */
993 typedef struct PrefilterFlowbit {
994  uint32_t *rule_id; /**< array of signature iid that are part of this prefilter */
995  RB_ENTRY(PrefilterFlowbit) __attribute__((__packed__)) rb;
996  uint32_t id; /**< flowbit id */
997  uint32_t rule_id_size; /**< size in elements of `rule_id` */
998  uint32_t rule_id_cnt; /**< usage in elements of `rule_id` */
999 } __attribute__((__packed__)) PrefilterFlowbit;
1001 static int PrefilterFlowbitCompare(const PrefilterFlowbit *a, const PrefilterFlowbit *b)
1002 {
1003  if (a->id > b->id)
1004  return 1;
1005  else if (a->id < b->id)
1006  return -1;
1007  else
1008  return 0;
1009 }
1010 
1011 /** red-black tree prototype for PFB (Prefilter Flow Bits) */
1013 RB_PROTOTYPE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare);
1014 RB_GENERATE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare);
1015 
1017  struct PFB fb_tree;
1018 };
1019 
1020 static void PrefilterFlowbitFree(void *vctx)
1021 {
1022  struct PrefilterEngineFlowbits *ctx = vctx;
1023  struct PrefilterFlowbit *rec, *safe = NULL;
1024  RB_FOREACH_SAFE (rec, PFB, &ctx->fb_tree, safe) {
1025  PFB_RB_REMOVE(&ctx->fb_tree, rec);
1026  SCFree(rec->rule_id);
1027  SCFree(rec);
1028  }
1029 
1030  SCFree(ctx);
1031 }
1032 
1033 static void PrefilterFlowbitMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
1034 {
1035  struct PrefilterEngineFlowbits *ctx = (struct PrefilterEngineFlowbits *)pectx;
1036  SCLogDebug("%" PRIu64 ": ctx %p", PcapPacketCntGet(p), ctx);
1037 
1038  if (p->flow == NULL) {
1039  SCReturn;
1040  }
1041 
1042  for (GenericVar *gv = p->flow->flowvar; gv != NULL; gv = gv->next) {
1043  if (gv->type != DETECT_FLOWBITS)
1044  continue;
1045 
1046  PrefilterFlowbit lookup;
1047  memset(&lookup, 0, sizeof(lookup));
1048  lookup.id = gv->idx;
1049  SCLogDebug("flowbit %u", gv->idx);
1050 
1051  PrefilterFlowbit *b = PFB_RB_FIND(&ctx->fb_tree, &lookup);
1052  if (b == NULL) {
1053  SCLogDebug("flowbit %u not in the tree", lookup.id);
1054  } else {
1055  SCLogDebug("flowbit %u found in the tree: %u", lookup.id, b->id);
1056 
1057  PrefilterAddSids(&det_ctx->pmq, b->rule_id, b->rule_id_cnt);
1058 #ifdef DEBUG
1059  for (uint32_t x = 0; x < b->rule_id_cnt; x++) {
1060  const Signature *s = det_ctx->de_ctx->sig_array[b->rule_id[x]];
1061  SCLogDebug("flowbit %u -> sig %u", gv->idx, s->id);
1062  }
1063 #endif
1064  }
1065  }
1066 }
1067 
1068 static void PrefilterFlowbitPostRuleMatch(
1069  DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f)
1070 {
1071  struct PrefilterEngineFlowbits *ctx = (struct PrefilterEngineFlowbits *)pectx;
1072  SCLogDebug("%" PRIu64 ": ctx %p", PcapPacketCntGet(p), ctx);
1073 
1074  if (p->flow == NULL) {
1075  SCReturn;
1076  }
1077 
1078  for (uint32_t i = 0; i < det_ctx->post_rule_work_queue.len; i++) {
1079  const PostRuleMatchWorkQueueItem *w = &det_ctx->post_rule_work_queue.q[i];
1080  if (w->sm_type != DETECT_FLOWBITS)
1081  continue;
1082 
1083  PrefilterFlowbit lookup;
1084  memset(&lookup, 0, sizeof(lookup));
1085  lookup.id = w->value;
1086 
1087  PrefilterFlowbit *b = PFB_RB_FIND(&ctx->fb_tree, &lookup);
1088  if (b == NULL) {
1089  SCLogDebug("flowbit %u not in the tree", lookup.id);
1090  } else {
1091  SCLogDebug("flowbit %u found in the tree: %u. Adding %u sids", lookup.id, b->id,
1092  b->rule_id_cnt);
1093  PrefilterAddSids(&det_ctx->pmq, b->rule_id, b->rule_id_cnt);
1094 #ifdef DEBUG
1095  // SCLogDebug("b %u", b->rule_id_cnt);
1096  for (uint32_t x = 0; x < b->rule_id_cnt; x++) {
1097  Signature *s = det_ctx->de_ctx->sig_array[b->rule_id[x]];
1098  SCLogDebug("flowbit %u -> sig %u (triggered by %u)", w->value, s->id,
1099  det_ctx->de_ctx->sig_array[w->id]->id);
1100  }
1101 #endif
1102  }
1103  }
1104 }
1105 
1106 #define BLOCK_SIZE 8
1108 static int AddBitAndSid(
1109  struct PrefilterEngineFlowbits *ctx, const Signature *s, const uint32_t flowbit_id)
1110 {
1111  PrefilterFlowbit x;
1112  memset(&x, 0, sizeof(x));
1113  x.id = flowbit_id;
1114 
1115  PrefilterFlowbit *pfb = PFB_RB_FIND(&ctx->fb_tree, &x);
1116  if (pfb == NULL) {
1117  PrefilterFlowbit *add = SCCalloc(1, sizeof(*add));
1118  if (add == NULL)
1119  return -1;
1120 
1121  add->id = flowbit_id;
1122  add->rule_id = SCCalloc(1, BLOCK_SIZE * sizeof(uint32_t));
1123  if (add->rule_id == NULL) {
1124  SCFree(add);
1125  return -1;
1126  }
1127  add->rule_id_size = BLOCK_SIZE;
1128  add->rule_id_cnt = 1;
1129  add->rule_id[0] = s->iid;
1130 
1131  PrefilterFlowbit *res = PFB_RB_INSERT(&ctx->fb_tree, add);
1132  SCLogDebug("not found, so added (res %p)", res);
1133  if (res != NULL) {
1134  // duplicate, shouldn't be possible after the FIND above
1135  BUG_ON(1);
1136  return -1;
1137  }
1138  } else {
1139  SCLogDebug("found! pfb %p id %u", pfb, pfb->id);
1140 
1141  if (pfb->rule_id_cnt < pfb->rule_id_size) {
1142  pfb->rule_id[pfb->rule_id_cnt++] = s->iid;
1143  } else {
1144  uint32_t *ptr =
1145  SCRealloc(pfb->rule_id, (pfb->rule_id_size + BLOCK_SIZE) * sizeof(uint32_t));
1146  if (ptr == NULL) {
1147  // memory stays in the tree
1148  return -1;
1149  }
1150  pfb->rule_id = ptr;
1151  pfb->rule_id_size += BLOCK_SIZE;
1152  pfb->rule_id[pfb->rule_id_cnt++] = s->iid;
1153  }
1154  }
1155  return 0;
1156 }
1157 
1158 static int AddBitsAndSid(const DetectEngineCtx *de_ctx, struct PrefilterEngineFlowbits *ctx,
1159  const DetectFlowbitsData *fb, const Signature *s)
1160 {
1161  if (fb->or_list_size == 0) {
1162  if (AddBitAndSid(ctx, s, fb->idx) < 0) {
1163  return -1;
1164  }
1165  } else {
1166  for (uint8_t i = 0; i < fb->or_list_size; i++) {
1167  SCLogDebug("flowbit OR: bit %u", fb->or_list[i]);
1168  if (AddBitAndSid(ctx, s, fb->or_list[i]) < 0) {
1169  return -1;
1170  }
1171  }
1172  }
1173  return 0;
1174 }
1175 
1176 static uint32_t NextMultiple(const uint32_t v, const uint32_t m)
1177 {
1178  return v + (m - v % m);
1179 }
1180 
1181 /** \internal
1182  * \brief adds sids for 'isset' prefilter flowbits
1183  * \retval int 1 if we added sid(s), 0 if we didn't, -1 on error */
1184 // TODO skip sids that aren't set by this sgh
1185 // TODO skip sids that doesn't have a isset in the same direction
1186 static int AddIssetSidsForBit(const DetectEngineCtx *de_ctx, const struct FBAnalyzer *fba,
1187  const DetectFlowbitsData *fb, PrefilterFlowbit *add)
1188 {
1189  int added = 0;
1190  for (uint32_t i = 0; i < fba->array[fb->idx].isset_sids_idx; i++) {
1191  const uint32_t sig_iid = fba->array[fb->idx].isset_sids[i];
1192  const Signature *s = de_ctx->sig_array[sig_iid];
1193  SCLogDebug("flowbit: %u => considering sid %u (iid:%u)", fb->idx, s->id, s->iid);
1194 
1195  /* Skip sids that aren't prefilter. These would just run all the time. */
1196  if (s->init_data->prefilter_sm == NULL ||
1198 #ifdef DEBUG
1199  const char *name = s->init_data->prefilter_sm
1201  : "none";
1202  SCLogDebug("flowbit: %u => rejected sid %u (iid:%u). No prefilter or prefilter not "
1203  "flowbits (%p, %s, %d)",
1204  fb->idx, s->id, sig_iid, s->init_data->prefilter_sm, name,
1206 #endif
1207  continue;
1208  }
1209 
1210  /* only add sids that match our bit */
1211  const DetectFlowbitsData *fs_fb =
1213  if (fs_fb->idx != fb->idx) {
1214  SCLogDebug(
1215  "flowbit: %u => rejected sid %u (iid:%u). Sig prefilters on different bit %u",
1216  fb->idx, s->id, sig_iid, fs_fb->idx);
1217  continue;
1218  }
1219 
1220  bool dup = false;
1221  for (uint32_t x = 0; x < add->rule_id_cnt; x++) {
1222  if (add->rule_id[x] == sig_iid) {
1223  dup = true;
1224  }
1225  }
1226 
1227  if (!dup) {
1228  if (add->rule_id_cnt < add->rule_id_size) {
1229  add->rule_id[add->rule_id_cnt++] = sig_iid;
1230  } else {
1231  uint32_t *ptr = SCRealloc(
1232  add->rule_id, (add->rule_id_size + BLOCK_SIZE) * sizeof(uint32_t));
1233  if (ptr == NULL) {
1234  return -1;
1235  }
1236  add->rule_id = ptr;
1237  add->rule_id_size += BLOCK_SIZE;
1238  add->rule_id[add->rule_id_cnt++] = sig_iid;
1239  }
1240  added = 1;
1241  SCLogDebug("flowbit: %u => accepted sid %u (iid:%u)", fb->idx, s->id, sig_iid);
1242  }
1243  }
1244  return added;
1245 }
1246 
1247 /* TODO shouldn't add sids for which Signature::num is < our num. Is this possible after sorting? */
1248 
1249 /** \brief For set flowbits, build "set" post-rule-match engine
1250  *
1251  * For set flowbits, a special post-rule-match engine is constructed
1252  * to update the running match array during rule matching.
1253  */
1254 static int AddBitSet(const DetectEngineCtx *de_ctx, struct FBAnalyzer *fba,
1255  struct PrefilterEngineFlowbits *ctx, const DetectFlowbitsData *fb, const Signature *s)
1256 {
1257  PrefilterFlowbit x;
1258  memset(&x, 0, sizeof(x));
1259  x.id = fb->idx;
1260  PrefilterFlowbit *pfb = PFB_RB_FIND(&ctx->fb_tree, &x);
1261  if (pfb == NULL) {
1262  PrefilterFlowbit *add = SCCalloc(1, sizeof(*add));
1263  if (add == NULL)
1264  return -1;
1265 
1266  add->id = fb->idx;
1267  add->rule_id_size = NextMultiple(fba->array[fb->idx].isset_sids_idx, BLOCK_SIZE);
1268  add->rule_id = SCCalloc(1, add->rule_id_size * sizeof(uint32_t));
1269  if (add->rule_id == NULL) {
1270  SCFree(add);
1271  return -1;
1272  }
1273 
1274  if (AddIssetSidsForBit(de_ctx, fba, fb, add) != 1) {
1275  SCLogDebug("no sids added");
1276  SCFree(add->rule_id);
1277  SCFree(add);
1278  return 0;
1279  }
1280  PrefilterFlowbit *res = PFB_RB_INSERT(&ctx->fb_tree, add);
1281  SCLogDebug("not found, so added (res %p)", res);
1282  BUG_ON(res != NULL); // TODO if res != NULL we have a duplicate which should be impossible
1283  } else {
1284  SCLogDebug("found! pfb %p id %u", pfb, pfb->id);
1285 
1286  int r = AddIssetSidsForBit(de_ctx, fba, fb, pfb);
1287  if (r < 0) {
1288  return -1;
1289  } else if (r == 0) {
1290  SCLogDebug("no sids added");
1291  return 0;
1292  }
1293  }
1294  return 1;
1295 }
1296 
1297 /** \brief build flowbit prefilter state(s)
1298  *
1299  * Build "set" and "isset" states.
1300  *
1301  * For each flowbit "isset" in the sgh, we need to check:
1302  * 1. is it supported
1303  * 2. is prefilter enabled
1304  * 3. does it match in the same dir or only opposing dir
1305  */
1306 static int PrefilterSetupFlowbits(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
1307 {
1308  if (sgh == NULL)
1309  return 0;
1310 
1311  SCLogDebug("sgh %p: setting up prefilter", sgh);
1312  struct PrefilterEngineFlowbits *isset_ctx = NULL;
1313  struct PrefilterEngineFlowbits *set_ctx = NULL;
1314 
1315  struct FBAnalyzer fb_analysis = DetectFlowbitsAnalyzeForGroup(de_ctx, sgh);
1316  if (fb_analysis.array == NULL)
1317  goto error;
1318 
1319  for (uint32_t i = 0; i < sgh->init->sig_cnt; i++) {
1320  Signature *s = sgh->init->match_array[i];
1321  if (s == NULL)
1322  continue;
1323 
1324  SCLogDebug("checking sid %u", s->id);
1325 
1326  /* first build the 'set' state */
1327  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]; sm != NULL;
1328  sm = sm->next) {
1329  if (sm->type != DETECT_FLOWBITS) {
1330  SCLogDebug("skip non flowbits sm");
1331  continue;
1332  }
1333 
1334  DetectFlowbitsData *fb = (DetectFlowbitsData *)sm->ctx;
1335  if (fb->cmd == DETECT_FLOWBITS_CMD_SET) {
1336  SCLogDebug(
1337  "DETECT_SM_LIST_POSTMATCH: sid %u DETECT_FLOWBITS set %u", s->id, fb->idx);
1338  } else {
1339  SCLogDebug("unsupported flowbits setting");
1340  continue;
1341  }
1342 
1343  if (fb_analysis.array[fb->idx].isnotset_sids_idx ||
1344  fb_analysis.array[fb->idx].unset_sids_idx) {
1345  SCLogDebug("flowbit %u not supported: unset in use", fb->idx);
1346  continue;
1347  }
1348 
1349  if (set_ctx == NULL) {
1350  set_ctx = SCCalloc(1, sizeof(*set_ctx));
1351  if (set_ctx == NULL)
1352  goto error;
1353  }
1354 
1355  SCLogDebug("setting up sets for sid %u", s->id);
1356  if (AddBitSet(de_ctx, &fb_analysis, set_ctx, fb, s) == 1) {
1357  // flag the set to trigger the post-rule match logic
1358  SCLogDebug("set up sets for sid %u", s->id);
1359  fb->post_rule_match_prefilter = true;
1360  }
1361 
1362  // TODO don't add for sigs that don't have isset in this sgh. Reasoning:
1363  // prefilter post match logic only makes sense in the same dir as otherwise
1364  // the regular 'isset' logic can simply run with the regular prefilters
1365  // before the rule loop
1366  }
1367 
1368  /* next, build the 'isset' state */
1369  if (s->init_data->prefilter_sm == NULL ||
1371  SCLogDebug("no prefilter or prefilter not flowbits");
1372  continue;
1373  }
1374 
1376  if (fb_analysis.array[fb->idx].isnotset_sids_idx ||
1377  fb_analysis.array[fb->idx].unset_sids_idx) {
1378  SCLogDebug("flowbit %u not supported: unset in use", fb->idx);
1379  s->init_data->prefilter_sm = NULL;
1380  s->flags &= ~SIG_FLAG_PREFILTER;
1381  continue;
1382  }
1383 
1384  SCLogDebug("isset: adding sid %u, flowbit %u", s->id, fb->idx);
1385 
1386  if (isset_ctx == NULL) {
1387  isset_ctx = SCCalloc(1, sizeof(*isset_ctx));
1388  if (isset_ctx == NULL)
1389  goto error;
1390  }
1391  if (AddBitsAndSid(de_ctx, isset_ctx, fb, s) < 0) {
1392  goto error;
1393  }
1394  }
1395 
1396  /* finally, register the states with their engines */
1397  static const char *g_prefilter_flowbits_isset = "flowbits:isset";
1398  if (isset_ctx != NULL) {
1399  enum SignatureHookPkt hook = SIGNATURE_HOOK_PKT_NOT_SET; // TODO review
1400  PrefilterAppendEngine(de_ctx, sgh, PrefilterFlowbitMatch, SIG_MASK_REQUIRE_FLOW, hook,
1401  isset_ctx, PrefilterFlowbitFree, g_prefilter_flowbits_isset);
1402  SCLogDebug("isset: added prefilter engine");
1403 
1404  if (set_ctx != NULL && !RB_EMPTY(&set_ctx->fb_tree)) {
1405  static const char *g_prefilter_flowbits_set = "flowbits:set";
1406  PrefilterAppendPostRuleEngine(de_ctx, sgh, PrefilterFlowbitPostRuleMatch, set_ctx,
1407  PrefilterFlowbitFree, g_prefilter_flowbits_set);
1408  SCLogDebug("set: added prefilter engine");
1409  } else {
1410  if (set_ctx) {
1411  PrefilterFlowbitFree(set_ctx);
1412  }
1413  SCLogDebug("set: NO prefilter engine added");
1414  }
1415  } else if (set_ctx != NULL) {
1416  PrefilterFlowbitFree(set_ctx);
1417  }
1418  FBAnalyzerFree(&fb_analysis);
1419  return 0;
1420 
1421 error:
1422  if (set_ctx) {
1423  PrefilterFlowbitFree(set_ctx);
1424  }
1425  if (isset_ctx) {
1426  PrefilterFlowbitFree(isset_ctx);
1427  }
1428  FBAnalyzerFree(&fb_analysis);
1429  return -1;
1430 }
1431 
1432 #ifdef UNITTESTS
1433 
1434 static int FlowBitsTestParse01(void)
1435 {
1436  char command[16] = "", name[16] = "";
1437 
1438  /* Single argument version. */
1439  FAIL_IF(!DetectFlowbitParse("noalert", command, sizeof(command), name,
1440  sizeof(name)));
1441  FAIL_IF(strcmp(command, "noalert") != 0);
1442 
1443  /* No leading or trailing spaces. */
1444  FAIL_IF(!DetectFlowbitParse("set,flowbit", command, sizeof(command), name,
1445  sizeof(name)));
1446  FAIL_IF(strcmp(command, "set") != 0);
1447  FAIL_IF(strcmp(name, "flowbit") != 0);
1448 
1449  /* Leading space. */
1450  FAIL_IF(!DetectFlowbitParse("set, flowbit", command, sizeof(command), name,
1451  sizeof(name)));
1452  FAIL_IF(strcmp(command, "set") != 0);
1453  FAIL_IF(strcmp(name, "flowbit") != 0);
1454 
1455  /* Trailing space. */
1456  FAIL_IF(!DetectFlowbitParse("set,flowbit ", command, sizeof(command), name,
1457  sizeof(name)));
1458  FAIL_IF(strcmp(command, "set") != 0);
1459  FAIL_IF(strcmp(name, "flowbit") != 0);
1460 
1461  /* Leading and trailing space. */
1462  FAIL_IF(!DetectFlowbitParse("set, flowbit ", command, sizeof(command), name,
1463  sizeof(name)));
1464  FAIL_IF(strcmp(command, "set") != 0);
1465  FAIL_IF(strcmp(name, "flowbit") != 0);
1466 
1467  /* Spaces are not allowed in the name. */
1468  FAIL_IF(DetectFlowbitParse("set,namewith space", command, sizeof(command),
1469  name, sizeof(name)));
1470 
1471  PASS;
1472 }
1473 
1474 /**
1475  * \test FlowBitsTestSig01 is a test for a valid noalert flowbits option
1476  *
1477  * \retval 1 on success
1478  * \retval 0 on failure
1479  */
1480 
1481 static int FlowBitsTestSig01(void)
1482 {
1483  Signature *s = NULL;
1484  DetectEngineCtx *de_ctx = NULL;
1485 
1488 
1489  de_ctx->flags |= DE_QUIET;
1490 
1491  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert,wrongusage; content:\"GET \"; sid:1;)");
1492  FAIL_IF_NOT_NULL(s);
1493 
1496  PASS;
1497 }
1498 
1499 /**
1500  * \test FlowBitsTestSig02 is a test for a valid isset,set,isnotset,unset flowbits options
1501  *
1502  * \retval 1 on success
1503  * \retval 0 on failure
1504  */
1505 
1506 static int FlowBitsTestSig02(void)
1507 {
1508  Signature *s = NULL;
1509  ThreadVars th_v;
1510  DetectEngineCtx *de_ctx = NULL;
1511 
1512  memset(&th_v, 0, sizeof(th_v));
1513 
1516 
1517  de_ctx->flags |= DE_QUIET;
1518 
1519  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;)");
1520  FAIL_IF_NOT_NULL(s);
1521 
1522  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;)");
1523  FAIL_IF_NOT_NULL(s);
1524 
1525  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;)");
1526  FAIL_IF_NOT_NULL(s);
1527 
1528  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;)");
1529  FAIL_IF_NOT_NULL(s);
1530 
1531  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;)");
1532  FAIL_IF_NOT_NULL(s);
1533 
1536 
1537  PASS;
1538 }
1539 
1540 /**
1541  * \test FlowBitsTestSig03 is a test for a invalid flowbits option
1542  *
1543  * \retval 1 on success
1544  * \retval 0 on failure
1545  */
1546 
1547 static int FlowBitsTestSig03(void)
1548 {
1549  Signature *s = NULL;
1550  DetectEngineCtx *de_ctx = NULL;
1551 
1554 
1555  de_ctx->flags |= DE_QUIET;
1556 
1557  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Unknown cmd\"; flowbits:wrongcmd; content:\"GET \"; sid:1;)");
1558  FAIL_IF_NOT_NULL(s);
1559 
1562  PASS;
1563 }
1564 
1565 /**
1566  * \test FlowBitsTestSig04 is a test check idx value
1567  *
1568  * \retval 1 on success
1569  * \retval 0 on failure
1570  */
1571 
1572 static int FlowBitsTestSig04(void)
1573 {
1574  Signature *s = NULL;
1575  DetectEngineCtx *de_ctx = NULL;
1576  int idx = 0;
1579 
1580  de_ctx->flags |= DE_QUIET;
1581 
1582  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"isset option\"; flowbits:isset,fbt; content:\"GET \"; sid:1;)");
1583  FAIL_IF_NULL(s);
1584 
1586  FAIL_IF(idx == 0);
1587 
1590  PASS;
1591 }
1592 
1593 /**
1594  * \test FlowBitsTestSig05 is a test check noalert flag
1595  *
1596  * \retval 1 on success
1597  * \retval 0 on failure
1598  */
1599 
1600 static int FlowBitsTestSig05(void)
1601 {
1602  Signature *s = NULL;
1603  DetectEngineCtx *de_ctx = NULL;
1604 
1607 
1608  de_ctx->flags |= DE_QUIET;
1609 
1610  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Noalert\"; flowbits:noalert; content:\"GET \"; sid:1;)");
1611  FAIL_IF_NULL(s);
1612  FAIL_IF((s->action & ACTION_ALERT) != 0);
1613 
1616  PASS;
1617 }
1618 
1619 /**
1620  * \test FlowBitsTestSig06 is a test set flowbits option
1621  *
1622  * \retval 1 on success
1623  * \retval 0 on failure
1624  */
1625 
1626 static int FlowBitsTestSig06(void)
1627 {
1628  uint8_t *buf = (uint8_t *)
1629  "GET /one/ HTTP/1.1\r\n"
1630  "Host: one.example.org\r\n"
1631  "\r\n";
1632  uint16_t buflen = strlen((char *)buf);
1634  FAIL_IF_NULL(p);
1635  Signature *s = NULL;
1636  ThreadVars th_v;
1637  DetectEngineThreadCtx *det_ctx = NULL;
1638  DetectEngineCtx *de_ctx = NULL;
1639  Flow f;
1640  GenericVar flowvar, *gv = NULL;
1641  int result = 0;
1642  uint32_t idx = 0;
1643 
1644  memset(&th_v, 0, sizeof(th_v));
1646  memset(&f, 0, sizeof(Flow));
1647  memset(&flowvar, 0, sizeof(GenericVar));
1648 
1649  FLOW_INITIALIZE(&f);
1650  p->flow = &f;
1651  p->flow->flowvar = &flowvar;
1652 
1653  p->src.family = AF_INET;
1654  p->dst.family = AF_INET;
1655  p->payload = buf;
1656  p->payload_len = buflen;
1657  p->proto = IPPROTO_TCP;
1658  p->flags |= PKT_HAS_FLOW;
1660 
1663 
1664  de_ctx->flags |= DE_QUIET;
1665 
1666  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow; sid:10;)");
1667  FAIL_IF_NULL(s);
1668 
1669  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1670  FAIL_IF_NOT(idx);
1672  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1673 
1674  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1675 
1676  gv = p->flow->flowvar;
1677  FAIL_IF_NULL(gv);
1678  for ( ; gv != NULL; gv = gv->next) {
1679  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1680  result = 1;
1681  }
1682  }
1683  FAIL_IF_NOT(result);
1684 
1685  PacketFree(p);
1686  FLOW_DESTROY(&f);
1687 
1688  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1691  PASS;
1692 }
1693 
1694 /**
1695  * \test FlowBitsTestSig07 is a test unset flowbits option
1696  *
1697  * \retval 1 on success
1698  * \retval 0 on failure
1699  */
1700 
1701 static int FlowBitsTestSig07(void)
1702 {
1703  uint8_t *buf = (uint8_t *)
1704  "GET /one/ HTTP/1.1\r\n"
1705  "Host: one.example.org\r\n"
1706  "\r\n";
1707  uint16_t buflen = strlen((char *)buf);
1709  FAIL_IF_NULL(p);
1710  Signature *s = NULL;
1711  ThreadVars th_v;
1712  DetectEngineThreadCtx *det_ctx = NULL;
1713  DetectEngineCtx *de_ctx = NULL;
1714  Flow f;
1715  GenericVar flowvar, *gv = NULL;
1716  int result = 0;
1717  uint32_t idx = 0;
1718 
1719  memset(&th_v, 0, sizeof(th_v));
1721  memset(&f, 0, sizeof(Flow));
1722  memset(&flowvar, 0, sizeof(GenericVar));
1723 
1724  FLOW_INITIALIZE(&f);
1725  p->flow = &f;
1726  p->flow->flowvar = &flowvar;
1727 
1728  p->src.family = AF_INET;
1729  p->dst.family = AF_INET;
1730  p->payload = buf;
1731  p->payload_len = buflen;
1732  p->proto = IPPROTO_TCP;
1733 
1736 
1737  de_ctx->flags |= DE_QUIET;
1738 
1739  s = de_ctx->sig_list = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit set\"; flowbits:set,myflow2; sid:10;)");
1740  FAIL_IF_NULL(s);
1741 
1742  s = s->next = SigInit(de_ctx,"alert ip any any -> any any (msg:\"Flowbit unset\"; flowbits:unset,myflow2; sid:11;)");
1743  FAIL_IF_NULL(s);
1744 
1745  idx = VarNameStoreRegister("myflow", VAR_TYPE_FLOW_BIT);
1746  FAIL_IF_NOT(idx);
1748  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
1749 
1750  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
1751 
1752  gv = p->flow->flowvar;
1753  FAIL_IF_NULL(gv);
1754 
1755  for ( ; gv != NULL; gv = gv->next) {
1756  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
1757  result = 1;
1758  }
1759  }
1760  FAIL_IF(result);
1761 
1762  PacketFree(p);
1763  FLOW_DESTROY(&f);
1764  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
1767  PASS;
1768 }
1769 
1770 /**
1771  * \brief this function registers unit tests for FlowBits
1772  */
1774 {
1775  UtRegisterTest("FlowBitsTestParse01", FlowBitsTestParse01);
1776  UtRegisterTest("FlowBitsTestSig01", FlowBitsTestSig01);
1777  UtRegisterTest("FlowBitsTestSig02", FlowBitsTestSig02);
1778  UtRegisterTest("FlowBitsTestSig03", FlowBitsTestSig03);
1779  UtRegisterTest("FlowBitsTestSig04", FlowBitsTestSig04);
1780  UtRegisterTest("FlowBitsTestSig05", FlowBitsTestSig05);
1781  UtRegisterTest("FlowBitsTestSig06", FlowBitsTestSig06);
1782  UtRegisterTest("FlowBitsTestSig07", FlowBitsTestSig07);
1783 }
1784 #endif /* UNITTESTS */
FBAnalyze::cnts
uint16_t cnts[DETECT_FLOWBITS_CMD_MAX]
Definition: detect-flowbits.c:501
SignatureInitData_::total_flowbits
uint16_t total_flowbits
Definition: detect.h:640
SignatureInitData_::rule_state_dependant_sids_idx
uint32_t rule_state_dependant_sids_idx
Definition: detect.h:673
FBAnalyze::isset_sids
uint32_t * isset_sids
Definition: detect-flowbits.c:508
SigTableElmt_::url
const char * url
Definition: detect.h:1521
Packet_::proto
uint8_t proto
Definition: decode.h:538
FBAnalyze::isnotset_sids_idx
uint32_t isnotset_sids_idx
Definition: detect-flowbits.c:513
g_flowbits_dump_write_m
SCMutex g_flowbits_dump_write_m
Definition: detect-flowbits.c:888
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:656
DETECT_FLOWBITS_CMD_MAX
#define DETECT_FLOWBITS_CMD_MAX
Definition: detect-flowbits.h:32
SigTableElmt_::desc
const char * desc
Definition: detect.h:1520
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1311
PostRuleMatchWorkQueueAppend
void PostRuleMatchWorkQueueAppend(DetectEngineThreadCtx *det_ctx, const Signature *s, const int type, const uint32_t value)
Definition: detect-engine-prefilter.c:1845
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1505
flow-util.h
DetectParseRegex
Definition: detect-parse.h:94
PostRuleMatchWorkQueue::len
uint32_t len
Definition: detect.h:1283
PostRuleMatchWorkQueueItem::sm_type
int sm_type
Definition: detect.h:1272
SigTableElmt_::name
const char * name
Definition: detect.h:1518
DETECT_TABLE_PACKET_FILTER_FLAG
#define DETECT_TABLE_PACKET_FILTER_FLAG
Definition: detect.h:568
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1693
FBAnalyze::isnotset_sids
uint32_t * isnotset_sids
Definition: detect-flowbits.c:512
FlowBitsRegisterTests
void FlowBitsRegisterTests(void)
this function registers unit tests for FlowBits
Definition: detect-flowbits.c:1773
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
PostRuleMatchWorkQueueItem
Definition: detect.h:1271
MAX_SIDS
#define MAX_SIDS
Definition: detect-flowbits.c:547
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:518
MAX_TOKENS
#define MAX_TOKENS
Definition: detect-flowbits.c:59
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1509
SignatureInitData_::prefilter_sm
SigMatch * prefilter_sm
Definition: detect.h:632
PcapPacketCntGet
uint64_t PcapPacketCntGet(const Packet *p)
Definition: decode.c:1180
SignatureInitData_::is_rule_state_dependant
bool is_rule_state_dependant
Definition: detect.h:670
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PrefilterEngineFlowbits::fb_tree
struct PFB fb_tree
Definition: detect-flowbits.c:1017
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:620
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:303
Packet_::flags
uint32_t flags
Definition: decode.h:562
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:641
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1408
ctx
struct Thresholds ctx
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:981
DetectFlowbitsData_::cmd
uint8_t cmd
Definition: detect-flowbits.h:36
SIGNATURE_HOOK_PKT_NOT_SET
@ SIGNATURE_HOOK_PKT_NOT_SET
Definition: detect.h:541
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2872
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:571
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3058
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:615
DetectParsePcreExec
int DetectParsePcreExec(DetectParseRegex *parse_regex, pcre2_match_data **match, const char *str, int start_offset, int options)
Definition: detect-parse.c:3856
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:547
MAX
#define MAX(x, y)
Definition: suricata-common.h:420
DETECT_TABLE_APP_FILTER_FLAG
#define DETECT_TABLE_APP_FILTER_FLAG
Definition: detect.h:570
RB_HEAD
RB_HEAD(PFB, PrefilterFlowbit)
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1500
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:621
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:569
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1503
Signature_::next
struct Signature_ * next
Definition: detect.h:764
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:1686
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:1300
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:496
DetectSetupParseRegexes
void DetectSetupParseRegexes(const char *parse_str, DetectParseRegex *detect_parse)
Definition: detect-parse.c:3982
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:1513
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:675
SignatureHookPkt
SignatureHookPkt
Definition: detect.h:540
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3620
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:994
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:495
FBAnalyze::set_sids_size
uint32_t set_sids_size
Definition: detect-flowbits.c:506
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:3455
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1710
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:516
DetectFlowbitFree
void DetectFlowbitFree(DetectEngineCtx *, void *)
Definition: detect-flowbits.c:480
FBAnalyzer::array_size
uint32_t array_size
Definition: detect-flowbits.c:497
Signature_::action
uint8_t action
Definition: detect.h:697
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:683
FBAnalyze::set_sids
uint32_t * set_sids
Definition: detect-flowbits.c:504
DetectEngineCtx_::max_fb_id
uint32_t max_fb_id
Definition: detect.h:1047
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:516
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect-engine-register.h:308
detect-engine-build.h
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:38
PrefilterFlowbit
Definition: detect-flowbits.c:993
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:761
SignatureInitData_::rule_state_dependant_sids_array
uint32_t * rule_state_dependant_sids_array
Definition: detect.h:671
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:1480
SignatureInitData_::rule_state_dependant_sids_size
uint32_t rule_state_dependant_sids_size
Definition: detect.h:672
FBAnalyze::isset_sids_idx
uint32_t isset_sids_idx
Definition: detect-flowbits.c:509
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:517
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:564
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:502
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:1689
SCEnumCharMap_
Definition: util-enum.h:27
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3865
util-spm.h
PostRuleMatchWorkQueue::q
PostRuleMatchWorkQueueItem * q
Definition: detect.h:1282
FBAnalyze::set_sids_idx
uint32_t set_sids_idx
Definition: detect-flowbits.c:505
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:991
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:455
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:510
Signature_::iid
SigIntId iid
Definition: detect.h:694
DETECT_TABLE_PACKET_PRE_STREAM_FLAG
#define DETECT_TABLE_PACKET_PRE_STREAM_FLAG
Definition: detect.h:567
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:1502
Signature_::id
uint32_t id
Definition: detect.h:727
DETECT_FLOWBITS_CMD_UNSET
#define DETECT_FLOWBITS_CMD_UNSET
Definition: detect-flowbits.h:29
detect-parse.h
Signature_
Signature container.
Definition: detect.h:682
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:514
PostRuleMatchWorkQueueItem::value
uint32_t value
Definition: detect.h:1273
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2833
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:1016
RB_ENTRY
#define RB_ENTRY(type)
Definition: tree.h:314
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:334
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1423
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:1000
Address_::family
char family
Definition: decode.h:114
Packet_::dst
Address dst
Definition: decode.h:521
SignatureInitData_::rule_state_flowbits_ids_array
uint32_t * rule_state_flowbits_ids_array
Definition: detect.h:674
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:983
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:1001
util-enum.h
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
FBAnalyze
Definition: detect-flowbits.c:500
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:662
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:520
DetectFlowbitsData_::post_rule_match_prefilter
bool post_rule_match_prefilter
Definition: detect-flowbits.h:39
BLOCK_SIZE
#define BLOCK_SIZE
Definition: detect-flowbits.c:1106
RB_PROTOTYPE
RB_PROTOTYPE(PFB, PrefilterFlowbit, rb, PrefilterFlowbitCompare)
DetectEngineThreadCtx_::post_rule_work_queue
PostRuleMatchWorkQueue post_rule_work_queue
Definition: detect.h:1406
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:1507