Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/detect-file-data.c
Line
Count
Source
1
/* Copyright (C) 2007-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
 *
23
 */
24
25
#include "suricata-common.h"
26
#include "threads.h"
27
#include "decode.h"
28
29
#include "detect.h"
30
#include "detect-parse.h"
31
32
#include "detect-engine.h"
33
#include "detect-engine-buffer.h"
34
#include "detect-engine-mpm.h"
35
#include "detect-engine-state.h"
36
#include "detect-engine-prefilter.h"
37
#include "detect-engine-content-inspection.h"
38
#include "detect-engine-file.h"
39
#include "detect-file-data.h"
40
41
#include "app-layer.h"
42
#include "app-layer-parser.h"
43
#include "app-layer-htp.h"
44
#include "app-layer-smtp.h"
45
46
#include "flow.h"
47
#include "flow-var.h"
48
#include "flow-util.h"
49
50
#include "util-debug.h"
51
#include "util-spm-bm.h"
52
#include "util-unittest.h"
53
#include "util-unittest-helper.h"
54
#include "util-file-decompression.h"
55
#include "util-profiling.h"
56
57
static int DetectFiledataSetup (DetectEngineCtx *, Signature *, const char *);
58
#ifdef UNITTESTS
59
static void DetectFiledataRegisterTests(void);
60
#endif
61
static void DetectFiledataSetupCallback(const DetectEngineCtx *de_ctx,
62
                                        Signature *s);
63
static int g_file_data_buffer_id = 0;
64
65
/* file API */
66
int PrefilterMpmFiledataRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
67
        const DetectBufferMpmRegistry *mpm_reg, int list_id);
68
69
// file protocols with common file handling
70
typedef struct {
71
    AppProto alproto;
72
    int direction;
73
    int to_client_progress;
74
    int to_server_progress;
75
} DetectFileHandlerProtocol_t;
76
77
/* Table with all filehandler registrations */
78
DetectFileHandlerTableElmt filehandler_table[DETECT_TBLSIZE_STATIC];
79
80
0
#define ALPROTO_WITHFILES_MAX 16
81
82
// file protocols with common file handling
83
DetectFileHandlerProtocol_t al_protocols[ALPROTO_WITHFILES_MAX] = {
84
    { .alproto = ALPROTO_NFS, .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT },
85
    { .alproto = ALPROTO_SMB, .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT },
86
    { .alproto = ALPROTO_FTP, .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT },
87
    { .alproto = ALPROTO_FTPDATA, .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT },
88
    { .alproto = ALPROTO_HTTP1,
89
            .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT,
90
            .to_client_progress = HTP_RESPONSE_PROGRESS_BODY,
91
            .to_server_progress = HTP_REQUEST_PROGRESS_BODY },
92
    { .alproto = ALPROTO_HTTP2,
93
            .direction = SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT,
94
            .to_client_progress = HTTP2StateDataServer,
95
            .to_server_progress = HTTP2StateDataClient },
96
    { .alproto = ALPROTO_SMTP,
97
            .direction = SIG_FLAG_TOSERVER,
98
            .to_server_progress = SMTP_REQUEST_DATA },
99
    { .alproto = ALPROTO_UNKNOWN }
100
};
101
102
void DetectFileRegisterProto(
103
        AppProto alproto, int direction, int to_client_progress, int to_server_progress)
