Skip to content

Commit

Permalink
lua: Add config override for lua sandbox limits
Browse files Browse the repository at this point in the history
  • Loading branch information
J0eJ0h committed Jan 29, 2024
1 parent 7f345b7 commit a9df34a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/detect-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ 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 @@ -167,6 +164,10 @@ void DetectLuaRegister(void)

#define DATATYPE_BUFFER BIT_U32(22)

// TODO: move to config
#define DEFAULT_LUA_ALLOC_LIMIT 500000
#define DEFAULT_LUA_INSTRUCTION_LIMIT 500000

#if 0
/** \brief dump stack from lua state to screen */
void LuaDumpStack(lua_State *state)
Expand Down Expand Up @@ -607,7 +608,7 @@ static void *DetectLuaThreadInit(void *data)
t->alproto = lua->alproto;
t->flags = lua->flags;

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

lua_State *luastate = sb_newstate(g_lua_alloc_limit, g_lua_instruction_limit);
lua_State *luastate = sb_newstate(ld->alloc_limit, ld->instruction_limit);
if (luastate == NULL)
return -1;
luaL_openlibs(luastate); // TODO: get sandbox config and load appropriate libs
Expand Down Expand Up @@ -1020,12 +1021,20 @@ static int DetectLuaSetup (DetectEngineCtx *de_ctx, Signature *s, const char *st
if (!enabled) {
SCLogError("Lua rules disabled by security configuration: security.lua.allow-rules");
goto error;
}
}

lua = DetectLuaParse(de_ctx, str);
if (lua == NULL)
goto error;

/* Load lua sandbox configurations */
intmax_t lua_alloc_limit = DEFAULT_LUA_ALLOC_LIMIT;
intmax_t lua_instruction_limit = DEFAULT_LUA_INSTRUCTION_LIMIT;
(void)ConfGetInt("security.lua.max-bytes", &lua_alloc_limit);
(void)ConfGetInt("security.lua.max-instructions", &lua_instruction_limit);
lua->alloc_limit = lua_alloc_limit;
lua->instruction_limit = lua_instruction_limit;

if (DetectLuaSetupPrime(de_ctx, lua, s) == -1) {
goto error;
}
Expand Down
2 changes: 2 additions & 0 deletions src/detect-lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ typedef struct DetectLuaData {
uint32_t sid;
uint32_t rev;
uint32_t gid;
uint64_t alloc_limit;
uint64_t instruction_limit;
} DetectLuaData;

#endif /* HAVE_LUA */
Expand Down
9 changes: 5 additions & 4 deletions src/util-lua-sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ static void *sb_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
return NULL;
}
void *nptr = SCRealloc(ptr, nsize);

ctx->alloc_bytes += nsize;
if(nptr != NULL) {
ctx->alloc_bytes += nsize;
}
return nptr;
}
}
Expand All @@ -77,7 +78,7 @@ static const luaL_Reg sb_restrictedlibs[] = { { LUA_GNAME, luaopen_base },
// {LUA_LOADLIBNAME, luaopen_package},
// {LUA_COLIBNAME, luaopen_coroutine},
{ LUA_TABLIBNAME, luaopen_table },
//{LUA_IOLIBNAME, luaopen_io},
// {LUA_IOLIBNAME, luaopen_io},
// {LUA_OSLIBNAME, luaopen_os},
{ LUA_STRLIBNAME, luaopen_string }, { LUA_MATHLIBNAME, luaopen_math },
{ LUA_UTF8LIBNAME, luaopen_utf8 },
Expand Down Expand Up @@ -143,7 +144,7 @@ lua_State *sb_newstate(uint64_t alloclimit, uint64_t instructionlimit)
}
if (sb->L == NULL) {
// TODO: log or error code?
free(sb);
SCFree(sb);
return NULL;
}

Expand Down

0 comments on commit a9df34a

Please sign in to comment.