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