104
0
{
105
0
    size_t i = 0;
106
0
    while (i < ALPROTO_WITHFILES_MAX && al_protocols[i].alproto != ALPROTO_UNKNOWN) {
107
0
        i++;
108
0
    }
109
0
    if (i == ALPROTO_WITHFILES_MAX) {
110
0
        return;
111
0
    }
112
0
    al_protocols[i].alproto = alproto;
113
0
    al_protocols[i].direction = direction;
114
0
    al_protocols[i].to_client_progress = to_client_progress;
115
0
    al_protocols[i].to_server_progress = to_server_progress;
116
0
    if (i + 1 < ALPROTO_WITHFILES_MAX) {
117
0
        al_protocols[i + 1].alproto = ALPROTO_UNKNOWN;
118
0
    }
119
0
}
120
121
void DetectFileRegisterFileProtocols(DetectFileHandlerTableElmt *reg)
122
237
{
123
1.89k
    for (size_t i = 0; i < g_alproto_max; i++) {
124
1.89k
        if (al_protocols[i].alproto == ALPROTO_UNKNOWN) {
125
237
            break;
126
237
        }
127
1.65k
        int direction = al_protocols[i].direction == 0
128
1.65k
                                ? (int)(SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT)
129
1.65k
                                : al_protocols[i].direction;
130
131
1.65k
        if (direction & SIG_FLAG_TOCLIENT) {
132
1.42k
            DetectAppLayerMpmRegister(reg->name, SIG_FLAG_TOCLIENT, reg->priority, reg->PrefilterFn,
133
1.42k
                    reg->GetData, al_protocols[i].alproto, al_protocols[i].to_client_progress);
134
1.42k
            DetectAppLayerInspectEngineRegister(reg->name, al_protocols[i].alproto,
135
1.42k
                    SIG_FLAG_TOCLIENT, al_protocols[i].to_client_progress, reg->Callback,
136
1.42k
                    reg->GetData);
137
1.42k
        }
138
1.65k
        if (direction & SIG_FLAG_TOSERVER) {
139
1.65k
            DetectAppLayerMpmRegister(reg->name, SIG_FLAG_TOSERVER, reg->priority, reg->PrefilterFn,
140
1.65k
                    reg->GetData, al_protocols[i].alproto, al_protocols[i].to_server_progress);
141
1.65k
            DetectAppLayerInspectEngineRegister(reg->name, al_protocols[i].alproto,
142
1.65k
                    SIG_FLAG_TOSERVER, al_protocols[i].to_server_progress, reg->Callback,
143
1.65k
                    reg->GetData);
144
1.65k
        }
145
1.65k
    }
146
237
}
147
148
/**
149
 * \brief Registration function for keyword: file_data
150
 */
151
void DetectFiledataRegister(void)
152
79
{
153
79
    sigmatch_table[DETECT_FILE_DATA].name = "file.data";
154
79
    sigmatch_table[DETECT_FILE_DATA].alias = "file_data";
155
79
    sigmatch_table[DETECT_FILE_DATA].desc = "make content keywords match on file data";
156
79
    sigmatch_table[DETECT_FILE_DATA].url = "/rules/file-keywords.html#file-data";
157
79
    sigmatch_table[DETECT_FILE_DATA].Setup = DetectFiledataSetup;
158
#ifdef UNITTESTS
159
    sigmatch_table[DETECT_FILE_DATA].RegisterTests = DetectFiledataRegisterTests;
160
#endif
161
79
    sigmatch_table[DETECT_FILE_DATA].flags = SIGMATCH_OPTIONAL_OPT | SIGMATCH_SUPPORT_DIR;
162
163
79
    filehandler_table[DETECT_FILE_DATA].name = "file_data";
164
79
    filehandler_table[DETECT_FILE_DATA].priority = 2;
165
79
    filehandler_table[DETECT_FILE_DATA].PrefilterFn = PrefilterMpmFiledataRegister;
166
79
    filehandler_table[DETECT_FILE_DATA].Callback = DetectEngineInspectFiledata;
167
168
79
    DetectBufferTypeRegisterSetupCallback("file_data", DetectFiledataSetupCallback);
169
170
79
    DetectBufferTypeSetDescriptionByName("file_data", "data from tracked files");
171
79
    DetectBufferTypeSupportsMultiInstance("file_data");
172
173
79
    g_file_data_buffer_id = DetectBufferTypeGetByName("file_data");
174
79
}
175
176
22.6k
static void SetupDetectEngineConfig(DetectEngineCtx *de_ctx) {
177
22.6k
    if (de_ctx->filedata_config)
178
21.5k
        return;
179
180
1.18k
    de_ctx->filedata_config = SCMalloc(g_alproto_max * sizeof(DetectFileDataCfg));
181
1.18k
    if (unlikely(de_ctx->filedata_config == NULL))
182
0
        return;
183
    /* initialize default */
184
48.5k
    for (AppProto i = 0; i < g_alproto_max; i++) {
185
47.3k
        de_ctx->filedata_config[i].content_limit = FILEDATA_CONTENT_LIMIT;
186
47.3k
        de_ctx->filedata_config[i].content_inspect_min_size = FILEDATA_CONTENT_INSPECT_MIN_SIZE;
187
47.3k
    }
188
189
    /* add protocol specific settings here */
190
191
    /* SMTP */
192
1.18k
    de_ctx->filedata_config[ALPROTO_SMTP].content_limit = smtp_config.content_limit;
193
1.18k
    de_ctx->filedata_config[ALPROTO_SMTP].content_inspect_min_size =
194
1.18k
            smtp_config.content_inspect_min_size;
195
1.18k
}
196
197
/**
198
 * \brief this function is used to parse filedata options
199
 * \brief into the current signature
200
 *
201
 * \param de_ctx pointer to the Detection Engine Context
202
 * \param s pointer to the Current Signature
203
 * \param str pointer to the user provided "filestore" option
204
 *
205
 * \retval 0 on Success
206
 * \retval -1 on Failure
207
 */
