suricata
util-lua.c
Go to the documentation of this file.
1/* Copyright (C) 2014-2022 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 Victor Julien <victor@inliniac.net>
22 *
23 * Common function for Lua
24 */
25
26#include "suricata-common.h"
27#include "detect.h"
28#include "pkt-var.h"
29#include "conf.h"
30
31#include "threads.h"
32#include "threadvars.h"
33#include "tm-threads.h"
34
35#include "util-print.h"
36#include "util-unittest.h"
37
38#include "util-debug.h"
39
40#include "output.h"
41#include "app-layer-htp.h"
42#include "app-layer.h"
43#include "app-layer-parser.h"
44#include "util-privs.h"
45#include "util-buffer.h"
46#include "util-proto-name.h"
47#include "util-logopenfile.h"
48#include "util-time.h"
49
50#include "lua.h"
51#include "lualib.h"
52#include "lauxlib.h"
53
54#include "util-lua.h"
55#include "util-lua-sandbox.h"
56
58{
59 lua_State *s = NULL;
60 s = luaL_newstate();
61 return s;
62}
63
65{
66 if (s != NULL) {
67 /* clear the stack */
68 while (lua_gettop(s) > 0) {
69 lua_pop(s, 1);
70 }
71 lua_close(s);
72 }
73}
74
75/* key for tv (threadvars) pointer */
76const char lua_ext_key_tv[] = "suricata:lua:tv:ptr";
77/* key for tx pointer */
78const char lua_ext_key_tx[] = "suricata:lua:tx:ptr";
79/* key for tx id */
80const char lua_ext_key_tx_id[] = "suricata:lua:tx_id";
81/* key for p (packet) pointer */
82const char lua_ext_key_p[] = "suricata:lua:pkt:ptr";
83/* key for f (flow) pointer */
84const char lua_ext_key_flow[] = "suricata:lua:flow:ptr";
85/* key for flow lock hint bool */
86const char lua_ext_key_flow_lock_hint[] = "suricata:lua:flow:lock_hint";
87/* key for direction */
88const char lua_ext_key_direction[] = "suricata:lua:direction";
89
90/* key for pa (packet alert) pointer */
91const char lua_ext_key_pa[] = "suricata:lua:pkt:alert:ptr";
92/* key for s (signature) pointer */
93const char lua_ext_key_s[] = "suricata:lua:signature:ptr";
94/* key for file pointer */
95const char lua_ext_key_file[] = "suricata:lua:file:ptr";
96/* key for DetectEngineThreadCtx pointer */
97const char lua_ext_key_det_ctx[] = "suricata:lua:det_ctx:ptr";
98/* key for streaming buffer pointer */
99const char lua_ext_key_streaming_buffer[] = "suricata:lua:streaming_buffer:ptr";
100
101/** \brief get tv pointer from the lua state */
103{
104 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
105 lua_gettable(luastate, LUA_REGISTRYINDEX);
106 void *tv = lua_touserdata(luastate, -1);
107 return (ThreadVars *)tv;
108}
109
111{
112 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
113 lua_pushlightuserdata(luastate, (void *)tv);
114 lua_settable(luastate, LUA_REGISTRYINDEX);
115}
116
117/** \brief get packet pointer from the lua state */
119{
120 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
121 lua_gettable(luastate, LUA_REGISTRYINDEX);
122 void *p = lua_touserdata(luastate, -1);
123 return (Packet *)p;
124}
125
127{
128 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
129 lua_pushlightuserdata(luastate, (void *)p);
130 lua_settable(luastate, LUA_REGISTRYINDEX);
131}
132
133/** \brief get tx pointer from the lua state */
134void *LuaStateGetTX(lua_State *luastate)
135{
136 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
137 lua_gettable(luastate, LUA_REGISTRYINDEX);
138 void *tx = lua_touserdata(luastate, -1);
139 return tx;
140}
141
142/** \brief get tx id from the lua state */
143uint64_t LuaStateGetTxId(lua_State *luastate)
144{
145 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
146 lua_gettable(luastate, LUA_REGISTRYINDEX);
147 uint64_t tx_id = lua_tointeger(luastate, -1);
148 return tx_id;
149}
150void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
151{
152 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
153 lua_pushlightuserdata(luastate, (void *)txptr);
154 lua_settable(luastate, LUA_REGISTRYINDEX);
155
156 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
157 lua_pushinteger(luastate, tx_id);
158 lua_settable(luastate, LUA_REGISTRYINDEX);
159}
160
162{
163 Flow *f = NULL;
164
165 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
166 lua_gettable(luastate, LUA_REGISTRYINDEX);
167 f = lua_touserdata(luastate, -1);
168
169 /* need flow lock hint */
170 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
171 lua_gettable(luastate, LUA_REGISTRYINDEX);
172
173 return f;
174}
175
176void LuaStateSetFlow(lua_State *luastate, Flow *f)
177{
178 /* flow */
179 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
180 lua_pushlightuserdata(luastate, (void *)f);
181 lua_settable(luastate, LUA_REGISTRYINDEX);
182
183 /* flow lock status hint */
184 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
185 /* locking is not required, set to 0 for backwards compatibility */
186 lua_pushboolean(luastate, 0);
187 lua_settable(luastate, LUA_REGISTRYINDEX);
188}
189
190/** \brief get packet alert pointer from the lua state */
192{
193 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
194 lua_gettable(luastate, LUA_REGISTRYINDEX);
195 void *pa = lua_touserdata(luastate, -1);
196 return (PacketAlert *)pa;
197}
198
200{
201 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
202 lua_pushlightuserdata(luastate, (void *)pa);
203 lua_settable(luastate, LUA_REGISTRYINDEX);
204}
205
206/** \brief get signature pointer from the lua state */
208{
209 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
210 lua_gettable(luastate, LUA_REGISTRYINDEX);
211 void *s = lua_touserdata(luastate, -1);
212 return (Signature *)s;
213}
214
215void LuaStateSetSignature(lua_State *luastate, const Signature *s)
216{
217 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
218 lua_pushlightuserdata(luastate, (void *)s);
219 lua_settable(luastate, LUA_REGISTRYINDEX);
220}
221
222/** \brief get file pointer from the lua state */
224{
225 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
226 lua_gettable(luastate, LUA_REGISTRYINDEX);
227 void *file = lua_touserdata(luastate, -1);
228 return (File *)file;
229}
230
231void LuaStateSetFile(lua_State *luastate, File *file)
232{
233 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
234 lua_pushlightuserdata(luastate, (void *)file);
235 lua_settable(luastate, LUA_REGISTRYINDEX);
236}
237
238/** \brief get DetectEngineThreadCtx pointer from the lua state */
240{
241 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
242 lua_gettable(luastate, LUA_REGISTRYINDEX);
243 void *det_ctx = lua_touserdata(luastate, -1);
244 return (DetectEngineThreadCtx *)det_ctx;
245}
246
248{
249 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
250 lua_pushlightuserdata(luastate, (void *)det_ctx);
251 lua_settable(luastate, LUA_REGISTRYINDEX);
252}
253
255{
256 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_streaming_buffer);
257 lua_gettable(luastate, LUA_REGISTRYINDEX);
258 void *b = lua_touserdata(luastate, -1);
259 return (LuaStreamingBuffer *)b;
260}
261
263{
264 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_streaming_buffer);
265 lua_pushlightuserdata(luastate, (void *)b);
266 lua_settable(luastate, LUA_REGISTRYINDEX);
267}
268
269/** \brief get packet pointer from the lua state */
271{
272 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_direction);
273 lua_gettable(luastate, LUA_REGISTRYINDEX);
274 int dir = lua_toboolean(luastate, -1);
275 return dir;
276}
277
278void LuaStateSetDirection(lua_State *luastate, int direction)
279{
280 lua_pushlightuserdata(luastate, (void *)&lua_ext_key_direction);
281 lua_pushboolean(luastate, direction);
282 lua_settable(luastate, LUA_REGISTRYINDEX);
283}
284
285/** \brief dump stack from lua state to screen */
287 int size = lua_gettop(state);
288 int i;
289
290 for (i = 1; i <= size; i++) {
291 int type = lua_type(state, i);
292 printf("Stack size=%d, level=%d, type=%d, ", size, i, type);
293
294 switch (type) {
295 case LUA_TFUNCTION:
296 printf("function %s", lua_tostring(state, i) ? "true" : "false");
297 break;
298 case LUA_TBOOLEAN:
299 printf("bool %s", lua_toboolean(state, i) ? "true" : "false");
300 break;
301 case LUA_TNUMBER:
302 printf("number %g", lua_tonumber(state, i));
303 break;
304 case LUA_TSTRING:
305 printf("string `%s'", lua_tostring(state, i));
306 break;
307 case LUA_TTABLE:
308 printf("table `%s'", lua_tostring(state, i));
309 break;
310 default:
311 printf("other %s", lua_typename(state, type));
312 break;
313
314 }
315 printf("\n");
316 }
317}
318
319int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
320{
321 if (input_len % 4 != 0) {
322 /* we're using a buffer sized at a multiple of 4 as lua_pushlstring generates
323 * invalid read errors in valgrind otherwise. Adding in a nul to be sure.
324 *
325 * Buffer size = len + 1 (for nul) + whatever makes it a multiple of 4 */
326 size_t buflen = input_len + 1 + ((input_len + 1) % 4);
327 uint8_t buf[buflen];
328 memset(buf, 0x00, buflen);
329 memcpy(buf, input, input_len);
330 buf[input_len] = '\0';
331
332 /* return value through luastate, as a luastring */
333 lua_pushlstring(luastate, (char *)buf, input_len);
334 } else {
335 lua_pushlstring(luastate, (char *)input, input_len);
336 }
337 return 1;
338}
339
340int LuaPushInteger(lua_State *luastate, lua_Integer n)
341{
342 lua_pushinteger(luastate, n);
343 return 1;
344}
uint16_t type
ThreadVars * tv
Flow data structure.
Definition flow.h:356
Signature container.
Definition detect.h:668
Per thread variable structure.
Definition threadvars.h:58
struct lua_State lua_State
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
Definition util-lua.c:262
const char lua_ext_key_file[]
Definition util-lua.c:95
const char lua_ext_key_flow_lock_hint[]
Definition util-lua.c:86
const char lua_ext_key_tv[]
Definition util-lua.c:76
File * LuaStateGetFile(lua_State *luastate)
get file pointer from the lua state
Definition util-lua.c:223
PacketAlert * LuaStateGetPacketAlert(lua_State *luastate)
get packet alert pointer from the lua state
Definition util-lua.c:191
Signature * LuaStateGetSignature(lua_State *luastate)
get signature pointer from the lua state
Definition util-lua.c:207
void LuaStateSetFlow(lua_State *luastate, Flow *f)
set a flow pointer in the lua state
Definition util-lua.c:176
void LuaPrintStack(lua_State *state)
dump stack from lua state to screen
Definition util-lua.c:286
int LuaStateGetDirection(lua_State *luastate)
get packet pointer from the lua state
Definition util-lua.c:270
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition util-lua.c:143
const char lua_ext_key_s[]
Definition util-lua.c:93
void LuaStateSetDirection(lua_State *luastate, int direction)
Definition util-lua.c:278
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition util-lua.c:319
const char lua_ext_key_p[]
Definition util-lua.c:82
DetectEngineThreadCtx * LuaStateGetDetCtx(lua_State *luastate)
get DetectEngineThreadCtx pointer from the lua state
Definition util-lua.c:239
lua_State * LuaGetState(void)
Definition util-lua.c:57
int LuaPushInteger(lua_State *luastate, lua_Integer n)
Definition util-lua.c:340
Flow * LuaStateGetFlow(lua_State *luastate)
get flow pointer from lua state
Definition util-lua.c:161
Packet * LuaStateGetPacket(lua_State *luastate)
get packet pointer from the lua state
Definition util-lua.c:118
const char lua_ext_key_streaming_buffer[]
Definition util-lua.c:99
const char lua_ext_key_tx_id[]
Definition util-lua.c:80
void LuaStateSetSignature(lua_State *luastate, const Signature *s)
Definition util-lua.c:215
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition util-lua.c:134
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
Definition util-lua.c:199
void LuaReturnState(lua_State *s)
Definition util-lua.c:64
const char lua_ext_key_tx[]
Definition util-lua.c:78
const char lua_ext_key_direction[]
Definition util-lua.c:88
LuaStreamingBuffer * LuaStateGetStreamingBuffer(lua_State *luastate)
Definition util-lua.c:254
const char lua_ext_key_det_ctx[]
Definition util-lua.c:97
void LuaStateSetPacket(lua_State *luastate, Packet *p)
Definition util-lua.c:126
void LuaStateSetDetCtx(lua_State *luastate, DetectEngineThreadCtx *det_ctx)
Definition util-lua.c:247
void LuaStateSetFile(lua_State *luastate, File *file)
Definition util-lua.c:231
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
Definition util-lua.c:150
const char lua_ext_key_flow[]
Definition util-lua.c:84
ThreadVars * LuaStateGetThreadVars(lua_State *luastate)
get tv pointer from the lua state
Definition util-lua.c:102
const char lua_ext_key_pa[]
Definition util-lua.c:91
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
Definition util-lua.c:110