suricata
util-mpm.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2014 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 Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef SURICATA_UTIL_MPM_H
25 #define SURICATA_UTIL_MPM_H
26 
27 #include "app-layer-protos.h"
28 // forward declaration for bindgen
29 #define SigIntId uint32_t
31 
32 #define MPM_INIT_HASH_SIZE 65536
33 
34 /**
35  * \brief Multiply two size_t values with overflow detection.
36  * \param a First factor.
37  * \param b Second factor.
38  * \retval The product a * b, or 0 if the multiplication overflows.
39  */
40 static inline size_t MpmCheckSafeSizetMult(size_t a, size_t b)
41 {
42  if (b > 0 && a > SIZE_MAX / b) {
43  return 0;
44  }
45  return a * b;
46 }
47 
48 enum {
50 
51  /* aho-corasick */
55  /* table size */
57 };
58 
59 /* Internal Pattern Index: 0 to pattern_cnt-1 */
60 typedef uint32_t MpmPatternIndex;
61 
62 typedef struct MpmThreadCtx_ {
63  void *ctx;
64 
65  uint32_t memory_cnt;
66  uint32_t memory_size;
67 
69 
70 typedef struct MpmPattern_ {
71  /* length of the pattern */
72  uint16_t len;
73  /* flags describing the pattern */
74  uint8_t flags;
75 
76  /* offset into the buffer where match may start */
77  uint16_t offset;
78 
79  /* offset into the buffer before which match much complete */
80  uint16_t depth;
81 
82  /* holds the original pattern that was added */
83  uint8_t *original_pat;
84  /* case sensitive */
85  uint8_t *cs;
86  /* case insensitive */
87  uint8_t *ci;
88  /* pattern id */
89  uint32_t id;
90 
91  /* sid(s) for this pattern */
92  uint32_t sids_size;
94 
95  struct MpmPattern_ *next;
97 
98 /* Indicates if this a global mpm_ctx. Global mpm_ctx is the one that
99  * is instantiated when we use "single". Non-global is "full", i.e.
100  * one per sgh. */
101 #define MPMCTX_FLAGS_GLOBAL BIT_U8(0)
102 #define MPMCTX_FLAGS_NODEPTH BIT_U8(1)
103 #define MPMCTX_FLAGS_CACHE_TO_DISK BIT_U8(2)
104 
105 typedef struct MpmConfig_ {
106  const char *cache_dir_path;
107  uint64_t cache_max_age_seconds; /* 0 means disabled/no pruning policy */
108  void *cache_stats;
110 
111 typedef struct MpmCtx_ {
112  void *ctx;
113  uint8_t mpm_type;
114 
115  uint8_t flags;
116 
117  uint16_t maxdepth;
118 
119  /* unique patterns */
120  uint32_t pattern_cnt;
121 
122  uint16_t minlen;
123  uint16_t maxlen;
124 
125  uint32_t memory_cnt;
126  uint32_t memory_size;
127 
128  uint32_t max_pat_id;
129 
130  /* hash used during ctx initialization */
133 
134 /* if we want to retrieve an unique mpm context from the mpm context factory
135  * we should supply this as the key */
136 #define MPM_CTX_FACTORY_UNIQUE_CONTEXT -1
137 
138 typedef struct MpmCtxFactoryItem {
139  const char *name;
142  int32_t id;
143  int32_t sm_list;
144  AppProto alproto; /**< ALPROTO_UNKNOWN is not an app item */
147 
148 typedef struct MpmCtxFactoryContainer_ {
150  int32_t max_id;
152 
153 /** pattern is case insensitive */
154 #define MPM_PATTERN_FLAG_NOCASE 0x01
155 /** pattern has a depth setting */
156 #define MPM_PATTERN_FLAG_DEPTH 0x04
157 /** pattern has an offset setting */
158 #define MPM_PATTERN_FLAG_OFFSET 0x08
159 /** the ctx uses it's own internal id instead of
160  * what is passed through the API */
161 #define MPM_PATTERN_CTX_OWNS_ID 0x20
162 #define MPM_PATTERN_FLAG_ENDSWITH 0x40
163 
164 #define MPM_FEATURE_FLAG_DEPTH BIT_U8(0)
165 #define MPM_FEATURE_FLAG_OFFSET BIT_U8(1)
166 #define MPM_FEATURE_FLAG_ENDSWITH BIT_U8(2)
167 
168 typedef struct MpmTableElmt_ {
169  const char *name;
170  void (*InitCtx)(struct MpmCtx_ *);
171  void (*InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *);
172  void (*DestroyCtx)(struct MpmCtx_ *);
173  void (*DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *);
174 
175  MpmConfig *(*ConfigInit)(void);
176  void (*ConfigDeinit)(MpmConfig **);
177  void (*ConfigCacheDirSet)(MpmConfig *, const char *dir_path);
178 
179  /** function pointers for adding patterns to the mpm ctx.
180  *
181  * \param mpm_ctx Mpm context to add the pattern to
182  * \param pattern pointer to the pattern
183  * \param pattern_len length of the pattern in bytes
184  * \param offset pattern offset setting
185  * \param depth pattern depth setting
186  * \param pid pattern id
187  * \param sid signature _internal_ id
188  * \param flags pattern flags
189  */
190  int (*AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t);
191  int (*AddPatternNocase)(struct MpmCtx_ *, const uint8_t *, uint16_t, uint16_t, uint16_t,
192  uint32_t, SigIntId, uint8_t);
193  int (*Prepare)(MpmConfig *, struct MpmCtx_ *);
194  void *(*CacheStatsInit)(void);
195  void (*CacheStatsPrint)(void *data);
196  void (*CacheStatsDeinit)(void *data);
199  /** \retval cnt number of patterns that matches: once per pattern max. */
200  uint32_t (*Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t);
201  void (*PrintCtx)(struct MpmCtx_ *);
202  void (*PrintThreadCtx)(struct MpmThreadCtx_ *);
203 #ifdef UNITTESTS
204  void (*RegisterUnittests)(void);
205 #endif
206  uint8_t feature_flags;
208 
210 extern uint8_t mpm_default_matcher;
211 
212 struct DetectEngineCtx_;
213 
215  struct DetectEngineCtx_ *, const char *, const int, const AppProto);
219 int32_t MpmFactoryIsMpmCtxAvailable(const struct DetectEngineCtx_ *, const MpmCtx *);
220 
221 void MpmTableSetup(void);
222 void MpmRegisterTests(void);
223 
224 void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher);
225 void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, MpmCtx *mpm_ctx, uint16_t);
226 void MpmDestroyThreadCtx(MpmThreadCtx *mpm_thread_ctx, const uint16_t matcher);
227 
228 int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen,
229  uint16_t offset, uint16_t depth,
230  uint32_t pid, SigIntId sid, uint8_t flags);
231 int SCMpmAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
232  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags);
233 
234 void MpmFreePattern(MpmCtx *mpm_ctx, MpmPattern *p);
235 
236 int MpmAddPattern(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset,
237  uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags);
238 
239 #endif /* SURICATA_UTIL_MPM_H */
MpmTableElmt_::PrintThreadCtx
void(* PrintThreadCtx)(struct MpmThreadCtx_ *)
Definition: util-mpm.h:202
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:113
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, MpmCtx *mpm_ctx, uint16_t)
Definition: util-mpm.c:195
MpmPatternIndex
uint32_t MpmPatternIndex
Definition: util-mpm.h:60
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
MpmTableElmt_::InitThreadCtx
void(* InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:171
MpmThreadCtx_
Definition: util-mpm.h:62
MPM_AC
@ MPM_AC
Definition: util-mpm.h:52
MpmTableElmt_::name
const char * name
Definition: util-mpm.h:169
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:34
MpmConfig
struct MpmConfig_ MpmConfig
MPM_TABLE_SIZE
@ MPM_TABLE_SIZE
Definition: util-mpm.h:56
SigIntId
#define SigIntId
Definition: util-mpm.h:29
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
MpmThreadCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:65
MpmCtxFactoryContainer
struct MpmCtxFactoryContainer_ MpmCtxFactoryContainer
MpmTableElmt
struct MpmTableElmt_ MpmTableElmt
MpmAddPatternCS
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:249
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:987
MpmCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:126
MpmAddPattern
int MpmAddPattern(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:435
SCMpmAddPatternCI
int SCMpmAddPatternCI(MpmCtx *mpm_ctx, const uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:258
MpmCtxFactoryItem::mpm_ctx_ts
MpmCtx * mpm_ctx_ts
Definition: util-mpm.h:140
MpmTableElmt_::CacheStatsPrint
void(* CacheStatsPrint)(void *data)
Definition: util-mpm.h:195
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:123
MpmTableElmt_::AddPattern
int(* AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:190
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
MpmTableElmt_::feature_flags
uint8_t feature_flags
Definition: util-mpm.h:206
MpmCtxFactoryItem
struct MpmCtxFactoryItem MpmCtxFactoryItem
p
Packet * p
Definition: fuzz_iprep.c:21
MpmPattern_::original_pat
uint8_t * original_pat
Definition: util-mpm.h:83
MpmCtx_::maxdepth
uint16_t maxdepth
Definition: util-mpm.h:117
MpmThreadCtx
struct MpmThreadCtx_ MpmThreadCtx
MpmTableElmt_::InitCtx
void(* InitCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:170
MpmConfig_::cache_dir_path
const char * cache_dir_path
Definition: util-mpm.h:106
MpmPattern_::flags
uint8_t flags
Definition: util-mpm.h:74
MpmTableElmt_::PrintCtx
void(* PrintCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:201
MpmCtx_::max_pat_id
uint32_t max_pat_id
Definition: util-mpm.h:128
MpmTableSetup
void MpmTableSetup(void)
Definition: util-mpm.c:224
MpmPattern
struct MpmPattern_ MpmPattern
MpmCtxFactoryItem::name
const char * name
Definition: util-mpm.h:139
MpmPattern_::next
struct MpmPattern_ * next
Definition: util-mpm.h:95
MpmPattern_::id
uint32_t id
Definition: util-mpm.h:89
MpmThreadCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:66
MpmTableElmt_::CacheStatsDeinit
void(* CacheStatsDeinit)(void *data)
Definition: util-mpm.h:196
MpmFactoryIsMpmCtxAvailable
int32_t MpmFactoryIsMpmCtxAvailable(const struct DetectEngineCtx_ *, const MpmCtx *)
mpm_default_matcher
uint8_t mpm_default_matcher
Definition: util-mpm.c:48
MpmCtxFactoryContainer_::items
MpmCtxFactoryItem * items
Definition: util-mpm.h:149
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:122
MPM_NOTSET
@ MPM_NOTSET
Definition: util-mpm.h:49
MpmPattern_::sids_size
uint32_t sids_size
Definition: util-mpm.h:92
MpmFreePattern
void MpmFreePattern(MpmCtx *mpm_ctx, MpmPattern *p)
Definition: util-mpm.c:353
MpmTableElmt_::CacheRuleset
int(* CacheRuleset)(MpmConfig *)
Definition: util-mpm.h:197
MpmPattern_
Definition: util-mpm.h:70
MpmCtxFactoryItem::sm_list
int32_t sm_list
Definition: util-mpm.h:143
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:200
MpmPattern_::ci
uint8_t * ci
Definition: util-mpm.h:87
MpmCtxFactoryItem::next
struct MpmCtxFactoryItem * next
Definition: util-mpm.h:145
MpmConfig_::cache_max_age_seconds
uint64_t cache_max_age_seconds
Definition: util-mpm.h:107
flags
uint8_t flags
Definition: decode-gre.h:0
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:120
MpmFactoryReClaimMpmCtx
void MpmFactoryReClaimMpmCtx(const struct DetectEngineCtx_ *, MpmCtx *)
MpmConfig_::cache_stats
void * cache_stats
Definition: util-mpm.h:108
MpmTableElmt_::AddPatternNocase
int(* AddPatternNocase)(struct MpmCtx_ *, const uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:191
MPM_HS
@ MPM_HS
Definition: util-mpm.h:54
MpmPattern_::offset
uint16_t offset
Definition: util-mpm.h:77
MpmPattern_::sids
SigIntId * sids
Definition: util-mpm.h:93
MpmRegisterTests
void MpmRegisterTests(void)
Definition: util-mpm.c:570
MpmPattern_::depth
uint16_t depth
Definition: util-mpm.h:80
MpmFactoryDeRegisterAllMpmCtxProfiles
void MpmFactoryDeRegisterAllMpmCtxProfiles(struct DetectEngineCtx_ *)
Definition: util-mpm.c:168
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:193
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:172
MpmCtxFactoryItem
Definition: util-mpm.h:138
MpmConfig_
Definition: util-mpm.h:105
MpmCtxFactoryItem::alproto
AppProto alproto
Definition: util-mpm.h:144
MpmCtxFactoryItem::id
int32_t id
Definition: util-mpm.h:142
MpmCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:125
MpmCtx_::init_hash
MpmPattern ** init_hash
Definition: util-mpm.h:131
MpmPattern_::len
uint16_t len
Definition: util-mpm.h:72
app-layer-protos.h
MpmTableElmt_::ConfigDeinit
void(* ConfigDeinit)(MpmConfig **)
Definition: util-mpm.h:176
MpmCtxFactoryItem::mpm_ctx_tc
MpmCtx * mpm_ctx_tc
Definition: util-mpm.h:141
MpmTableElmt_::ConfigCacheDirSet
void(* ConfigCacheDirSet)(MpmConfig *, const char *dir_path)
Definition: util-mpm.h:177
MpmFactoryRegisterMpmCtxProfile
int32_t MpmFactoryRegisterMpmCtxProfile(struct DetectEngineCtx_ *, const char *, const int, const AppProto)
Register a new Mpm Context.
Definition: util-mpm.c:59
MpmTableElmt_::CachePrune
int(* CachePrune)(MpmConfig *)
Definition: util-mpm.h:198
MpmCtx
struct MpmCtx_ MpmCtx
MpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:173
MpmCtxFactoryContainer_::max_id
int32_t max_id
Definition: util-mpm.h:150
MpmCtx_
Definition: util-mpm.h:111
MpmPattern_::cs
uint8_t * cs
Definition: util-mpm.h:85
MpmCtx_::flags
uint8_t flags
Definition: util-mpm.h:115
MpmTableElmt_
Definition: util-mpm.h:168
MpmCtx_::ctx
void * ctx
Definition: util-mpm.h:112
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
MpmFactoryGetMpmCtxForProfile
MpmCtx * MpmFactoryGetMpmCtxForProfile(const struct DetectEngineCtx_ *, int32_t, int)
MpmThreadCtx_::ctx
void * ctx
Definition: util-mpm.h:63
MpmTableElmt_::RegisterUnittests
void(* RegisterUnittests)(void)
Definition: util-mpm.h:204
MpmCtxFactoryContainer_
Definition: util-mpm.h:148
MpmDestroyThreadCtx
void MpmDestroyThreadCtx(MpmThreadCtx *mpm_thread_ctx, const uint16_t matcher)
Definition: util-mpm.c:202
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:53