Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/detect-icmpv6hdr.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 Philippe Antoine <p.antoine@catenacyber.fr>
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-buffer.h"
31
#include "detect-engine-mpm.h"
32
#include "detect-engine-prefilter.h"
33
#include "detect-engine-content-inspection.h"
34
#include "detect-fast-pattern.h"
35
#include "detect-icmpv6hdr.h"
36
#include "util-validate.h"
37
38
/* prototypes */
39
static int DetectICMPv6hdrSetup (DetectEngineCtx *, Signature *, const char *);
40
#ifdef UNITTESTS
41
void DetectICMPv6hdrRegisterTests (void);
42
#endif
43
44
static int g_icmpv6hdr_buffer_id = 0;
45
46
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
47
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);
48
49
/**
50
 * \brief Registration function for icmpv6.hdr: keyword
51
 */
52
void DetectICMPv6hdrRegister(void)
53
12
{
54
12
    sigmatch_table[DETECT_ICMPV6HDR].name = "icmpv6.hdr";
55
12
    sigmatch_table[DETECT_ICMPV6HDR].desc = "sticky buffer to match on the ICMP V6 header";
56
12
    sigmatch_table[DETECT_ICMPV6HDR].url = "/rules/header-keywords.html#icmpv6hdr";
57
12
    sigmatch_table[DETECT_ICMPV6HDR].Setup = DetectICMPv6hdrSetup;
58
12
    sigmatch_table[DETECT_ICMPV6HDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
59
#ifdef UNITTESTS
60
    sigmatch_table[DETECT_ICMPV6HDR].RegisterTests = DetectICMPv6hdrRegisterTests;
61
#endif
62
63
12
    g_icmpv6hdr_buffer_id = DetectBufferTypeRegister("icmpv6.hdr");
64
12
    BUG_ON(g_icmpv6hdr_buffer_id < 0);
65
66
12
    DetectBufferTypeSupportsPacket("icmpv6.hdr");
67
68
12
    DetectPktMpmRegister("icmpv6.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
69
70
12
    DetectPktInspectEngineRegister("icmpv6.hdr", GetData, DetectEngineInspectPktBufferGeneric);
71
12
}
72
73
/**
74
 * \brief setup icmpv6.hdr sticky buffer
75
 *
76
 * \param de_ctx pointer to the Detection Engine Context
77
 * \param s pointer to the Current Signature
78
 * \param _unused unused
79
 *
80
 * \retval 0 on Success
81
 * \retval -1 on Failure
82
 */
83
static int DetectICMPv6hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
84
1.22k
{
85
    // ICMPv6 comes only with IPv6
86
1.22k
    s->proto.flags |= DETECT_PROTO_IPV6;
87
1.22k
    if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMPV6)))
88
146
        return -1;
89
90
1.07k
    s->flags |= SIG_FLAG_REQUIRE_PACKET;
91
92
1.07k
    if (SCDetectBufferSetActiveList(de_ctx, s, g_icmpv6hdr_buffer_id) < 0)
93
5
        return -1;
94
95
1.07k
    return 0;
96
1.07k
}
97
98
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
99
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
100
50.1k
{
101
50.1k
    SCEnter();
102
103
50.1k
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
104
50.1k
    if (buffer->inspect == NULL) {
105
36.9k
        uint32_t hlen = ICMPV6_HEADER_LEN;
106
36.9k
        if (!PacketIsICMPv6(p)) {
107
            // DETECT_PROTO_IPV6 does not prefilter
108
21.2k
            return NULL;
109
21.2k
        }
110
15.7k
        const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
111
15.7k
        if (((uint8_t *)icmpv6h + (ptrdiff_t)hlen) >
112
15.7k
                ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
113
0
            SCLogDebug("data out of range: %p > %p", ((uint8_t *)icmpv6h + (ptrdiff_t)hlen),
114
0
                    ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
115
0
            SCReturnPtr(NULL, "InspectionBuffer");
116
0
        }
117
118
15.7k
        const uint32_t data_len = hlen;
119
15.7k
        const uint8_t *data = (const uint8_t *)icmpv6h;
120
121
15.7k
        InspectionBufferSetupAndApplyTransforms(
122
15.7k
                det_ctx, list_id, buffer, data, data_len, transforms);
123
15.7k
    }
124
125
28.8k
    SCReturnPtr(buffer, "InspectionBuffer");
126
50.1k
}
127
128
#ifdef UNITTESTS
129
#include "tests/detect-icmpv6hdr.c"
130
#endif