• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23350763619

20 Mar 2026 03:49PM UTC coverage: 76.429% (-2.9%) from 79.315%
23350763619

Pull #15029

github

web-flow
Merge 5fca25ef1 into 6587e363a
Pull Request #15029: Fw updates/v5

379 of 449 new or added lines in 26 files covered. (84.41%)

13267 existing lines in 297 files now uncovered.

244170 of 319471 relevant lines covered (76.43%)

3147764.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.25
/src/app-layer-frames.c
1
/* Copyright (C) 2007-2024 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 "util-print.h"
27

28
#include "flow.h"
29
#include "stream-tcp.h"
30
#include "rust.h"
31
#include "app-layer-frames.h"
32
#include "app-layer-parser.h"
33

34
struct FrameConfig {
35
    SC_ATOMIC_DECLARE(uint64_t, types);
36
};
37
/* This array should be allocated to contain g_alproto_max protocols. */
38
static struct FrameConfig *frame_config;
39

40
void FrameConfigInit(void)
41
{
38✔
42
    frame_config = SCCalloc(g_alproto_max, sizeof(struct FrameConfig));
38✔
43
    if (unlikely(frame_config == NULL)) {
38✔
44
        FatalError("Unable to alloc frame_config.");
×
45
    }
×
46
    for (AppProto p = 0; p < g_alproto_max; p++) {
1,558✔
47
        SC_ATOMIC_INIT(frame_config[p].types);
1,520✔
48
    }
1,520✔
49
}
38✔
50

51
void FrameConfigDeInit(void)
52
{
24✔
53
    SCFree(frame_config);
24✔
54
}
24✔
55

56
void FrameConfigEnableAll(void)
UNCOV
57
{
×
UNCOV
58
    const uint64_t bits = UINT64_MAX;
×
UNCOV
59
    for (AppProto p = 0; p < g_alproto_max; p++) {
×
UNCOV
60
        struct FrameConfig *fc = &frame_config[p];
×
UNCOV
61
        SC_ATOMIC_OR(fc->types, bits);
×
UNCOV
62
    }
×
UNCOV
63
}
×
64

65
void FrameConfigEnable(const AppProto p, const uint8_t type)
66
{
36,422✔
67
    const uint64_t bits = BIT_U64(type);
36,422✔
68
    struct FrameConfig *fc = &frame_config[p];
36,422✔
69
    SC_ATOMIC_OR(fc->types, bits);
36,422✔
70
}
36,422✔
71

72
static inline bool FrameConfigTypeIsEnabled(const AppProto p, const uint8_t type)
73
{
3,014,266✔
74
    struct FrameConfig *fc = &frame_config[p];
3,014,266✔
75
    const uint64_t bits = BIT_U64(type);
3,014,266✔
76
    const bool enabled = (SC_ATOMIC_GET(fc->types) & bits) != 0;
3,014,266✔
77
    return enabled;
3,014,266✔
78
}
3,014,266✔
79

80
#ifdef DEBUG
81
static void FrameDebug(const char *prefix, const Frames *frames, const Frame *frame)
82
{
83
    const char *type_name = "unknown";
84
    if (frame->type == FRAME_STREAM_TYPE) {
85
        type_name = "stream";
86
    } else if (frames != NULL) {
87
        type_name = AppLayerParserGetFrameNameById(frames->ipproto, frames->alproto, frame->type);
88
    }
89
    SCLogDebug("[%s] %p: frame:%p type:%u/%s id:%" PRIi64 " flags:%02x offset:%" PRIu64
90
               ", len:%" PRIi64 ", inspect_progress:%" PRIu64 ", events:%u %u/%u/%u/%u",
91
            prefix, frames, frame, frame->type, type_name, frame->id, frame->flags, frame->offset,
92
            frame->len, frame->inspect_progress, frame->event_cnt, frame->events[0],
93
            frame->events[1], frame->events[2], frame->events[3]);
94
}
95
#else
96
#define FrameDebug(prefix, frames, frame)
97
#endif
98

99
/**
100
 * \note "open" means a frame that has no length set (len == -1)
101
 * \todo perhaps we can search backwards */
102
Frame *FrameGetLastOpenByType(Frames *frames, const uint8_t frame_type)
103
{
311,277✔
104
    Frame *candidate = NULL;
311,277✔
105

106
    SCLogDebug(
311,277✔
107
            "frames %p cnt %u, looking for last of type %" PRIu8, frames, frames->cnt, frame_type);
311,277✔
108
    for (uint16_t i = 0; i < frames->cnt; i++) {
1,552,002✔
109
        if (i < FRAMES_STATIC_CNT) {
1,240,725✔
110
            Frame *frame = &frames->sframes[i];
606,117✔
111
            FrameDebug("get_by_id(static)", frames, frame);
606,117✔
112
            if (frame->type == frame_type && frame->len == -1)
606,117✔
113
                candidate = frame;
8,159✔
114
        } else {
634,608✔
115
            const uint16_t o = i - FRAMES_STATIC_CNT;
634,608✔
116
            Frame *frame = &frames->dframes[o];
634,608✔
117
            FrameDebug("get_by_id(dynamic)", frames, frame);
634,608✔
118
            if (frame->type == frame_type && frame->len == -1)
634,608✔
119
                candidate = frame;
15✔
120
        }
634,608✔
121
    }
1,240,725✔
122
    return candidate;
311,277✔
123
}
311,277✔
124

