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