Coverage Report

Created: 2026-06-07 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-udphdr.c
Line
Count
Source
1
/* Copyright (C) 2007-2019 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
27
#include "detect.h"
28
#include "detect-parse.h"
29
#include "detect-engine.h"
30
#include "detect-engine-mpm.h"
31
#include "detect-engine-prefilter.h"
32
#include "detect-engine-content-inspection.h"
33
#include "detect-fast-pattern.h"
34
#include "detect-udphdr.h"
35
36
/* prototypes */
37
static int DetectUdphdrSetup (DetectEngineCtx *, Signature *, const char *);
38
#ifdef UNITTESTS
39
void DetectUdphdrRegisterTests (void);
40
#endif
41
42
static int g_udphdr_buffer_id = 0;
43
44
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);
46
/**
47
 * \brief Registration function for udp.hdr: keyword
48
 */
49
void DetectUdphdrRegister(void)
50
34
{
51
34
    sigmatch_table[DETECT_UDPHDR].name = "udp.hdr";
52
34
    sigmatch_table[DETECT_UDPHDR].desc = "sticky buffer to match on the UDP header";
53
34
    sigmatch_table[DETECT_UDPHDR].url = "/rules/header-keywords.html#udphdr";
54
34
    sigmatch_table[DETECT_UDPHDR].Setup = DetectUdphdrSetup;
55
34
    sigmatch_table[DETECT_UDPHDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
56
#ifdef UNITTESTS
57
    sigmatch_table[DETECT_UDPHDR].RegisterTests = DetectUdphdrRegisterTests;
58
#endif
59
60
34
    g_udphdr_buffer_id = DetectBufferTypeRegister("udp.hdr");
61
34
    BUG_ON(g_udphdr_buffer_id < 0);
62
63
34
    DetectBufferTypeSupportsPacket("udp.hdr");
64
65
34
    DetectPktMpmRegister("udp.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
66
67
34
    DetectPktInspectEngineRegister("udp.hdr", GetData,
68
34
            DetectEngineInspectPktBufferGeneric);
69
34
    return;
70
34
}
71
72
/**
73
 * \brief set up the udp.hdr sticky buffer
74
 *
75
 * \param de_ctx pointer to the Detection Engine Context
76
 * \param s pointer to the Current Signature
77
 * \param _unused unused
78
 *
79
 * \retval 0 on Success
80
 * \retval -1 on Failure
81
 */
82
static int DetectUdphdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
83
632
{
84
632
    if (!(DetectProtoContainsProto(&s->proto, IPPROTO_UDP)))
85
5
        return -1;
86
87
627
    s->flags |= SIG_FLAG_REQUIRE_PACKET;
88
89
627
    if (DetectBufferSetActiveList(de_ctx, s, g_udphdr_buffer_id) < 0)
90
3
        return -1;
91
92
624
    return 0;
93
627
}
94
95
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
96
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
97
1.95k
{
98
1.95k
    SCEnter();
99
100
1.95k
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
101
1.95k
    if (buffer->inspect == NULL) {
102
1.95k
        if (p->udph == NULL) {
103
1.26k
            return NULL;
104
1.26k
        }
105
697
        if (((uint8_t *)p->udph + (ptrdiff_t)UDP_HEADER_LEN) >
106
697
                ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)))
107
0
        {
108
0
            SCLogDebug("data out of range: %p > %p",
109
0
                    ((uint8_t *)p->udph + (ptrdiff_t)UDP_HEADER_LEN),
110
0
                    ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
111
0
            return NULL;
112
0
        }
113
114
697
        const uint32_t data_len = UDP_HEADER_LEN;
115
697
        const uint8_t *data = (const uint8_t *)p->udph;
116
117
697
        InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
118
697
        InspectionBufferApplyTransforms(buffer, transforms);
119
697
    }
120
121
697
    return buffer;
122
1.95k
}
123
124
#ifdef UNITTESTS
125
#include "tests/detect-udphdr.c"
126
#endif