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 */
40typedef struct SCLuaSbState {
42
43 /* Allocation limits */
45 uint64_t alloc_limit;
46
47 /* Execution Limits */
50 // used by lua_sethook
52
53 /* Errors. */
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 */
68lua_State *SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit);
69
70/*
71 * Replaces lua_close. Handles freeing the SCLuaSbState
72 */
74
75/**
76 * Retreive the SCLuaSbState from a lua_State.
77 */
79
80/*
81 * Resets the instruction counter for the sandbox to 0
82 */
84
85/*
86 * Replaces luaL_openlibs. Only opens allowed packages for the sandbox and
87 * masks out dangerous functions from the base.
88 */
90
91#endif /* SURICATA_UTIL_LUA_SANDBOX_H */
uint64_t alloc_limit
uint64_t instruction_limit
bool instruction_count_error
uint64_t instruction_count
lua_State * L
bool blocked_function_error
struct lua_State lua_State
void SCLuaSbStateClose(lua_State *sb)
void SCLuaSbResetInstructionCounter(lua_State *sb)
void SCLuaSbLoadLibs(lua_State *L)
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
SCLuaSbState * SCLuaSbGetContext(lua_State *L)