208
static int DetectFiledataSetup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
209
72.1k
{
210
72.1k
    SCEnter();
211
212
72.1k
    if (s->alproto != ALPROTO_UNKNOWN && !AppLayerParserSupportsFiles(IPPROTO_TCP, s->alproto) &&
213
179
            !AppLayerParserSupportsFiles(IPPROTO_UDP, s->alproto)) {
214
179
        SCLogError("The 'file_data' keyword cannot be used with protocol %s",
215
179
                AppLayerGetProtoName(s->alproto));
216
179
        return -1;
217
179
    }
218
219
71.9k
    if (s->alproto == ALPROTO_SMTP && (s->init_data->init_flags & SIG_FLAG_INIT_FLOW) &&
220
178
        !(s->flags & SIG_FLAG_TOSERVER) && (s->flags & SIG_FLAG_TOCLIENT)) {
221
7
        SCLogError("The 'file-data' keyword cannot be used with SMTP flow:to_client or "
222
7
                   "flow:from_server.");
223
7
        return -1;
224
7
    }
225
226
71.9k
    if (SCDetectBufferSetActiveList(de_ctx, s, DetectBufferTypeGetByName("file_data")) < 0)
227
67
        return -1;
228
229
71.8k
    s->init_data->init_flags |= SIG_FLAG_INIT_FILEDATA;
230
71.8k
    if ((s->init_data->init_flags & SIG_FLAG_INIT_FORCE_TOCLIENT) == 0) {
231
        // we cannot use a transactional rule with a fast pattern to client and this
232
71.7k
        if (s->init_data->init_flags & SIG_FLAG_INIT_TXDIR_FAST_TOCLIENT) {
233
1
            SCLogError("fast_pattern cannot be used on to_client keyword for "
234
1
                       "transactional rule with a streaming buffer to server %u",
235
1
                    s->id);
236
1
            return -1;
237
1
        }
238
71.7k
        s->init_data->init_flags |= SIG_FLAG_INIT_TXDIR_STREAMING_TOSERVER;
239
71.7k
    }
240
241
71.8k
    SetupDetectEngineConfig(de_ctx);
242
71.8k
    return 0;
243
71.8k
}
244
245
static void DetectFiledataSetupCallback(const DetectEngineCtx *de_ctx,
246
                                        Signature *s)
247
49.3k
{
248
49.3k
    if (s->alproto == ALPROTO_HTTP1 || s->alproto == ALPROTO_UNKNOWN ||
249
45.7k
            s->alproto == ALPROTO_HTTP) {
250
45.7k
        AppLayerHtpEnableResponseBodyCallback();
251
45.7k
    }
252
253
    /* server body needs to be inspected in sync with stream if possible */
254
49.3k
    s->init_data->init_flags |= SIG_FLAG_INIT_NEED_FLUSH;
255
256
49.3k
    SCLogDebug("callback invoked by %u", s->id);
257
49.3k
}
258
259
/* common */
260
261
static void PrefilterMpmFiledataFree(void *ptr)
262
76.5k
{
263
76.5k
    SCFree(ptr);
264
76.5k
}
265
266
/* file API based inspection */
267
268
static inline InspectionBuffer *FiledataWithXformsGetDataCallback(DetectEngineThreadCtx *det_ctx,
269
        const DetectEngineTransforms *transforms, const int list_id, int local_file_id,
270
        InspectionBuffer *base_buffer)
271
18.9k
{
272
18.9k
    InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_file_id);
273
18.9k
    if (buffer == NULL) {
274
0
        SCLogDebug("list_id: %d: no buffer", list_id);
275
0
        return NULL;
276
0
    }
