suricata
app-layer-events.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2024 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 Anoop Saldanha <anoopsaldanha@gmail.com>
23  */
24 
25 #include "suricata-common.h"
26 #include "app-layer-events.h"
27 #include "util-enum.h"
28 #include "detect.h"
29 #include "util-debug.h"
30 
31 int SCAppLayerGetEventIdByName(const char *event_name, SCEnumCharMap *table, uint8_t *event_id)
32 {
33  int value = SCMapEnumNameToValue(event_name, table);
34  if (value == -1) {
35  SCLogError("event \"%s\" not present in enum table.", event_name);
36  /* this should be treated as fatal */
37  return -1;
38  } else if (value < -1 || value > UINT8_MAX) {
39  SCLogError("event \"%s\" has out of range value", event_name);
40  /* this should be treated as fatal */
41  return -1;
42  }
43  *event_id = (uint8_t)value;
44  return 0;
45 }
46 
47 /* events raised during protocol detection are stored in the
48  * packets storage, not in the flow. */
50  { "APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS",
52  { "APPLAYER_WRONG_DIRECTION_FIRST_DATA",
54  { "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION",
56  { "APPLAYER_PROTO_DETECTION_SKIPPED",
58  { "APPLAYER_NO_TLS_AFTER_STARTTLS",
60  { "APPLAYER_UNEXPECTED_PROTOCOL",
62  { NULL,
63  -1 },
64 };
65 
67  uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
68 {
69  *event_name = SCMapEnumValueToName(event_id, app_layer_event_pkt_table);
70  if (*event_name == NULL) {
71  SCLogError("event \"%d\" not present in "
72  "app-layer-event's enum map table.",
73  event_id);
74  /* yes this is fatal */
75  return -1;
76  }
77 
78  *event_type = APP_LAYER_EVENT_TYPE_PACKET;
79 
80  return 0;
81 }
82 
83 int AppLayerGetPktEventInfo(const char *event_name, uint8_t *event_id)
84 {
85  return SCAppLayerGetEventIdByName(event_name, app_layer_event_pkt_table, event_id);
86 }
87 
88 #define DECODER_EVENTS_BUFFER_STEPS 8
89 
90 /**
91  * \brief Set an app layer decoder event.
92  *
93  * \param sevents Pointer to a AppLayerDecoderEvents pointer. If *sevents is NULL
94  * memory will be allocated.
95  * \param event The event to be stored.
96  */
98 {
99  if (*sevents == NULL) {
100  AppLayerDecoderEvents *new_devents = SCCalloc(1, sizeof(AppLayerDecoderEvents));
101  if (new_devents == NULL)
102  return;
103 
104  *sevents = new_devents;
105 
106  }
107  if ((*sevents)->cnt == UCHAR_MAX) {
108  /* we're full */
109  return;
110  }
111  if ((*sevents)->cnt == (*sevents)->events_buffer_size) {
112  int steps = DECODER_EVENTS_BUFFER_STEPS;
113  if (UCHAR_MAX - (*sevents)->cnt < steps)
114  steps = UCHAR_MAX - (*sevents)->cnt < steps;
115 
116  void *ptr = SCRealloc((*sevents)->events,
117  ((*sevents)->cnt + steps) * sizeof(uint8_t));
118  if (ptr == NULL) {
119  /* couldn't grow buffer, but no reason to free old
120  * so we keep the events that may already be here */
121  return;
122  }
123  (*sevents)->events = ptr;
124  (*sevents)->events_buffer_size += steps;
125  }
126 
127  (*sevents)->events[(*sevents)->cnt++] = event;
128 }
129 
131 {
132  if (events != NULL) {
133  events->cnt = 0;
134  events->event_last_logged = 0;
135  }
136 }
137 
139 {
140  if (events && *events != NULL) {
141  if ((*events)->events != NULL)
142  SCFree((*events)->events);
143  SCFree(*events);
144  *events = NULL;
145  }
146 }
147 
149  { "NO_MEMORY", FILE_DECODER_EVENT_NO_MEM },
150  { "INVALID_SWF_LENGTH", FILE_DECODER_EVENT_INVALID_SWF_LENGTH },
151  { "INVALID_SWF_VERSION", FILE_DECODER_EVENT_INVALID_SWF_VERSION },
152  { "Z_DATA_ERROR", FILE_DECODER_EVENT_Z_DATA_ERROR },
153  { "Z_STREAM_ERROR", FILE_DECODER_EVENT_Z_STREAM_ERROR },
154  { "Z_BUF_ERROR", FILE_DECODER_EVENT_Z_BUF_ERROR },
155  { "Z_UNKNOWN_ERROR", FILE_DECODER_EVENT_Z_UNKNOWN_ERROR },
156  { "LZMA_IO_ERROR", FILE_DECODER_EVENT_LZMA_IO_ERROR },
157  { "LZMA_HEADER_TOO_SHORT_ERROR", FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR },
158  { "LZMA_DECODER_ERROR", FILE_DECODER_EVENT_LZMA_DECODER_ERROR },
159  { "LZMA_MEMLIMIT_ERROR", FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR },
160  { "LZMA_XZ_ERROR", FILE_DECODER_EVENT_LZMA_XZ_ERROR },
161  { "LZMA_UNKNOWN_ERROR", FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR },
162  {
163  "TOO_MANY_BUFFERS",
165  },
166  {
167  "POST_MATCH_QUEUE_FAILED",
169  },
170  { NULL, -1 },
171 };
172 
174  const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
175 {
176  if (SCAppLayerGetEventIdByName(event_name, det_ctx_event_table, event_id) == 0) {
177  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
178  return 0;
179  }
180  return -1;
181 }
APPLAYER_UNEXPECTED_PROTOCOL
@ APPLAYER_UNEXPECTED_PROTOCOL
Definition: app-layer-events.h:51
FILE_DECODER_EVENT_INVALID_SWF_VERSION
@ FILE_DECODER_EVENT_INVALID_SWF_VERSION
Definition: detect.h:1552
det_ctx_event_table
SCEnumCharMap det_ctx_event_table[]
Definition: app-layer-events.c:148
AppLayerDecoderEvents_::event_last_logged
uint8_t event_last_logged
Definition: app-layer-events.h:41
FILE_DECODER_EVENT_Z_UNKNOWN_ERROR
@ FILE_DECODER_EVENT_Z_UNKNOWN_ERROR
Definition: detect.h:1556
DetectEngineGetEventInfo
int DetectEngineGetEventInfo(const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
Definition: app-layer-events.c:173
APPLAYER_PROTO_DETECTION_SKIPPED
@ APPLAYER_PROTO_DETECTION_SKIPPED
Definition: app-layer-events.h:49
AppLayerDecoderEventsResetEvents
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)
Definition: app-layer-events.c:130
AppLayerDecoderEvents_
Data structure to store app layer decoder events.
Definition: app-layer-events.h:33
SCAppLayerDecoderEventsSetEventRaw
void SCAppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
Definition: app-layer-events.c:97
APPLAYER_NO_TLS_AFTER_STARTTLS
@ APPLAYER_NO_TLS_AFTER_STARTTLS
Definition: app-layer-events.h:50
util-debug.h
DETECT_EVENT_TOO_MANY_BUFFERS
@ DETECT_EVENT_TOO_MANY_BUFFERS
Definition: detect.h:1564
APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS
@ APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS
Definition: app-layer-events.h:46
AppLayerGetPktEventInfo
int AppLayerGetPktEventInfo(const char *event_name, uint8_t *event_id)
Definition: app-layer-events.c:83
APP_LAYER_EVENT_TYPE_TRANSACTION
@ APP_LAYER_EVENT_TYPE_TRANSACTION
Definition: app-layer-events.h:55
AppLayerEventType
AppLayerEventType
Definition: app-layer-events.h:54
detect.h
AppLayerDecoderEvents_::cnt
uint8_t cnt
Definition: app-layer-events.h:37
FILE_DECODER_EVENT_Z_BUF_ERROR
@ FILE_DECODER_EVENT_Z_BUF_ERROR
Definition: detect.h:1555
FILE_DECODER_EVENT_LZMA_DECODER_ERROR
@ FILE_DECODER_EVENT_LZMA_DECODER_ERROR
Definition: detect.h:1559
FILE_DECODER_EVENT_LZMA_XZ_ERROR
@ FILE_DECODER_EVENT_LZMA_XZ_ERROR
Definition: detect.h:1561
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
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
app_layer_event_pkt_table
SCEnumCharMap app_layer_event_pkt_table[]
Definition: app-layer-events.c:49
SCMapEnumNameToValue
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:40
FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR
@ FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR
Definition: detect.h:1558
suricata-common.h
FILE_DECODER_EVENT_Z_DATA_ERROR
@ FILE_DECODER_EVENT_Z_DATA_ERROR
Definition: detect.h:1553
SCEnumCharMap_
Definition: util-enum.h:27
DECODER_EVENTS_BUFFER_STEPS
#define DECODER_EVENTS_BUFFER_STEPS
Definition: app-layer-events.c:88
APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION
@ APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION
Definition: app-layer-events.h:48
FILE_DECODER_EVENT_NO_MEM
@ FILE_DECODER_EVENT_NO_MEM
Definition: detect.h:1550
SCAppLayerDecoderEventsFreeEvents
void SCAppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
Definition: app-layer-events.c:138
app-layer-events.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
APP_LAYER_EVENT_TYPE_PACKET
@ APP_LAYER_EVENT_TYPE_PACKET
Definition: app-layer-events.h:56
FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR
@ FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR
Definition: detect.h:1560
AppLayerGetEventInfoById
int AppLayerGetEventInfoById(uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
Definition: app-layer-events.c:66
FILE_DECODER_EVENT_Z_STREAM_ERROR
@ FILE_DECODER_EVENT_Z_STREAM_ERROR
Definition: detect.h:1554
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
util-enum.h
FILE_DECODER_EVENT_LZMA_IO_ERROR
@ FILE_DECODER_EVENT_LZMA_IO_ERROR
Definition: detect.h:1557
APPLAYER_WRONG_DIRECTION_FIRST_DATA
@ APPLAYER_WRONG_DIRECTION_FIRST_DATA
Definition: app-layer-events.h:47
FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR
@ FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR
Definition: detect.h:1562
SCAppLayerGetEventIdByName
int SCAppLayerGetEventIdByName(const char *event_name, SCEnumCharMap *table, uint8_t *event_id)
Definition: app-layer-events.c:31
FILE_DECODER_EVENT_INVALID_SWF_LENGTH
@ FILE_DECODER_EVENT_INVALID_SWF_LENGTH
Definition: detect.h:1551
DETECT_EVENT_POST_MATCH_QUEUE_FAILED
@ DETECT_EVENT_POST_MATCH_QUEUE_FAILED
Definition: detect.h:1565