suricata
util-lua-sandbox.h
Go to the documentation of this file.
1 /* Copyright (C) 2023-2024 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  * \author Jo Johnson <pyrojoe314@gmail.com>
22  */
23 
24 #ifndef SURICATA_UTIL_LUA_SANDBOX_H
25 #define SURICATA_UTIL_LUA_SANDBOX_H
26 
27 #include "lua.h"
28 #include "suricata-common.h"
29 
30 /*
31  * Lua sandbox usage: The only needed changes to use the sandboxed lua state are
32  * to replace calls to lua_newstate and lua_close with SCLuaSbStateNew and SCLuaSbStateClose
33  * Additionally, SCLuaSbLoadRestricted can be used to load a restricted set of packages
34  * that prevent side effecting outside of the lua runtime
35  */
36 
37 /*
38  * Struct to store a lua_state and the additional metadata required to sandbox it
39  */
40 typedef struct SCLuaSbState {
42 
43  /* Allocation limits */
44  size_t alloc_bytes;
45  uint64_t alloc_limit;
46 
47  /* Execution Limits */
51 
52  /* Errors. */
57 
58 /*
59  * Replaces luaL_newstate. Sets an upper bound for allocations and bytecode
60  * instructions for the lua runtime on this state.
61  *
62  * alloclimit - maximium number of bytes lua can allocate before receiving out of memory.
63  * A value of zero will not limit allocations
64  * instructionlimit - maximum number of lua bytecode instructions before an error is thrown
65  * A value of zero will not limit the number of instructions
66  */
67 lua_State *SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit);
68 
69 /*
70  * Replaces lua_close. Handles freeing the SCLuaSbState
71  */
73 
74 /**
75  * Retreive the SCLuaSbState from a lua_State.
76  */
78 
79 /*
80  * Resets the instruction counter for the sandbox to 0
81  */
83 
84 /*
85  * Replaces luaL_openlibs. Only opens allowed packages for the sandbox and
86  * masks out dangerous functions from the base.
87  */
88 void SCLuaSbLoadLibs(lua_State *L);
89 
90 #endif /* SURICATA_UTIL_LUA_SANDBOX_H */
SCLuaSbStateClose
void SCLuaSbStateClose(lua_State *sb)
Definition: util-lua-sandbox.c:345
SCLuaSbState::memory_limit_error
bool memory_limit_error
Definition: util-lua-sandbox.h:55
SCLuaSbState
Definition: util-lua-sandbox.h:40
SCLuaSbState::L
lua_State * L
Definition: util-lua-sandbox.h:41
SCLuaSbLoadLibs
void SCLuaSbLoadLibs(lua_State *L)
Definition: util-lua-sandbox.c:268
SCLuaSbState
struct SCLuaSbState SCLuaSbState
SCLuaSbState::hook_instruction_count
uint64_t hook_instruction_count
Definition: util-lua-sandbox.h:50
lua_State
struct lua_State lua_State
Definition: suricata-common.h:506
SCLuaSbResetInstructionCounter
void SCLuaSbResetInstructionCounter(lua_State *sb)
Definition: util-lua-sandbox.c:372
SCLuaSbState::instruction_count
uint64_t instruction_count
Definition: util-lua-sandbox.h:48
SCLuaSbState::blocked_function_error
bool blocked_function_error
Definition: util-lua-sandbox.h:53
suricata-common.h
SCLuaSbState::instruction_limit
uint64_t instruction_limit
Definition: util-lua-sandbox.h:49
SCLuaSbState::alloc_bytes
size_t alloc_bytes
Definition: util-lua-sandbox.h:44
SCLuaSbState::alloc_limit
uint64_t alloc_limit
Definition: util-lua-sandbox.h:45
SCLuaSbState::instruction_count_error
bool instruction_count_error
Definition: util-lua-sandbox.h:54
SCLuaSbGetContext
SCLuaSbState * SCLuaSbGetContext(lua_State *L)
Definition: util-lua-sandbox.c:336
SCLuaSbStateNew
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
Definition: util-lua-sandbox.c:304