suricata
util-var-name.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 Victor Julien <victor@inliniac.net>
22  *
23  * Generic variable name utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "util-hash-string.h"
29 #include "util-hashlist.h"
30 #include "util-var-name.h"
31 #include "util-validate.h"
32 
33 /* Overall Design:
34  *
35  * Base Store: "base"
36  *
37  * Used during keyword registration. Operates under lock. Base is shared
38  * between all detect engines, detect engine versions and tenants.
39  * Each variable name is ref counted.
40  *
41  * During the freeing of a detect engine / tenant, unregistration decreases
42  * the ref cnt.
43  *
44  * Base has both a string to id and a id to string hash table. String to
45  * id is used during parsing/registration. id to string during unregistration.
46  *
47  *
48  * Active Store Pointer (atomic)
49  *
50  * The "active" store atomic pointer points to the active store. The call
51  * to `VarNameStoreActivate` will build a new lookup store and hot swap
52  * the pointer.
53  *
54  * Ensuring memory safety. During the hot swap, the pointer is replaced, so
55  * any new call to the lookup functions will automatically use the new store.
56  * This leaves the case of any lookup happening concurrently with the pointer
57  * swap. For this case we add the old store to a free list. It gets a timestamp
58  * before which it cannot be freed.
59  *
60  *
61  * Free List
62  *
63  * The free list contains old stores that are waiting to get removed. They
64  * contain a timestamp that is checked before they are freed.
65  *
66  */
67 typedef struct VarNameStore_ {
70  uint32_t max_id;
75 
76 /** \brief Name2idx mapping structure for flowbits, flowvars and pktvars. */
77 typedef struct VariableName_ {
78  char *name;
79  enum VarTypes type; /* flowbit, pktvar, etc */
80  uint32_t id;
81  uint32_t ref_cnt;
83 
84 #define VARNAME_HASHSIZE 0x1000
85 #define VARID_HASHSIZE 0x1000
86 
87 static SCMutex base_lock = SCMUTEX_INITIALIZER;
88 static VarNameStore base = { .names = NULL, .ids = NULL, .max_id = 0 };
89 static TAILQ_HEAD(, VarNameStore_) free_list = TAILQ_HEAD_INITIALIZER(free_list);
90 static SC_ATOMIC_DECLARE(VarNameStorePtr, active);
91 
92 static uint32_t VariableNameHash(HashListTable *ht, void *buf, uint16_t buflen);
93 static char VariableNameCompare(void *buf1, uint16_t len1, void *buf2, uint16_t len2);
94 static uint32_t VariableIdHash(HashListTable *ht, void *ptr, uint16_t _unused);
95 static char VariableIdCompare(void *ptr1, uint16_t _unused1, void *ptr2, uint16_t _unused2);
96 static void VariableNameFree(void *data);
97 
98 void VarNameStoreInit(void)
99 {
100  SCMutexLock(&base_lock);
101  base.names = HashListTableInit(
102  VARNAME_HASHSIZE, VariableNameHash, VariableNameCompare, VariableNameFree);
103  if (base.names == NULL) {
104  FatalError("failed to initialize variable name hash (names)");
105  }
106 
107  /* base.names owns the allocation, so use a NULL Free pointer here */
108  base.ids = HashListTableInit(VARID_HASHSIZE, VariableIdHash, VariableIdCompare, NULL);
109  if (base.ids == NULL) {
110  FatalError("failed to initialize variable name hash (names)");
111  }
112  SC_ATOMIC_INITPTR(active);
113  SCMutexUnlock(&base_lock);
114 }
115 
117 {
118  SCMutexLock(&base_lock);
119  VarNameStore *s = SC_ATOMIC_GET(active);
120  if (s) {
123  SCFree(s);
124  s = NULL;
125  }
126  SC_ATOMIC_SET(active, NULL);
127 
128  while ((s = TAILQ_FIRST(&free_list))) {
129  TAILQ_REMOVE(&free_list, s, next);
132  SCFree(s);
133  }
134 
135  for (HashListTableBucket *b = HashListTableGetListHead(base.names); b != NULL;
136  b = HashListTableGetListNext(b)) {
139  if (vn->ref_cnt > 0) {
140  SCLogWarning("%s (type %u, id %u) still has ref_cnt %u", vn->name, vn->type, vn->id,
141  vn->ref_cnt);
142  }
143  }
144  HashListTableFree(base.ids);
145  base.ids = NULL;
146  HashListTableFree(base.names);
147  base.names = NULL;
148  base.max_id = 0;
149  SCMutexUnlock(&base_lock);
150 }
151 
152 /**
153  * \retval id or 0 on error
154  */
155 uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
156 {
157  if (name == NULL) {
158  return 0;
159  }
160  SCMutexLock(&base_lock);
161  uint32_t id = 0;
162 
163  SCLogDebug("registering: name %s type %u", name, type);
164  VariableName lookup = { .type = type, .name = (char *)name };
165  VariableName *found = (VariableName *)HashListTableLookup(base.names, (void *)&lookup, 0);
166  if (found == NULL) {
167  VariableName *vn = SCCalloc(1, sizeof(VariableName));
168  if (likely(vn != NULL)) {
169  vn->type = type;
170  vn->name = SCStrdup(name);
171  if (vn->name != NULL) {
172  vn->ref_cnt = 1;
173  id = vn->id = ++base.max_id;
174  HashListTableAdd(base.names, (void *)vn, 0);
175  HashListTableAdd(base.ids, (void *)vn, 0);
176  SCLogDebug(
177  "new registration %s id %u type %u -> %u", vn->name, vn->id, vn->type, id);
178  } else {
179  SCFree(vn);
180  }
181  }
182  } else {
183  id = found->id;
184  found->ref_cnt++;
185  SCLogDebug("existing registration %s ref_cnt %u -> %u", name, found->ref_cnt, id);
186  }
187  SCMutexUnlock(&base_lock);
188  return id;
189 }
190 
191 const char *VarNameStoreSetupLookup(const uint32_t id, const enum VarTypes type)
192 {
193  const char *name = NULL;
194  SCMutexLock(&base_lock);
195  VariableName lookup = { .type = type, .id = id };
196  VariableName *found = (VariableName *)HashListTableLookup(base.ids, (void *)&lookup, 0);
197  if (found) {
198  name = found->name;
199  }
200  SCMutexUnlock(&base_lock);
201  return name;
202 }
203 
204 void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
205 {
206  if (unlikely(id == 0)) {
207  /* There was an error registering the varname, so nothing to unregister */
208  return;
209  }
210  SCMutexLock(&base_lock);
211  VariableName lookup = { .type = type, .id = id };
212  VariableName *found = (VariableName *)HashListTableLookup(base.ids, (void *)&lookup, 0);
213  if (found) {
214  SCLogDebug("found %s ref_cnt %u", found->name, found->ref_cnt);
215  DEBUG_VALIDATE_BUG_ON(found->ref_cnt == 0);
216  found->ref_cnt--;
217  }
218  SCMutexUnlock(&base_lock);
219 }
220 
222 {
223  int result = 0;
224  SCMutexLock(&base_lock);
225  SCLogDebug("activating new lookup store");
226 
227  VarNameStore *new_active = NULL;
228 
229  // create lookup hash for id to string, strings should point to base
230  for (HashListTableBucket *b = HashListTableGetListHead(base.names); b != NULL;
231  b = HashListTableGetListNext(b)) {
233  BUG_ON(vn == NULL);
234  SCLogDebug("base: %s/%u/%u", vn->name, vn->id, vn->ref_cnt);
235  if (vn->ref_cnt == 0)
236  continue;
237 
238  if (new_active == NULL) {
239  new_active = SCCalloc(1, sizeof(*new_active));
240  if (new_active == NULL) {
241  result = -1;
242  goto out;
243  }
244 
245  new_active->names = HashListTableInit(
246  VARNAME_HASHSIZE, VariableNameHash, VariableNameCompare, NULL);
247  if (new_active->names == NULL) {
248  SCFree(new_active);
249  result = -1;
250  goto out;
251  }
252  new_active->ids =
253  HashListTableInit(VARID_HASHSIZE, VariableIdHash, VariableIdCompare, NULL);
254  if (new_active->ids == NULL) {
255  HashListTableFree(new_active->names);
256  SCFree(new_active);
257  result = -1;
258  goto out;
259  }
260  }
261 
262  /* memory is still owned by "base" */
263  HashListTableAdd(new_active->names, (void *)vn, 0);
264  HashListTableAdd(new_active->ids, (void *)vn, 0);
265  }
266 
267  if (new_active) {
268  SCTime_t now = SCTimeGetTime();
269 
270  VarNameStore *old_active = SC_ATOMIC_GET(active);
271  if (old_active) {
272  SCTime_t free_after = SCTIME_ADD_SECS(now, 60);
273  old_active->free_after = free_after;
274 
275  TAILQ_INSERT_TAIL(&free_list, old_active, next);
276  SCLogDebug("old active is stored in free list");
277  }
278 
279  SC_ATOMIC_SET(active, new_active);
280  SCLogDebug("new store active");
281 
282  VarNameStore *s = NULL;
283  while ((s = TAILQ_FIRST(&free_list))) {
284  char timebuf[64];
285  CreateIsoTimeString(s->free_after, timebuf, sizeof(timebuf));
286 
287  if (SCTIME_CMP_LTE(now, s->free_after)) {
288  SCLogDebug("not yet freeing store %p before %s", s, timebuf);
289  break;
290  }
291  SCLogDebug("freeing store %p with time %s", s, timebuf);
292  TAILQ_REMOVE(&free_list, s, next);
295  SCFree(s);
296  }
297  }
298 out:
299  SCLogDebug("activating new lookup store: complete %d", result);
300  SCMutexUnlock(&base_lock);
301  return result;
302 }
303 
304 /** \brief find name for id+type at packet time.
305  * As the `active` store won't be modified, we don't need locks. */
306 const char *VarNameStoreLookupById(const uint32_t id, const enum VarTypes type)
307 {
308  const char *name = NULL;
309 
310  /* coverity[missing_lock] */
311  const VarNameStore *current = SC_ATOMIC_GET(active);
312  if (current) {
313  VariableName lookup = { .type = type, .id = id };
314  /* coverity[missing_lock] */
315  const VariableName *found = HashListTableLookup(current->ids, (void *)&lookup, 0);
316  if (found) {
317  return found->name;
318  }
319  }
320 
321  return name;
322 }
323 
324 /** \brief find name for id+type at packet time.
325  * As the `active` store won't be modified, we don't need locks. */
326 uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type)
327 {
328  /* coverity[missing_lock] */
329  const VarNameStore *current = SC_ATOMIC_GET(active);
330  if (current) {
331  VariableName lookup = { .name = (char *)name, .type = type };
332  /* coverity[missing_lock] */
333  const VariableName *found = HashListTableLookup(current->names, (void *)&lookup, 0);
334  if (found) {
335  return found->id;
336  }
337  }
338 
339  return 0;
340 }
341 
342 static uint32_t VariableNameHash(HashListTable *ht, void *buf, uint16_t buflen)
343 {
344  VariableName *vn = (VariableName *)buf;
345  uint32_t hash =
346  StringHashDjb2((const uint8_t *)vn->name, (uint32_t)strlen(vn->name)) + vn->type;
347  return (hash % VARNAME_HASHSIZE);
348 }
349 
350 static char VariableNameCompare(void *buf1, uint16_t len1, void *buf2, uint16_t len2)
351 {
352  VariableName *vn1 = (VariableName *)buf1;
353  VariableName *vn2 = (VariableName *)buf2;
354  return (vn1->type == vn2->type && strcmp(vn1->name, vn2->name) == 0);
355 }
356 
357 static uint32_t VariableIdHash(HashListTable *ht, void *ptr, uint16_t _unused)
358 {
359  VariableName *vn = (VariableName *)ptr;
360  uint32_t hash = vn->id << vn->type;
361  return (hash % VARID_HASHSIZE);
362 }
363 
364 static char VariableIdCompare(void *ptr1, uint16_t _unused1, void *ptr2, uint16_t _unused2)
365 {
366  VariableName *vn1 = (VariableName *)ptr1;
367  VariableName *vn2 = (VariableName *)ptr2;
368 
369  return (vn1->id == vn2->id && vn1->type == vn2->type);
370 }
371 
372 static void VariableNameFree(void *data)
373 {
374  VariableName *vn = (VariableName *)data;
375  if (vn == NULL)
376  return;
377  if (vn->name != NULL) {
378  SCFree(vn->name);
379  vn->name = NULL;
380  }
381  SCFree(vn);
382 }
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
util-hash-string.h
VARID_HASHSIZE
#define VARID_HASHSIZE
Definition: util-var-name.c:85
VarNameStore_::ids
HashListTable * ids
Definition: util-var-name.c:69
util-hashlist.h
VarNameStore_::max_id
uint32_t max_id
Definition: util-var-name.c:70
CreateIsoTimeString
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:209
VarNameStore_
Definition: util-var-name.c:67
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
VarNameStore_::free_after
SCTime_t free_after
Definition: util-var-name.c:71
VarNameStoreInit
void VarNameStoreInit(void)
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
util-var-name.h
VarNameStoreSetupLookup
const char * VarNameStoreSetupLookup(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:191
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
VarNameStoreRegister
uint32_t VarNameStoreRegister(const char *name, const enum VarTypes type)
Definition: util-var-name.c:155
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
VarNameStore_::names
HashListTable * names
Definition: util-var-name.c:68
VariableName_::ref_cnt
uint32_t ref_cnt
Definition: util-var-name.c:81
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:114
TAILQ_ENTRY
#define TAILQ_ENTRY(type)
Definition: queue.h:239
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
TAILQ_HEAD_INITIALIZER
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:236
VarNameStoreLookupByName
uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type)
find name for id+type at packet time. As the active store won't be modified, we don't need locks.
Definition: util-var-name.c:326
HashListTableInit
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hashlist.c:35
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
VariableName_::name
char * name
Definition: util-var-name.c:78
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
detect.h
VarNameStoreUnregister
void VarNameStoreUnregister(const uint32_t id, const enum VarTypes type)
Definition: util-var-name.c:204
VariableName
struct VariableName_ VariableName
Name2idx mapping structure for flowbits, flowvars and pktvars.
VarNameStore
struct VarNameStore_ VarNameStore
VariableName_::id
uint32_t id
Definition: util-var-name.c:80
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:259
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
SC_ATOMIC_DECLARE
#define SC_ATOMIC_DECLARE(type, name)
wrapper for declaring atomic variables.
Definition: util-atomic.h:280
type
uint16_t type
Definition: decode-vlan.c:106
VarNameStoreDestroy
void VarNameStoreDestroy(void)
Definition: util-var-name.c:116
SCTime_t
Definition: util-time.h:40
name
const char * name
Definition: tm-threads.c:2163
HashListTable_
Definition: util-hashlist.h:37
suricata-common.h
VarNameStoreLookupById
const char * VarNameStoreLookupById(const uint32_t id, const enum VarTypes type)
find name for id+type at packet time. As the active store won't be modified, we don't need locks.
Definition: util-var-name.c:306
VariableName_
Name2idx mapping structure for flowbits, flowvars and pktvars.
Definition: util-var-name.c:77
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:514
VariableName_::type
enum VarTypes type
Definition: util-var-name.c:79
util-validate.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
HashListTableBucket_
Definition: util-hashlist.h:28
SC_ATOMIC_INITPTR
#define SC_ATOMIC_INITPTR(name)
Definition: util-atomic.h:317
VarNameStorePtr
VarNameStore * VarNameStorePtr
Definition: util-var-name.c:74
TAILQ_HEAD
#define TAILQ_HEAD(name, type)
Definition: queue.h:230
VarTypes
VarTypes
Definition: util-var.h:28
likely
#define likely(expr)
Definition: util-optimize.h:32
id
uint32_t id
Definition: detect-flowbits.c:944
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
VARNAME_HASHSIZE
#define VARNAME_HASHSIZE
Definition: util-var-name.c:84
SCTIME_ADD_SECS
#define SCTIME_ADD_SECS(ts, s)
Definition: util-time.h:64
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCMutex
#define SCMutex
Definition: threads-debug.h:114
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
SCTIME_CMP_LTE
#define SCTIME_CMP_LTE(a, b)
Definition: util-time.h:106
VarNameStoreActivate
int VarNameStoreActivate(void)
Definition: util-var-name.c:221
StringHashDjb2
uint32_t StringHashDjb2(const uint8_t *data, uint32_t datalen)
Definition: util-hash-string.c:22