125
Frame *FrameGetById(Frames *frames, const int64_t id)
126
{
149,232✔
127
    SCLogDebug("frames %p cnt %u, looking for %" PRIi64, frames, frames->cnt, id);
149,232✔
128
    for (uint16_t i = 0; i < frames->cnt; i++) {
1,971,442✔
129
        if (i < FRAMES_STATIC_CNT) {
1,971,332✔
130
            Frame *frame = &frames->sframes[i];
367,632✔
131
            FrameDebug("get_by_id(static)", frames, frame);
367,632✔
132
            if (frame->id == id)
367,632✔
133
                return frame;
68,390✔
134
        } else {
1,603,700✔
135
            const uint16_t o = i - FRAMES_STATIC_CNT;
1,603,700✔
136
            Frame *frame = &frames->dframes[o];
1,603,700✔
137
            FrameDebug("get_by_id(dynamic)", frames, frame);
1,603,700✔
138
            if (frame->id == id)
1,603,700✔
139
                return frame;
80,732✔
140
        }
1,603,700✔
141
    }
1,971,332✔
142
    return NULL;
110✔
143
}
149,232✔
144

145
Frame *FrameGetByIndex(Frames *frames, const uint32_t idx)
146
{
11,168,001✔
147
    if (idx >= frames->cnt)
11,168,001✔
148
        return NULL;
×
149

150
    if (idx < FRAMES_STATIC_CNT) {
11,168,001✔
151
        Frame *frame = &frames->sframes[idx];
797,245✔
152
        FrameDebug("get_by_idx(s)", frames, frame);
797,245✔
153
        return frame;
797,245✔
154
    } else {
10,370,756✔
155
        const uint32_t o = idx - FRAMES_STATIC_CNT;
10,370,756✔
156
        Frame *frame = &frames->dframes[o];
10,370,756✔
157
        FrameDebug("get_by_idx(d)", frames, frame);
10,370,756✔
158
        return frame;
10,370,756✔
159
    }
10,370,756✔
160
}
11,168,001✔
161

162
static Frame *FrameNew(Frames *frames, uint64_t offset, int64_t len)
163
{
1,245,077✔
164
    DEBUG_VALIDATE_BUG_ON(frames == NULL);
1,245,077✔
165

166
    if (frames->cnt < FRAMES_STATIC_CNT) {
1,245,077✔
167
        Frame *frame = &frames->sframes[frames->cnt];
723,200✔
168
        frames->sframes[frames->cnt].offset = offset;
723,200✔
169
        frames->sframes[frames->cnt].len = len;
723,200✔
170
        frames->sframes[frames->cnt].id = ++frames->base_id;
723,200✔
171
        frames->cnt++;
723,200✔
172
        return frame;
723,200✔
173
    } else if (frames->dframes == NULL) {
723,200✔
174
        DEBUG_VALIDATE_BUG_ON(frames->dyn_size != 0);
8,820✔
175
        DEBUG_VALIDATE_BUG_ON(frames->cnt != FRAMES_STATIC_CNT);
8,820✔
176

177
        frames->dframes = SCCalloc(8, sizeof(Frame));
8,820✔
178
        if (frames->dframes == NULL) {
8,820✔
179
            return NULL;
×
180
        }
×
181
        frames->cnt++;
8,820✔
182
        DEBUG_VALIDATE_BUG_ON(frames->cnt != FRAMES_STATIC_CNT + 1);
8,820✔
183

184
        frames->dyn_size = 8;
8,820✔
185
        frames->dframes[0].offset = offset;
8,820✔
186
        frames->dframes[0].len = len;
8,820✔
187
        frames->dframes[0].id = ++frames->base_id;
8,820✔
188
        return &frames->dframes[0];
8,820✔
189
    } else {
513,057✔
190
        DEBUG_VALIDATE_BUG_ON(frames->cnt < FRAMES_STATIC_CNT);
513,057✔
191

192
        /* need to handle dynamic storage of frames now */
193
        const uint16_t dyn_cnt = frames->cnt - FRAMES_STATIC_CNT;
513,057✔
194
        if (dyn_cnt < frames->dyn_size) {
513,057✔
195
            DEBUG_VALIDATE_BUG_ON(frames->dframes == NULL);
403,488✔
196

197
            // fall through
198
        } else {
403,488✔
199
            if (frames->dyn_size == 256) {
109,569✔
200
                SCLogDebug("limit reached! 256 dynamic frames already");
102,097✔
201
                // limit reached
202
                // TODO figure out if this should lead to an event of sorts
203
                return NULL;
102,097✔
204
            }
102,097✔
205

206
            /* realloc time */
207
            uint16_t new_dyn_size = frames->dyn_size * 2;
7,472✔
208
            uint32_t new_alloc_size = new_dyn_size * sizeof(Frame);
7,472✔
209

210
            void *ptr = SCRealloc(frames->dframes, new_alloc_size);
7,472✔
211
            if (ptr == NULL) {
7,472✔
212
                return NULL;
×
213
            }
×
214

215
            memset((uint8_t *)ptr + (frames->dyn_size * sizeof(Frame)), 0x00,
7,472✔
216
                    (frames->dyn_size * sizeof(Frame)));
7,472✔
217
            frames->dframes = ptr;
7,472✔
218
            frames->dyn_size = new_dyn_size;
7,472✔
219
        }
7,472✔
220

221
        frames->cnt++;
410,960✔
222
        frames->dframes[dyn_cnt].offset = offset;
410,960✔
223
        frames->dframes[dyn_cnt].len = len;
410,960✔
224
        frames->dframes[dyn_cnt].id = ++frames->base_id;
410,960✔
225
        return &frames->dframes[dyn_cnt];
410,960✔
226
    }
513,057✔
227
}
1,245,077✔
228

