Skip to content

Commit d63ad75

Browse files
jasonishvictorjulien
authored andcommitted
lua: add "builtins" file to consolidate registration
Use a single array of built-ins and provide 2 functions for registering them: - SCLuaLoadBuiltIn: for loading built-in modules in sandboxed environments. - SCLuaRequirefBuiltIns: registers built-in modules with the standard package tool, allows built-ins to be loaded by output scripts that are not restricted I hope to refactor the sandbox so they can use SCLuaRequirefBuiltIns as well.
1 parent c8b28b1 commit d63ad75

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ noinst_HEADERS = \
506506
util-landlock.h \
507507
util-logopenfile.h \
508508
util-log-redis.h \
509+
util-lua-builtins.h \
509510
util-lua-common.h \
510511
util-lua-dataset.h \
511512
util-lua-dnp3.h \
@@ -1056,6 +1057,7 @@ libsuricata_c_a_SOURCES = \
10561057
util-logopenfile.c \
10571058
util-log-redis.c \
10581059
util-lua.c \
1060+
util-lua-builtins.c \
10591061
util-lua-common.c \
10601062
util-lua-dataset.c \
10611063
util-lua-dnp3.c \

src/output-lua.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "suricata-common.h"
2626
#include "output-lua.h"
2727

28+
#include "util-lua-builtins.h"
2829
#include "util-print.h"
2930
#include "util-unittest.h"
3031
#include "util-debug.h"
@@ -417,6 +418,7 @@ static int LuaScriptInit(const char *filename, LogLuaScriptOptions *options) {
417418
if (luastate == NULL)
418419
goto error;
419420
luaL_openlibs(luastate);
421+
SCLuaRequirefBuiltIns(luastate);
420422

421423
int status = luaL_loadfile(luastate, filename);
422424
if (status) {
@@ -551,6 +553,7 @@ static lua_State *LuaScriptSetup(const char *filename)
551553
}
552554

553555
luaL_openlibs(luastate);
556+
SCLuaRequirefBuiltIns(luastate);
554557

555558
int status = luaL_loadfile(luastate, filename);
556559
if (status) {

src/util-lua-builtins.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#include "suricata-common.h"
19+
#include "util-lua-builtins.h"
20+
#include "util-lua-hashlib.h"
21+
#include "util-lua-dataset.h"
22+
23+
#include "lauxlib.h"
24+
25+
static const luaL_Reg builtins[] = {
26+
{ "suricata.hashlib", SCLuaLoadHashlib },
27+
{ "suricata.dataset", LuaLoadDatasetLib },
28+
{ NULL, NULL },
29+
};
30+
31+
/**
32+
* \brief Load a Suricata built-in module in a sand-boxed environment.
33+
*/
34+
bool SCLuaLoadBuiltIns(lua_State *L, const char *name)
35+
{
36+
for (const luaL_Reg *lib = builtins; lib->name; lib++) {
37+
if (strcmp(name, lib->name) == 0) {
38+
lib->func(L);
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
/**
46+
* \brief Register Suricata built-in modules for loading in a
47+
* non-sandboxed environment.
48+
*/
49+
void SCLuaRequirefBuiltIns(lua_State *L)
50+
{
51+
for (const luaL_Reg *lib = builtins; lib->name; lib++) {
52+
luaL_requiref(L, lib->name, lib->func, 0);
53+
lua_pop(L, 1);
54+
}
55+
}

src/util-lua-builtins.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#ifndef SURICATA_UTIL_LUA_BUILTINS_H
19+
#define SURICATA_UTIL_LUA_BUILTINS_H
20+
21+
#include "lua.h"
22+
23+
bool SCLuaLoadBuiltIns(lua_State *L, const char *name);
24+
void SCLuaRequirefBuiltIns(lua_State *L);
25+
26+
#endif /* SURICATA_UTIL_LUA_BUILTINS_H */

src/util-lua-dataset.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ static const luaL_Reg datasetlib[] = {
120120
};
121121
// clang-format on
122122

123-
void LuaLoadDatasetLib(lua_State *luastate)
123+
int LuaLoadDatasetLib(lua_State *luastate)
124124
{
125125
luaL_newmetatable(luastate, "dataset::metatable");
126126
lua_pushvalue(luastate, -1);
127127
lua_setfield(luastate, -2, "__index");
128128
luaL_setfuncs(luastate, datasetlib, 0);
129129
luaL_newlib(luastate, datasetlib);
130+
131+
return 1;
130132
}

src/util-lua-dataset.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
#include "lua.h"
2222

23-
void LuaLoadDatasetLib(lua_State *luastate);
23+
int LuaLoadDatasetLib(lua_State *luastate);
2424

2525
#endif /* SURICATA_UTIL_LUA_DATASET_H */

src/util-lua-sandbox.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131
#include "util-debug.h"
3232
#include "util-lua-sandbox.h"
33-
#include "util-lua-dataset.h"
34-
#include "util-lua-hashlib.h"
33+
#include "util-lua-builtins.h"
3534

3635
#define SANDBOX_CTX "SANDBOX_CTX"
3736

@@ -264,11 +263,7 @@ static int SCLuaSbRequire(lua_State *L)
264263
{
265264
const char *module_name = luaL_checkstring(L, 1);
266265

267-
if (strcmp(module_name, "suricata.dataset") == 0) {
268-
LuaLoadDatasetLib(L);
269-
return 1;
270-
} else if (strcmp(module_name, "suricata.hashlib") == 0) {
271-
SCLuaLoadHashlib(L);
266+
if (SCLuaLoadBuiltIns(L, module_name)) {
272267
return 1;
273268
}
274269

0 commit comments

Comments
 (0)