Go to the documentation of this file.
42 static void TransformLuaxform(
45 #define LUAXFORM_MAX_ARGS 10
51 #define LFLAG_ERROR_LOGGED BIT_U32(27)
52 #define LFLAG_BLOCKED_FUNCTION_LOGGED BIT_U32(28)
53 #define LFLAG_INSTRUCTION_LIMIT_LOGGED BIT_U32(29)
54 #define LFLAG_MEMORY_LIMIT_LOGGED BIT_U32(30)
73 static void DetectTransformLuaxformId(
const uint8_t **data, uint32_t *
length,
const void *context)
77 *data = (uint8_t *)lua->
id_data;
104 static int DetectTransformLuaxformSetupPrime(
108 if (luastate == NULL)
111 luaL_openlibs(luastate);
117 int status = luaL_loadfile(luastate, ld->
filename);
119 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
124 if (lua_pcall(luastate, 0, 0, 0) != 0) {
125 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
129 lua_getglobal(luastate,
"transform");
130 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
131 SCLogError(
"no transform function in script");
134 lua_pop(luastate, 1);
151 FatalError(
"unable to allocate memory for Lua transform: %s", optsstr);
154 lua->
copystr = strdup(optsstr);
155 lua->
id_data = strdup(optsstr);
157 FatalError(
"unable to allocate memory for Lua transform: %s", optsstr);
163 char *saveptr = NULL;
164 char *token = strtok_r(lua->
copystr,
",", &saveptr);
166 lua->
args[count++] = token;
167 token = strtok_r(NULL,
",", &saveptr);
187 DetectTransformLuaxformFree(
de_ctx, lua);
191 static void *DetectLuaxformThreadInit(
void *data)
198 FatalError(
"unable to allocate luaxform context memory");
224 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
230 lua_getglobal(t->
luastate,
"thread_init");
231 if (lua_isfunction(t->
luastate, -1)) {
233 if (lua_pcall(t->
luastate, 0, 0, 0) != 0) {
234 SCLogError(
"couldn't run script 'thread_init' function: %s",
251 static void DetectLuaxformThreadFree(
void *
ctx)
277 if (
SCConfGetBool(
"security.lua.allow-rules", &enabled) == 1 && !enabled) {
278 SCLogError(
"Lua rules disabled by security configuration: security.lua.allow-rules");
289 int allow_restricted_functions = 0;
290 (void)
SCConfGetInt(
"security.lua.max-bytes", &lua_alloc_limit);
291 (void)
SCConfGetInt(
"security.lua.max-instructions", &lua_instruction_limit);
292 (void)
SCConfGetBool(
"security.lua.allow-restricted-functions", &allow_restricted_functions);
296 if (lua_alloc_limit < 0) {
297 SCLogError(
"security.lua.max-bytes must be non-negative (got %" PRIdMAX
"), "
298 "use 0 for no limit",
307 if (DetectTransformLuaxformSetupPrime(
de_ctx, lua, s) == -1) {
312 de_ctx,
"luaxform", DetectLuaxformThreadInit, (
void *)lua, DetectLuaxformThreadFree, 0);
322 DetectTransformLuaxformFree(
de_ctx, lua);
326 static void TransformLuaxform(
340 lua_getglobal(tlua->
luastate,
"transform");
342 const uint8_t *input = buffer->
inspect;
359 for (
int i = 1; i < lua->
arg_count + 1; i++) {
370 if (LUA_OK != lua_pcall(tlua->
luastate, 2, 2, 0)) {
371 const char *reason = lua_tostring(tlua->
luastate, -1);
386 reason =
"memory limit exceeded";
388 if (!(tlua->
flags & flag)) {
389 SCLogWarning(
"luaxform '%s' failed to run successfully: %s "
390 "(buffer keeps the untransformed data)",
396 int return_value_count = lua_gettop(tlua->
luastate);
397 if (return_value_count != 2) {
400 SCLogWarning(
"luaxform '%s': expected 2 return values (buffer, length) "
401 "but got %d; keeping the untransformed buffer",
408 if (lua_isstring(tlua->
luastate, -2)) {
410 const char *transformed_buffer = lua_tolstring(tlua->
luastate, -2, &real_len);
412 lua_Integer transformed_buffer_byte_count = lua_tointegerx(tlua->
luastate, -1, &isnum);
417 if (transformed_buffer == NULL || !isnum || transformed_buffer_byte_count < 0 ||
418 (
size_t)transformed_buffer_byte_count > real_len ||
419 (
size_t)transformed_buffer_byte_count > UINT32_MAX) {
423 SCLogWarning(
"luaxform '%s' returned an invalid length "
424 "%" PRIi64
" (string len %zu); keeping the "
425 "untransformed buffer",
426 lua->
filename, (int64_t)transformed_buffer_byte_count, real_len);
428 SCLogWarning(
"luaxform '%s' returned a non-numeric length; keeping "
429 "the untransformed buffer",
434 }
else if (transformed_buffer_byte_count > 0) {
439 (uint32_t)transformed_buffer_byte_count);
440 SCLogDebug(
"transform returns [nbytes %" PRIi64
"] \"%p\"",
441 (int64_t)transformed_buffer_byte_count, transformed_buffer);
446 SCLogWarning(
"luaxform '%s' returned a non-string buffer; keeping the "
447 "untransformed buffer",
455 while (lua_gettop(tlua->
luastate) > 0) {
465 "pass inspection buffer to a Lua function along with "
466 "arguments supplied to the transform";
SigTableElmt * sigmatch_table
void SCLuaSbUpdateBytesLimit(lua_State *L)
void(* Free)(DetectEngineCtx *, void *)
uint64_t SCLuaSbResetBytesLimit(lua_State *L)
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
SCLuaSbState * SCLuaSbGetContext(lua_State *L)
#define SIGMATCH_QUOTES_OPTIONAL
main detection engine ctx
#define DEFAULT_LUA_INSTRUCTION_LIMIT
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
void(* TransformId)(const uint8_t **data, uint32_t *length, const void *context)
StatsCounterId lua_transform_errors
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
struct lua_State lua_State
void SCLuaSbRestoreBytesLimit(lua_State *L, const uint64_t cfg_limit)
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
void StatsCounterIncr(StatsThreadContext *stats, StatsCounterId id)
Increments the local counter.
#define SCLogWarning(...)
Macro used to log WARNING messages.
StatsCounterId lua_transform_memory_limit_errors
void SCLuaSbStateClose(lua_State *L)
void SCLuaRequirefBuiltIns(lua_State *L)
Register Suricata built-in modules for loading in a non-sandboxed environment.
bool blocked_function_error
StatsCounterId lua_transform_blocked_function_errors
void SCLuaSbLoadLibs(lua_State *L)
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
#define DEFAULT_LUA_ALLOC_LIMIT
void SCLuaSbResetInstructionCounter(lua_State *L)
@ DETECT_TRANSFORM_LUAXFORM
#define SCLogError(...)
Macro used to log ERROR messages.
int SCDetectSignatureAddTransform(Signature *s, int transform, void *options)
bool instruction_count_error
char * DetectLoadCompleteSigPath(const DetectEngineCtx *de_ctx, const char *sig_file)
Create the path if default-rule-path was specified.
lua_State * SCLuaSbStateNew(uint64_t alloclimit, uint64_t instructionlimit)
Allocate a new Lua sandbox.
int DetectUnregisterThreadCtxFuncs(DetectEngineCtx *de_ctx, void *data, const char *name)
Remove Thread keyword context registration.
StatsCounterId lua_transform_instruction_limit_errors
void(* Transform)(DetectEngineThreadCtx *, InspectionBuffer *, const void *context)
int LuaPushInteger(lua_State *luastate, lua_Integer n)
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)