229
static void FrameClean(Frame *frame)
230
{
1,161,256✔
231
    memset(frame, 0, sizeof(*frame));
1,161,256✔
232
}
1,161,256✔
233

234
static void FrameCopy(Frame *dst, Frame *src)
235
{
13,986,477✔
236
    memcpy(dst, src, sizeof(*dst));
13,986,477✔
237
}
13,986,477✔
238

239
#ifdef DEBUG
240
static void AppLayerFrameDumpForFrames(const char *prefix, const Frames *frames)
241
{
242
    SCLogDebug("prefix: %s", prefix);
243
    for (uint16_t i = 0; i < frames->cnt; i++) {
244
        if (i < FRAMES_STATIC_CNT) {
245
            const Frame *frame = &frames->sframes[i];
246
            FrameDebug(prefix, frames, frame);
247
        } else {
248
            const uint16_t o = i - FRAMES_STATIC_CNT;
249
            const Frame *frame = &frames->dframes[o];
250
            FrameDebug(prefix, frames, frame);
251
        }
252
    }
253
    SCLogDebug("prefix: %s", prefix);
254
}
255
#endif
256

257
static inline uint64_t FrameLeftEdge(const TcpStream *stream, const Frame *frame)
258
{
14,422,017✔
259
    const int64_t app_progress = STREAM_APP_PROGRESS(stream);
14,422,017✔
260

261
    const int64_t frame_offset = frame->offset;
14,422,017✔
262
    const int64_t frame_data = app_progress - frame_offset;
14,422,017✔
263

264
    SCLogDebug("frame_offset %" PRIi64 ", frame_data %" PRIi64 ", frame->len %" PRIi64,
14,422,017✔
265
            frame_offset, frame_data, frame->len);
14,422,017✔
266
    DEBUG_VALIDATE_BUG_ON(frame_offset > app_progress);
14,422,017✔
267

268
    /* length unknown, make sure to have at least 2500 */
269
    if (frame->len < 0) {
14,422,017✔
270
        if (frame_data <= 2500) {
201,197✔
271
            SCLogDebug("got <= 2500 bytes (%" PRIu64 "), returning offset %" PRIu64, frame_data,
125,237✔
272
                    frame_offset);
125,237✔
273
            return frame_offset;
125,237✔
274
        } else {
125,237✔
275
            SCLogDebug("got > 2500 bytes (%" PRIu64 "), returning offset %" PRIu64, frame_data,
75,960✔
276
                    (frame_offset + (frame_data - 2500)));
75,960✔
277
            return frame_offset + (frame_data - 2500);
75,960✔
278
        }
75,960✔
279

280
        /* length specified */
281
    } else {
14,220,820✔
282
        /* have all data for the frame, we can skip it */
283
        if (frame->len <= frame_data) {
14,220,820✔
284
            uint64_t x = frame_offset + frame_data;
14,195,627✔
285
            SCLogDebug("x %" PRIu64, x);
14,195,627✔
286
            return x;
14,195,627✔
287
            /*
288

289
                [ stream      <frame_data> ]
290
                             [ frame        .......]
291

292
             */
293
        } else if (frame_data < 2500) {
14,195,627✔
294
            uint64_t x = frame_offset;
13,995✔
295
            SCLogDebug("x %" PRIu64, x);
13,995✔
296
            return x;
13,995✔
297
        } else {
13,995✔
298
            uint64_t x = frame_offset + (frame_data - 2500);
11,198✔
299
            SCLogDebug("x %" PRIu64, x);
11,198✔
300
            return x;
11,198✔
301
        }
11,198✔
302
    }
14,220,820✔
303
}
14,422,017✔
304

305
/** Stream buffer slides forward, we need to update and age out
306
 *  frame offsets/frames. Aging out means we move existing frames
307
 *  into the slots we'd free up.
308
 *
309
 *  Start:
310
 *
311
 *  [ stream ]
312
 *    [ frame   ...........]
313
 *      offset: 2
314
 *      len: 19
315
 *
316
 *  Slide:
317
 *         [ stream ]
318
 *    [ frame ....          .]
319
 *      offset: 2
320
 *       len: 19
321
 *
322
 *  Slide:
323
 *                [ stream ]
324
 *    [ frame ...........    ]
325
 *      offset: 2
326
 *      len: 19
327
 */
