Skip to content

Commit

Permalink
Wire up lua sandbox to detection
Browse files Browse the repository at this point in the history
  • Loading branch information
J0eJ0h committed Jan 4, 2024
1 parent a3a2924 commit 638ed0f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ noinst_HEADERS = \
util-lua-http.h \
util-lua-ja3.h \
util-luajit.h \
util-lua-sandbox.h \
util-lua-smtp.h \
util-lua-ssh.h \
util-lua-tls.h \
Expand Down Expand Up @@ -1173,6 +1174,7 @@ libsuricata_c_a_SOURCES = \
util-lua-http.c \
util-lua-ja3.c \
util-luajit.c \
util-lua-sandbox.c \
util-lua-smtp.c \
util-lua-ssh.c \
util-lua-tls.c \
Expand Down
12 changes: 8 additions & 4 deletions src/detect-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void DetectLuaRegister(void)
#else /* HAVE_LUA */

#include "util-lua.h"
#include "util-lua-sandbox.h"

static int DetectLuaMatch (DetectEngineThreadCtx *,
Packet *, const Signature *, const SigMatchCtx *);
Expand All @@ -105,6 +106,9 @@ static void DetectLuaRegisterTests(void);
static void DetectLuaFree(DetectEngineCtx *, void *);
static int g_smtp_generic_list_id = 0;

// TODO: move to config
static const uint64_t g_lua_alloc_limit = 500000, g_lua_instruction_limit = 500000;

/**
* \brief Registration function for keyword: lua
*/
Expand Down Expand Up @@ -605,7 +609,7 @@ static void *DetectLuaThreadInit(void *data)
t->alproto = lua->alproto;
t->flags = lua->flags;

t->luastate = LuaGetState();
t->luastate = sb_newstate(g_lua_alloc_limit, g_lua_instruction_limit);
if (t->luastate == NULL) {
SCLogError("luastate pool depleted");
goto error;
Expand Down Expand Up @@ -707,7 +711,7 @@ static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld, const
{
int status;

lua_State *luastate = luaL_newstate();
lua_State *luastate = sb_newstate(g_lua_alloc_limit, g_lua_instruction_limit);
if (luastate == NULL)
return -1;
luaL_openlibs(luastate);
Expand Down Expand Up @@ -989,10 +993,10 @@ static int DetectLuaSetupPrime(DetectEngineCtx *de_ctx, DetectLuaData *ld, const

/* pop the table */
lua_pop(luastate, 1);
lua_close(luastate);
sb_close(luastate);
return 0;
error:
lua_close(luastate);
sb_close(luastate);
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions src/detect-lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifdef HAVE_LUA

#include "util-lua.h"
#include "util-lua-sandbox.h"

typedef struct DetectLuaThreadData {
lua_State *luastate;
Expand Down
7 changes: 6 additions & 1 deletion src/util-lua-sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* \author Jo Johnson <[email protected]>
*/

#ifndef __UTIL_LUA_SANDBOX_H__
#define __UTIL_LUA_SANDBOX_H__

#ifndef HAVE_LUA
/* If we don't have Lua, create a typedef for sb_lua_State so the
* exported Lua functions don't fail the build. */
Expand Down Expand Up @@ -85,4 +88,6 @@ void sb_resetinstructioncounter(lua_State *sb);
*/
LUALIB_API void sb_loadrestricted(lua_State *L);

#endif
#endif /* HAVE_LUA */

#endif /* __UTIL_LUA_SANDBOX_H__ */
3 changes: 2 additions & 1 deletion src/util-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <lauxlib.h>

#include "util-lua.h"
#include "util-lua-sandbox.h"

lua_State *LuaGetState(void)
{
Expand All @@ -77,7 +78,7 @@ void LuaReturnState(lua_State *s)
#ifdef HAVE_LUAJIT
LuajitReturnState(s);
#else
lua_close(s);
sb_close(s);
#endif
}
}
Expand Down

0 comments on commit 638ed0f

Please sign in to comment.