suricata
app-layer-events.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2022 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 "decode.h"
27 #include "flow.h"
28 #include "app-layer-events.h"
29 #include "app-layer-parser.h"
30 #include "util-enum.h"
31 
32 /* events raised during protocol detection are stored in the
33  * packets storage, not in the flow. */
35  { "APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS",
37  { "APPLAYER_WRONG_DIRECTION_FIRST_DATA",
39  { "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION",
41  { "APPLAYER_PROTO_DETECTION_SKIPPED",
43  { "APPLAYER_NO_TLS_AFTER_STARTTLS",
45  { "APPLAYER_UNEXPECTED_PROTOCOL",
47  { NULL,
48  -1 },
49 };
50 
51 int AppLayerGetEventInfoById(int event_id, const char **event_name,
52  AppLayerEventType *event_type)
53 {
54  *event_name = SCMapEnumValueToName(event_id, app_layer_event_pkt_table);
55  if (*event_name == NULL) {
56  SCLogError("event \"%d\" not present in "
57  "app-layer-event's enum map table.",
58  event_id);
59  /* yes this is fatal */
60  return -1;
61  }
62 
63  *event_type = APP_LAYER_EVENT_TYPE_PACKET;
64 
65  return 0;
66 }
67 
68 int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
69 {
70  *event_id = SCMapEnumNameToValue(event_name, app_layer_event_pkt_table);
71  if (*event_id == -1) {
72  SCLogError("event \"%s\" not present in "
73  "app-layer-event's packet event table.",
74  event_name);
75  /* this should be treated as fatal */
76  return -1;
77  }
78 
79  return 0;
80 }
81 
82 #define DECODER_EVENTS_BUFFER_STEPS 8
83 
84 /**
85  * \brief Set an app layer decoder event.
86  *
87  * \param sevents Pointer to a AppLayerDecoderEvents pointer. If *sevents is NULL
88  * memory will be allocated.
89  * \param event The event to be stored.
90  */
92 {
93  if (*sevents == NULL) {
94  AppLayerDecoderEvents *new_devents = SCCalloc(1, sizeof(AppLayerDecoderEvents));
95  if (new_devents == NULL)
96  return;
97 
98  *sevents = new_devents;
99 
100  }
101  if ((*sevents)->cnt == UCHAR_MAX) {
102  /* we're full */
103  return;
104  }
105  if ((*sevents)->cnt == (*sevents)->events_buffer_size) {
106  int steps = DECODER_EVENTS_BUFFER_STEPS;
107  if (UCHAR_MAX - (*sevents)->cnt < steps)
108  steps = UCHAR_MAX - (*sevents)->cnt < steps;
109 
110  void *ptr = SCRealloc((*sevents)->events,
111  ((*sevents)->cnt + steps) * sizeof(uint8_t));
112  if (ptr == NULL) {
113  /* couldn't grow buffer, but no reason to free old
114  * so we keep the events that may already be here */
115  return;
116  }
117  (*sevents)->events = ptr;
118  (*sevents)->events_buffer_size += steps;
119  }
120 
121  (*sevents)->events[(*sevents)->cnt++] = event;
122 }
123 
125 {
126  if (events != NULL) {
127  events->cnt = 0;
128  events->event_last_logged = 0;
129  }
130 }
131 
132 
134 {
135  if (events && *events != NULL) {
136  if ((*events)->events != NULL)
137  SCFree((*events)->events);
138  SCFree(*events);
139  *events = NULL;
140  }
141 }
142 
144  { "NO_MEMORY", FILE_DECODER_EVENT_NO_MEM },
145  { "INVALID_SWF_LENGTH", FILE_DECODER_EVENT_INVALID_SWF_LENGTH },
146  { "INVALID_SWF_VERSION", FILE_DECODER_EVENT_INVALID_SWF_VERSION },
147  { "Z_DATA_ERROR", FILE_DECODER_EVENT_Z_DATA_ERROR },
148  { "Z_STREAM_ERROR", FILE_DECODER_EVENT_Z_STREAM_ERROR },
149  { "Z_BUF_ERROR", FILE_DECODER_EVENT_Z_BUF_ERROR },
150  { "Z_UNKNOWN_ERROR", FILE_DECODER_EVENT_Z_UNKNOWN_ERROR },
151  { "LZMA_IO_ERROR", FILE_DECODER_EVENT_LZMA_IO_ERROR },
152  { "LZMA_HEADER_TOO_SHORT_ERROR", FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR },
153  { "LZMA_DECODER_ERROR", FILE_DECODER_EVENT_LZMA_DECODER_ERROR },
154  { "LZMA_MEMLIMIT_ERROR", FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR },
155  { "LZMA_XZ_ERROR", FILE_DECODER_EVENT_LZMA_XZ_ERROR },
156  { "LZMA_UNKNOWN_ERROR", FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR },
157  {
158  "TOO_MANY_BUFFERS",
160  },
161  { NULL, -1 },
162 };
163 
164 int DetectEngineGetEventInfo(const char *event_name, int *event_id, AppLayerEventType *event_type)
165 {
166  *event_id = SCMapEnumNameToValue(event_name, det_ctx_event_table);
167  if (*event_id == -1) {
168  SCLogError("event \"%s\" not present in "
169  "det_ctx's enum map table.",
170  event_name);
171  /* this should be treated as fatal */
172  return -1;
173  }
174  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
175 
176  return 0;
177 }
APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS
@ APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS
Definition: app-layer-events.h:48
FILE_DECODER_EVENT_LZMA_IO_ERROR
@ FILE_DECODER_EVENT_LZMA_IO_ERROR
Definition: detect.h:1309
FILE_DECODER_EVENT_INVALID_SWF_VERSION
@ FILE_DECODER_EVENT_INVALID_SWF_VERSION
Definition: detect.h:1304
det_ctx_event_table
SCEnumCharMap det_ctx_event_table[]
Definition: app-layer-events.c:143
APPLAYER_WRONG_DIRECTION_FIRST_DATA
@ APPLAYER_WRONG_DIRECTION_FIRST_DATA
Definition: app-layer-events.h:49
AppLayerDecoderEvents_::event_last_logged
uint8_t event_last_logged
Definition: app-layer-events.h:43
AppLayerDecoderEventsFreeEvents
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
Definition: app-layer-events.c:133
FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR
@ FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR
Definition: detect.h:1314
AppLayerDecoderEventsResetEvents
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)
Definition: app-layer-events.c:124
AppLayerDecoderEvents_
Data structure to store app layer decoder events.
Definition: app-layer-events.h:35
FILE_DECODER_EVENT_LZMA_DECODER_ERROR
@ FILE_DECODER_EVENT_LZMA_DECODER_ERROR
Definition: detect.h:1311
APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION
@ APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION
Definition: app-layer-events.h:50
decode.h
FILE_DECODER_EVENT_Z_UNKNOWN_ERROR
@ FILE_DECODER_EVENT_Z_UNKNOWN_ERROR
Definition: detect.h:1308
APPLAYER_PROTO_DETECTION_SKIPPED
@ APPLAYER_PROTO_DETECTION_SKIPPED
Definition: app-layer-events.h:51
FILE_DECODER_EVENT_INVALID_SWF_LENGTH
@ FILE_DECODER_EVENT_INVALID_SWF_LENGTH
Definition: detect.h:1303
DETECT_EVENT_TOO_MANY_BUFFERS
@ DETECT_EVENT_TOO_MANY_BUFFERS
Definition: detect.h:1316
APPLAYER_NO_TLS_AFTER_STARTTLS
@ APPLAYER_NO_TLS_AFTER_STARTTLS
Definition: app-layer-events.h:52
AppLayerGetEventInfoById
int AppLayerGetEventInfoById(int event_id, const char **event_name, AppLayerEventType *event_type)
Definition: app-layer-events.c:51
app-layer-parser.h
AppLayerDecoderEvents_::cnt
uint8_t cnt
Definition: app-layer-events.h:39
FILE_DECODER_EVENT_Z_DATA_ERROR
@ FILE_DECODER_EVENT_Z_DATA_ERROR
Definition: detect.h:1305
FILE_DECODER_EVENT_Z_STREAM_ERROR
@ FILE_DECODER_EVENT_Z_STREAM_ERROR
Definition: detect.h:1306
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:34
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
suricata-common.h
SCEnumCharMap_
Definition: util-enum.h:27
AppLayerDecoderEventsSetEventRaw
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
Definition: app-layer-events.c:91
DECODER_EVENTS_BUFFER_STEPS
#define DECODER_EVENTS_BUFFER_STEPS
Definition: app-layer-events.c:82
app-layer-events.h
FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR
@ FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR
Definition: detect.h:1310
FILE_DECODER_EVENT_Z_BUF_ERROR
@ FILE_DECODER_EVENT_Z_BUF_ERROR
Definition: detect.h:1307
DetectEngineGetEventInfo
int DetectEngineGetEventInfo(const char *event_name, int *event_id, AppLayerEventType *event_type)
Definition: app-layer-events.c:164
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR
@ FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR
Definition: detect.h:1312
FILE_DECODER_EVENT_NO_MEM
@ FILE_DECODER_EVENT_NO_MEM
Definition: detect.h:1302
AppLayerGetPktEventInfo
int AppLayerGetPktEventInfo(const char *event_name, int *event_id)
Definition: app-layer-events.c:68
FILE_DECODER_EVENT_LZMA_XZ_ERROR
@ FILE_DECODER_EVENT_LZMA_XZ_ERROR
Definition: detect.h:1313
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
APPLAYER_UNEXPECTED_PROTOCOL
@ APPLAYER_UNEXPECTED_PROTOCOL
Definition: app-layer-events.h:53
util-enum.h