328
static int FrameSlide(const char *ds, Frames *frames, const TcpStream *stream, const uint32_t slide)
329
{
15,062✔
330
    SCLogDebug("start: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
15,062✔
331
               ", next %" PRIu64,
15,062✔
332
            (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
15,062✔
333
            STREAM_BASE_OFFSET(stream), STREAM_BASE_OFFSET(stream) + slide);
15,062✔
334
    DEBUG_VALIDATE_BUG_ON(frames == NULL);
15,062✔
335
    SCLogDebug("%s frames %p: sliding %u bytes", ds, frames, slide);
15,062✔
336
    uint64_t le = STREAM_APP_PROGRESS(stream);
15,062✔
337
    const uint64_t next_base = STREAM_BASE_OFFSET(stream) + slide;
15,062✔
338
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
339
    const uint16_t start = frames->cnt;
340
    uint16_t removed = 0;
341
#endif
342
    uint16_t x = 0;
15,062✔
343
    for (uint16_t i = 0; i < frames->cnt; i++) {
456,818✔
344
        if (i < FRAMES_STATIC_CNT) {
441,756✔
345
            Frame *frame = &frames->sframes[i];
11,280✔
346
            FrameDebug("slide(s)", frames, frame);
11,280✔
347
            if (frame->len >= 0 && frame->offset + frame->len <= next_base) {
11,280✔
348
                // remove by not incrementing 'x'
349
                SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
189✔
350
                FrameClean(frame);
189✔
351
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
352
                removed++;
353
#endif
354
            } else {
11,091✔
355
                Frame *nframe = &frames->sframes[x];
11,091✔
356
                FrameCopy(nframe, frame);
11,091✔
357
                if (frame != nframe) {
11,091✔
358
                    FrameClean(frame);
10✔
359
                }
10✔
360
                le = MIN(le, FrameLeftEdge(stream, nframe));
11,091✔
361
                x++;
11,091✔
362
            }
11,091✔
363
        } else {
430,476✔
364
            const uint16_t o = i - FRAMES_STATIC_CNT;
430,476✔
365
            Frame *frame = &frames->dframes[o];
430,476✔
366
            FrameDebug("slide(d)", frames, frame);
430,476✔
367
            if (frame->len >= 0 && frame->offset + frame->len <= next_base) {
430,476✔
368
                // remove by not incrementing 'x'
369
                SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
5,837✔
370
                FrameClean(frame);
5,837✔
371
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
372
                removed++;
373
#endif
374
            } else {
424,639✔
375
                Frame *nframe;
424,639✔
376
                if (x >= FRAMES_STATIC_CNT) {
424,639✔
377
                    nframe = &frames->dframes[x - FRAMES_STATIC_CNT];
424,478✔
378
                } else {
424,478✔
379
                    nframe = &frames->sframes[x];
161✔
380
                }
161✔
381
                FrameCopy(nframe, frame);
424,639✔
382
                if (frame != nframe) {
424,639✔
383
                    FrameClean(frame);
6,323✔
384
                }
6,323✔
385
                le = MIN(le, FrameLeftEdge(stream, nframe));
424,639✔
386
                x++;
424,639✔
387
            }
424,639✔
388
        }
430,476✔
389
    }
441,756✔
390
    frames->cnt = x;
15,062✔
391
    uint64_t o = STREAM_BASE_OFFSET(stream) + slide;
15,062✔
392
    DEBUG_VALIDATE_BUG_ON(o > le);
15,062✔
393
    DEBUG_VALIDATE_BUG_ON(le - o > UINT32_MAX);
15,062✔
394
    frames->left_edge_rel = (uint32_t)(le - o);
15,062✔
395

396
#ifdef DEBUG
397
    SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
398
               " (+slide), cnt %u, removed %u, start %u",
399
            (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream) + slide,
400
            frames->left_edge_rel, STREAM_BASE_OFFSET(stream) + slide, frames->cnt, removed, start);
401
    char pf[32] = "";
402
    snprintf(pf, sizeof(pf), "%s:post_slide", ds);
403
    AppLayerFrameDumpForFrames(pf, frames);
404
#endif
405
    DEBUG_VALIDATE_BUG_ON(x != start - removed);
15,062✔
406
    return 0;
15,062✔
407
}
15,062✔
408

409
void AppLayerFramesSlide(Flow *f, const uint32_t slide, const uint8_t direction)
410
{
34,392✔
411
    FramesContainer *frames_container = AppLayerFramesGetContainer(f);
34,392✔
412
    if (frames_container == NULL)
34,392✔
413
        return;
19,329✔
414
    Frames *frames;
15,063✔
415
    TcpSession *ssn = f->protoctx;
15,063✔
416
    TcpStream *stream;
15,063✔
417
    if (direction == STREAM_TOSERVER) {
15,063✔
418
        stream = &ssn->client;
6,987✔
419
        frames = &frames_container->toserver;
6,987✔
420
        FrameSlide("toserver", frames, stream, slide);
6,987✔
421
    } else {
8,076✔
422
        stream = &ssn->server;
8,076✔
423
        frames = &frames_container->toclient;
8,076✔
424
        FrameSlide("toclient", frames, stream, slide);
8,076✔
425
    }
8,076✔
426
}
15,063✔
427

428
static void FrameFreeSingleFrame(Frames *frames, Frame *r)
429
{
180,911✔
430
    FrameDebug("free", frames, r);
180,911✔
431
    FrameClean(r);
180,911✔
432
}
180,911✔
433

