suricata
detect-engine-inspect-buffer.c
Go to the documentation of this file.
1 /* Copyright (C) 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  */
23 
24 #include "suricata-common.h"
26 #include "detect.h"
27 
28 #include "util-validate.h"
29 #ifdef UNITTESTS
30 #include "util-unittest.h"
31 #endif
32 
34 {
35  /* single buffers */
36  for (uint32_t i = 0; i < det_ctx->inspect.to_clear_idx; i++) {
37  const uint32_t idx = det_ctx->inspect.to_clear_queue[i];
38  InspectionBuffer *buffer = &det_ctx->inspect.buffers[idx];
39  buffer->inspect = NULL;
40  buffer->initialized = false;
41  }
42  det_ctx->inspect.to_clear_idx = 0;
43 
44  /* multi buffers */
45  for (uint32_t i = 0; i < det_ctx->multi_inspect.to_clear_idx; i++) {
46  const uint32_t idx = det_ctx->multi_inspect.to_clear_queue[i];
47  InspectionBufferMultipleForList *mbuffer = &det_ctx->multi_inspect.buffers[idx];
48  for (uint32_t x = 0; x <= mbuffer->max; x++) {
49  InspectionBuffer *buffer = &mbuffer->inspection_buffers[x];
50  buffer->inspect = NULL;
51  buffer->initialized = false;
52  }
53  mbuffer->init = 0;
54  mbuffer->max = 0;
55  }
56  det_ctx->multi_inspect.to_clear_idx = 0;
57 }
58 
60 {
61  return &det_ctx->inspect.buffers[list_id];
62 }
63 
64 static InspectionBufferMultipleForList *SCInspectionBufferGetMulti(
65  DetectEngineThreadCtx *det_ctx, const int list_id)
66 {
67  InspectionBufferMultipleForList *buffer = &det_ctx->multi_inspect.buffers[list_id];
68  if (!buffer->init) {
71  det_ctx->multi_inspect.to_clear_queue[det_ctx->multi_inspect.to_clear_idx++] = list_id;
72  buffer->init = 1;
73  }
74  return buffer;
75 }
76 
77 /** \brief for a InspectionBufferMultipleForList get a InspectionBuffer
78  * \param fb the multiple buffer array
79  * \param local_id the index to get a buffer
80  * \param buffer the inspect buffer or NULL in case of error */
82  DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
83 {
84  if (unlikely(local_id >= 1024)) {
86  return NULL;
87  }
88 
89  InspectionBufferMultipleForList *fb = SCInspectionBufferGetMulti(det_ctx, list_id);
90 
91  if (local_id >= fb->size) {
92  uint32_t old_size = fb->size;
93  uint32_t new_size = local_id + 1;
94  uint32_t grow_by = new_size - old_size;
95  SCLogDebug("size is %u, need %u, so growing by %u", old_size, new_size, grow_by);
96 
97  SCLogDebug("fb->inspection_buffers %p", fb->inspection_buffers);
98  void *ptr = SCRealloc(fb->inspection_buffers, (local_id + 1) * sizeof(InspectionBuffer));
99  if (ptr == NULL)
100  return NULL;
101 
102  InspectionBuffer *to_zero = (InspectionBuffer *)ptr + old_size;
103  SCLogDebug("ptr %p to_zero %p", ptr, to_zero);
104  memset((uint8_t *)to_zero, 0, (grow_by * sizeof(InspectionBuffer)));
105  fb->inspection_buffers = ptr;
106  fb->size = new_size;
107  }
108 
109  fb->max = MAX(fb->max, local_id);
110  InspectionBuffer *buffer = &fb->inspection_buffers[local_id];
111  SCLogDebug("using buffer %p", buffer);
112 #ifdef DEBUG_VALIDATION
113  buffer->multi = true;
114 #endif
115  return buffer;
116 }
117 
118 static inline void InspectionBufferApplyTransformsInternal(DetectEngineThreadCtx *det_ctx,
119  InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
120 {
121  if (transforms) {
122  for (int i = 0; i < DETECT_TRANSFORMS_MAX; i++) {
123  const int id = transforms->transforms[i].transform;
124  if (id == 0)
125  break;
126  DEBUG_VALIDATE_BUG_ON(sigmatch_table[id].Transform == NULL);
127  sigmatch_table[id].Transform(det_ctx, buffer, transforms->transforms[i].options);
128  SCLogDebug("applied transform %s", sigmatch_table[id].name);
129  }
130  }
131 }
132 
134  const DetectEngineTransforms *transforms)
135 {
136  InspectionBufferApplyTransformsInternal(det_ctx, buffer, transforms);
137 }
138 
139 void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
140 {
141  memset(buffer, 0, sizeof(*buffer));
142  buffer->buf = SCCalloc(initial_size, sizeof(uint8_t));
143  if (buffer->buf != NULL) {
144  buffer->size = initial_size;
145  }
146 }
147 
148 /** \brief setup the buffer empty */
150 {
151 #ifdef DEBUG_VALIDATION
153  DEBUG_VALIDATE_BUG_ON(!buffer->multi);
154 #endif
155  buffer->inspect = NULL;
156  buffer->inspect_len = 0;
157  buffer->len = 0;
158  buffer->initialized = true;
159 }
160 
161 /** \brief setup the buffer with our initial data */
163  const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
164 {
165 #ifdef DEBUG_VALIDATION
166  DEBUG_VALIDATE_BUG_ON(!buffer->multi);
167 #endif
168  buffer->inspect = buffer->orig = data;
169  buffer->inspect_len = buffer->orig_len = data_len;
170  buffer->len = 0;
171  buffer->initialized = true;
172 
173  InspectionBufferApplyTransformsInternal(det_ctx, buffer, transforms);
174 }
175 
176 static inline void InspectionBufferSetupInternal(DetectEngineThreadCtx *det_ctx, const int list_id,
177  InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
178 {
179 #ifdef DEBUG_VALIDATION
180  DEBUG_VALIDATE_BUG_ON(buffer->multi);
181  DEBUG_VALIDATE_BUG_ON(buffer != SCInspectionBufferGet(det_ctx, list_id));
182 #endif
183  /* 'initialized' doubles as set-flag for the clear queue: enqueue the
184  * list_id exactly once per transaction. Queuing on inspect == NULL
185  * would append again for every NULL-data setup, overflowing the
186  * fixed-size queue (Redmine #9114). */
187  if (!buffer->initialized) {
188 #ifdef UNITTESTS
189  if (det_ctx && list_id != -1)
190 #endif
191  {
192  /* defensive: the set-flag invariant above is what bounds
193  * the queue; the guard aborts loudly in debug-validation
194  * builds on a future violation, release builds rely on
195  * the invariant itself */
197  det_ctx->inspect.to_clear_queue[det_ctx->inspect.to_clear_idx++] = list_id;
198  }
199  buffer->initialized = true;
200  }
201  buffer->inspect = buffer->orig = data;
202  buffer->inspect_len = buffer->orig_len = data_len;
203  buffer->len = 0;
204 }
205 /** \brief setup the buffer with our initial data */
206 void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id,
207  InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
208 {
209  InspectionBufferSetupInternal(det_ctx, list_id, buffer, data, data_len);
210 }
211 
212 /** \brief setup the buffer with our initial data */
214  InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len,
215  const DetectEngineTransforms *transforms)
216 {
217  InspectionBufferSetupInternal(det_ctx, list_id, buffer, data, data_len);
218  InspectionBufferApplyTransformsInternal(det_ctx, buffer, transforms);
219 }
220 
222 {
223  if (buffer->buf != NULL) {
224  SCFree(buffer->buf);
225  }
226  memset(buffer, 0, sizeof(*buffer));
227 }
228 
229 /**
230  * \brief make sure that the buffer has at least 'min_size' bytes
231  * Expand the buffer if necessary
232  */
233 uint8_t *SCInspectionBufferCheckAndExpand(InspectionBuffer *buffer, uint32_t min_size)
234 {
235  if (likely(buffer->size >= min_size))
236  return buffer->buf;
237 
238  uint32_t new_size = (buffer->size == 0) ? 4096 : buffer->size;
239  while (new_size < min_size) {
240  /* guard against overflow */
241  if (new_size > UINT32_MAX / 2) {
242  new_size = min_size;
243  break;
244  }
245  new_size *= 2;
246  }
247 
248  void *ptr = SCRealloc(buffer->buf, new_size);
249  if (ptr != NULL) {
250  buffer->buf = ptr;
251  buffer->size = new_size;
252  } else {
253  return NULL;
254  }
255  return buffer->buf;
256 }
257 
258 void SCInspectionBufferTruncate(InspectionBuffer *buffer, uint32_t buf_len)
259 {
260  DEBUG_VALIDATE_BUG_ON(buffer->buf == NULL);
261  DEBUG_VALIDATE_BUG_ON(buf_len > buffer->size);
262  buffer->inspect = buffer->buf;
263  buffer->inspect_len = buf_len;
264  buffer->initialized = true;
265 }
266 
267 void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
268 {
269  SCInspectionBufferCheckAndExpand(buffer, buf_len);
270 
271  if (buffer->size) {
272  uint32_t copy_size = MIN(buf_len, buffer->size);
273  memcpy(buffer->buf, buf, copy_size);
274  buffer->inspect = buffer->buf;
275  buffer->inspect_len = copy_size;
276  buffer->initialized = true;
277  }
278 }
279 
280 // Returns wether the inspection buffer will make the transform run in-place
281 // Especially useful for transforms that produces an output longer than the input
283 {
284  return buffer->inspect == buffer->buf;
285 }
286 
287 #ifdef UNITTESTS
288 /** NULL setups on the same list_id must not append to the clear queue more
289  * than once; queue holds one entry per buffer type (Redmine #9114). */
290 static int InspectionBufferSetupOverflowUtest(void)
291 {
292  DetectEngineThreadCtx det_ctx = { 0 };
293  det_ctx.inspect.buffers_size = 2;
294  det_ctx.inspect.buffers = SCCalloc(det_ctx.inspect.buffers_size, sizeof(InspectionBuffer));
295  FAIL_IF_NULL(det_ctx.inspect.buffers);
296  det_ctx.inspect.to_clear_queue = SCCalloc(det_ctx.inspect.buffers_size, sizeof(uint32_t));
298 
299  InspectionBuffer *b = &det_ctx.inspect.buffers[1];
300  /* repeated NULL setups: one queue entry, buffer stays inactive */
301  for (uint32_t i = 0; i < det_ctx.inspect.buffers_size + 10; i++) {
302  InspectionBufferSetup(&det_ctx, 1, b, NULL, 0);
303  }
304  FAIL_IF(det_ctx.inspect.to_clear_idx != 1);
305  FAIL_IF(det_ctx.inspect.to_clear_queue[0] != 1);
306  FAIL_IF(b->inspect != NULL);
307 
308  /* real data: no second append, buffer becomes active */
309  InspectionBufferSetup(&det_ctx, 1, b, (const uint8_t *)"abc", 3);
310  FAIL_IF(det_ctx.inspect.to_clear_idx != 1);
311  FAIL_IF(b->inspect == NULL || b->inspect_len != 3);
312 
313  /* clean drains the queue and resets the buffer */
314  InspectionBufferClean(&det_ctx);
315  FAIL_IF(det_ctx.inspect.to_clear_idx != 0);
316  FAIL_IF(b->inspect != NULL || b->initialized);
317 
318  /* after clean the list_id is queued again on next setup */
319  InspectionBufferSetup(&det_ctx, 1, b, NULL, 0);
320  FAIL_IF(det_ctx.inspect.to_clear_idx != 1);
321  FAIL_IF(det_ctx.inspect.to_clear_queue[0] != 1);
322 
323  SCFree(det_ctx.inspect.to_clear_queue);
324  SCFree(det_ctx.inspect.buffers);
325  PASS;
326 }
327 
329 {
330  UtRegisterTest("InspectionBufferSetupOverflowUtest", InspectionBufferSetupOverflowUtest);
331 }
332 #endif /* UNITTESTS */
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine-inspect-buffer.c:133
DetectEngineThreadCtx_::inspect
struct DetectEngineThreadCtx_::@101 inspect
DetectEngineThreadCtx_::to_clear_idx
uint32_t to_clear_idx
Definition: detect.h:1362
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:59
InspectionBuffer::initialized
bool initialized
Definition: detect-engine-inspect-buffer.h:38
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine-inspect-buffer.c:267
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:366
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
name
const char * name
Definition: detect-engine-proto.c:47
InspectionBufferClean
void InspectionBufferClean(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-inspect-buffer.c:33
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
TransformData_::options
void * options
Definition: detect.h:363
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:213
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:206
InspectionBuffer::orig
const uint8_t * orig
Definition: detect-engine-inspect-buffer.h:49
DetectEngineInspectBufferRegisterTests
void DetectEngineInspectBufferRegisterTests(void)
Definition: detect-engine-inspect-buffer.c:328
DetectEngineSetEvent
void DetectEngineSetEvent(DetectEngineThreadCtx *det_ctx, uint8_t e)
Definition: detect-engine.c:5471
DetectEngineThreadCtx_::buffers_size
uint32_t buffers_size
Definition: detect.h:1361
MIN
#define MIN(x, y)
Definition: suricata-common.h:413
InspectionBuffer::size
uint32_t size
Definition: detect-engine-inspect-buffer.h:46
MAX
#define MAX(x, y)
Definition: suricata-common.h:417
InspectionBufferMultipleForList::init
uint32_t init
Definition: detect.h:358
DETECT_TRANSFORMS_MAX
#define DETECT_TRANSFORMS_MAX
Definition: detect.h:49
InspectionBuffer::orig_len
uint32_t orig_len
Definition: detect-engine-inspect-buffer.h:48
util-unittest.h
SCInspectionBufferInPlace
bool SCInspectionBufferInPlace(const InspectionBuffer *buffer)
Definition: detect-engine-inspect-buffer.c:282
InspectionBufferMultipleForList::size
uint32_t size
Definition: detect.h:356
DetectEngineThreadCtx_::multi_inspect
struct DetectEngineThreadCtx_::@102 multi_inspect
InspectionBufferMultipleForList
Definition: detect.h:354
detect-engine-inspect-buffer.h
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine-inspect-buffer.c:221
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
DETECT_EVENT_TOO_MANY_BUFFERS
@ DETECT_EVENT_TOO_MANY_BUFFERS
Definition: detect.h:1573
DetectEngineThreadCtx_
Definition: detect.h:1295
DetectEngineThreadCtx_::buffers
InspectionBuffer * buffers
Definition: detect.h:1360
detect.h
InspectionBuffer::buf
uint8_t * buf
Definition: detect-engine-inspect-buffer.h:45
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:367
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
DetectEngineThreadCtx_::to_clear_queue
uint32_t * to_clear_queue
Definition: detect.h:1363
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine-inspect-buffer.c:139
TransformData_::transform
int transform
Definition: detect.h:362
util-validate.h
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect-engine-inspect-buffer.h:37
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
InspectionBuffer::len
uint32_t len
Definition: detect-engine-inspect-buffer.h:44
SCFree
#define SCFree(p)
Definition: util-mem.h:61
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine-inspect-buffer.c:149
SCInspectionBufferCheckAndExpand
uint8_t * SCInspectionBufferCheckAndExpand(InspectionBuffer *buffer, uint32_t min_size)
make sure that the buffer has at least 'min_size' bytes Expand the buffer if necessary
Definition: detect-engine-inspect-buffer.c:233
likely
#define likely(expr)
Definition: util-optimize.h:32
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SigTableElmt_::Transform
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, const void *context)
Definition: detect.h:1520
InspectionBufferMultipleForList::inspection_buffers
InspectionBuffer * inspection_buffers
Definition: detect.h:355
InspectionBufferMultipleForList::max
uint32_t max
Definition: detect.h:357
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:162
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine-inspect-buffer.c:81
SCInspectionBufferTruncate
void SCInspectionBufferTruncate(InspectionBuffer *buffer, uint32_t buf_len)
Definition: detect-engine-inspect-buffer.c:258