277
18.9k
    if (buffer->initialized) {
278
144
        SCLogDebug("list_id: %d: returning %p", list_id, buffer);
279
144
        return buffer;
280
144
    }
281
282
18.8k
    InspectionBufferSetupMulti(
283
18.8k
            det_ctx, buffer, transforms, base_buffer->inspect, base_buffer->inspect_len);
284
18.8k
    buffer->inspect_offset = base_buffer->inspect_offset;
285
18.8k
    SCLogDebug("xformed buffer %p size %u", buffer, buffer->inspect_len);
286
18.8k
    SCReturnPtr(buffer, "InspectionBuffer");
287
18.9k
}
288
289
static InspectionBuffer *FiledataGetDataCallback(DetectEngineThreadCtx *det_ctx,
290
        const DetectEngineTransforms *transforms, Flow *f, uint8_t flow_flags, File *cur_file,
291
        const int list_id, const int base_id, int local_file_id, void *txv)
292
32.8k
{
293
32.8k
    SCEnter();
294
32.8k
    SCLogDebug("starting: list_id %d base_id %d", list_id, base_id);
295
296
32.8k
    InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, base_id, local_file_id);
297
32.8k
    SCLogDebug("base: buffer %p", buffer);
298
32.8k
    if (buffer == NULL)
299
0
        return NULL;
300
32.8k
    if (base_id != list_id && buffer->inspect != NULL) {
301
524
        SCLogDebug("handle xform %s", (list_id != base_id) ? "true" : "false");
302
524
        return FiledataWithXformsGetDataCallback(
303
524
                det_ctx, transforms, list_id, local_file_id, buffer);
304
524
    }
305
32.3k
    if (buffer->initialized) {
306
2.68k
        SCLogDebug("base_id: %d, not first: use %p", base_id, buffer);
307
2.68k
        return buffer;
308
2.68k
    }
309
310
29.6k
    const uint64_t file_size = FileDataSize(cur_file);
311
29.6k
    const DetectEngineCtx *de_ctx = det_ctx->de_ctx;
312
29.6k
    uint32_t content_limit = FILEDATA_CONTENT_LIMIT;
313
29.6k
    uint32_t content_inspect_min_size = FILEDATA_CONTENT_INSPECT_MIN_SIZE;
314
29.6k
    if (de_ctx->filedata_config) {
315
28.8k
        content_limit = de_ctx->filedata_config[f->alproto].content_limit;
316
28.8k
        content_inspect_min_size = de_ctx->filedata_config[f->alproto].content_inspect_min_size;
317
28.8k
    }
318
319
29.6k
    SCLogDebug("[list %d] content_limit %u, content_inspect_min_size %u", list_id, content_limit,
320
29.6k
            content_inspect_min_size);
321
322
29.6k
    SCLogDebug("[list %d] file %p size %" PRIu64 ", state %d", list_id, cur_file, file_size,
323
29.6k
            cur_file->state);
324
325
    /* no new data */
326
29.6k
    if (cur_file->content_inspected == file_size) {
327
343
        SCLogDebug("no new data");
328
343
        goto empty_return;
329
343
    }
330
331
29.3k
    if (file_size == 0) {
332
0
        SCLogDebug("no data to inspect for this transaction");
333
0
        goto empty_return;
334
0
    }
335
336
29.3k
    SCLogDebug("offset %" PRIu64, StreamingBufferGetOffset(cur_file->sb));
337
29.3k
    SCLogDebug("size %" PRIu64, cur_file->size);
338
29.3k
    SCLogDebug("content_inspected %" PRIu64, cur_file->content_inspected);
339
29.3k
    SCLogDebug("inspect_window %" PRIu32, cur_file->inspect_window);
340
29.3k
    SCLogDebug("inspect_min_size %" PRIu32, cur_file->inspect_min_size);
341
342
29.3k
    bool ips = false;
343
29.3k
    uint64_t offset = 0;
344
29.3k
    if (f->alproto == ALPROTO_HTTP1) {
345
346
26.5k
        htp_tx_t *tx = txv;
347
26.5k
        HtpState *htp_state = f->alstate;
348
26.5k
        ips = htp_state->cfg->http_body_inline;
349
350
26.5k
        const bool body_done = AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx,
351
26.5k
                                       flow_flags) > HTP_RESPONSE_PROGRESS_BODY;