434
static void FramesClear(Frames *frames)
435
{
82,075✔
436
    DEBUG_VALIDATE_BUG_ON(frames == NULL);
82,075✔
437

438
    SCLogDebug("frames %u", frames->cnt);
82,075✔
439
    for (uint16_t i = 0; i < frames->cnt; i++) {
262,986✔
440
        if (i < FRAMES_STATIC_CNT) {
180,911✔
441
            Frame *r = &frames->sframes[i];
66,235✔
442
            SCLogDebug("removing frame %p", r);
66,235✔
443
            FrameFreeSingleFrame(frames, r);
66,235✔
444
        } else {
114,676✔
445
            const uint16_t o = i - FRAMES_STATIC_CNT;
114,676✔
446
            Frame *r = &frames->dframes[o];
114,676✔
447
            SCLogDebug("removing frame %p", r);
114,676✔
448
            FrameFreeSingleFrame(frames, r);
114,676✔
449
        }
114,676✔
450
    }
180,911✔
451
    frames->cnt = 0;
82,075✔
452
}
82,075✔
453

454
void FramesFree(Frames *frames)
455
{
55,404✔
456
    DEBUG_VALIDATE_BUG_ON(frames == NULL);
55,404✔
457
    FramesClear(frames);
55,404✔
458
    SCFree(frames->dframes);
55,404✔
459
    frames->dframes = NULL;
55,404✔
460
}
55,404✔
461

462
/** \brief create new frame using a pointer to start of the frame
463
 */
464
Frame *AppLayerFrameNewByPointer(Flow *f, const StreamSlice *stream_slice,
465
        const uint8_t *frame_start, const int64_t len, int dir, uint8_t frame_type)
466
{
165,370✔
467
    SCLogDebug("frame_start:%p stream_slice->input:%p stream_slice->offset:%" PRIu64, frame_start,
165,370✔
468
            stream_slice->input, stream_slice->offset);
165,370✔
469

470
    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
165,370✔
471
        return NULL;
112,308✔
472

473
        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
474
#if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
53,071✔
475
    if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
53,071✔
476
        return NULL;
24✔
477
    if (frame_start < stream_slice->input ||
53,047✔
478
            frame_start > stream_slice->input + stream_slice->input_len)
53,047✔
479
        return NULL;
480
#endif
53,047✔
481
    DEBUG_VALIDATE_BUG_ON(frame_start < stream_slice->input);
2,147,536,694✔
482
    DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
2,147,536,694✔
483
    DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
2,147,536,694✔
484

485
    ptrdiff_t ptr_offset = frame_start - stream_slice->input;
2,147,536,694✔
486
#ifdef DEBUG
487
    uint64_t offset = ptr_offset + stream_slice->offset;
488
    SCLogDebug("flow %p direction %s frame %p starting at %" PRIu64 " len %" PRIi64
489
               " (offset %" PRIu64 ")",
490
            f, dir == 0 ? "toserver" : "toclient", frame_start, offset, len, stream_slice->offset);
491
#endif
492
    DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
2,147,536,694✔
493

494
    FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
2,147,536,694✔
495
    if (frames_container == NULL)
2,147,536,694✔
496
        return NULL;
×
497

498
    Frames *frames;
2,147,536,694✔
499
    if (dir == 0) {
2,147,536,694✔
500
        frames = &frames_container->toserver;
24,702✔
501
    } else {
2,147,511,992✔
502
        frames = &frames_container->toclient;
2,147,511,992✔
503
    }
2,147,511,992✔
504

505
    uint64_t abs_frame_offset = stream_slice->offset + ptr_offset;
2,147,536,694✔
506

507
    Frame *r = FrameNew(frames, abs_frame_offset, len);
2,147,536,694✔
508
    if (r != NULL) {
2,147,536,694✔
509
        r->type = frame_type;
52,041✔
510
        FrameDebug("new_by_ptr", frames, r);
52,041✔
511
    }
52,041✔
512
    return r;
2,147,536,694✔
513
}
2,147,536,694✔
514

515
static Frame *AppLayerFrameUdp(
516
        Flow *f, const uint32_t frame_start_rel, const int64_t len, int dir, uint8_t frame_type)
517
{
32,140✔
518
    DEBUG_VALIDATE_BUG_ON(f->proto != IPPROTO_UDP);
32,140✔
519

520
    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
32,140✔
521
        return NULL;
×
522

523
    FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
32,140✔
524
    if (frames_container == NULL)
32,140✔
525
        return NULL;
×
526

527
    Frames *frames;
32,140✔
528
    if (dir == 0) {
32,140✔
529
        frames = &frames_container->toserver;
25,664✔
530
    } else {
25,664✔
531
        frames = &frames_container->toclient;
6,476✔
532
    }
6,476✔
533

534
    Frame *r = FrameNew(frames, frame_start_rel, len);
32,140✔
535
    if (r != NULL) {
32,140✔
536
        r->type = frame_type;
32,140✔
537
    }
32,140✔
538
    return r;
32,140✔
539
}
32,140✔
540

541
/** \brief create new frame using a relative offset from the start of the stream slice
542
 */
543
Frame *SCAppLayerFrameNewByRelativeOffset(Flow *f, const void *ss, const uint32_t frame_start_rel,
544
        const int64_t len, int dir, uint8_t frame_type)
