suricata
util-lua-dataset.c
Go to the documentation of this file.
1 /* Copyright (C) 2025 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  * Dataset API for Lua.
22  *
23  * local dataset = require("suricata.dataset")
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "util-lua-dataset.h"
29 
30 #include "app-layer-protos.h" /* Required by util-lua-common. */
31 #include "util-lua-common.h"
32 #include "util-lua.h"
33 #include "util-debug.h"
34 
35 #include "datasets.h"
36 
37 /*
38  * The handle stores a copy of the set name, not a Dataset* pointer.
39  * The engine frees and re-creates sets on rule reload
40  * (DatasetPostReloadCleanup) while output states keep running; a cached
41  * pointer would dangle. Re-resolving the name on every use follows the
42  * reload to the new set and fails cleanly if the set is gone.
43  */
44 struct LuaDataset {
46 };
47 
48 static int LuaDatasetGC(lua_State *luastate)
49 {
50  SCLogDebug("gc:start");
51  struct LuaDataset *s = SCLuaTestUserData(luastate, 1, "dataset::metatable", SCLUA_TUD_DATASET);
52  if (s != NULL) {
53  s->name[0] = '\0';
54  }
55  SCLogDebug("gc:done");
56  return 0;
57 }
58 
59 static int LuaDatasetGetRef(lua_State *luastate)
60 {
61  SCLogDebug("get");
62  struct LuaDataset *s = SCLuaCheckUserData(luastate, 1, "dataset::metatable", SCLUA_TUD_DATASET);
63 
64  const char *name = lua_tostring(luastate, 2);
65  if (name == NULL) {
66  LUA_ERROR("null string");
67  }
68 
69  if (DatasetFind(name, DATASET_TYPE_STRING) == NULL) {
70  LUA_ERROR("dataset not found");
71  }
72  if (strlen(name) >= sizeof(s->name)) {
73  LUA_ERROR("dataset name too long");
74  }
75  snprintf(s->name, sizeof(s->name), "%s", name);
76  return 0;
77 }
78 
79 static int LuaDatasetAdd(lua_State *luastate)
80 {
81  SCLogDebug("add:start");
82  struct LuaDataset *s = SCLuaCheckUserData(luastate, 1, "dataset::metatable", SCLUA_TUD_DATASET);
83  if (s->name[0] == '\0') {
84  LUA_ERROR("dataset is not initialized (call :get first)");
85  }
86 
87  /* re-resolve the set: the cached name is the only engine state we
88  * hold, so a set freed and reloaded under the same name is followed */
90  if (dataset == NULL) {
91  LUA_ERROR("dataset not found");
92  }
93 
94  size_t real_len = 0;
95  const uint8_t *str = (const uint8_t *)lua_tolstring(luastate, 2, &real_len);
96  if (str == NULL) {
97  LUA_ERROR("1st arg is not a string");
98  }
99 
100  if (!lua_isinteger(luastate, 3)) {
101  LUA_ERROR("2nd arg is not a string");
102  }
103  lua_Integer n = lua_tointeger(luastate, 3);
104  if (n < 0 || (size_t)n > real_len) {
105  LUA_ERROR("length out of range for supplied string");
106  }
107  uint32_t str_len = (uint32_t)n;
108 
109  int r = SCDatasetAdd(dataset, str, str_len);
110  /* return value through luastate, as a luanumber */
111  lua_pushnumber(luastate, (lua_Number)r);
112  SCLogDebug("add:end");
113  return 1;
114 }
115 
116 static int LuaDatasetNew(lua_State *luastate)
117 {
118  SCLogDebug("new:start");
119  struct LuaDataset *s = SCLuaNewUserData(luastate, SCLUA_TUD_DATASET, sizeof(*s));
120  if (s == NULL) {
121  LUA_ERROR("failed to get userdata");
122  }
123  memset(s, 0, sizeof(*s));
124  luaL_getmetatable(luastate, "dataset::metatable");
125  lua_setmetatable(luastate, -2);
126  SCLogDebug("new:done");
127  return 1;
128 }
129 
130 // clang-format off
131 static const luaL_Reg datasetlib[] = {
132  { "new", LuaDatasetNew },
133  { "get", LuaDatasetGetRef },
134  { "add", LuaDatasetAdd },
135  { "__gc", LuaDatasetGC },
136  { NULL, NULL }
137 };
138 // clang-format on
139 
141 {
142  luaL_newmetatable(luastate, "dataset::metatable");
143  lua_pushvalue(luastate, -1);
144  lua_setfield(luastate, -2, "__index");
145  luaL_setfuncs(luastate, datasetlib, 0);
146  luaL_newlib(luastate, datasetlib);
147 
148  return 1;
149 }
LUA_ERROR
#define LUA_ERROR(msg)
Definition: util-lua-common.h:195
util-lua-common.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
name
const char * name
Definition: detect-engine-proto.c:47
SCLUA_TUD_DATASET
@ SCLUA_TUD_DATASET
Definition: util-lua-common.h:56
util-lua.h
util-lua-dataset.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:527
datasets.h
util-debug.h
LuaDataset::name
char name[DATASET_NAME_MAX_LEN+1]
Definition: util-lua-dataset.c:45
SCLuaCheckUserData
void * SCLuaCheckUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:101
SCLuaTestUserData
void * SCLuaTestUserData(lua_State *L, int idx, const char *mt, uint32_t type_id)
Definition: util-lua-common.c:123
SCDatasetAdd
int SCDatasetAdd(Dataset *set, const uint8_t *data, const uint32_t data_len)
Definition: datasets.c:1336
LuaDataset
Definition: util-lua-dataset.c:44
SCLuaNewUserData
void * SCLuaNewUserData(lua_State *L, uint32_t type_id, size_t size)
Definition: util-lua-common.c:42
suricata-common.h
DATASET_NAME_MAX_LEN
#define DATASET_NAME_MAX_LEN
Definition: datasets.h:54
LuaLoadDatasetLib
int LuaLoadDatasetLib(lua_State *luastate)
Definition: util-lua-dataset.c:140
DatasetFind
Dataset * DatasetFind(const char *name, enum DatasetTypes type)
look for set by name without creating it
Definition: datasets.c:319
str
#define str(s)
Definition: suricata-common.h:313
DATASET_TYPE_STRING
@ DATASET_TYPE_STRING
Definition: datasets.h:47
app-layer-protos.h
Dataset
Definition: datasets.h:55