352
353
26.5k
        SCLogDebug("response.body_limit %u file_size %" PRIu64
354
26.5k
                   ", cur_file->inspect_min_size %" PRIu32 ", EOF %s, progress > body? %s",
355
26.5k
                htp_state->cfg->response.body_limit, file_size, cur_file->inspect_min_size,
356
26.5k
                flow_flags & STREAM_EOF ? "true" : "false", BOOL2STR(body_done));
357
358
26.5k
        if (!htp_state->cfg->http_body_inline) {
359
            /* inspect the body if the transfer is complete or we have hit
360
             * our body size limit */
361
26.5k
            if ((htp_state->cfg->response.body_limit == 0 ||
362
26.5k
                        file_size < htp_state->cfg->response.body_limit) &&
363
26.5k
                    file_size < cur_file->inspect_min_size && !body_done &&
364
6.73k
                    !(flow_flags & STREAM_EOF)) {
365
6.50k
                SCLogDebug("we still haven't seen the entire response body.  "
366
6.50k
                           "Let's defer body inspection till we see the "
367
6.50k
                           "entire body.");
368
6.50k
                goto empty_return;
369
6.50k
            }
370
20.0k
            SCLogDebug("inline and we're continuing");
371
20.0k
        }
372
373
26.5k
        bool force = (flow_flags & STREAM_EOF) || (cur_file->state > FILE_STATE_OPENED) ||
374
4.34k
                     body_done || htp_state->cfg->http_body_inline;
375
        /* get the inspect buffer
376
         *
377
         * make sure that we have at least the configured inspect_win size.
378
         * If we have more, take at least 1/4 of the inspect win size before
379
         * the new data.
380
         */
381
20.0k
        if (cur_file->content_inspected == 0) {
382
15.8k
            if (!force && file_size < cur_file->inspect_min_size) {
383
3
                SCLogDebug("skip as file_size %" PRIu64 " < inspect_min_size %u", file_size,
384
3
                        cur_file->inspect_min_size);
385
3
                goto empty_return;
386
3
            }
387
15.8k
        } else {
388
4.21k
            uint64_t new_data = file_size - cur_file->content_inspected;
389
4.21k
            DEBUG_VALIDATE_BUG_ON(new_data == 0);
390
4.21k
            if (new_data < cur_file->inspect_window) {
391
3.97k
                uint64_t inspect_short = cur_file->inspect_window - new_data;
392
3.97k
                if (cur_file->content_inspected < inspect_short) {
393
0
                    offset = 0;
394
0
                    SCLogDebug("offset %" PRIu64, offset);
395
3.97k
                } else {
396
3.97k
                    offset = cur_file->content_inspected - inspect_short;
397
3.97k
                    SCLogDebug("offset %" PRIu64, offset);
398
3.97k
                }
399
3.97k
            } else {
400
235
                BUG_ON(cur_file->content_inspected == 0);
401
235
                uint32_t margin = cur_file->inspect_window / 4;
402
235
                if ((uint64_t)margin <= cur_file->content_inspected) {
403
235
                    offset = cur_file->content_inspected - (cur_file->inspect_window / 4);
404
235
                } else {
405
0
                    offset = 0;
406
0
                }
407
235
                SCLogDebug("offset %" PRIu64 " (data from offset %" PRIu64 ")", offset,
408
235
                        file_size - offset);
409
235
            }
410
4.21k
        }
411
412
20.0k
    } else {
413
2.78k
        if ((content_limit == 0 || file_size < content_limit) &&
414
2.62k
                file_size < content_inspect_min_size && !(flow_flags & STREAM_EOF) &&
415
2.38k
                !(cur_file->state > FILE_STATE_OPENED)) {
416
703
            SCLogDebug("we still haven't seen the entire content. "
417
703
                       "Let's defer content inspection till we see the "
418
703
                       "entire content. We've seen %ld and need at least %d",
419
703
                    file_size, content_inspect_min_size);
420
703
            goto empty_return;
421
703
        }
422
2.07k
        offset = cur_file->content_inspected;
423
2.07k
    }
424
425
22.1k
    const uint8_t *data;
426
22.1k
    uint32_t data_len;
427
428
22.1k
    SCLogDebug("Fetching data at offset: %ld", offset);
429
22.1k
    StreamingBufferGetDataAtOffset(cur_file->sb, &data, &data_len, offset);
