@ -84,6 +84,8 @@ static void *LuaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
*/
static int LuaBlockedFunction ( lua_State * L )
{
SCLuaSbState * context = SCLuaSbGetContext ( L ) ;
context - > blocked_function_error = true ;
lua_Debug ar ;
lua_getstack ( L , 0 , & ar ) ;
lua_getinfo ( L , " n " , & ar ) ;
@ -311,19 +313,24 @@ lua_State *SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
return sb - > L ;
}
static SCLuaSbState * GetContext ( lua_State * L )
/**
* Get the Suricata Lua sandbox context from the lua_State .
*
* Note : May return null if this Lua state was not allocated from the
* sandbox .
*/
SCLuaSbState * SCLuaSbGetContext ( lua_State * L )
{
lua_pushstring ( L , SANDBOX_CTX ) ;
lua_gettable ( L , LUA_REGISTRYINDEX ) ;
SCLuaSbState * ctx = lua_touserdata ( L , - 1 ) ;
// TODO: log if null?
lua_pop ( L , 1 ) ;
return ctx ;
}
void SCLuaSbStateClose ( lua_State * L )
{
SCLuaSbState * sb = GetContext( L ) ;
SCLuaSbState * sb = SCLuaSb GetContext( L ) ;
lua_close ( sb - > L ) ;
SCFree ( sb ) ;
}
@ -334,7 +341,7 @@ void SCLuaSbStateClose(lua_State *L)
static void HookFunc ( lua_State * L , lua_Debug * ar )
{
( void ) ar ;
SCLuaSbState * sb = GetContext( L ) ;
SCLuaSbState * sb = SCLuaSb GetContext( L ) ;
sb - > instruction_count + = sb - > hook_instruction_count ;
@ -349,8 +356,9 @@ static void HookFunc(lua_State *L, lua_Debug *ar)
*/
void SCLuaSbResetInstructionCounter ( lua_State * L )
{
SCLuaSbState * sb = GetContext( L ) ;
SCLuaSbState * sb = SCLuaSb GetContext( L ) ;
if ( sb ! = NULL ) {
sb - > blocked_function_error = false ;
sb - > instruction_count = 0 ;
lua_sethook ( L , HookFunc , LUA_MASKCOUNT , sb - > hook_instruction_count ) ;
}
@ -358,7 +366,7 @@ void SCLuaSbResetInstructionCounter(lua_State *L)
static void SetInstructionCount ( lua_State * L , uint64_t instruction_limit )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
ctx - > instruction_limit = instruction_limit ;
}
@ -366,7 +374,7 @@ static void SetInstructionCount(lua_State *L, uint64_t instruction_limit)
static uint64_t GetInstructionCount ( lua_State * L )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
return ctx - > instruction_count ;
}
@ -375,7 +383,7 @@ static uint64_t GetInstructionCount(lua_State *L)
static int L_TotalAlloc ( lua_State * L )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
lua_pushinteger ( L , ctx - > alloc_bytes ) ;
} else {
@ -386,7 +394,7 @@ static int L_TotalAlloc(lua_State *L)
static int L_GetAllocLimit ( lua_State * L )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
lua_pushinteger ( L , ctx - > alloc_limit ) ;
} else {
@ -397,7 +405,7 @@ static int L_GetAllocLimit(lua_State *L)
static int L_SetAllocLimit ( lua_State * L )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
ctx - > alloc_limit = luaL_checkinteger ( L , 1 ) ;
}
@ -412,7 +420,7 @@ static int L_GetInstructionCount(lua_State *L)
static int L_GetInstructionLimit ( lua_State * L )
{
SCLuaSbState * ctx = GetContext( L ) ;
SCLuaSbState * ctx = SCLuaSb GetContext( L ) ;
if ( ctx ! = NULL ) {
lua_pushinteger ( L , ctx - > instruction_limit ) ;
} else {