/src/suricata7/src/app-layer-events.c
Line | Count | Source |
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. */ |
34 | | SCEnumCharMap app_layer_event_pkt_table[ ] = { |
35 | | { "APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS", |
36 | | APPLAYER_MISMATCH_PROTOCOL_BOTH_DIRECTIONS }, |
37 | | { "APPLAYER_WRONG_DIRECTION_FIRST_DATA", |
38 | | APPLAYER_WRONG_DIRECTION_FIRST_DATA }, |
39 | | { "APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION", |
40 | | APPLAYER_DETECT_PROTOCOL_ONLY_ONE_DIRECTION }, |
41 | | { "APPLAYER_PROTO_DETECTION_SKIPPED", |
42 | | APPLAYER_PROTO_DETECTION_SKIPPED }, |
43 | | { "APPLAYER_NO_TLS_AFTER_STARTTLS", |
44 | | APPLAYER_NO_TLS_AFTER_STARTTLS }, |
45 | | { "APPLAYER_UNEXPECTED_PROTOCOL", |
46 | | APPLAYER_UNEXPECTED_PROTOCOL }, |
47 | | { NULL, |
48 | | -1 }, |
49 | | }; |
50 | | |
51 | | int AppLayerGetEventInfoById(int event_id, const char **event_name, |
52 | | AppLayerEventType *event_type) |
53 | 29.9k | { |
54 | 29.9k | *event_name = SCMapEnumValueToName(event_id, app_layer_event_pkt_table); |
55 | 29.9k | if (*event_name == NULL) { |
56 | 0 | SCLogError("event \"%d\" not present in " |
57 | 0 | "app-layer-event's enum map table.", |
58 | 0 | event_id); |
59 | | /* yes this is fatal */ |
60 | 0 | return -1; |
61 | 0 | } |
62 | | |
63 | 29.9k | *event_type = APP_LAYER_EVENT_TYPE_PACKET; |
64 | | |
65 | 29.9k | return 0; |
66 | 29.9k | } |
67 | | |
68 | | int AppLayerGetPktEventInfo(const char *event_name, int *event_id) |
69 | 1.45k | { |
70 | 1.45k | *event_id = SCMapEnumNameToValue(event_name, app_layer_event_pkt_table); |
71 | 1.45k | if (*event_id == -1) { |
72 | 268 | SCLogError("event \"%s\" not present in " |
73 | 268 | "app-layer-event's packet event table.", |
74 | 268 | event_name); |
75 | | /* this should be treated as fatal */ |
76 | 268 | return -1; |
77 | 268 | } |
78 | | |
79 | 1.18k | return 0; |
80 | 1.45k | } |
81 | | |
82 | 39.3M | #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 | | */ |
91 | | void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event) |
92 | 55.4M | { |
93 | 55.4M | if (*sevents == NULL) { |
94 | 39.2M | AppLayerDecoderEvents *new_devents = SCMalloc(sizeof(AppLayerDecoderEvents)); |
95 | 39.2M | if (new_devents == NULL) |
96 | 0 | return; |
97 | | |
98 | 39.2M | memset(new_devents, 0, sizeof(AppLayerDecoderEvents)); |
99 | 39.2M | *sevents = new_devents; |
100 | | |
101 | 39.2M | } |
102 | 55.4M | if ((*sevents)->cnt == UCHAR_MAX) { |
103 | | /* we're full */ |
104 | 7.63M | return; |
105 | 7.63M | } |
106 | 47.8M | if ((*sevents)->cnt == (*sevents)->events_buffer_size) { |
107 | 39.3M | int steps = DECODER_EVENTS_BUFFER_STEPS; |
108 | 39.3M | if (UCHAR_MAX - (*sevents)->cnt < steps) |
109 | 11.2k | steps = UCHAR_MAX - (*sevents)->cnt < steps; |
110 | | |
111 | 39.3M | void *ptr = SCRealloc((*sevents)->events, |
112 | 39.3M | ((*sevents)->cnt + steps) * sizeof(uint8_t)); |
113 | 39.3M | if (ptr == NULL) { |
114 | | /* couldn't grow buffer, but no reason to free old |
115 | | * so we keep the events that may already be here */ |
116 | 0 | return; |
117 | 0 | } |
118 | 39.3M | (*sevents)->events = ptr; |
119 | 39.3M | (*sevents)->events_buffer_size += steps; |
120 | 39.3M | } |
121 | | |
122 | 47.8M | (*sevents)->events[(*sevents)->cnt++] = event; |
123 | 47.8M | } |
124 | | |
125 | | void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events) |
126 | 19.4M | { |
127 | 19.4M | if (events != NULL) { |
128 | 6.13M | events->cnt = 0; |
129 | 6.13M | events->event_last_logged = 0; |
130 | 6.13M | } |
131 | 19.4M | } |
132 | | |
133 | | |
134 | | void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events) |
135 | 40.6M | { |
136 | 40.6M | if (events && *events != NULL) { |
137 | 39.2M | if ((*events)->events != NULL) |
138 | 39.2M | SCFree((*events)->events); |
139 | 39.2M | SCFree(*events); |
140 | 39.2M | *events = NULL; |
141 | 39.2M | } |
142 | 40.6M | } |
143 | | |
144 | | SCEnumCharMap det_ctx_event_table[] = { |
145 | | { "NO_MEMORY", FILE_DECODER_EVENT_NO_MEM }, |
146 | | { "INVALID_SWF_LENGTH", FILE_DECODER_EVENT_INVALID_SWF_LENGTH }, |
147 | | { "INVALID_SWF_VERSION", FILE_DECODER_EVENT_INVALID_SWF_VERSION }, |
148 | | { "Z_DATA_ERROR", FILE_DECODER_EVENT_Z_DATA_ERROR }, |
149 | | { "Z_STREAM_ERROR", FILE_DECODER_EVENT_Z_STREAM_ERROR }, |
150 | | { "Z_BUF_ERROR", FILE_DECODER_EVENT_Z_BUF_ERROR }, |
151 | | { "Z_UNKNOWN_ERROR", FILE_DECODER_EVENT_Z_UNKNOWN_ERROR }, |
152 | | { "LZMA_IO_ERROR", FILE_DECODER_EVENT_LZMA_IO_ERROR }, |
153 | | { "LZMA_HEADER_TOO_SHORT_ERROR", FILE_DECODER_EVENT_LZMA_HEADER_TOO_SHORT_ERROR }, |
154 | | { "LZMA_DECODER_ERROR", FILE_DECODER_EVENT_LZMA_DECODER_ERROR }, |
155 | | { "LZMA_MEMLIMIT_ERROR", FILE_DECODER_EVENT_LZMA_MEMLIMIT_ERROR }, |
156 | | { "LZMA_XZ_ERROR", FILE_DECODER_EVENT_LZMA_XZ_ERROR }, |
157 | | { "LZMA_UNKNOWN_ERROR", FILE_DECODER_EVENT_LZMA_UNKNOWN_ERROR }, |
158 | | { |
159 | | "TOO_MANY_BUFFERS", |
160 | | DETECT_EVENT_TOO_MANY_BUFFERS, |
161 | | }, |
162 | | { NULL, -1 }, |
163 | | }; |
164 | | |
165 | | int DetectEngineGetEventInfo(const char *event_name, int *event_id, AppLayerEventType *event_type) |
166 | 3 | { |
167 | 3 | *event_id = SCMapEnumNameToValue(event_name, det_ctx_event_table); |
168 | 3 | if (*event_id == -1) { |
169 | 2 | SCLogError("event \"%s\" not present in " |
170 | 2 | "det_ctx's enum map table.", |
171 | 2 | event_name); |
172 | | /* this should be treated as fatal */ |
173 | 2 | return -1; |
174 | 2 | } |
175 | 1 | *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; |
176 | | |
177 | 1 | return 0; |
178 | 3 | } |