430
22.1k
    SCLogDebug("data_len %u", data_len);
431
    /* update inspected tracker */
432
22.1k
    buffer->inspect_offset = offset;
433
434
22.1k
    if (ips && file_size < cur_file->inspect_min_size) {
435
        // don't update content_inspected yet
436
22.1k
    } else {
437
22.1k
        SCLogDebug("content inspected: %" PRIu64, cur_file->content_inspected);
438
22.1k
        cur_file->content_inspected = MAX(cur_file->content_inspected, offset + data_len);
439
22.1k
        SCLogDebug("content inspected: %" PRIu64, cur_file->content_inspected);
440
22.1k
    }
441
442
22.1k
    InspectionBufferSetupMulti(det_ctx, buffer, NULL, data, data_len);
443
22.1k
    SCLogDebug("[list %d] [before] buffer offset %" PRIu64 "; buffer len %" PRIu32
444
22.1k
               "; data_len %" PRIu32 "; file_size %" PRIu64,
445
22.1k
            list_id, buffer->inspect_offset, buffer->inspect_len, data_len, file_size);
446
447
22.1k
    if (f->alproto == ALPROTO_HTTP1 && flow_flags & STREAM_TOCLIENT) {
448
19.3k
        HtpState *htp_state = f->alstate;
449
        /* built-in 'transformation' */
450
19.3k
        if (htp_state->cfg->swf_decompression_enabled) {
451
19.3k
            int swf_file_type = FileIsSwfFile(data, data_len);
452
19.3k
            if (swf_file_type == FILE_SWF_ZLIB_COMPRESSION ||
453
19.3k
                    swf_file_type == FILE_SWF_LZMA_COMPRESSION) {
454
4
                SCLogDebug("decompressing ...");
455
4
                (void)FileSwfDecompression(data, data_len, det_ctx, buffer,
456
4
                        htp_state->cfg->swf_compression_type, htp_state->cfg->swf_decompress_depth,
457
4
                        htp_state->cfg->swf_compress_depth);
458
4
                SCLogDebug("uncompressed buffer %p size %u; buf: \"%s\"", buffer,
459
4
                        buffer->inspect_len, (char *)buffer->inspect);
460
4
            }
461
19.3k
        }
462
19.3k
    }
463
464
22.1k
    SCLogDebug("content inspected: %" PRIu64, cur_file->content_inspected);
465
466
    /* get buffer for the list id if it is different from the base id */
467
22.1k
    if (list_id != base_id) {
468
18.4k
        SCLogDebug("regular %d has been set up: now handle xforms id %d", base_id, list_id);
469
18.4k
        InspectionBuffer *tbuffer = FiledataWithXformsGetDataCallback(
470
18.4k
                det_ctx, transforms, list_id, local_file_id, buffer);
471
18.4k
        SCReturnPtr(tbuffer, "InspectionBuffer");
472
18.4k
    }
473
22.1k
    SCReturnPtr(buffer, "InspectionBuffer");
474
475
7.55k
empty_return:
476
7.55k
    InspectionBufferSetupMultiEmpty(buffer);
477
7.55k
    return NULL;
478
22.1k
}
479
480
uint8_t DetectEngineInspectFiledata(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
481
        const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
482
        void *alstate, void *txv, uint64_t tx_id)
483
0
{
484
0
    const DetectEngineTransforms *transforms = NULL;
485
0
    if (!engine->mpm) {
486
0
        transforms = engine->v2.transforms;
487
0
    }
488
489
0
    AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
490
0
    FileContainer *ffc = files.fc;
491
0
    if (ffc == NULL) {
492
0
        return DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES;
493
0
    }
494
0
    if (ffc->head == NULL) {
495
0
        const bool eof = (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) >
496
0
                          engine->progress);
497
0
        if (eof && engine->match_on_null) {
498
0
            return DETECT_ENGINE_INSPECT_SIG_MATCH;
499
0
        }
500
0
        return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
501
0
    }
502
503
0
    int local_file_id = 0;
504
0
    File *file = ffc->head;
