luasandbox
1.4.0
Generic Lua sandbox for dynamic data analysis
|
Generic Lua sandbox for dynamic data analysis. More...
Go to the source code of this file.
Macros | |
#define | LSB_EXPORT |
#define | LSB_ERROR_SIZE 256 |
#define | LSB_SHUTTING_DOWN "shutting down" |
#define | LSB_CONFIG_TABLE "lsb_config" |
#define | LSB_THIS_PTR "lsb_this_ptr" |
#define | LSB_MEMORY_LIMIT "memory_limit" |
#define | LSB_INSTRUCTION_LIMIT "instruction_limit" |
#define | LSB_INPUT_LIMIT "input_limit" |
#define | LSB_OUTPUT_LIMIT "output_limit" |
#define | LSB_LOG_LEVEL "log_level" |
#define | LSB_LUA_PATH "path" |
#define | LSB_LUA_CPATH "cpath" |
#define | LSB_NIL_ERROR "<nil error message>" |
Typedefs | |
typedef struct lsb_lua_sandbox | lsb_lua_sandbox |
Enumerations | |
enum | lsb_state { LSB_UNKNOWN = 0, LSB_RUNNING = 1, LSB_TERMINATED = 2, LSB_STOP = 3 } |
enum | lsb_usage_stat { LSB_US_LIMIT = 0, LSB_US_CURRENT = 1, LSB_US_MAXIMUM = 2, LSB_US_MAX } |
enum | lsb_usage_type { LSB_UT_MEMORY = 0, LSB_UT_INSTRUCTION = 1, LSB_UT_OUTPUT = 2, LSB_UT_MAX } |
Functions | |
LSB_EXPORT lsb_lua_sandbox * | lsb_create (void *parent, const char *lua_file, const char *cfg, lsb_logger *logger) |
Allocates and initializes the structure around the Lua sandbox allowing full specification of the sandbox configuration using a Lua configuration string. More... | |
LSB_EXPORT lsb_err_value | lsb_init (lsb_lua_sandbox *lsb, const char *state_file) |
Initializes the Lua sandbox and loads/runs the Lua script that was specified in lua_create_sandbox. More... | |
LSB_EXPORT void | lsb_stop_sandbox_clean (lsb_lua_sandbox *lsb) |
Changes the sandbox state to LSB_STOP to allow for a clean exit. More... | |
LSB_EXPORT void | lsb_stop_sandbox (lsb_lua_sandbox *lsb) |
Aborts the running sandbox from a different thread of execution. More... | |
LSB_EXPORT char * | lsb_destroy (lsb_lua_sandbox *lsb) |
Frees the memory associated with the sandbox. More... | |
LSB_EXPORT size_t | lsb_usage (lsb_lua_sandbox *lsb, lsb_usage_type utype, lsb_usage_stat ustat) |
Retrieve the sandbox usage statistics. More... | |
LSB_EXPORT lsb_state | lsb_get_state (lsb_lua_sandbox *lsb) |
Retrieve the current sandbox status. More... | |
LSB_EXPORT const char * | lsb_get_error (lsb_lua_sandbox *lsb) |
Return the last error in human readable form. More... | |
LSB_EXPORT void | lsb_set_error (lsb_lua_sandbox *lsb, const char *err) |
Sets the last error string. More... | |
LSB_EXPORT lua_State * | lsb_get_lua (lsb_lua_sandbox *lsb) |
Access the Lua pointer. More... | |
LSB_EXPORT const char * | lsb_get_lua_file (lsb_lua_sandbox *lsb) |
Returns the filename of the Lua source. More... | |
LSB_EXPORT void * | lsb_get_parent (lsb_lua_sandbox *lsb) |
Access the parent pointer stored in the sandbox. More... | |
LSB_EXPORT const lsb_logger * | lsb_get_logger (lsb_lua_sandbox *lsb) |
Access the logger struct stored in the sandbox. More... | |
LSB_EXPORT void | lsb_add_function (lsb_lua_sandbox *lsb, lua_CFunction func, const char *func_name) |
Create a CFunction for use by the Sandbox. More... | |
LSB_EXPORT lsb_err_value | lsb_pcall_setup (lsb_lua_sandbox *lsb, const char *func_name) |
Helper function to load the Lua function and set the instruction limits. More... | |
LSB_EXPORT void | lsb_pcall_teardown (lsb_lua_sandbox *lsb) |
Helper function to update the statistics after the call. More... | |
LSB_EXPORT void | lsb_terminate (lsb_lua_sandbox *lsb, const char *err) |
Change the sandbox state to LSB_TERMINATED due to a fatal error. More... | |
Variables | |
LSB_EXPORT lsb_err_id | LSB_ERR_INIT |
LSB_EXPORT lsb_err_id | LSB_ERR_LUA |
LSB_EXPORT lsb_err_id | LSB_ERR_TERMINATED |
Generic Lua sandbox for dynamic data analysis.
Definition in file luasandbox.h.
#define LSB_CONFIG_TABLE "lsb_config" |
Definition at line 31 of file luasandbox.h.
#define LSB_ERROR_SIZE 256 |
Definition at line 28 of file luasandbox.h.
#define LSB_EXPORT |
Definition at line 24 of file luasandbox.h.
#define LSB_INPUT_LIMIT "input_limit" |
Definition at line 35 of file luasandbox.h.
#define LSB_INSTRUCTION_LIMIT "instruction_limit" |
Definition at line 34 of file luasandbox.h.
#define LSB_LOG_LEVEL "log_level" |
Definition at line 37 of file luasandbox.h.
#define LSB_LUA_CPATH "cpath" |
Definition at line 39 of file luasandbox.h.
#define LSB_LUA_PATH "path" |
Definition at line 38 of file luasandbox.h.
#define LSB_MEMORY_LIMIT "memory_limit" |
Definition at line 33 of file luasandbox.h.
#define LSB_NIL_ERROR "<nil error message>" |
Definition at line 40 of file luasandbox.h.
#define LSB_OUTPUT_LIMIT "output_limit" |
Definition at line 36 of file luasandbox.h.
#define LSB_SHUTTING_DOWN "shutting down" |
Definition at line 30 of file luasandbox.h.
#define LSB_THIS_PTR "lsb_this_ptr" |
Definition at line 32 of file luasandbox.h.
typedef struct lsb_lua_sandbox lsb_lua_sandbox |
Definition at line 65 of file luasandbox.h.
enum lsb_state |
Enumerator | |
---|---|
LSB_UNKNOWN | |
LSB_RUNNING | |
LSB_TERMINATED | |
LSB_STOP |
Definition at line 42 of file luasandbox.h.
enum lsb_usage_stat |
Enumerator | |
---|---|
LSB_US_LIMIT | |
LSB_US_CURRENT | |
LSB_US_MAXIMUM | |
LSB_US_MAX |
Definition at line 49 of file luasandbox.h.
enum lsb_usage_type |
Enumerator | |
---|---|
LSB_UT_MEMORY | |
LSB_UT_INSTRUCTION | |
LSB_UT_OUTPUT | |
LSB_UT_MAX |
Definition at line 57 of file luasandbox.h.
LSB_EXPORT void lsb_add_function | ( | lsb_lua_sandbox * | lsb, |
lua_CFunction | func, | ||
const char * | func_name | ||
) |
Create a CFunction for use by the Sandbox.
The Lua sandbox pointer is pushed to upvalue index 1.
lsb | Pointer to the sandbox. |
func | Lua CFunction pointer. |
func_name | Function name exposed to the Lua sandbox. |
LSB_EXPORT lsb_lua_sandbox* lsb_create | ( | void * | parent, |
const char * | lua_file, | ||
const char * | cfg, | ||
lsb_logger * | logger | ||
) |
Allocates and initializes the structure around the Lua sandbox allowing full specification of the sandbox configuration using a Lua configuration string.
memory_limit = 1024*1024*1 instruction_limit = 10000 output_limit = 64*1024 path = '/modules/?.lua' cpath = '/modules/?.so' remove_entries = { [''] = {'collectgarbage','coroutine','dofile','load','loadfile','loadstring', 'newproxy','print'}, os = {'getenv','execute','exit','remove','rename','setlocale','tmpname'} } disable_modules = {io = 1}
parent | Pointer to associate the owner to this sandbox. |
lua_file | Filename of the Lua script to run in this sandbox. |
cfg | Lua structure defining the full sandbox restrictions (may contain optional host configuration options, everything is available to the sandbox through the read_config API. |
logger | Struct for error reporting/debug printing (NULL to disable) |
LSB_EXPORT char* lsb_destroy | ( | lsb_lua_sandbox * | lsb | ) |
Frees the memory associated with the sandbox.
lsb | Sandbox pointer to discard. |
LSB_EXPORT const char* lsb_get_error | ( | lsb_lua_sandbox * | lsb | ) |
Return the last error in human readable form.
lsb | Pointer to the sandbox. |
LSB_EXPORT const lsb_logger* lsb_get_logger | ( | lsb_lua_sandbox * | lsb | ) |
Access the logger struct stored in the sandbox.
The logger callback is only available to modules in debug mode (same as print).
lsb | Pointer to the sandbox. |
LSB_EXPORT lua_State* lsb_get_lua | ( | lsb_lua_sandbox * | lsb | ) |
Access the Lua pointer.
lsb | Pointer to the sandbox. |
LSB_EXPORT const char* lsb_get_lua_file | ( | lsb_lua_sandbox * | lsb | ) |
Returns the filename of the Lua source.
lsb | Pointer to the sandbox. |
LSB_EXPORT void* lsb_get_parent | ( | lsb_lua_sandbox * | lsb | ) |
Access the parent pointer stored in the sandbox.
lsb | Pointer to the sandbox. |
LSB_EXPORT lsb_state lsb_get_state | ( | lsb_lua_sandbox * | lsb | ) |
Retrieve the current sandbox status.
lsb | Pointer to the sandbox. |
LSB_EXPORT lsb_err_value lsb_init | ( | lsb_lua_sandbox * | lsb, |
const char * | state_file | ||
) |
Initializes the Lua sandbox and loads/runs the Lua script that was specified in lua_create_sandbox.
lsb | Pointer to the sandbox. |
state_file | Filename where the global data is read. Use a NULL or empty string for no data restoration. The global _PRESERVATION_VERSION variable will be examined during restoration; if the previous version does not match the current version the restoration will be aborted and the sandbox will start cleanly. _PRESERVATION_VERSION should be incremented any time an incompatible change is made to the global data schema. If no version is set the check will always succeed and a version of zero is assigned. |
LSB_EXPORT lsb_err_value lsb_pcall_setup | ( | lsb_lua_sandbox * | lsb, |
const char * | func_name | ||
) |
Helper function to load the Lua function and set the instruction limits.
lsb | Pointer to the sandbox. |
func_name | Name of the function to load |
LSB_EXPORT void lsb_pcall_teardown | ( | lsb_lua_sandbox * | lsb | ) |
Helper function to update the statistics after the call.
lsb | Pointer to the sandbox. |
LSB_EXPORT void lsb_set_error | ( | lsb_lua_sandbox * | lsb, |
const char * | err | ||
) |
Sets the last error string.
lsb | Pointer to the sandbox. |
err | Error message. |
LSB_EXPORT void lsb_stop_sandbox | ( | lsb_lua_sandbox * | lsb | ) |
Aborts the running sandbox from a different thread of execution.
A "shutting down" Lua error message is generated.
lsb | sandbox to abort |
LSB_EXPORT void lsb_stop_sandbox_clean | ( | lsb_lua_sandbox * | lsb | ) |
Changes the sandbox state to LSB_STOP to allow for a clean exit.
This call is not thread safe.
lsb | sandbox to clean stop |
LSB_EXPORT void lsb_terminate | ( | lsb_lua_sandbox * | lsb, |
const char * | err | ||
) |
Change the sandbox state to LSB_TERMINATED due to a fatal error.
lsb | Pointer to the sandbox. |
err | Reason for termination |
LSB_EXPORT size_t lsb_usage | ( | lsb_lua_sandbox * | lsb, |
lsb_usage_type | utype, | ||
lsb_usage_stat | ustat | ||
) |
Retrieve the sandbox usage statistics.
lsb | Pointer to the sandbox. |
utype | Type of statistic to retrieve i.e. memory. |
ustat | Type of statistic to retrieve i.e. current. |
LSB_EXPORT lsb_err_id LSB_ERR_INIT |
LSB_EXPORT lsb_err_id LSB_ERR_LUA |
LSB_EXPORT lsb_err_id LSB_ERR_TERMINATED |