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