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 static int DetectEngineInspectMQTTQosGeneric(DetectEngineCtx *de_ctx,
47  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
48  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
49 
50 /**
51  * \brief Registration function for mqtt.qos: keyword
52  */
54 {
56  sigmatch_table[DETECT_AL_MQTT_QOS].desc = "match MQTT fixed header QOS level";
57  sigmatch_table[DETECT_AL_MQTT_QOS].url = "/rules/mqtt-keywords.html#mqtt-qos";
59  sigmatch_table[DETECT_AL_MQTT_QOS].Setup = DetectMQTTQosSetup;
61 #ifdef UNITTESTS
63 #endif
64 
66  DetectEngineInspectMQTTQosGeneric, NULL);
67 
68  mqtt_qos_id = DetectBufferTypeGetByName("mqtt.qos");
69 }
70 
71 static int DetectEngineInspectMQTTQosGeneric(DetectEngineCtx *de_ctx,
72  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
73  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
74 {
76  de_ctx, det_ctx, s, engine->smd, f, flags, alstate, txv, tx_id);
77 }
78 
79 /**
80  * \internal
81  * \brief Function to match fixed header QOS field of an MQTT Tx
82  *
83  * \param det_ctx Pointer to the pattern matcher thread.
84  * \param f Pointer to the current flow.
85  * \param flags Flags.
86  * \param state App layer state.
87  * \param txv Pointer to the transaction.
88  * \param s Pointer to the Signature.
89  * \param ctx Pointer to the sigmatch that we will cast into uint8_t.
90  *
91  * \retval 0 no match.
92  * \retval 1 match.
93  */
94 static int DetectMQTTQosMatch(DetectEngineThreadCtx *det_ctx,
95  Flow *f, uint8_t flags, void *state,
96  void *txv, const Signature *s,
97  const SigMatchCtx *ctx)
98 {
99  const uint8_t *de = (const uint8_t *)ctx;
100 
101  if (!de)
102  return 0;
103 
104  return rs_mqtt_tx_has_qos(txv, *de);
105 }
106 
107 /**
108  * \internal
109  * \brief This function is used to parse options passed via mqtt.qos: keyword
110  *
111  * \param rawstr Pointer to the user provided options
112  *
113  * \retval de pointer to DetectMQTTQosData on success
114  * \retval NULL on failure
115  */
116 static uint8_t *DetectMQTTQosParse(const char *rawstr)
117 {
118  uint8_t *de = NULL;
119  int ret = 0;
120  uint8_t val;
121 
122  ret = StringParseU8RangeCheck(&val, 10, 0, rawstr, 0, 2);
123  if (ret < 0) {
124  SCLogError(SC_ERR_UNKNOWN_VALUE, "invalid MQTT QOS level: %s", rawstr);
125  return NULL;
126  }
127 
128  de = SCMalloc(sizeof(uint8_t));
129  if (unlikely(de == NULL))
130  return NULL;
131  *de = val;
132 
133  return de;
134 }
135 
136 /**
137  * \internal
138  * \brief this function is used to add the parsed sigmatch into the current signature
139  *
140  * \param de_ctx pointer to the Detection Engine Context
141  * \param s pointer to the Current Signature
142  * \param rawstr pointer to the user provided options
143  *
144  * \retval 0 on Success
145  * \retval -1 on Failure
146  */
147 static int DetectMQTTQosSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
148 {
149  uint8_t *de = NULL;
150  SigMatch *sm = NULL;
151 
153  return -1;
154 
155  de = DetectMQTTQosParse(rawstr);
156  if (de == NULL)
157  goto error;
158 
159  sm = SigMatchAlloc();
160  if (sm == NULL)
161  goto error;
162 
163  sm->type = DETECT_AL_MQTT_QOS;
164  sm->ctx = (SigMatchCtx *)de;
165 
166  SigMatchAppendSMToList(s, sm, mqtt_qos_id);
167 
168  return 0;
169 
170 error:
171  if (de != NULL)
172  SCFree(de);
173  if (sm != NULL)
174  SCFree(sm);
175  return -1;
176 }
177 
178 /**
179  * \internal
180  * \brief this function will free memory associated with DetectMQTTQosData
181  *
182  * \param de pointer to DetectMQTTQosData
183  */
185 {
186  if (de_ptr != NULL)
187  SCFree(de_ptr);
188 }
189 
190 /*
191  * ONLY TESTS BELOW THIS COMMENT
192  */
193 
194 #ifdef UNITTESTS
195 /**
196  * \test MQTTQosTestParse01 is a test for a valid value
197  *
198  * \retval 1 on success
199  * \retval 0 on failure
200  */
201 static int MQTTQosTestParse01 (void)
202 {
203  uint8_t *de = NULL;
204 
205  de = DetectMQTTQosParse("0");
206  FAIL_IF_NULL(de);
207  FAIL_IF_NOT(*de == 0);
208  DetectMQTTQosFree(NULL, de);
209 
210  de = DetectMQTTQosParse(" 0");
211  FAIL_IF_NULL(de);
212  FAIL_IF_NOT(*de == 0);
213  DetectMQTTQosFree(NULL, de);
214 
215  de = DetectMQTTQosParse("1");
216  FAIL_IF_NULL(de);
217  FAIL_IF_NOT(*de == 1);
218  DetectMQTTQosFree(NULL, de);
219 
220  de = DetectMQTTQosParse("2");
221  FAIL_IF_NULL(de);
222  FAIL_IF_NOT(*de == 2);
223  DetectMQTTQosFree(NULL, de);
224 
225  PASS;
226 }
227 
228 /**
229  * \test MQTTQosTestParse02 is a test for an invalid value
230  *
231  * \retval 1 on success
232  * \retval 0 on failure
233  */
234 static int MQTTQosTestParse02 (void)
235 {
236  uint8_t *de = NULL;
237  de = DetectMQTTQosParse("3");
238  if (de) {
239  DetectMQTTQosFree(NULL, de);
240  FAIL;
241  }
242 
243  PASS;
244 }
245 
246 /**
247  * \test MQTTQosTestParse04 is a test for an invalid value
248  *
249  * \retval 1 on success
250  * \retval 0 on failure
251  */
252 static int MQTTQosTestParse03 (void)
253 {
254  uint8_t *de = NULL;
255  de = DetectMQTTQosParse("12");
256  if (de) {
257  DetectMQTTQosFree(NULL, de);
258  FAIL;
259  }
260 
261  PASS;
262 }
263 
264 
265 #endif /* UNITTESTS */
266 
267 /**
268  * \brief this function registers unit tests for MQTTQos
269  */
271 {
272 #ifdef UNITTESTS
273  UtRegisterTest("MQTTQosTestParse01", MQTTQosTestParse01);
274  UtRegisterTest("MQTTQosTestParse02", MQTTQosTestParse02);
275  UtRegisterTest("MQTTQosTestParse03", MQTTQosTestParse03);
276 #endif /* UNITTESTS */
277 }
util-byte.h
DetectEngineAppInspectionEngine_
Definition: detect.h:398
SigTableElmt_::url
const char * url
Definition: detect.h:1270
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
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:184
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
DetectEngineInspectGenericList
int DetectEngineInspectGenericList(const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Flow *f, const uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1941
SigTableElmt_::name
const char * name
Definition: detect.h:1267
DetectMQTTQosRegister
void DetectMQTTQosRegister(void)
Registration function for mqtt.qos: keyword.
Definition: detect-mqtt-qos.c:53
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:270
Flow_
Flow data structure.
Definition: flow.h:353
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1238
rust.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
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:1077
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
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:1060
de
uint8_t de
Definition: app-layer-htp.c:563
SC_ERR_UNKNOWN_VALUE
@ SC_ERR_UNKNOWN_VALUE
Definition: util-error.h:159
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:324
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:225
conf.h
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
StringParseU8RangeCheck
int StringParseU8RangeCheck(uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
Definition: util-byte.c:457
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:316
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
detect-mqtt-qos.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:322
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
DETECT_AL_MQTT_QOS
@ DETECT_AL_MQTT_QOS
Definition: detect-engine-register.h:275
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
SigMatch_
a single match condition for a signature
Definition: detect.h:321
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:349
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259