suricata
detect-mqtt-qos.c
Go to the documentation of this file.
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 Sascha Steinbiss <sascha@steinbiss.name>
22  */
23 
24 #include "suricata-common.h"
25 #include "conf.h"
26 #include "detect.h"
27 #include "detect-parse.h"
28 #include "detect-engine.h"
30 #include "detect-mqtt-qos.h"
31 #include "util-byte.h"
32 #include "util-unittest.h"
33 
34 #include "rust.h"
35 
36 static int mqtt_qos_id = 0;
37 
38 static int DetectMQTTQosMatch(DetectEngineThreadCtx *det_ctx,
39  Flow *f, uint8_t flags, void *state,
40  void *txv, const Signature *s,
41  const SigMatchCtx *ctx);
42 static int DetectMQTTQosSetup (DetectEngineCtx *, Signature *, const char *);
43 void MQTTQosRegisterTests(void);
45 
46 /**
47  * \brief Registration function for mqtt.qos: keyword
48  */
50 {
52  sigmatch_table[DETECT_AL_MQTT_QOS].desc = "match MQTT fixed header QOS level";
53  sigmatch_table[DETECT_AL_MQTT_QOS].url = "/rules/mqtt-keywords.html#mqtt-qos";
55  sigmatch_table[DETECT_AL_MQTT_QOS].Setup = DetectMQTTQosSetup;
57 #ifdef UNITTESTS
59 #endif
60 
63 
64  mqtt_qos_id = DetectBufferTypeGetByName("mqtt.qos");
65 }
66 
67 /**
68  * \internal
69  * \brief Function to match fixed header QOS field of an MQTT Tx
70  *
71  * \param det_ctx Pointer to the pattern matcher thread.
72  * \param f Pointer to the current flow.
73  * \param flags Flags.
74  * \param state App layer state.
75  * \param txv Pointer to the transaction.
76  * \param s Pointer to the Signature.
77  * \param ctx Pointer to the sigmatch that we will cast into uint8_t.
78  *
79  * \retval 0 no match.
80  * \retval 1 match.
81  */
82 static int DetectMQTTQosMatch(DetectEngineThreadCtx *det_ctx,
83  Flow *f, uint8_t flags, void *state,
84  void *txv, const Signature *s,
85  const SigMatchCtx *ctx)
86 {
87  const uint8_t *de = (const uint8_t *)ctx;
88 
89  if (!de)
90  return 0;
91 
92  return rs_mqtt_tx_has_qos(txv, *de);
93 }
94 
95 /**
96  * \internal
97  * \brief This function is used to parse options passed via mqtt.qos: keyword
98  *
99  * \param rawstr Pointer to the user provided options
100  *
101  * \retval de pointer to DetectMQTTQosData on success
102  * \retval NULL on failure
103  */
104 static uint8_t *DetectMQTTQosParse(const char *rawstr)
105 {
106  uint8_t *de = NULL;
107  int ret = 0;
108  uint8_t val;
109 
110  ret = StringParseU8RangeCheck(&val, 10, 0, rawstr, 0, 2);
111  if (ret < 0) {
112  SCLogError("invalid MQTT QOS level: %s", rawstr);
113  return NULL;
114  }
115 
116  de = SCMalloc(sizeof(uint8_t));
117  if (unlikely(de == NULL))
118  return NULL;
119  *de = val;
120 
121  return de;
122 }
123 
124 /**
125  * \internal
126  * \brief this function is used to add the parsed sigmatch into the current signature
127  *
128  * \param de_ctx pointer to the Detection Engine Context
129  * \param s pointer to the Current Signature
130  * \param rawstr pointer to the user provided options
131  *
132  * \retval 0 on Success
133  * \retval -1 on Failure
134  */
135 static int DetectMQTTQosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
136 {
137  uint8_t *de = NULL;
138 
140  return -1;
141 
142  de = DetectMQTTQosParse(rawstr);
143  if (de == NULL)
144  goto error;
145 
146  if (SigMatchAppendSMToList(de_ctx, s, DETECT_AL_MQTT_QOS, (SigMatchCtx *)de, mqtt_qos_id) ==
147  NULL) {
148  goto error;
149  }
150 
151  return 0;
152 
153 error:
154  if (de != NULL)
155  SCFree(de);
156  return -1;
157 }
158 
159 /**
160  * \internal
161  * \brief this function will free memory associated with DetectMQTTQosData
162  *
163  * \param de pointer to DetectMQTTQosData
164  */
166 {
167  if (de_ptr != NULL)
168  SCFree(de_ptr);
169 }
170 
171 /*
172  * ONLY TESTS BELOW THIS COMMENT
173  */
174 
175 #ifdef UNITTESTS
176 /**
177  * \test MQTTQosTestParse01 is a test for a valid value
178  *
179  * \retval 1 on success
180  * \retval 0 on failure
181  */
182 static int MQTTQosTestParse01 (void)
183 {
184  uint8_t *de = NULL;
185 
186  de = DetectMQTTQosParse("0");
187  FAIL_IF_NULL(de);
188  FAIL_IF_NOT(*de == 0);
189  DetectMQTTQosFree(NULL, de);
190 
191  de = DetectMQTTQosParse(" 0");
192  FAIL_IF_NULL(de);
193  FAIL_IF_NOT(*de == 0);
194  DetectMQTTQosFree(NULL, de);
195 
196  de = DetectMQTTQosParse("1");
197  FAIL_IF_NULL(de);
198  FAIL_IF_NOT(*de == 1);
199  DetectMQTTQosFree(NULL, de);
200 
201  de = DetectMQTTQosParse("2");
202  FAIL_IF_NULL(de);
203  FAIL_IF_NOT(*de == 2);
204  DetectMQTTQosFree(NULL, de);
205 
206  PASS;
207 }
208 
209 /**
210  * \test MQTTQosTestParse02 is a test for an invalid value
211  *
212  * \retval 1 on success
213  * \retval 0 on failure
214  */
215 static int MQTTQosTestParse02 (void)
216 {
217  uint8_t *de = NULL;
218  de = DetectMQTTQosParse("3");
219  if (de) {
220  DetectMQTTQosFree(NULL, de);
221  FAIL;
222  }
223 
224  PASS;
225 }
226 
227 /**
228  * \test MQTTQosTestParse04 is a test for an invalid value
229  *
230  * \retval 1 on success
231  * \retval 0 on failure
232  */
233 static int MQTTQosTestParse03 (void)
234 {
235  uint8_t *de = NULL;
236  de = DetectMQTTQosParse("12");
237  if (de) {
238  DetectMQTTQosFree(NULL, de);
239  FAIL;
240  }
241 
242  PASS;
243 }
244 
245 
246 #endif /* UNITTESTS */
247 
248 /**
249  * \brief this function registers unit tests for MQTTQos
250  */
252 {
253 #ifdef UNITTESTS
254  UtRegisterTest("MQTTQosTestParse01", MQTTQosTestParse01);
255  UtRegisterTest("MQTTQosTestParse02", MQTTQosTestParse02);
256  UtRegisterTest("MQTTQosTestParse03", MQTTQosTestParse03);
257 #endif /* UNITTESTS */
258 }
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1295
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1752
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
DetectMQTTQosFree
void DetectMQTTQosFree(DetectEngineCtx *de_ctx, void *)
Definition: detect-mqtt-qos.c:165
SigTableElmt_::desc
const char * desc
Definition: detect.h:1294
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1282
SigTableElmt_::name
const char * name
Definition: detect.h:1292
DetectMQTTQosRegister
void DetectMQTTQosRegister(void)
Registration function for mqtt.qos: keyword.
Definition: detect-mqtt-qos.c:49
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
MQTTQosRegisterTests
void MQTTQosRegisterTests(void)
this function registers unit tests for MQTTQos
Definition: detect-mqtt-qos.c:251
Flow_
Flow data structure.
Definition: flow.h:350
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:835
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1263
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1277
util-unittest.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1119
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:262
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1091
de
uint8_t de
Definition: app-layer-htp.c:579
detect.h
conf.h
StringParseU8RangeCheck
int StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
Definition: util-byte.c:462
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:341
detect-mqtt-qos.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2126
DETECT_AL_MQTT_QOS
@ DETECT_AL_MQTT_QOS
Definition: detect-engine-register.h:300
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:592
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:216
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:447
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1284