luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
luasandbox.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Generic Lua sandbox for dynamic data analysis @file */
8 
9 #ifndef luasandbox_h_
10 #define luasandbox_h_
11 
12 #include "luasandbox/error.h"
13 
14 #ifdef _WIN32
15 #ifdef luasandbox_EXPORTS
16 #define LSB_EXPORT __declspec(dllexport)
17 #else
18 #define LSB_EXPORT __declspec(dllimport)
19 #endif
20 #else
21 #if __GNUC__ >= 4
22 #define LSB_EXPORT __attribute__ ((visibility ("default")))
23 #else
24 #define LSB_EXPORT
25 #endif
26 #endif
27 
28 #define LSB_ERROR_SIZE 256
29 
30 #define LSB_SHUTTING_DOWN "shutting down"
31 #define LSB_CONFIG_TABLE "lsb_config"
32 #define LSB_THIS_PTR "lsb_this_ptr"
33 #define LSB_MEMORY_LIMIT "memory_limit"
34 #define LSB_INSTRUCTION_LIMIT "instruction_limit"
35 #define LSB_INPUT_LIMIT "input_limit"
36 #define LSB_OUTPUT_LIMIT "output_limit"
37 #define LSB_LOG_LEVEL "log_level"
38 #define LSB_LUA_PATH "path"
39 #define LSB_LUA_CPATH "cpath"
40 #define LSB_NIL_ERROR "<nil error message>"
41 
42 typedef enum {
47 } lsb_state;
48 
49 typedef enum {
53 
56 
57 typedef enum {
61 
64 
66 
67 #ifdef __cplusplus
68 extern "C"
69 {
70 #endif
71 
72 #include "luasandbox/lua.h"
73 
77 
78 /**
79  * Allocates and initializes the structure around the Lua sandbox allowing
80  * full specification of the sandbox configuration using a Lua configuration
81  * string.
82  * memory_limit = 1024*1024*1
83  * instruction_limit = 10000
84  * output_limit = 64*1024
85  * path = '/modules/?.lua'
86  * cpath = '/modules/?.so'
87  * remove_entries = {
88  * [''] =
89  * {'collectgarbage','coroutine','dofile','load','loadfile','loadstring',
90  * 'newproxy','print'},
91  * os = {'getenv','execute','exit','remove','rename','setlocale','tmpname'}
92  * }
93  * disable_modules = {io = 1}
94  *
95  *
96  * @param parent Pointer to associate the owner to this sandbox.
97  * @param lua_file Filename of the Lua script to run in this sandbox.
98  * @param cfg Lua structure defining the full sandbox restrictions (may contain
99  * optional host configuration options, everything is available to
100  * the sandbox through the read_config API.
101  * @param logger Struct for error reporting/debug printing (NULL to disable)
102  * @return lsb_lua_sandbox Sandbox pointer or NULL on failure.
103  */
105 lsb_create(void *parent, const char *lua_file, const char *cfg,
106  lsb_logger *logger);
107 
108 /**
109  * Initializes the Lua sandbox and loads/runs the Lua script that was specified
110  * in lua_create_sandbox.
111  *
112  * @param lsb Pointer to the sandbox.
113  * @param state_file Filename where the global data is read. Use a NULL or empty
114  * string for no data restoration. The global
115  * _PRESERVATION_VERSION variable will be examined during
116  * restoration; if the previous version does not match the
117  * current version the restoration will be aborted and the
118  * sandbox will start cleanly. _PRESERVATION_VERSION should be
119  * incremented any time an incompatible change is made to the
120  * global data schema. If no version is set the check will
121  * always succeed and a version of zero is assigned.
122  *
123  * @return lsb_err_value NULL on success error message on failure
124  *
125  */
127 lsb_init(lsb_lua_sandbox *lsb, const char *state_file);
128 
129 /**
130  * Changes the sandbox state to LSB_STOP to allow for a clean exit. This call is
131  * not thread safe.
132  *
133  * @param lsb sandbox to clean stop
134  *
135  * @return
136  *
137  */
139 
140 /**
141  * Aborts the running sandbox from a different thread of execution. A "shutting
142  * down" Lua error message is generated.
143  *
144  * @param lsb sandbox to abort
145  *
146  * @return
147  *
148  */
150 
151 /**
152  * Frees the memory associated with the sandbox.
153  *
154  * @param lsb Sandbox pointer to discard.
155  *
156  * @return NULL on success, pointer to an error message on failure that MUST BE
157  * FREED by the caller.
158  */
160 
161 /**
162  * Retrieve the sandbox usage statistics.
163  *
164  * @param lsb Pointer to the sandbox.
165  * @param utype Type of statistic to retrieve i.e. memory.
166  * @param ustat Type of statistic to retrieve i.e. current.
167  *
168  * @return size_t Count or number of bytes depending on the statistic.
169  */
171  lsb_usage_type utype,
172  lsb_usage_stat ustat);
173 /**
174  * Retrieve the current sandbox status.
175  *
176  * @param lsb Pointer to the sandbox.
177  *
178  * @return lsb_state code
179  */
181 
182 /**
183  * Return the last error in human readable form.
184  *
185  * @param lsb Pointer to the sandbox.
186  *
187  * @return const char* error message
188  */
189 LSB_EXPORT const char* lsb_get_error(lsb_lua_sandbox *lsb);
190 
191 /**
192  * Sets the last error string.
193  *
194  * @param lsb Pointer to the sandbox.
195  * @param err Error message.
196  *
197  * @return const char* error message
198  */
199 LSB_EXPORT void lsb_set_error(lsb_lua_sandbox *lsb, const char *err);
200 
201 /**
202  * Access the Lua pointer.
203  *
204  * @param lsb Pointer to the sandbox.
205  *
206  * @return lua_State* The lua_State pointer.
207  */
209 
210 /**
211  * Returns the filename of the Lua source.
212  *
213  * @param lsb Pointer to the sandbox.
214  *
215  * @return const char* filename.
216  */
218 
219 /**
220  * Access the parent pointer stored in the sandbox.
221  *
222  * @param lsb Pointer to the sandbox.
223  *
224  * @return void* The parent pointer passed to init.
225  */
227 
228 /**
229  * Access the logger struct stored in the sandbox. The logger callback is only
230  * available to modules in debug mode (same as print).
231  *
232  * @param lsb Pointer to the sandbox.
233  *
234  * @return lsb_logger Pointer to the logger struct
235  *
236  */
238 
239 /**
240  * Create a CFunction for use by the Sandbox. The Lua sandbox pointer is pushed
241  * to upvalue index 1.
242  *
243  * @param lsb Pointer to the sandbox.
244  * @param func Lua CFunction pointer.
245  * @param func_name Function name exposed to the Lua sandbox.
246  */
248  lua_CFunction func,
249  const char *func_name);
250 
251 /**
252  * Helper function to load the Lua function and set the instruction limits
253  *
254  * @param lsb Pointer to the sandbox.
255  * @param func_name Name of the function to load
256  *
257  * @return lsb_err_value NULL on success error message on failure
258  */
260 lsb_pcall_setup(lsb_lua_sandbox *lsb, const char *func_name);
261 
262 /**
263  * Helper function to update the statistics after the call
264  *
265  * @param lsb Pointer to the sandbox.
266  */
268 
269 /**
270  * Change the sandbox state to LSB_TERMINATED due to a fatal error.
271  *
272  * @param lsb Pointer to the sandbox.
273  * @param err Reason for termination
274  */
275 LSB_EXPORT void lsb_terminate(lsb_lua_sandbox *lsb, const char *err);
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif
LSB_EXPORT lua_State * lsb_get_lua(lsb_lua_sandbox *lsb)
Access the Lua pointer.
struct lsb_lua_sandbox lsb_lua_sandbox
Definition: luasandbox.h:65
const char * lsb_err_value
Definition: error.h:15
LSB_EXPORT lsb_err_id LSB_ERR_TERMINATED
lsb_usage_type
Definition: luasandbox.h:57
LSB_EXPORT lsb_state lsb_get_state(lsb_lua_sandbox *lsb)
Retrieve the current sandbox status.
const char lsb_err_id[]
Definition: error.h:14
LSB_EXPORT size_t lsb_usage(lsb_lua_sandbox *lsb, lsb_usage_type utype, lsb_usage_stat ustat)
Retrieve the sandbox usage statistics.
LSB_EXPORT lsb_err_id LSB_ERR_INIT
lsb_usage_stat
Definition: luasandbox.h:49
LSB_EXPORT void lsb_set_error(lsb_lua_sandbox *lsb, const char *err)
Sets the last error string.
Error handling and logging.
LSB_EXPORT const char * lsb_get_error(lsb_lua_sandbox *lsb)
Return the last error in human readable form.
int(* lua_CFunction)(lua_State *L)
Definition: lua.h:52
struct lua_State lua_State
Definition: lua.h:50
LSB_EXPORT void lsb_stop_sandbox(lsb_lua_sandbox *lsb)
Aborts the running sandbox from a different thread of execution.
LSB_EXPORT const lsb_logger * lsb_get_logger(lsb_lua_sandbox *lsb)
Access the logger struct stored in 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_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_EXPORT lsb_err_id LSB_ERR_LUA
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 san...
LSB_EXPORT void lsb_stop_sandbox_clean(lsb_lua_sandbox *lsb)
Changes the sandbox state to LSB_STOP to allow for a clean exit.
LSB_EXPORT const char * lsb_get_lua_file(lsb_lua_sandbox *lsb)
Returns the filename of the Lua source.
lsb_state
Definition: luasandbox.h:42
LSB_EXPORT void lsb_pcall_teardown(lsb_lua_sandbox *lsb)
Helper function to update the statistics after the call.
#define LSB_EXPORT
Definition: luasandbox.h:24
LSB_EXPORT void * lsb_get_parent(lsb_lua_sandbox *lsb)
Access the parent pointer stored in the sandbox.
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_EXPORT char * lsb_destroy(lsb_lua_sandbox *lsb)
Frees the memory associated with the sandbox.
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.