545
{
2,314,899✔
546
    // need to hide StreamSlice argument
547
    // as we cannot bindgen a C function with an argument whose type
548
    // is defined in rust (at least before a suricata_core crate)
549
    const StreamSlice *stream_slice = (const StreamSlice *)ss;
2,314,899✔
550
    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
2,314,899✔
551
        return NULL;
1,165,952✔
552

553
        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
554
#if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
1,148,951✔
555
    if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
1,148,951✔
556
        return NULL;
557
    if (stream_slice->input == NULL)
1,148,951✔
558
        return NULL;
559
#else
560
    DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
2,147,483,647✔
561
#endif
2,147,483,647✔
562
    DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
2,148,632,598✔
563
    DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
2,148,632,598✔
564

565
    if (f->proto == IPPROTO_UDP) {
2,148,632,598✔
566
        return AppLayerFrameUdp(f, frame_start_rel, len, dir, frame_type);
32,140✔
567
    }
32,140✔
568

569
    FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
2,148,600,458✔
570
    if (frames_container == NULL)
2,148,600,458✔
571
        return NULL;
×
572

573
    Frames *frames;
2,148,600,458✔
574
    if (dir == 0) {
2,148,600,458✔
575
        frames = &frames_container->toserver;
555,993✔
576
    } else {
2,148,044,465✔
577
        frames = &frames_container->toclient;
2,148,044,465✔
578
    }
2,148,044,465✔
579

580
    const uint64_t frame_abs_offset = (uint64_t)frame_start_rel + stream_slice->offset;
2,148,600,458✔
581
#ifdef DEBUG_VALIDATION
582
    const TcpSession *ssn = f->protoctx;
583
    const TcpStream *stream = dir == 0 ? &ssn->client : &ssn->server;
584
    BUG_ON(stream_slice->offset != STREAM_APP_PROGRESS(stream));
585
    BUG_ON(frame_abs_offset > STREAM_APP_PROGRESS(stream) + stream_slice->input_len);
586
#endif
587
    Frame *r = FrameNew(frames, frame_abs_offset, len);
2,148,600,458✔
588
    if (r != NULL) {
2,148,600,458✔
589
        r->type = frame_type;
1,015,720✔
590
    }
1,015,720✔
591
    return r;
2,148,600,458✔
592
}
2,148,600,458✔
593

594
void AppLayerFrameDump(Flow *f)
595
{
536,858✔
596
#ifdef DEBUG
597
    if (f->proto == IPPROTO_TCP && f->protoctx && f->alparser) {
598
        FramesContainer *frames_container = AppLayerFramesGetContainer(f);
599
        if (frames_container != NULL) {
600
            AppLayerFrameDumpForFrames("toserver::dump", &frames_container->toserver);
601
            AppLayerFrameDumpForFrames("toclient::dump", &frames_container->toclient);
602
        }
603
    }
604
#endif
605
}
536,858✔
606

607
/** \brief create new frame using the absolute offset from the start of the stream
608
 */
609
Frame *AppLayerFrameNewByAbsoluteOffset(Flow *f, const StreamSlice *stream_slice,
610
        const uint64_t frame_start, const int64_t len, int dir, uint8_t frame_type)
611
{
76,365✔
612
    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
76,365✔
613
        return NULL;
33,280✔
614

615
        /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
616
#if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
43,080✔
617
    if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
43,080✔
618
        return NULL;
619
    if (stream_slice->input == NULL)
43,080✔
620
        return NULL;
1✔
621
#else
622
    DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
5✔
623
#endif
5✔
624
    DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
43,084✔
625
    DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
43,084✔
626
    DEBUG_VALIDATE_BUG_ON(frame_start < stream_slice->offset);
43,084✔
627
    DEBUG_VALIDATE_BUG_ON(frame_start - stream_slice->offset >= (uint64_t)INT_MAX);
43,084✔
628

629
    FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
43,084✔
630
    if (frames_container == NULL)
43,084✔
631
        return NULL;
×
632

633
    Frames *frames;
43,084✔
634
    if (dir == 0) {
43,084✔
635
        frames = &frames_container->toserver;
22,863✔
636
    } else {
22,868✔
637
        frames = &frames_container->toclient;
20,221✔
638
    }
20,221✔
639

640
    SCLogDebug("flow %p direction %s frame type %u offset %" PRIu64 " len %" PRIi64
43,084✔
641
               " (slice offset %" PRIu64 ")",
43,084✔
642
            f, dir == 0 ? "toserver" : "toclient", frame_type, frame_start, len,
43,084✔
643
            stream_slice->offset);
43,084✔
644
    Frame *r = FrameNew(frames, frame_start, len);
43,084✔
645
    if (r != NULL) {
43,084✔
646
        r->type = frame_type;
43,079✔
647
    }
43,079✔
648
    return r;
43,084✔
649
}
43,084✔
650

651
void AppLayerFrameAddEvent(Frame *r, uint8_t e)
652
{
143✔
653
    if (r != NULL) {
143✔
654
        if (r->event_cnt < 4) { // TODO
143✔
655
            r->events[r->event_cnt++] = e;
143✔
656
        }
143✔
657
        FrameDebug("add_event", NULL, r);
143✔
658
    }
143✔
659
}
143✔
660

661
void SCAppLayerFrameAddEventById(const Flow *f, const int dir, const FrameId id, uint8_t e)
662
{
143✔
663
    Frame *frame = AppLayerFrameGetById(f, dir, id);
143✔
664
    AppLayerFrameAddEvent(frame, e);
143✔
665
}
143✔
666

667
void AppLayerFrameSetLength(Frame *frame, int64_t len)
668
{
43✔
669
    if (frame != NULL) {
43✔
670
        frame->len = len;
43✔
671
        FrameDebug("set_length", NULL, frame);
43✔
672
    }
43✔
673
}
43✔
674

