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 */
76 const char lua_ext_key_tv[] = "suricata:lua:tv:ptr";
77 /* key for tx pointer */
78 const char lua_ext_key_tx[] = "suricata:lua:tx:ptr";
79 /* key for tx id */
80 const char lua_ext_key_tx_id[] = "suricata:lua:tx_id";
81 /* key for p (packet) pointer */
82 const char lua_ext_key_p[] = "suricata:lua:pkt:ptr";
83 /* key for f (flow) pointer */
84 const char lua_ext_key_flow[] = "suricata:lua:flow:ptr";
85 /* key for flow lock hint bool */
86 const char lua_ext_key_flow_lock_hint[] = "suricata:lua:flow:lock_hint";
87 /* key for direction */
88 const char lua_ext_key_direction[] = "suricata:lua:direction";
89 
90 /* key for pa (packet alert) pointer */
91 const char lua_ext_key_pa[] = "suricata:lua:pkt:alert:ptr";
92 /* key for s (signature) pointer */
93 const char lua_ext_key_s[] = "suricata:lua:signature:ptr";
94 /* key for file pointer */
95 const char lua_ext_key_file[] = "suricata:lua:file:ptr";
96 /* key for DetectEngineThreadCtx pointer */
97 const char lua_ext_key_det_ctx[] = "suricata:lua:det_ctx:ptr";
98 /* key for streaming buffer pointer */
99 const 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 
126 void LuaStateSetPacket(lua_State *luastate, Packet *p)
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 */
134 void *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 */
143 uint64_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 }
150 void 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 
176 void 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 
215 void 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 
231 void 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 
278 void 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 */
286 void LuaPrintStack(lua_State *state) {
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 
319 int 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 
340 int LuaPushInteger(lua_State *luastate, lua_Integer n)
341 {
342  lua_pushinteger(luastate, n);
343  return 1;
344 }
LuaStateSetTX
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
Definition: util-lua.c:150
LuaStateSetPacket
void LuaStateSetPacket(lua_State *luastate, Packet *p)
Definition: util-lua.c:126
tm-threads.h
LuaStateGetStreamingBuffer
LuaStreamingBuffer * LuaStateGetStreamingBuffer(lua_State *luastate)
Definition: util-lua.c:254
LuaStateSetThreadVars
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
Definition: util-lua.c:110
lua_ext_key_det_ctx
const char lua_ext_key_det_ctx[]
Definition: util-lua.c:97
lua_ext_key_flow_lock_hint
const char lua_ext_key_flow_lock_hint[]
Definition: util-lua.c:86
LuaStreamingBuffer_
Definition: util-lua.h:34
LuaStateGetSignature
Signature * LuaStateGetSignature(lua_State *luastate)
get signature pointer from the lua state
Definition: util-lua.c:207
util-lua.h
LuaGetState
lua_State * LuaGetState(void)
Definition: util-lua.c:57
threads.h
Flow_
Flow data structure.
Definition: flow.h:360
lua_ext_key_file
const char lua_ext_key_file[]
Definition: util-lua.c:95
util-privs.h
lua_ext_key_pa
const char lua_ext_key_pa[]
Definition: util-lua.c:91
LuaStateSetFlow
void LuaStateSetFlow(lua_State *luastate, Flow *f)
set a flow pointer in the lua state
Definition: util-lua.c:176
LuaStateGetDetCtx
DetectEngineThreadCtx * LuaStateGetDetCtx(lua_State *luastate)
get DetectEngineThreadCtx pointer from the lua state
Definition: util-lua.c:239
util-unittest.h
lua_State
struct lua_State lua_State
Definition: suricata-common.h:500
LuaStateGetThreadVars
ThreadVars * LuaStateGetThreadVars(lua_State *luastate)
get tv pointer from the lua state
Definition: util-lua.c:102
lua_ext_key_s
const char lua_ext_key_s[]
Definition: util-lua.c:93
app-layer-htp.h
util-debug.h
DetectEngineThreadCtx_
Definition: detect.h:1090
LuaStateSetFile
void LuaStateSetFile(lua_State *luastate, File *file)
Definition: util-lua.c:231
util-print.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
lua_ext_key_direction
const char lua_ext_key_direction[]
Definition: util-lua.c:88
pkt-var.h
util-time.h
app-layer-parser.h
LuaStateGetDirection
int LuaStateGetDirection(lua_State *luastate)
get packet pointer from the lua state
Definition: util-lua.c:270
LuaStateSetDetCtx
void LuaStateSetDetCtx(lua_State *luastate, DetectEngineThreadCtx *det_ctx)
Definition: util-lua.c:247
Packet_
Definition: decode.h:479
type
uint16_t type
Definition: decode-vlan.c:107
conf.h
lua_ext_key_tx
const char lua_ext_key_tx[]
Definition: util-lua.c:78
lua_ext_key_tv
const char lua_ext_key_tv[]
Definition: util-lua.c:76
util-proto-name.h
util-lua-sandbox.h
LuaStateGetTX
void * LuaStateGetTX(lua_State *luastate)
get tx pointer from the lua state
Definition: util-lua.c:134
File_
Definition: util-file.h:79
lua_ext_key_tx_id
const char lua_ext_key_tx_id[]
Definition: util-lua.c:80
LuaStateGetTxId
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition: util-lua.c:143
suricata-common.h
LuaStateGetPacketAlert
PacketAlert * LuaStateGetPacketAlert(lua_State *luastate)
get packet alert pointer from the lua state
Definition: util-lua.c:191
LuaStateSetPacketAlert
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
Definition: util-lua.c:199
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LuaStateSetDirection
void LuaStateSetDirection(lua_State *luastate, int direction)
Definition: util-lua.c:278
lua_ext_key_p
const char lua_ext_key_p[]
Definition: util-lua.c:82
LuaStateSetStreamingBuffer
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
Definition: util-lua.c:262
lua_ext_key_streaming_buffer
const char lua_ext_key_streaming_buffer[]
Definition: util-lua.c:99
util-logopenfile.h
LuaStateGetFile
File * LuaStateGetFile(lua_State *luastate)
get file pointer from the lua state
Definition: util-lua.c:223
util-buffer.h
Signature_
Signature container.
Definition: detect.h:601
LuaPrintStack
void LuaPrintStack(lua_State *state)
dump stack from lua state to screen
Definition: util-lua.c:286
LuaStateGetFlow
Flow * LuaStateGetFlow(lua_State *luastate)
get flow pointer from lua state
Definition: util-lua.c:161
PacketAlert_
Definition: decode.h:245
lua_ext_key_flow
const char lua_ext_key_flow[]
Definition: util-lua.c:84
LuaStateSetSignature
void LuaStateSetSignature(lua_State *luastate, const Signature *s)
Definition: util-lua.c:215
LuaStateGetPacket
Packet * LuaStateGetPacket(lua_State *luastate)
get packet pointer from the lua state
Definition: util-lua.c:118
output.h
LuaPushInteger
int LuaPushInteger(lua_State *luastate, lua_Integer n)
Definition: util-lua.c:340
LuaReturnState
void LuaReturnState(lua_State *s)
Definition: util-lua.c:64
LuaPushStringBuffer
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition: util-lua.c:319
app-layer.h