505
0
    for (; file != NULL; file = file->next) {
506
0
        InspectionBuffer *buffer = FiledataGetDataCallback(det_ctx, transforms, f, flags, file,
507
0
                engine->sm_list, engine->sm_list_base, local_file_id, txv);
508
0
        if (buffer == NULL) {
509
0
            local_file_id++;
510
0
            continue;
511
0
        }
512
513
0
        bool eof = (file->state == FILE_STATE_CLOSED);
514
0
        uint8_t ciflags = eof ? DETECT_CI_FLAGS_END : 0;
515
0
        if (buffer->inspect_offset == 0)
516
0
            ciflags |= DETECT_CI_FLAGS_START;
517
518
0
        const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
519
0
                buffer->inspect, buffer->inspect_len, buffer->inspect_offset, ciflags,
520
0
                DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE);
521
0
        if (match) {
522
0
            return DETECT_ENGINE_INSPECT_SIG_MATCH;
523
0
        }
524
0
        local_file_id++;
525
0
    }
526
527
0
    return DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
528
0
}
529
530
/** \brief Filedata Filedata Mpm prefilter callback
531
 *
532
 *  \param det_ctx detection engine thread ctx
533
 *  \param pectx inspection context
534
 *  \param p packet to inspect
535
 *  \param f flow to inspect
536
 *  \param txv tx to inspect
537
 *  \param idx transaction id
538
 *  \param flags STREAM_* flags including direction
539
 */
540
static void PrefilterTxFiledata(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
541
        Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *txd, const uint8_t flags)
542
58.1k
{
543
58.1k
    SCEnter();
544
545
58.1k
    if (!AppLayerParserHasFilesInDir(txd, flags))
546
40.8k
        return;
547
548
17.3k
    const PrefilterMpmFiledata *ctx = (const PrefilterMpmFiledata *)pectx;
549
17.3k
    const MpmCtx *mpm_ctx = ctx->mpm_ctx;
550
17.3k
    const int list_id = ctx->list_id;
551
552
17.3k
    AppLayerGetFileState files = AppLayerParserGetTxFiles(f, txv, flags);
553
17.3k
    FileContainer *ffc = files.fc;
554
17.3k
    if (ffc != NULL) {
555
17.3k
        int local_file_id = 0;
556
30.8k
        for (File *file = ffc->head; file != NULL; file = file->next) {
557
13.4k
            InspectionBuffer *buffer = FiledataGetDataCallback(det_ctx, ctx->transforms, f, flags,
558
13.4k
                    file, list_id, ctx->base_list_id, local_file_id, txv);
559
13.4k
            if (buffer == NULL) {
560
4.58k
                local_file_id++;
561
4.58k
                continue;
562
4.58k
            }
563
8.88k
            SCLogDebug("[%" PRIu64 "] buffer size %u", p->pcap_cnt, buffer->inspect_len);
564
565
8.88k
            if (buffer->inspect_len >= mpm_ctx->minlen) {
566
6.95k
                uint32_t prev_rule_id_array_cnt = det_ctx->pmq.rule_id_array_cnt;
567
6.95k
                (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx, &det_ctx->mtc, &det_ctx->pmq,
568
6.95k
                        buffer->inspect, buffer->inspect_len);
569
6.95k
                PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
570
571
6.95k
                if (det_ctx->pmq.rule_id_array_cnt > prev_rule_id_array_cnt) {
572
956
                    SCLogDebug(
573
956
                            "%u matches", det_ctx->pmq.rule_id_array_cnt - prev_rule_id_array_cnt);
574
956
                }
575
6.95k
            }
576
8.88k
            local_file_id++;
577
8.88k
        }
578
17.3k
    }
579
17.3k
}
580
581
int PrefilterMpmFiledataRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
582
        const DetectBufferMpmRegistry *mpm_reg, int list_id)
583
77.3k
{
584
77.3k
    PrefilterMpmFiledata *pectx = SCCalloc(1, sizeof(*pectx));
585
77.3k
    if (pectx == NULL)
586
0
        return -1;
587
77.3k
    pectx->list_id = list_id;
588
77.3k
    pectx->base_list_id = mpm_reg->sm_list_base;
589
77.3k
    pectx->mpm_ctx = mpm_ctx;
590
77.3k
    pectx->transforms = &mpm_reg->transforms;
591
592
77.3k
    return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTxFiledata,
593
77.3k
            mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
594
77.3k
            pectx, PrefilterMpmFiledataFree, mpm_reg->pname);
595
77.3k
}
596
597
#ifdef UNITTESTS
598
#include "tests/detect-file-data.c"
599
#endif