675
void SCAppLayerFrameSetLengthById(const Flow *f, const int dir, const FrameId id, int64_t len)
676
{
43✔
677
    Frame *frame = AppLayerFrameGetById(f, dir, id);
43✔
678
    AppLayerFrameSetLength(frame, len);
43✔
679
}
43✔
680

681
void AppLayerFrameSetTxId(Frame *r, uint64_t tx_id)
682
{
162,662✔
683
    if (r != NULL) {
162,662✔
684
        r->flags |= FRAME_FLAG_TX_ID_SET;
162,662✔
685
        r->tx_id = tx_id;
162,662✔
686
        FrameDebug("set_txid", NULL, r);
162,662✔
687
    }
162,662✔
688
}
162,662✔
689

690
void SCAppLayerFrameSetTxIdById(const Flow *f, const int dir, const FrameId id, uint64_t tx_id)
691
{
127,040✔
692
    Frame *frame = AppLayerFrameGetById(f, dir, id);
127,040✔
693
    AppLayerFrameSetTxId(frame, tx_id);
127,040✔
694
}
127,040✔
695

696
Frame *AppLayerFrameGetById(const Flow *f, const int dir, const FrameId frame_id)
697
{
147,469✔
698
    FramesContainer *frames_container = AppLayerFramesGetContainer(f);
147,469✔
699
    SCLogDebug("get frame_id %" PRIi64 " direction %u/%s frames_container %p", frame_id, dir,
147,469✔
700
            dir == 0 ? "toserver" : "toclient", frames_container);
147,469✔
701
    if (frames_container == NULL)
147,469✔
702
        return NULL;
×
703

704
    Frames *frames;
147,469✔
705
    if (dir == 0) {
147,469✔
706
        frames = &frames_container->toserver;
87,570✔
707
    } else {
87,570✔
708
        frames = &frames_container->toclient;
59,899✔
709
    }
59,899✔
710
    SCLogDebug("frames %p", frames);
147,469✔
711
    return FrameGetById(frames, frame_id);
147,469✔
712
}
147,469✔
713

714
Frame *AppLayerFrameGetLastOpenByType(Flow *f, const int dir, const uint8_t frame_type)
715
{
425,527✔
716
    if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
425,527✔
717
        return NULL;
102,944✔
718

719
    FramesContainer *frames_container = AppLayerFramesGetContainer(f);
322,583✔
720
    SCLogDebug("get frame_type %" PRIu8 " direction %u/%s frames_container %p", frame_type, dir,
322,583✔
721
            dir == 0 ? "toserver" : "toclient", frames_container);
322,583✔
722
    if (frames_container == NULL)
322,583✔
723
        return NULL;
11,305✔
724

725
    Frames *frames;
311,278✔
726
    if (dir == 0) {
311,278✔
727
        frames = &frames_container->toserver;
291,777✔
728
    } else {
291,778✔
729
        frames = &frames_container->toclient;
19,501✔
730
    }
19,501✔
731
    SCLogDebug("frames %p", frames);
311,278✔
732
    return FrameGetLastOpenByType(frames, frame_type);
311,278✔
733
}
322,583✔
734

735
static inline bool FrameIsDone(const Frame *frame, const uint64_t abs_right_edge)
736
{
14,452,929✔
737
    /* frame with negative length means we don't know the size yet. */
738
    if (frame->len < 0)
14,452,929✔
739
        return false;
195,054✔
740

741
    const int64_t frame_abs_offset = frame->offset;
14,257,875✔
742
    const int64_t frame_right_edge = frame_abs_offset + frame->len;
14,257,875✔
743
    if ((uint64_t)frame_right_edge <= abs_right_edge) {
14,257,875✔
744
        SCLogDebug("frame %p id %" PRIi64 " is done", frame, frame->id);
902,182✔
745
        return true;
902,182✔
746
    }
902,182✔
747
    return false;
13,355,693✔
748
}
14,257,875✔
749

