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
{
41
lua_State
*
L
;
42
43
/* Allocation limits */
44
size_t
alloc_bytes
;
45
uint64_t
alloc_limit
;
46
47
/* Execution Limits */
48
uint64_t
instruction_count
;
49
uint64_t
instruction_limit
;
50
// used by lua_sethook
51
int
hook_instruction_count
;
52
53
/* Errors. */
54
bool
blocked_function_error
;
55
bool
instruction_count_error
;
56
bool
memory_limit_error
;
57
}
SCLuaSbState
;
58
59
/*
60
* Replaces luaL_newstate. Sets an upper bound for allocations and bytecode
61
* instructions for the lua runtime on this state.
62
*
63
* alloclimit - maximium number of bytes lua can allocate before receiving out of memory.
64
* A value of zero will not limit allocations
65
* instructionlimit - maximum number of lua bytecode instructions before an error is thrown
66
* A value of zero will not limit the number of instructions
67
*/
68
lua_State
*
SCLuaSbStateNew
(uint64_t alloclimit, uint64_t instructionlimit);
69
70
/*
71
* Replaces lua_close. Handles freeing the SCLuaSbState
72
*/
73
void
SCLuaSbStateClose
(
lua_State
*sb);
74
75
/**
76
* Retreive the SCLuaSbState from a lua_State.
77
*/
78
SCLuaSbState
*
SCLuaSbGetContext
(
lua_State
*L);
79
80
/*
81
* Resets the instruction counter for the sandbox to 0
82
*/
83
void
SCLuaSbResetInstructionCounter
(
lua_State
*sb);
84
85
/*
86
* Replaces luaL_openlibs. Only opens allowed packages for the sandbox and
87
* masks out dangerous functions from the base.
88
*/
89
void
SCLuaSbLoadLibs
(
lua_State
*L);
90
91
#endif
/* SURICATA_UTIL_LUA_SANDBOX_H */
SCLuaSbStateClose
void SCLuaSbStateClose(lua_State *sb)
Definition:
util-lua-sandbox.c:360
SCLuaSbState::memory_limit_error
bool memory_limit_error
Definition:
util-lua-sandbox.h:56
SCLuaSbState
Definition:
util-lua-sandbox.h:40
SCLuaSbState::hook_instruction_count
int hook_instruction_count
Definition:
util-lua-sandbox.h:51
SCLuaSbState::L
lua_State * L
Definition:
util-lua-sandbox.h:41
SCLuaSbLoadLibs
void SCLuaSbLoadLibs(lua_State *L)
Definition:
util-lua-sandbox.c:279
SCLuaSbState
struct SCLuaSbState SCLuaSbState
lua_State
struct lua_State lua_State
Definition:
suricata-common.h:515
SCLuaSbResetInstructionCounter
void SCLuaSbResetInstructionCounter(lua_State *sb)
Definition:
util-lua-sandbox.c:387
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:54
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:55
SCLuaSbGetContext
SCLuaSbState * SCLuaSbGetContext(lua_State *L)
Definition:
util-lua-sandbox.c:351
SCLuaSbStateNew
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
Definition:
util-lua-sandbox.c:319
src
util-lua-sandbox.h
Generated on Fri Feb 21 2025 23:30:36 for suricata by
1.8.18