suricata
detect-engine-alert.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2026 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 #include "suricata-common.h"
19 #include "suricata.h"
20 
21 #include "detect.h"
22 #include "detect-parse.h"
23 #include "detect-engine-alert.h"
25 #include "detect-engine-tag.h"
26 
27 #include "decode.h"
28 #include "packet.h"
29 
30 #include "flow.h"
31 #include "flow-private.h"
32 
33 #ifdef QA_SIMULATION
34 #include "util-exception-policy.h"
35 #endif
36 
37 #include "util-profiling.h"
38 #include "util-validate.h"
39 
40 #include "action-globals.h"
41 #include "capture-hooks.h"
42 
43 /** tag signature we use for tag alerts */
44 static Signature g_tag_signature;
45 /** tag packet alert structure for tag alerts */
46 static PacketAlert g_tag_pa;
47 
49 {
50  memset(&g_tag_signature, 0x00, sizeof(g_tag_signature));
51 
52  g_tag_signature.id = TAG_SIG_ID;
53  g_tag_signature.gid = TAG_SIG_GEN;
54  g_tag_signature.iid = TAG_SIG_ID;
55  g_tag_signature.rev = 1;
56  g_tag_signature.prio = 2;
57 
58  memset(&g_tag_pa, 0x00, sizeof(g_tag_pa));
59 
60  g_tag_pa.action = ACTION_ALERT;
61  g_tag_pa.s = &g_tag_signature;
62 }
63 
64 /**
65  * \brief Handle a packet and check if needs a threshold logic
66  * Also apply rule action if necessary.
67  *
68  * \param de_ctx Detection Context
69  * \param sig Signature pointer
70  * \param p Packet structure
71  *
72  * \retval 1 alert is not suppressed
73  * \retval 0 alert is suppressed
74  */
75 static int PacketAlertHandle(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
76  const Signature *s, Packet *p, PacketAlert *pa)
77 {
78  SCEnter();
79  int ret = 1;
80  const DetectThresholdData *td = NULL;
81  const SigMatchData *smd;
82 
83  if (!(PacketIsIPv4(p) || PacketIsIPv6(p))) {
84  SCReturnInt(1);
85  }
86 
87  /* handle suppressions first */
88  if (s->sm_arrays[DETECT_SM_LIST_SUPPRESS] != NULL) {
90  smd = NULL;
91  do {
93  if (td != NULL) {
94  SCLogDebug("td %p", td);
95 
96  /* PacketAlertThreshold returns 2 if the alert is suppressed but
97  * we do need to apply rule actions to the packet. */
99  ret = PacketAlertThreshold(de_ctx, det_ctx, td, p, s, pa);
100  if (ret == 0 || ret == 2) {
102  /* It doesn't match threshold, remove it */
103  SCReturnInt(ret);
104  }
106  }
107  } while (smd != NULL);
108  }
109 
110  /* if we're still here, consider thresholding */
111  if (s->sm_arrays[DETECT_SM_LIST_THRESHOLD] != NULL) {
113  smd = NULL;
114  do {
116  if (td != NULL) {
117  SCLogDebug("td %p", td);
118 
119  /* PacketAlertThreshold returns 2 if the alert is suppressed but
120  * we do need to apply rule actions to the packet. */
122  ret = PacketAlertThreshold(de_ctx, det_ctx, td, p, s, pa);
123  if (ret == 0 || ret == 2) {
125  /* It doesn't match threshold, remove it */
126  SCReturnInt(ret);
127  }
129  }
130  } while (smd != NULL);
131  }
132  SCReturnInt(1);
133 }
134 
135 #ifdef UNITTESTS
136 /**
137  * \brief Check if a certain sid alerted, this is used in the test functions
138  *
139  * \param p Packet on which we want to check if the signature alerted or not
140  * \param sid Signature id of the signature that has to be checked for a match
141  *
142  * \retval match A value > 0 on a match; 0 on no match
143  */
144 int PacketAlertCheck(Packet *p, uint32_t sid)
145 {
146  int match = 0;
147 
148  for (uint16_t i = 0; i < p->alerts.cnt; i++) {
149  BUG_ON(p->alerts.alerts[i].s == NULL);
150  if (p->alerts.alerts[i].s->id == sid)
151  match++;
152  }
153 
154  return match;
155 }
156 #endif
157 
158 static inline void RuleActionToFlow(const uint8_t action, Flow *f, const bool fw_rule)
159 {
160  if (action & ACTION_ACCEPT) {
161  DEBUG_VALIDATE_BUG_ON(!fw_rule);
163  SCLogDebug("setting flow action accept");
164  }
165 
166  /* pass:flow can be set if accept:flow is present */
167  if (action & ACTION_PASS) {
168  if (f->flags & (FLOW_ACTION_DROP | FLOW_ACTION_PASS)) {
169  /* drop or pass already set. First to set wins. */
170  SCLogDebug("not setting %s flow already set to %s",
171  (action & ACTION_PASS) ? "pass" : "drop",
172  (f->flags & FLOW_ACTION_DROP) ? "drop" : "pass");
173  } else {
174  f->flags |= FLOW_ACTION_PASS;
175  SCLogDebug("setting flow action pass");
176  }
177 
178  // TODO firewall drop:flow should override FLOW_ACTION_PASS
179  } else if (action & (ACTION_DROP | ACTION_REJECT_ANY)) {
180  /* drop:flow from TD rules will override a accept:flow from
181  * firewall rules. */
182  if (f->flags & FLOW_ACTION_ACCEPT) {
184  f->flags |= FLOW_ACTION_DROP;
185  if (fw_rule)
187  SCLogDebug("replaced FLOW_ACTION_ACCEPT with FLOW_ACTION_DROP");
188  }
189  if (f->flags & (FLOW_ACTION_DROP | FLOW_ACTION_PASS)) {
190  /* drop or pass already set. First to set wins. */
191  SCLogDebug("not setting %s flow already set to %s",
192  (action & ACTION_PASS) ? "pass" : "drop",
193  (f->flags & FLOW_ACTION_DROP) ? "drop" : "pass");
194  } else {
195  f->flags |= FLOW_ACTION_DROP;
196  SCLogDebug("setting flow action drop");
197  }
198  }
199 
200  if (fw_rule) {
202  }
203 }
204 
205 /** \brief Apply action(s) and Set 'drop' sig info,
206  * if applicable
207  * \param p packet
208  * \param s signature -- for id, sig pointer, not actions
209  * \param pa packet alert struct -- match, including actions after thresholding (rate_filter) */
210 static void PacketApplySignatureActions(Packet *p, const Signature *s, const PacketAlert *pa)
211 {
212  SCLogDebug("packet %" PRIu64 ", sid %u: action %02x alert_flags %02x", PcapPacketCntGet(p),
213  s->id, pa->action, pa->flags);
214 
215  const bool is_fw_rule = s->flags & SIG_FLAG_FIREWALL ? true : false;
216  /* REJECT also sets ACTION_DROP, just make it more visible with this check */
217  if (pa->action & ACTION_DROP_REJECT) {
218  uint8_t drop_reason = is_fw_rule ? PKT_DROP_REASON_FW_RULES : PKT_DROP_REASON_RULES;
219  if (is_fw_rule) {
222  } else if (s->detect_table == DETECT_TABLE_PACKET_PRE_FLOW) {
223  drop_reason = PKT_DROP_REASON_FW_FLOW_PRE_HOOK;
224  }
225  }
226  /* PacketDrop will update the packet action, too */
227  PacketDrop(p, pa->action,
230  : drop_reason);
231  SCLogDebug("[packet %p][DROP sid %u]", p, s->id);
232 
233  if (p->alerts.drop.action == 0) {
234  p->alerts.drop.iid = s->iid;
235  p->alerts.drop.action = pa->action;
236  p->alerts.drop.s = (Signature *)s;
237  }
238  if ((p->flow != NULL) && (pa->flags & PACKET_ALERT_FLAG_APPLY_ACTION_TO_FLOW)) {
239  RuleActionToFlow(pa->action, p->flow, is_fw_rule);
240  }
241 
243  } else {
244  if (pa->action & ACTION_ACCEPT) {
245  const enum ActionScope as = pa->s->action_scope;
246  SCLogDebug("packet %" PRIu64 ": ACCEPT %u as:%u flags:%02x", PcapPacketCntGet(p), s->id,
247  as, pa->flags);
248  if (as == ACTION_SCOPE_PACKET || as == ACTION_SCOPE_FLOW ||
250  SCLogDebug("packet %" PRIu64 ": sid:%u ACCEPT", PcapPacketCntGet(p), s->id);
251  p->action |= ACTION_ACCEPT;
252  }
253  } else if (pa->action & (ACTION_ALERT | ACTION_CONFIG)) {
254  // nothing to set in the packet
255  } else if (pa->action & ACTION_PASS) {
256  SCLogDebug("[packet %p][PASS sid %u]", p, s->id);
257  // nothing to set in the packet
258  } else if (pa->action != 0) {
259  DEBUG_VALIDATE_BUG_ON(1); // should be unreachable
260  }
261 
262  if ((pa->action & (ACTION_PASS | ACTION_ACCEPT)) && (p->flow != NULL) &&
264  RuleActionToFlow(pa->action, p->flow, is_fw_rule);
265  }
266  }
267 }
268 
270 {
271  det_ctx->alert_queue_size = 0;
272  det_ctx->alert_queue = SCCalloc(packet_alert_max, sizeof(PacketAlert));
273  if (det_ctx->alert_queue == NULL) {
274  FatalError("failed to allocate %" PRIu64 " bytes for the alert queue",
275  (uint64_t)(packet_alert_max * sizeof(PacketAlert)));
276  }
278  SCLogDebug("alert queue initialized to %u elements (%" PRIu64 " bytes)", packet_alert_max,
279  (uint64_t)(packet_alert_max * sizeof(PacketAlert)));
280 }
281 
283 {
284  SCFree(det_ctx->alert_queue);
285  det_ctx->alert_queue_capacity = 0;
286 }
287 
288 static inline uint16_t AlertQueueExpandDo(DetectEngineThreadCtx *det_ctx, uint16_t new_cap)
289 {
290  DEBUG_VALIDATE_BUG_ON(det_ctx->alert_queue_capacity >= new_cap);
291 
292  void *tmp_queue = SCRealloc(det_ctx->alert_queue, new_cap * sizeof(PacketAlert));
293  if (unlikely(tmp_queue == NULL)) {
294  /* queue capacity didn't change */
295  return det_ctx->alert_queue_capacity;
296  }
297  det_ctx->alert_queue = tmp_queue;
298  det_ctx->alert_queue_capacity = new_cap;
299  SCLogDebug("Alert queue size expanded: %u elements, bytes: %" PRIuMAX "",
300  det_ctx->alert_queue_capacity, (uintmax_t)(new_cap * sizeof(PacketAlert)));
301  return new_cap;
302 }
303 
304 /** \internal
305  * \retval the new capacity
306  */
307 static uint16_t AlertQueueExpand(DetectEngineThreadCtx *det_ctx)
308 {
309 #ifdef QA_SIMULATION
310  if (unlikely(g_eps_is_alert_queue_fail_mode))
311  return det_ctx->alert_queue_capacity;
312 #endif
313  if (det_ctx->alert_queue_capacity == UINT16_MAX) {
314  return det_ctx->alert_queue_capacity;
315  }
316 
317  uint16_t new_cap;
318  if (det_ctx->alert_queue_capacity > (UINT16_MAX / 2)) {
319  new_cap = UINT16_MAX;
320  } else {
321  new_cap = det_ctx->alert_queue_capacity * 2;
322  }
323  return AlertQueueExpandDo(det_ctx, new_cap);
324 }
325 
326 static inline int PacketAlertSetContext(
327  DetectEngineThreadCtx *det_ctx, PacketAlert *pa, const Signature *s)
328 {
329  pa->json_info = NULL;
330  if (det_ctx->json_content_len) {
331  /* We have some JSON attached in the current detection so let's try
332  to see if some need to be used for current signature. */
333  struct PacketContextData *current_json = NULL;
334  for (uint8_t i = 0; i < det_ctx->json_content_len; i++) {
335  if (s == det_ctx->json_content[i].id) {
336  SCLogDebug("signature %p, content index %u", s, i);
337  if (current_json == NULL) {
338  /* Allocate the first one */
339  current_json = SCCalloc(1, sizeof(struct PacketContextData));
340  if (current_json == NULL) {
341  /* Allocation error, let's return now */
342  return -1;
343  }
344  if (pa->json_info == NULL) {
345  /* If this is the first one, set it */
346  pa->json_info = current_json;
347  }
348  current_json->next = NULL;
349  } else {
350  /* Allocate the next one */
351  struct PacketContextData *next_json =
352  SCCalloc(1, sizeof(struct PacketContextData));
353  if (next_json) {
354  current_json->next = next_json;
355  current_json = next_json;
356  current_json->next = NULL;
357  } else {
358  /* Allocation error, let's return now */
359  return -1;
360  }
361  }
362  current_json->json_string = SCStrdup(det_ctx->json_content[i].json_content);
363  if (current_json->json_string == NULL) {
364  return -1;
365  }
366  SCLogDebug("json content %u, value '%s' (%p)", (unsigned int)i,
367  current_json->json_string, s);
368  }
369  }
370  }
371 
372  return 0;
373 }
374 
375 /** \internal
376  */
377 static inline PacketAlert PacketAlertSet(DetectEngineThreadCtx *det_ctx, const Signature *s,
378  uint64_t tx_id, const uint8_t sub_state, uint8_t alert_flags)
379 {
380  PacketAlert pa;
381  pa.iid = s->iid;
382  pa.action = s->action;
383  pa.s = (Signature *)s;
384  pa.flags = alert_flags;
385  /* Set tx_id if the frame has it */
386  pa.tx_id = tx_id;
387  pa.sub_state = sub_state;
388  pa.frame_id = (alert_flags & PACKET_ALERT_FLAG_FRAME) ? det_ctx->frame_id : 0;
389  PacketAlertSetContext(det_ctx, &pa, s);
390  return pa;
391 }
392 
393 /**
394  * \brief Append signature to local packet alert queue for later preprocessing
395  */
396 static void AlertQueueAppend(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p,
397  const uint64_t tx_id, const uint8_t sub_state, uint8_t alert_flags)
398 {
399  /* first time we see a drop action signature, set that in the packet */
400  /* we do that even before inserting into the queue, so we save it even if appending fails */
401  if (p->alerts.drop.action == 0 && s->action & ACTION_DROP) {
402  p->alerts.drop = PacketAlertSet(det_ctx, s, tx_id, sub_state, alert_flags);
403  SCLogDebug("sid %u: set PacketAlert drop action. s->iid %" PRIu32 "", s->id, s->iid);
404  }
405 
406  uint16_t pos = det_ctx->alert_queue_size;
407  if (pos == det_ctx->alert_queue_capacity) {
408  /* we must grow the alert queue */
409  if (pos == AlertQueueExpand(det_ctx)) {
410  /* this means we failed to expand the queue */
411  p->alerts.discarded++;
412  return;
413  }
414  }
415  det_ctx->alert_queue[pos] = PacketAlertSet(det_ctx, s, tx_id, sub_state, alert_flags);
416 
417  SCLogDebug("packet %" PRIu64 ": appending sid %" PRIu32 ", s->iid %" PRIu32 " to alert queue",
418  PcapPacketCntGet(p), s->id, s->iid);
419  det_ctx->alert_queue_size++;
420 }
421 
422 /**
423  * \brief Append signature to local packet alert queue for later preprocessing
424  */
426  uint64_t tx_id, uint8_t sub_state, uint8_t alert_flags)
427 {
429  return AlertQueueAppend(det_ctx, s, p, tx_id, sub_state, alert_flags);
430 }
431 
432 /**
433  * \brief Append signature to local packet alert queue for later preprocessing
434  * This does not automatically set PACKET_ALERT_FLAG_STATE_MATCH as this
435  * comes from the packet alert path.
436  */
438  uint64_t tx_id, const uint8_t sub_state, uint8_t alert_flags)
439 {
440  alert_flags |= PACKET_ALERT_FLAG_TX;
441  return AlertQueueAppend(det_ctx, s, p, tx_id, sub_state, alert_flags);
442 }
443 
444 /**
445  * \brief Append signature to local packet alert queue for later preprocessing
446  */
448  DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint8_t alert_flags)
449 {
450  return AlertQueueAppend(det_ctx, s, p, PACKET_ALERT_NOTX, 0, alert_flags);
451 }
452 
453 /** \internal
454  * \brief sort helper for sorting alerts by priority
455  *
456  * Sorting is done first based on num and then using tx_id, if nums are equal.
457  * The Signature::num field is set based on internal priority. Higher priority
458  * rules have lower nums.
459  */
460 static int AlertQueueSortHelperFirewall(const void *a, const void *b)
461 {
462  const PacketAlert *pa0 = a;
463  const PacketAlert *pa1 = b;
464  if (pa0->s->detect_table == pa1->s->detect_table) {
465  if (pa1->iid == pa0->iid) {
466  if (pa1->tx_id == PACKET_ALERT_NOTX) {
467  return -1;
468  } else if (pa0->tx_id == PACKET_ALERT_NOTX) {
469  return 1;
470  }
471  return pa0->tx_id < pa1->tx_id ? 1 : -1;
472  } else {
473  return pa0->iid < pa1->iid ? -1 : 1;
474  }
475  }
476  return pa0->s->detect_table < pa1->s->detect_table ? -1 : 1;
477 }
478 
479 static int AlertQueueSortHelper(const void *a, const void *b)
480 {
481  const PacketAlert *pa0 = a;
482  const PacketAlert *pa1 = b;
483  if (pa1->iid == pa0->iid) {
484  if (pa1->tx_id == PACKET_ALERT_NOTX) {
485  return -1;
486  } else if (pa0->tx_id == PACKET_ALERT_NOTX) {
487  return 1;
488  }
489  return pa0->tx_id < pa1->tx_id ? 1 : -1;
490  } else {
491  return pa0->iid < pa1->iid ? -1 : 1;
492  }
493 }
494 
495 /** \internal
496  * \brief Check if Signature action should be applied to flow and apply
497  *
498  */
499 static inline void FlowApplySignatureActions(
500  Packet *p, PacketAlert *pa, const Signature *s, uint8_t alert_flags)
501 {
502  /* For DROP and PASS sigs we need to apply the action to the flow if
503  * - sig is IP or PD only
504  * - match is in applayer
505  * - match is in stream */
506  if (pa->action & (ACTION_DROP | ACTION_PASS | ACTION_ACCEPT)) {
509 
510  if (s->action_scope == ACTION_SCOPE_FLOW) {
512  } else if (s->action_scope == ACTION_SCOPE_AUTO) {
513  enum SignaturePropertyFlowAction flow_action =
515  if (flow_action == SIG_PROP_FLOW_ACTION_FLOW) {
517  } else if (flow_action == SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL) {
520  }
521  }
522  }
523 
525  SCLogDebug("packet %" PRIu64 " sid %u action %02x alert_flags %02x (set "
526  "PACKET_ALERT_FLAG_APPLY_ACTION_TO_FLOW)",
527  PcapPacketCntGet(p), s->id, s->action, pa->flags);
528  }
529  }
530 }
531 
532 /** \internal
533  * \brief handle immediate actions for a firewall rule match
534  *
535  * Delayed action (accept) is handled after TD has also run, as TD drop
536  * can overrule a FW accept. So returning `accept` here means "will accept if TD agrees".
537  *
538  * \retval pol firewall policy with action that is (drop) or should possibly be (accept)
539  * applied to the packet and flow.
540  */
541 static struct DetectFirewallPolicy HandleFirewallRule(
543 {
544  const Signature *s = pa->s;
545  struct DetectFirewallPolicy pol = { .action = 0, .action_scope = 0 };
546  int res = PacketAlertHandle(de_ctx, det_ctx, s, p, pa);
547  SCLogDebug("packet %" PRIu64 ": fw sid %u: res %d", PcapPacketCntGet(p), s->id, res);
548  if (res > 0) {
549  /* Now, if we have an alert, we have to check if we want
550  * to tag this session or src/dst host */
551  if (s->sm_arrays[DETECT_SM_LIST_TMATCH] != NULL) {
554  while (1) {
555  /* tags are set only for alerts */
557  sigmatch_table[smd->type].Match(det_ctx, p, (Signature *)s, smd->ctx);
558  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
559  if (smd->is_last)
560  break;
561  smd++;
562  }
563  }
564  /* if this is a terminating action, pass the action to the caller. */
565  if (s->action_scope != ACTION_SCOPE_HOOK ||
567  pol.action = s->action;
568  pol.action_scope = s->action_scope;
569  if (pol.action & ACTION_DROP) {
570  uint8_t drop_reason = PKT_DROP_REASON_FW_RULES;
573  } else if (s->detect_table == DETECT_TABLE_PACKET_PRE_FLOW) {
574  drop_reason = PKT_DROP_REASON_FW_FLOW_PRE_HOOK;
575  }
576  PacketDrop(p, pol.action, drop_reason);
577  if (p->flow && pol.action_scope == ACTION_SCOPE_FLOW) {
579  SCLogDebug("packet %" PRIu64 ": FLOW_ACTION_DROP set by firewall by sid %u",
580  PcapPacketCntGet(p), s->id);
581  }
582  }
583  if (pol.action & ACTION_PASS) {
584  if (p->flow && pol.action_scope == ACTION_SCOPE_FLOW) {
586  SCLogDebug("packet %" PRIu64 ": FLOW_ACTION_PASS set by firewall by sid %u",
587  PcapPacketCntGet(p), s->id);
588  }
589  }
590  }
591  /* add the alert for logging if required. */
592  if (s->action & ACTION_ALERT) {
593  if (p->alerts.cnt < packet_alert_max) {
594  p->alerts.alerts[p->alerts.cnt++] = *pa;
595  } else {
597  }
598  }
599  }
600  return pol;
601 }
602 
603 /*
604  * Queue order after sorting:
605  *
606  * Firewall use case: rules are sorted by Signature::detect_table, Signature::iid (which reflects
607  * order in the rule file(s)) This means:
608  * - pre_flow
609  * - pre_stream
610  * - packet:filter (firewall)
611  * - packet:td (IDS/IPS rules)
612  * - app:filter (firewall)
613  * - app:td (IDS/IPS rules)
614  *
615  * Firewall DROPs are immediate.
616  * Firewall ACCEPTs depend on TD not dropping.
617  * Firewall rules are not affected by PASS.
618  * TD rules are affected by PASS, both set by Firewall and TD rules.
619  *
620  * Non-Firewall use case: rules are sorted by Signature::iid (which reflect order based on flowbits,
621  * action-order, priority, etc)
622  */
623 static inline void PacketAlertFinalizeProcessQueue(
625 {
626  const bool have_fw_rules = EngineModeIsFirewall();
627 
628  if (det_ctx->alert_queue_size > 1) {
629  /* sort the alert queue before thresholding and appending to Packet */
630  qsort(det_ctx->alert_queue, det_ctx->alert_queue_size, sizeof(PacketAlert),
631  have_fw_rules ? AlertQueueSortHelperFirewall : AlertQueueSortHelper);
632  }
633 
634  bool alerted = false;
635  bool dropped = false;
636  bool skip_td = false;
637  bool skip_fw = false;
638  bool fw_accept_packet = false; // same as skip_fw?
639  bool fw_accept_flow = false;
640 
641 #ifdef DEBUG
642  SCLogDebug("packet %" PRIu64 ": starting alert event queue", PcapPacketCntGet(p));
643  for (uint16_t x = 0; x < det_ctx->alert_queue_size; x++) {
644  const PacketAlert *pa = &det_ctx->alert_queue[x];
645  const Signature *s = pa->s;
646  SCLogDebug("(list) %s sid %u: action %02x scope %u iid %u detect_table %u",
647  (s->flags & SIG_FLAG_FIREWALL) ? "fw" : "td", s->id, s->action, s->action_scope,
648  s->iid, s->detect_table);
649  }
650 #endif /* DEBUG */
651  uint8_t skip_table_id = 0;
652  bool skip_table = false;
653  for (uint16_t i = 0; i < det_ctx->alert_queue_size; i++) {
654  PacketAlert *pa = &det_ctx->alert_queue[i];
655  const Signature *s = pa->s;
656 
657  if (s->flags & SIG_FLAG_FIREWALL) {
658  if (skip_fw) {
659  continue;
660  }
661  /* skip for this table, but not for later. Mostly for showing
662  * alerts for both packet:filter and app:filter together. */
663  if (skip_table) {
664  if (s->detect_table <= skip_table_id)
665  continue;
666  skip_table = false;
667  }
668 
669  if (dropped) {
670  SCLogDebug("Skipping firewall signature after a drop.");
671  continue;
672  }
673 
674  const uint16_t pre_alert_cnt = p->alerts.cnt;
675  struct DetectFirewallPolicy pol = HandleFirewallRule(de_ctx, det_ctx, p, pa);
676  /* drop is immediate */
677  if (pol.action & ACTION_DROP) {
678  dropped = true;
679  } else if (pol.action & ACTION_ACCEPT) {
680  SCLogDebug("fw sid %u setting skip_fw for action %02x scope %s", s->id, pol.action,
682  /* see if we want to skip other alerts for this table */
683  if (pol.action_scope == ACTION_SCOPE_HOOK) {
684  skip_table_id = s->detect_table;
685  skip_table = true;
686  } else {
687  skip_fw = true;
688  }
689  fw_accept_packet = true;
690  if (pol.action_scope == ACTION_SCOPE_FLOW)
691  fw_accept_flow = true;
692  }
693  if (pol.action & ACTION_PASS) {
694  skip_td = true;
695  }
696  if (pre_alert_cnt < p->alerts.cnt)
697  alerted = true;
698  if (dropped)
699  goto fw_dropped;
700 
701  continue;
702  }
703 
704  SCLogDebug("sid: %u, action %02x, firewall? %s", s->id, pa->action,
706 
707  /* if a firewall rule told us to skip, we don't count the skipped
708  * alerts. */
709  if (have_fw_rules && skip_td) {
710  continue;
711  }
712 
713  int res = PacketAlertHandle(de_ctx, det_ctx, s, p, pa);
714  SCLogDebug("sid %u: res %d", pa->s->id, res);
715  if (res > 0) {
716  /* Now, if we have an alert, we have to check if we want
717  * to tag this session or src/dst host */
718  if (s->sm_arrays[DETECT_SM_LIST_TMATCH] != NULL) {
721  while (1) {
722  /* tags are set only for alerts */
724  sigmatch_table[smd->type].Match(det_ctx, p, (Signature *)s, smd->ctx);
725  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
726  if (smd->is_last)
727  break;
728  smd++;
729  }
730  }
731 
732  bool skip_action_set = false;
733  if ((p->action & (ACTION_DROP | ACTION_ACCEPT)) != 0) {
734  if (p->action & ACTION_DROP) {
735  if (pa->action & (ACTION_PASS | ACTION_ACCEPT)) {
736  skip_action_set = true;
737  }
738  } else {
739  if (pa->action & (ACTION_DROP)) {
740  skip_action_set = true;
741  }
742  }
743  }
744  SCLogDebug("packet %" PRIu64 ": i:%u sid:%u skip_action_set %s", PcapPacketCntGet(p), i,
745  s->id, BOOL2STR(skip_action_set));
746  if (!skip_action_set) {
747  /* set actions on the flow */
748  FlowApplySignatureActions(p, pa, s, pa->flags);
749 
750  SCLogDebug("sid %u: action %02x (DROP %s, PASS %s)", pa->s->id, pa->action,
752 
753  /* set actions on packet */
754  PacketApplySignatureActions(p, s, pa);
755  }
756  }
757 
758  /* Thresholding removes this alert */
759  if (res == 0 || res == 2 || (s->action & (ACTION_ALERT | ACTION_PASS)) == 0) {
760  SCLogDebug("sid:%u: skipping alert because of thresholding (res=%d) or NOALERT (%02x)",
761  s->id, res, s->action);
762  /* we will not copy this to the AlertQueue */
763  p->alerts.suppressed++;
764  } else if (p->alerts.cnt < packet_alert_max) {
765  p->alerts.alerts[p->alerts.cnt++] = *pa;
766  SCLogDebug("appending sid %" PRIu32 " alert to Packet::alerts at pos %u; action:%02x",
767  s->id, i, pa->action);
768 
769  if (pa->action & ACTION_ALERT) {
770  alerted = true;
771  }
772  /* pass with alert, we're done. Alert is logged. */
773  if (pa->action & ACTION_PASS) {
774  SCLogDebug("sid:%u: is a pass rule, so break out of loop", s->id);
775  if (!have_fw_rules)
776  break;
777  SCLogDebug("skipping td");
778  skip_td = true;
779  continue;
780  }
781  } else {
782  p->alerts.discarded++;
783  }
784 
785  if (s->action & ACTION_DROP) {
786  dropped = true;
787  }
788  }
789 
790 fw_dropped:
791  /* after threat detection has been handled, see if the fw intended to accept (drop is handled
792  * immediately by the fw), as fw accept can be overruled by td drop. */
793  if (have_fw_rules) {
794  if (p->action & ACTION_DROP) {
795  SCLogDebug("packet %" PRIu64 ": dropped by TD", PcapPacketCntGet(p));
796  } else if (fw_accept_packet) {
797  p->action |= ACTION_ACCEPT;
798  if (p->flow && fw_accept_flow) {
800  SCLogDebug("packet %" PRIu64 ": FLOW_ACTION_ACCEPT set from firewall",
802  }
803  }
804  }
805 
806  /* Set flag on flow to indicate that it has alerts. We use the bool to
807  * exclude pass-only entries in `Packet::alerts` */
808  if (alerted && p->flow != NULL) {
809  if (!FlowHasAlerts(p->flow)) {
812  }
813  }
814 
815  /* Notify capture layer about packets with real alerts (not pass-only),
816  * so capture impl can update per-capture context (e.g. pcap-file alert counts). */
817  if (alerted) {
819  }
820 }
821 
822 /**
823  * \brief Check the threshold of the sigs that match, set actions, break on pass action
824  * This function iterate the packet alerts array, removing those that didn't match
825  * the threshold, and those that match after a signature with the action "pass".
826  * The array is sorted by action priority/order
827  * \param de_ctx detection engine context
828  * \param det_ctx detection engine thread context
829  * \param p pointer to the packet
830  */
832 {
833  SCEnter();
834 
835  if (det_ctx->alert_queue_size > 0) {
836  PacketAlertFinalizeProcessQueue(de_ctx, det_ctx, p);
837  if (det_ctx->json_content_len)
839  }
840 
841  /* At this point, we should have all the new alerts. Now check the tag
842  * keyword context for sessions and hosts */
843  if (!(p->flags & PKT_PSEUDO_STREAM_END))
844  TagHandlePacket(de_ctx, det_ctx, p);
845 }
846 
847 #ifdef UNITTESTS
849 #endif
PacketCheckAction
bool PacketCheckAction(const Packet *p, const uint8_t a)
Definition: packet.c:50
PKT_DROP_REASON_RULES_THRESHOLD
@ PKT_DROP_REASON_RULES_THRESHOLD
Definition: decode.h:394
DetectEngineThreadCtx_::alert_queue_size
uint16_t alert_queue_size
Definition: detect.h:1385
FlowSetHasAlertsFlag
void FlowSetHasAlertsFlag(Flow *f)
Set flag to indicate that flow has alerts.
Definition: flow.c:154
PacketAlerts_::firewall_discarded
uint16_t firewall_discarded
Definition: decode.h:291
FLOW_ACTION_BY_FIREWALL
#define FLOW_ACTION_BY_FIREWALL
Definition: flow.h:125
PacketAlert_::s
const struct Signature_ * s
Definition: decode.h:254
AlertQueueFree
void AlertQueueFree(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-alert.c:282
Flow_::flags
uint64_t flags
Definition: flow.h:403
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PACKET_ALERT_FLAG_STATE_MATCH
#define PACKET_ALERT_FLAG_STATE_MATCH
Definition: decode.h:270
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
ACTION_PASS
#define ACTION_PASS
Definition: action-globals.h:34
PKT_DROP_REASON_FW_RULES
@ PKT_DROP_REASON_FW_RULES
Definition: decode.h:407
KEYWORD_PROFILING_SET_LIST
#define KEYWORD_PROFILING_SET_LIST(ctx, list)
Definition: util-profiling.h:46
PcapPacketCntGet
uint64_t PcapPacketCntGet(const Packet *p)
Definition: decode.c:1180
DETECT_TABLE_PACKET_PRE_STREAM
@ DETECT_TABLE_PACKET_PRE_STREAM
Definition: detect.h:560
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SigMatchData_::is_last
bool is_last
Definition: detect.h:367
ActionScopeToString
const char * ActionScopeToString(enum ActionScope s)
Definition: detect-parse.c:4022
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:289
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:144
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:368
action-globals.h
Packet_::flags
uint32_t flags
Definition: decode.h:562
Packet_::action
uint8_t action
Definition: decode.h:624
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:354
DETECT_SM_LIST_THRESHOLD
@ DETECT_SM_LIST_THRESHOLD
Definition: detect.h:133
TAG_SIG_ID
#define TAG_SIG_ID
Definition: detect-engine-tag.h:42
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:981
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:293
ACTION_SCOPE_FLOW
@ ACTION_SCOPE_FLOW
Definition: action-globals.h:45
ACTION_REJECT_ANY
#define ACTION_REJECT_ANY
Definition: action-globals.h:38
PacketAlerts_::drop
PacketAlert drop
Definition: decode.h:296
CaptureHooksOnPacketWithAlerts
void CaptureHooksOnPacketWithAlerts(const Packet *p)
Definition: capture-hooks.c:53
ACTION_DROP_REJECT
#define ACTION_DROP_REJECT
Definition: action-globals.h:40
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:745
p
Packet * p
Definition: fuzz_iprep.c:21
EngineModeIsFirewall
bool EngineModeIsFirewall(void)
Definition: suricata.c:239
SigMatchData_
Data needed for Match()
Definition: detect.h:365
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
SigMatchData_::type
uint16_t type
Definition: detect.h:366
DetectFirewallPolicy::action
uint8_t action
Definition: detect.h:933
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:637
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:64
PacketAlert_::tx_id
uint64_t tx_id
Definition: decode.h:255
PacketAlert_::action
uint8_t action
Definition: decode.h:251
SIG_FLAG_FIREWALL
#define SIG_FLAG_FIREWALL
Definition: detect.h:245
AlertQueueInit
void AlertQueueInit(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-alert.c:269
Signature_::gid
uint32_t gid
Definition: detect.h:728
FLOW_ACTION_PASS
#define FLOW_ACTION_PASS
Definition: flow.h:116
SignaturePropertyFlowAction
SignaturePropertyFlowAction
Definition: detect.h:83
capture-hooks.h
PacketAlert_::json_info
struct PacketContextData * json_info
Definition: decode.h:257
decode.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1300
DETECT_THRESHOLD
@ DETECT_THRESHOLD
Definition: detect-engine-register.h:65
AlertQueueAppendAppTxFromPacket
void AlertQueueAppendAppTxFromPacket(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint64_t tx_id, const uint8_t sub_state, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing This does not automatically set ...
Definition: detect-engine-alert.c:437
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1313
util-exception-policy.h
PKT_DROP_REASON_FW_FLOW_PRE_HOOK
@ PKT_DROP_REASON_FW_FLOW_PRE_HOOK
Definition: decode.h:411
BOOL2STR
#define BOOL2STR(b)
Definition: util-debug.h:542
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect.h
PacketContextData
Definition: decode.h:242
DETECT_TABLE_PACKET_PRE_FLOW
@ DETECT_TABLE_PACKET_PRE_FLOW
Definition: detect.h:559
PacketAlerts_::discarded
uint16_t discarded
Definition: decode.h:290
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
detect-engine-tag.h
PKT_DROP_REASON_RULES
@ PKT_DROP_REASON_RULES
Definition: decode.h:393
Signature_::action
uint8_t action
Definition: detect.h:697
util-profiling.h
PACKET_ALERT_FLAG_APPLY_ACTION_TO_FLOW
#define PACKET_ALERT_FLAG_APPLY_ACTION_TO_FLOW
Definition: decode.h:268
Signature_::flags
uint32_t flags
Definition: detect.h:683
PKT_ALERT_CTX_USED
#define PKT_ALERT_CTX_USED
Definition: decode.h:1300
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:516
DetectEngineThreadCtx_::frame_id
int64_t frame_id
Definition: detect.h:1377
ACTION_SCOPE_AUTO
@ ACTION_SCOPE_AUTO
Definition: action-globals.h:43
detect-engine-alert.h
DetectEngineThreadCtx_::alert_queue_capacity
uint16_t alert_queue_capacity
Definition: detect.h:1386
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1480
PacketAlert_::iid
SigIntId iid
Definition: decode.h:250
signature_properties
const struct SignatureProperties signature_properties[SIG_TYPE_MAX]
Definition: detect-engine.c:119
PACKET_ALERT_NOTX
#define PACKET_ALERT_NOTX
Definition: detect.h:55
SIG_TYPE_NOT_SET
@ SIG_TYPE_NOT_SET
Definition: detect.h:65
PACKET_ALERT_FLAG_APPLY_ACTION_TO_PACKET
#define PACKET_ALERT_FLAG_APPLY_ACTION_TO_PACKET
Definition: decode.h:282
PacketAlertFinalize
void PacketAlertFinalize(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
Check the threshold of the sigs that match, set actions, break on pass action This function iterate t...
Definition: detect-engine-alert.c:831
PACKET_ALERT_FLAG_STREAM_MATCH
#define PACKET_ALERT_FLAG_STREAM_MATCH
Definition: decode.h:272
TagHandlePacket
void TagHandlePacket(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
Search tags for src and dst. Update entries of the tag, remove if necessary.
Definition: detect-engine-tag.c:523
PACKET_ALERT_FLAG_FRAME
#define PACKET_ALERT_FLAG_FRAME
Definition: decode.h:278
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
PacketAlert_::frame_id
int64_t frame_id
Definition: decode.h:256
PacketAlert_::flags
uint8_t flags
Definition: decode.h:252
PacketAlert_::sub_state
uint8_t sub_state
Definition: decode.h:253
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
suricata-common.h
SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL
@ SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL
Definition: detect.h:86
ActionScope
ActionScope
Definition: action-globals.h:42
DetectThresholdData_
Definition: detect-threshold.h:62
ACTION_SCOPE_HOOK
@ ACTION_SCOPE_HOOK
Definition: action-globals.h:46
Signature_::action_scope
uint8_t action_scope
Definition: detect.h:704
packet.h
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
Signature_::rev
uint32_t rev
Definition: detect.h:729
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:517
ACTION_CONFIG
#define ACTION_CONFIG
Definition: action-globals.h:35
PacketContextData::next
struct PacketContextData * next
Definition: decode.h:244
DETECT_SM_LIST_TMATCH
@ DETECT_SM_LIST_TMATCH
Definition: detect.h:129
PKT_DROP_REASON_FW_STREAM_PRE_HOOK
@ PKT_DROP_REASON_FW_STREAM_PRE_HOOK
Definition: decode.h:410
SIG_TYPE_MAX
@ SIG_TYPE_MAX
Definition: detect.h:79
Signature_::prio
int prio
Definition: detect.h:730
AlertQueueAppendPacket
void AlertQueueAppendPacket(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing.
Definition: detect-engine-alert.c:447
SigGetThresholdTypeIter
const DetectThresholdData * SigGetThresholdTypeIter(const Signature *sig, const SigMatchData **psm, int list)
Return next DetectThresholdData for signature.
Definition: detect-engine-threshold.c:677
FLOW_ACTION_ACCEPT
#define FLOW_ACTION_ACCEPT
Definition: flow.h:63
util-validate.h
DetectEngineThreadCtx_::alert_queue
PacketAlert * alert_queue
Definition: detect.h:1387
PACKET_ALERT_FLAG_RATE_FILTER_MODIFIED
#define PACKET_ALERT_FLAG_RATE_FILTER_MODIFIED
Definition: decode.h:276
DetectEngineThreadCtx_::json_content
SigJsonContent * json_content
Definition: detect.h:1337
PacketAlerts_::suppressed
uint16_t suppressed
Definition: decode.h:292
DetectFirewallPolicy
Definition: detect.h:932
Signature_::iid
SigIntId iid
Definition: detect.h:694
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-engine-alert.c
Signature_::id
uint32_t id
Definition: detect.h:727
ACTION_SCOPE_PACKET
@ ACTION_SCOPE_PACKET
Definition: action-globals.h:44
detect-parse.h
Signature_
Signature container.
Definition: detect.h:682
PACKET_ALERT_FLAG_TX
#define PACKET_ALERT_FLAG_TX
Definition: decode.h:274
PacketAlertTagInit
void PacketAlertTagInit(void)
Definition: detect-engine-alert.c:48
PacketDrop
void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
issue drop action
Definition: packet.c:34
ACTION_ACCEPT
#define ACTION_ACCEPT
Definition: action-globals.h:36
suricata.h
PacketAlert_
Definition: decode.h:249
Signature_::detect_table
uint8_t detect_table
Definition: detect.h:716
FlowHasAlerts
int FlowHasAlerts(const Flow *f)
Check if flow has alerts.
Definition: flow.c:165
packet_alert_max
uint16_t packet_alert_max
Definition: decode.c:82
DetectEngineThreadCtx_::json_content_len
uint8_t json_content_len
Definition: detect.h:1339
TAG_SIG_GEN
#define TAG_SIG_GEN
Definition: detect-engine-tag.h:41
PacketContextData::json_string
char * json_string
Definition: decode.h:243
SIG_PROP_FLOW_ACTION_FLOW
@ SIG_PROP_FLOW_ACTION_FLOW
Definition: detect.h:85
PacketAlertThreshold
int PacketAlertThreshold(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectThresholdData *td, Packet *p, const Signature *s, PacketAlert *pa)
Make the threshold logic for signatures.
Definition: detect-engine-threshold.c:1197
flow.h
PKT_FIRST_ALERTS
#define PKT_FIRST_ALERTS
Definition: decode.h:1360
DetectFirewallPolicy::action_scope
uint8_t action_scope
Definition: detect.h:934
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
AlertQueueAppendAppTx
void AlertQueueAppendAppTx(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint64_t tx_id, uint8_t sub_state, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing.
Definition: detect-engine-alert.c:425
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
Signature_::type
enum SignatureType type
Definition: detect.h:685
DETECT_SM_LIST_SUPPRESS
@ DETECT_SM_LIST_SUPPRESS
Definition: detect.h:132
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
SignatureProperties::flow_action
enum SignaturePropertyFlowAction flow_action
Definition: detect.h:90
SigJsonContent::json_content
char json_content[SIG_JSON_CONTENT_ITEM_LEN]
Definition: detect.h:1294
SigJsonContent::id
void * id
Definition: detect.h:1293
detect-engine-threshold.h