Coverage Report

Created: 2026-05-16 07:38

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