Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/detect-icmpv4hdr.c
Line
Count
Source
1
/* Copyright (C) 2020 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 Jeff Lucovsky <jeff@lucovsky.org>
22
 *
23
 */
24
25
#include "suricata-common.h"
26
27
#include "detect.h"
28
#include "detect-engine.h"
29
#include "detect-engine-buffer.h"
30
#include "detect-engine-mpm.h"
31
#include "detect-icmpv4hdr.h"
32
#include "detect-engine-prefilter.h"
33
34
/* prototypes */
35
static int DetectIcmpv4HdrSetup(DetectEngineCtx *, Signature *, const char *);
36
#ifdef UNITTESTS
37
void DetectIcmpv4HdrRegisterTests(void);
38
#endif
39
40
static int g_icmpv4hdr_buffer_id = 0;
41
42
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
43
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);
44
45
/**
46
 * \brief Registration function for icmpv4.hdr: keyword
47
 */
48
void DetectIcmpv4HdrRegister(void)
49
79
{
50
79
    sigmatch_table[DETECT_ICMPV4HDR].name = "icmpv4.hdr";
51
79
    sigmatch_table[DETECT_ICMPV4HDR].desc = "sticky buffer to match on the ICMP v4 header";
52
79
    sigmatch_table[DETECT_ICMPV4HDR].url = "/rules/header-keywords.html#icmpv4-hdr";
53
79
    sigmatch_table[DETECT_ICMPV4HDR].Setup = DetectIcmpv4HdrSetup;
54
79
    sigmatch_table[DETECT_ICMPV4HDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
55
#ifdef UNITTESTS
56
    sigmatch_table[DETECT_ICMPV4HDR].RegisterTests = DetectIcmpv4HdrRegisterTests;
57
#endif
58
59
79
    g_icmpv4hdr_buffer_id = DetectBufferTypeRegister("icmpv4.hdr");
60
79
    BUG_ON(g_icmpv4hdr_buffer_id < 0);
61
62
79
    DetectBufferTypeSupportsPacket("icmpv4.hdr");
63
64
79
    DetectPktMpmRegister("icmpv4.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
65
66
79
    DetectPktInspectEngineRegister("icmpv4.hdr", GetData, DetectEngineInspectPktBufferGeneric);
67
79
}
68
69
/**
70
 * \brief setup icmpv4.hdr sticky buffer
71
 *
72
 * \param de_ctx pointer to the Detection Engine Context
73
 * \param s pointer to the Current Signature
74
 * \param _unused unused
75
 *
76
 * \retval 0 on Success
77
 * \retval -1 on Failure
78
 */
79
static int DetectIcmpv4HdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
80
704
{
81
704
    if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMP)))
82
107
        return -1;
83
84
597
    s->proto.flags |= DETECT_PROTO_IPV4;
85
597
    s->flags |= SIG_FLAG_REQUIRE_PACKET;
86
87
597
    if (SCDetectBufferSetActiveList(de_ctx, s, g_icmpv4hdr_buffer_id) < 0)
88
1
        return -1;
89
90
596
    return 0;
91
597
}
92
93
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
94
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
95
17.3k
{
96
17.3k
    SCEnter();
97
98
17.3k
    if (!PacketIsICMPv4(p)) {
99
16.8k
        SCReturnPtr(NULL, "InspectionBuffer");
100
16.8k
    }
101
102
447
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
103
447
    if (buffer->inspect == NULL) {
104
385
        const ICMPV4Hdr *icmpv4h = PacketGetICMPv4(p);
105
385
        uint16_t hlen = ICMPV4_GET_HLEN_ICMPV4H(p);
106
385
        if (((uint8_t *)icmpv4h + (ptrdiff_t)hlen) >
107
385
                ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
108
0
            SCLogDebug("data out of range: %p > %p", ((uint8_t *)icmpv4h + (ptrdiff_t)hlen),
109
0
                    ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
110
0
            SCReturnPtr(NULL, "InspectionBuffer");
111
0
        }
112
113
385
        const uint32_t data_len = hlen;
114
385
        const uint8_t *data = (const uint8_t *)icmpv4h;
115
116
385
        InspectionBufferSetupAndApplyTransforms(
117
385
                det_ctx, list_id, buffer, data, data_len, transforms);
118
385
    }
119
120
447
    SCReturnPtr(buffer, "InspectionBuffer");
121
447
}
122
123
#ifdef UNITTESTS
124
#include "tests/detect-icmpv4hdr.c"
125
#endif