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  SigMatch *sm = NULL;
139 
141  return -1;
142 
143  de = DetectMQTTQosParse(rawstr);
144  if (de == NULL)
145  goto error;
146 
147  sm = SigMatchAlloc();
148  if (sm == NULL)
149  goto error;
150 
151  sm->type = DETECT_AL_MQTT_QOS;
152  sm->ctx = (SigMatchCtx *)de;
153 
154  SigMatchAppendSMToList(s, sm, mqtt_qos_id);
155 
156  return 0;
157 
158 error:
159  if (de != NULL)
160  SCFree(de);
161  if (sm != NULL)
162  SCFree(sm);
163  return -1;
164 }
165 
166 /**
167  * \internal
168  * \brief this function will free memory associated with DetectMQTTQosData
169  *
170  * \param de pointer to DetectMQTTQosData
171  */
173 {
174  if (de_ptr != NULL)
175  SCFree(de_ptr);
176 }
177 
178 /*
179  * ONLY TESTS BELOW THIS COMMENT
180  */
181 
182 #ifdef UNITTESTS
183 /**
184  * \test MQTTQosTestParse01 is a test for a valid value
185  *
186  * \retval 1 on success
187  * \retval 0 on failure
188  */
189 static int MQTTQosTestParse01 (void)
190 {
191  uint8_t *de = NULL;
192 
193  de = DetectMQTTQosParse("0");
194  FAIL_IF_NULL(de);
195  FAIL_IF_NOT(*de == 0);
196  DetectMQTTQosFree(NULL, de);
197 
198  de = DetectMQTTQosParse(" 0");
199  FAIL_IF_NULL(de);
200  FAIL_IF_NOT(*de == 0);
201  DetectMQTTQosFree(NULL, de);
202 
203  de = DetectMQTTQosParse("1");
204  FAIL_IF_NULL(de);
205  FAIL_IF_NOT(*de == 1);
206  DetectMQTTQosFree(NULL, de);
207 
208  de = DetectMQTTQosParse("2");
209  FAIL_IF_NULL(de);
210  FAIL_IF_NOT(*de == 2);
211  DetectMQTTQosFree(NULL, de);
212 
213  PASS;
214 }
215 
216 /**
217  * \test MQTTQosTestParse02 is a test for an invalid value
218  *
219  * \retval 1 on success
220  * \retval 0 on failure
221  */
222 static int MQTTQosTestParse02 (void)
223 {
224  uint8_t *de = NULL;
225  de = DetectMQTTQosParse("3");
226  if (de) {
227  DetectMQTTQosFree(NULL, de);
228  FAIL;
229  }
230 
231  PASS;
232 }
233 
234 /**
235  * \test MQTTQosTestParse04 is a test for an invalid value
236  *
237  * \retval 1 on success
238  * \retval 0 on failure
239  */
240 static int MQTTQosTestParse03 (void)
241 {
242  uint8_t *de = NULL;
243  de = DetectMQTTQosParse("12");
244  if (de) {
245  DetectMQTTQosFree(NULL, de);
246  FAIL;
247  }
248 
249  PASS;
250 }
251 
252 
253 #endif /* UNITTESTS */
254 
255 /**
256  * \brief this function registers unit tests for MQTTQos
257  */
259 {
260 #ifdef UNITTESTS
261  UtRegisterTest("MQTTQosTestParse01", MQTTQosTestParse01);
262  UtRegisterTest("MQTTQosTestParse02", MQTTQosTestParse02);
263  UtRegisterTest("MQTTQosTestParse03", MQTTQosTestParse03);
264 #endif /* UNITTESTS */
265 }
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1241
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1498
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:172
SigTableElmt_::desc
const char * desc
Definition: detect.h:1240
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1228
SigTableElmt_::name
const char * name
Definition: detect.h:1238
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:258
Flow_
Flow data structure.
Definition: flow.h:357
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1209
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1223
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:1079
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:228
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:1025
de
uint8_t de
Definition: app-layer-htp.c:576
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:316
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:224
conf.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:239
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:308
detect-mqtt-qos.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:314
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:76
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:1948
DETECT_AL_MQTT_QOS
@ DETECT_AL_MQTT_QOS
Definition: detect-engine-register.h:290
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:540
SigMatch_
a single match condition for a signature
Definition: detect.h:313
FAIL
#define FAIL
Fail a test.
Definition: util-unittest.h:60
ALPROTO_MQTT
@ ALPROTO_MQTT
Definition: app-layer-protos.h:56
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:354
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1230