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