750
static void FramePrune(Frames *frames, const TcpStream *stream, const bool eof)
751
{
634,491✔
752
#ifdef DEBUG_VALIDATION
753
    const uint64_t frames_le_start = (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream);
754
#endif
755
    SCLogDebug("start: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64,
634,491✔
756
            (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
634,491✔
757
            STREAM_BASE_OFFSET(stream));
634,491✔
758
    const uint64_t acked = StreamTcpGetUsable(stream, eof);
634,491✔
759
    uint64_t le = STREAM_APP_PROGRESS(stream);
634,491✔
760

761
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
762
    const uint16_t start = frames->cnt;
763
    uint16_t removed = 0;
764
#endif
765
    uint16_t x = 0;
634,491✔
766
    for (uint16_t i = 0; i < frames->cnt; i++) {
15,141,281✔
767
        if (i < FRAMES_STATIC_CNT) {
14,506,790✔
768
            Frame *frame = &frames->sframes[i];
1,133,301✔
769
            FrameDebug("prune(s)", frames, frame);
1,133,301✔
770
            if (eof || FrameIsDone(frame, acked)) {
1,133,301✔
771
                // remove by not incrementing 'x'
772
                SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
663,817✔
773
                FrameDebug("remove(s)", frames, frame);
663,817✔
774
                FrameClean(frame);
663,817✔
775
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
776
                removed++;
777
#endif
778
            } else {
663,817✔
779
                const uint64_t fle = FrameLeftEdge(stream, frame);
469,484✔
780
                le = MIN(le, fle);
469,484✔
781
                SCLogDebug("le %" PRIu64 ", frame fle %" PRIu64, le, fle);
469,484✔
782
                Frame *nframe = &frames->sframes[x];
469,484✔
783
                FrameCopy(nframe, frame);
469,484✔
784
                if (frame != nframe) {
469,484✔
785
                    FrameClean(frame);
3,173✔
786
                }
3,173✔
787
                x++;
469,484✔
788
            }
469,484✔
789
        } else {
13,373,489✔
790
            const uint16_t o = i - FRAMES_STATIC_CNT;
13,373,489✔
791
            Frame *frame = &frames->dframes[o];
13,373,489✔
792
            FrameDebug("prune(d)", frames, frame);
13,373,489✔
793
            if (eof || FrameIsDone(frame, acked)) {
13,373,489✔
794
                // remove by not incrementing 'x'
795
                SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
292,226✔
796
                FrameDebug("remove(d)", frames, frame);
292,226✔
797
                FrameClean(frame);
292,226✔
798
#if defined(DEBUG) || defined(DEBUG_VALIDATION)
799
                removed++;
800
#endif
801
            } else {
13,081,263✔
802
                const uint64_t fle = FrameLeftEdge(stream, frame);
13,081,263✔
803
                le = MIN(le, fle);
13,081,263✔
804
                SCLogDebug("le %" PRIu64 ", frame fle %" PRIu64, le, fle);
13,081,263✔
805
                Frame *nframe;
13,081,263✔
806
                if (x >= FRAMES_STATIC_CNT) {
13,081,263✔
807
                    nframe = &frames->dframes[x - FRAMES_STATIC_CNT];
13,074,383✔
808
                } else {
13,074,383✔
809
                    nframe = &frames->sframes[x];
6,880✔
810
                }
6,880✔
811
                FrameCopy(nframe, frame);
13,081,263✔
812
                if (frame != nframe) {
13,081,263✔
813
                    FrameClean(frame);
8,770✔
814
                }
8,770✔
815
                x++;
13,081,263✔
816
            }
13,081,263✔
817
        }
13,373,489✔
818
    }
14,506,790✔
819
    frames->cnt = x;
634,491✔
820
    DEBUG_VALIDATE_BUG_ON(le < STREAM_BASE_OFFSET(stream));
634,491✔
821
    DEBUG_VALIDATE_BUG_ON(le - STREAM_BASE_OFFSET(stream) > UINT32_MAX);
634,491✔
822
    frames->left_edge_rel = (uint32_t)(le - STREAM_BASE_OFFSET(stream));
634,491✔
823
#ifdef DEBUG
824
    SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
825
               ", cnt %u, removed %u, start %u",
826
            (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
827
            STREAM_BASE_OFFSET(stream), frames->cnt, removed, start);
828
    AppLayerFrameDumpForFrames("post_slide", frames);
829
#endif
830
    if (frames->cnt > 0) { // if we removed all this can fail
634,491✔
831
        DEBUG_VALIDATE_BUG_ON(frames_le_start > le);
225,089✔
832
    }
225,089✔
833
    DEBUG_VALIDATE_BUG_ON(x != start - removed);
634,491✔
834
}
634,491✔
835

836
void FramesPrune(Flow *f, Packet *p)
837
{
849,836✔
838
    if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
849,836✔
839
        return;
×
840
    FramesContainer *frames_container = AppLayerFramesGetContainer(f);
849,836✔
841
    if (frames_container == NULL)
849,836✔
842
        return;
186,825✔
843

844
    Frames *frames;
2,148,146,664✔
845

846
    if (p->proto == IPPROTO_UDP) {
2,148,146,664✔
847
        SCLogDebug("clearing all UDP frames");
26,671✔
848
        if (PKT_IS_TOSERVER(p)) {
26,671✔
849
            frames = &frames_container->toserver;
21,332✔
850
        } else {
21,332✔
851
            frames = &frames_container->toclient;
5,339✔
852
        }
5,339✔
853
        FramesClear(frames);
26,671✔
854
        return;
26,671✔
855
    }
26,671✔
856

857
    TcpSession *ssn = f->protoctx;
2,148,119,993✔
858

859
    if (ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED) {
2,148,119,993✔
860
        AppLayerFramesFreeContainer(f);
1,855✔
861
        return;
1,855✔
862
    }
1,855✔
863

864
    TcpStream *stream;
2,148,118,138✔
865
    if (PKT_IS_TOSERVER(p)) {
2,148,118,138✔
866
        stream = &ssn->client;
319,899✔
867
        frames = &frames_container->toserver;
319,899✔
868
    } else {
2,147,803,546✔
869
        stream = &ssn->server;
2,147,798,239✔
870
        frames = &frames_container->toclient;
2,147,798,239✔
871
    }
2,147,798,239✔
872

873
    const bool eof = ssn->state == TCP_CLOSED || PKT_IS_PSEUDOPKT(p);
2,148,118,138✔
874
    SCLogDebug("eof %s", eof ? "TRUE" : "false");
2,148,118,138✔
875
    FramePrune(frames, stream, eof);
2,148,118,138✔
876
}
2,148,118,138✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc