suricata
util-spm-hs.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-2023 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 Justin Viiret <justin.viiret@intel.com>
22  *
23  * Single pattern matcher that uses the Hyperscan regex matcher.
24  */
25 
26 #include "suricata-common.h"
27 #include "util-hyperscan.h"
28 #include "util-spm.h"
29 #include "util-spm-hs.h"
30 #include "util-debug.h"
31 #include "util-validate.h"
32 
33 #ifdef BUILD_HYPERSCAN
34 
35 /**
36  * \internal
37  * \brief Hyperscan match callback, called by hs_scan.
38  */
39 static int MatchEvent(unsigned int id, unsigned long long from,
40  unsigned long long to, unsigned int flags, void *context)
41 {
42  uint64_t *match_offset = context;
43  DEBUG_VALIDATE_BUG_ON(*match_offset != UINT64_MAX);
44  *match_offset = to;
45  return 1; /* Terminate matching. */
46 }
47 
48 typedef struct SpmHsCtx_ {
49  hs_database_t *db;
50  uint16_t needle_len;
51 } SpmHsCtx;
52 
53 static void HSDestroyCtx(SpmCtx *ctx)
54 {
55  if (ctx == NULL) {
56  return;
57  }
58  SpmHsCtx *sctx = ctx->ctx;
59  if (sctx) {
60  hs_free_database(sctx->db);
61  SCFree(sctx);
62  }
63  SCFree(ctx);
64 }
65 
66 static int HSBuildDatabase(const uint8_t *needle, uint16_t needle_len,
67  int nocase, SpmHsCtx *sctx,
68  SpmGlobalThreadCtx *global_thread_ctx)
69 {
70  char *expr = HSRenderPattern(needle, needle_len);
71  if (expr == NULL) {
72  SCLogDebug("HSRenderPattern returned NULL");
73  return -1;
74  }
75 
76  unsigned flags = nocase ? HS_FLAG_CASELESS : 0;
77 
78  hs_database_t *db = NULL;
79  hs_compile_error_t *compile_err = NULL;
80  hs_error_t err = hs_compile(expr, flags, HS_MODE_BLOCK, NULL, &db,
81  &compile_err);
82  if (err != HS_SUCCESS) {
83  HSLogCompileError(expr, compile_err, err);
84  SCFree(expr);
85  return -1;
86  }
87 
88  SCFree(expr);
89 
90  /* Update scratch for this database. */
91  hs_scratch_t *scratch = global_thread_ctx->ctx;
92  err = hs_alloc_scratch(db, &scratch);
93  if (err != HS_SUCCESS) {
94  /* If scratch allocation failed, this is not recoverable: other SPM
95  * contexts may need this scratch space. */
96  SCLogError("Unable to alloc scratch for Hyperscan, returned %d.", err);
97  hs_free_database(db);
98  return -1;
99  }
100  global_thread_ctx->ctx = scratch;
101  sctx->db = db;
102  sctx->needle_len = needle_len;
103 
104  return 0;
105 }
106 
107 static SpmCtx *HSInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase,
108  SpmGlobalThreadCtx *global_thread_ctx)
109 {
110  SpmCtx *ctx = SCCalloc(1, sizeof(SpmCtx));
111  if (ctx == NULL) {
112  SCLogDebug("Unable to alloc SpmCtx.");
113  return NULL;
114  }
115  ctx->matcher = SPM_HS;
116 
117  SpmHsCtx *sctx = SCCalloc(1, sizeof(SpmHsCtx));
118  if (sctx == NULL) {
119  SCLogDebug("Unable to alloc SpmHsCtx.");
120  SCFree(ctx);
121  return NULL;
122  }
123  ctx->ctx = sctx;
124 
125  if (HSBuildDatabase(needle, needle_len, nocase, sctx,
126  global_thread_ctx) != 0) {
127  SCLogDebug("HSBuildDatabase failed.");
128  HSDestroyCtx(ctx);
129  return NULL;
130  }
131 
132  return ctx;
133 }
134 
135 static uint8_t *HSScan(const SpmCtx *ctx, SpmThreadCtx *thread_ctx,
136  const uint8_t *haystack, uint32_t haystack_len)
137 {
138  const SpmHsCtx *sctx = ctx->ctx;
139  hs_scratch_t *scratch = thread_ctx->ctx;
140 
141  if (unlikely(haystack_len == 0)) {
142  return NULL;
143  }
144 
145  uint64_t match_offset = UINT64_MAX;
146  hs_error_t err = hs_scan(sctx->db, (const char *)haystack, haystack_len, 0,
147  scratch, MatchEvent, &match_offset);
148  if (err != HS_SUCCESS && err != HS_SCAN_TERMINATED) {
149  /* An error value (other than HS_SCAN_TERMINATED) from hs_scan()
150  * indicates that it was passed an invalid database or scratch region,
151  * which is not something we can recover from at scan time. */
152  SCLogError("Hyperscan returned fatal error %d.", err);
153  exit(EXIT_FAILURE);
154  }
155 
156  if (match_offset == UINT64_MAX) {
157  return NULL;
158  }
159 
160  DEBUG_VALIDATE_BUG_ON(match_offset < sctx->needle_len);
161 
162  /* Note: existing API returns non-const ptr */
163  return (uint8_t *)haystack + (match_offset - sctx->needle_len);
164 }
165 
166 static SpmGlobalThreadCtx *HSInitGlobalThreadCtx(void)
167 {
168  SpmGlobalThreadCtx *global_thread_ctx = SCCalloc(1, sizeof(SpmGlobalThreadCtx));
169  if (global_thread_ctx == NULL) {
170  SCLogDebug("Unable to alloc SpmGlobalThreadCtx.");
171  return NULL;
172  }
173  global_thread_ctx->matcher = SPM_HS;
174 
175  /* We store scratch in the HS-specific ctx. This will be initialized as
176  * patterns are compiled by SpmInitCtx. */
177  global_thread_ctx->ctx = NULL;
178 
179  return global_thread_ctx;
180 }
181 
182 static void HSDestroyGlobalThreadCtx(SpmGlobalThreadCtx *global_thread_ctx)
183 {
184  if (global_thread_ctx == NULL) {
185  return;
186  }
187  hs_free_scratch(global_thread_ctx->ctx);
188  SCFree(global_thread_ctx);
189 }
190 
191 static void HSDestroyThreadCtx(SpmThreadCtx *thread_ctx)
192 {
193  if (thread_ctx == NULL) {
194  return;
195  }
196  hs_free_scratch(thread_ctx->ctx);
197  SCFree(thread_ctx);
198 }
199 
200 static SpmThreadCtx *HSMakeThreadCtx(const SpmGlobalThreadCtx *global_thread_ctx)
201 {
202  SpmThreadCtx *thread_ctx = SCCalloc(1, sizeof(SpmThreadCtx));
203  if (thread_ctx == NULL) {
204  SCLogDebug("Unable to alloc SpmThreadCtx.");
205  return NULL;
206  }
207  thread_ctx->matcher = SPM_HS;
208 
209  if (global_thread_ctx->ctx != NULL) {
210  hs_scratch_t *scratch = NULL;
211  hs_error_t err = hs_clone_scratch(global_thread_ctx->ctx, &scratch);
212  if (err != HS_SUCCESS) {
213  SCLogError("Unable to clone scratch (error %d).", err);
214  exit(EXIT_FAILURE);
215  }
216  thread_ctx->ctx = scratch;
217  }
218 
219  return thread_ctx;
220 }
221 
222 void SpmHSRegister(void)
223 {
224  spm_table[SPM_HS].name = "hs";
225  spm_table[SPM_HS].InitGlobalThreadCtx = HSInitGlobalThreadCtx;
226  spm_table[SPM_HS].DestroyGlobalThreadCtx = HSDestroyGlobalThreadCtx;
227  spm_table[SPM_HS].MakeThreadCtx = HSMakeThreadCtx;
228  spm_table[SPM_HS].DestroyThreadCtx = HSDestroyThreadCtx;
229  spm_table[SPM_HS].InitCtx = HSInitCtx;
230  spm_table[SPM_HS].DestroyCtx = HSDestroyCtx;
231  spm_table[SPM_HS].Scan = HSScan;
232 }
233 
234 #endif /* BUILD_HYPERSCAN */
SpmThreadCtx_::ctx
void * ctx
Definition: util-spm.h:57
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SpmTableElmt_::Scan
uint8_t *(* Scan)(const SpmCtx *ctx, SpmThreadCtx *thread_ctx, const uint8_t *haystack, uint32_t haystack_len)
Definition: util-spm.h:69
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SpmTableElmt_::InitCtx
SpmCtx *(* InitCtx)(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *g_thread_ctx)
Definition: util-spm.h:66
ctx
struct Thresholds ctx
SpmTableElmt_::name
const char * name
Definition: util-spm.h:61
SpmTableElmt_::MakeThreadCtx
SpmThreadCtx *(* MakeThreadCtx)(const SpmGlobalThreadCtx *g_thread_ctx)
Definition: util-spm.h:64
util-hyperscan.h
SPM_HS
@ SPM_HS
Definition: util-spm.h:31
util-debug.h
SpmTableElmt_::InitGlobalThreadCtx
SpmGlobalThreadCtx *(* InitGlobalThreadCtx)(void)
Definition: util-spm.h:62
SpmGlobalThreadCtx_::matcher
uint8_t matcher
Definition: util-spm.h:49
SpmCtx_
Definition: util-spm.h:41
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SpmTableElmt_::DestroyCtx
void(* DestroyCtx)(SpmCtx *)
Definition: util-spm.h:68
util-spm.h
SpmGlobalThreadCtx_::ctx
void * ctx
Definition: util-spm.h:50
util-spm-hs.h
util-validate.h
SpmGlobalThreadCtx_
Definition: util-spm.h:48
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
spm_table
SpmTableElmt spm_table[SPM_TABLE_SIZE]
Definition: util-spm.c:63
SpmHSRegister
void SpmHSRegister(void)
SpmThreadCtx_::matcher
uint8_t matcher
Definition: util-spm.h:56
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
SpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(SpmThreadCtx *thread_ctx)
Definition: util-spm.h:65
SpmTableElmt_::DestroyGlobalThreadCtx
void(* DestroyGlobalThreadCtx)(SpmGlobalThreadCtx *g_thread_ctx)
Definition: util-spm.h:63
SpmThreadCtx_
Definition: util-spm.h:55