Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/util-lua.c
Line
Count
Source
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
57
lua_State *LuaGetState(void)
58
0
{
59
0
    lua_State *s = NULL;
60
0
    s = luaL_newstate();
61
0
    return s;
62
0
}
63
64
void LuaReturnState(lua_State *s)
65
0
{
66
0
    if (s != NULL) {
67
        /* clear the stack */
68
0
        while (lua_gettop(s) > 0) {
69
0
            lua_pop(s, 1);
70
0
        }
71
0
        lua_close(s);
72
0
    }
73
0
}
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 */
102
ThreadVars *LuaStateGetThreadVars(lua_State *luastate)
103
0
{
104
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
105
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
106
0
    void *tv = lua_touserdata(luastate, -1);
107
0
    return (ThreadVars *)tv;
108
0
}
109
110
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
111
0
{
112
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
113
0
    lua_pushlightuserdata(luastate, (void *)tv);
114
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
115
0
}
116
117
/** \brief get packet pointer from the lua state */
118
Packet *LuaStateGetPacket(lua_State *luastate)
119
0
{
120
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
121
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
122
0
    void *p = lua_touserdata(luastate, -1);
123
0
    return (Packet *)p;
124
0
}
125
126
void LuaStateSetPacket(lua_State *luastate, Packet *p)
127
0
{
128
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
129
0
    lua_pushlightuserdata(luastate, (void *)p);
130
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
131
0
}
132
133
/** \brief get tx pointer from the lua state */
134
void *LuaStateGetTX(lua_State *luastate)
135
0
{
136
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
137
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
138
0
    void *tx = lua_touserdata(luastate, -1);
139
0
    return tx;
140
0
}
141
142
/** \brief get tx id from the lua state */
143
uint64_t LuaStateGetTxId(lua_State *luastate)
144
0
{
145
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
146
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
147
0
    uint64_t tx_id = lua_tointeger(luastate, -1);
148
0
    return tx_id;
149
0
}
150
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
151
0
{
152
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
153
0
    lua_pushlightuserdata(luastate, (void *)txptr);
154
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
155
156
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
157
0
    lua_pushinteger(luastate, tx_id);
158
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
159
0
}
160
161
Flow *LuaStateGetFlow(lua_State *luastate)
162
0
{
163
0
    Flow *f = NULL;
164
165
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
166
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
167
0
    f = lua_touserdata(luastate, -1);
168
169
    /* need flow lock hint */
170
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
171
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
172
173
0
    return f;
174
0
}
175
176
void LuaStateSetFlow(lua_State *luastate, Flow *f)
177
0
{
178
    /* flow */
179
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
180
0
    lua_pushlightuserdata(luastate, (void *)f);
181
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
182
183
    /* flow lock status hint */
184
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
185
    /* locking is not required, set to 0 for backwards compatibility */
186
0
    lua_pushboolean(luastate, 0);
187
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
188
0
}
189
190
/** \brief get packet alert pointer from the lua state */
191
PacketAlert *LuaStateGetPacketAlert(lua_State *luastate)
192
0
{
193
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
194
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
195
0
    void *pa = lua_touserdata(luastate, -1);
196
0
    return (PacketAlert *)pa;
197
0
}
198
199
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
200
0
{
201
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
202
0
    lua_pushlightuserdata(luastate, (void *)pa);
203
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
204
0
}
205
206
/** \brief get signature pointer from the lua state */
207
Signature *LuaStateGetSignature(lua_State *luastate)
208
0
{
209
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
210
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
211
0
    void *s = lua_touserdata(luastate, -1);
212
0
    return (Signature *)s;
213
0
}
214
215
void LuaStateSetSignature(lua_State *luastate, const Signature *s)
216
0
{
217
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
218
0
    lua_pushlightuserdata(luastate, (void *)s);
219
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
220
0
}
221
222
/** \brief get file pointer from the lua state */
223
File *LuaStateGetFile(lua_State *luastate)
224
0
{
225
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
226
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
227
0
    void *file = lua_touserdata(luastate, -1);
228
0
    return (File *)file;
229
0
}
230
231
void LuaStateSetFile(lua_State *luastate, File *file)
232
0
{
233
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
234
0
    lua_pushlightuserdata(luastate, (void *)file);
235
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
236
0
}
237
238
/** \brief get DetectEngineThreadCtx pointer from the lua state */
239
DetectEngineThreadCtx *LuaStateGetDetCtx(lua_State *luastate)
240
0
{
241
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
242
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
243
0
    void *det_ctx = lua_touserdata(luastate, -1);
244
0
    return (DetectEngineThreadCtx *)det_ctx;
245
0
}
246
247
void LuaStateSetDetCtx(lua_State *luastate, DetectEngineThreadCtx *det_ctx)
248
0
{
249
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
250
0
    lua_pushlightuserdata(luastate, (void *)det_ctx);
251
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
252
0
}
253
254
LuaStreamingBuffer *LuaStateGetStreamingBuffer(lua_State *luastate)
255
0
{
256
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_streaming_buffer);
257
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
258
0
    void *b = lua_touserdata(luastate, -1);
259
0
    return (LuaStreamingBuffer *)b;
260
0
}
261
262
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
263
0
{
264
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_streaming_buffer);
265
0
    lua_pushlightuserdata(luastate, (void *)b);
266
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
267
0
}
268
269
/** \brief get packet pointer from the lua state */
270
int LuaStateGetDirection(lua_State *luastate)
271
0
{
272
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_direction);
273
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
274
0
    int dir = lua_toboolean(luastate, -1);
275
0
    return dir;
276
0
}
277
278
void LuaStateSetDirection(lua_State *luastate, int direction)
279
0
{
280
0
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_direction);
281
0
    lua_pushboolean(luastate, direction);
282
0
    lua_settable(luastate, LUA_REGISTRYINDEX);
283
0
}
284
285
/** \brief dump stack from lua state to screen */
286
0
void LuaPrintStack(lua_State *state) {
287
0
    int size = lua_gettop(state);
288
0
    int i;
289
290
0
    for (i = 1; i <= size; i++) {
291
0
        int type = lua_type(state, i);
292
0
        printf("Stack size=%d, level=%d, type=%d, ", size, i, type);
293
294
0
        switch (type) {
295
0
            case LUA_TFUNCTION:
296
0
                printf("function %s", lua_tostring(state, i) ? "true" : "false");
297
0
                break;
298
0
            case LUA_TBOOLEAN:
299
0
                printf("bool %s", lua_toboolean(state, i) ? "true" : "false");
300
0
                break;
301
0
            case LUA_TNUMBER:
302
0
                printf("number %g", lua_tonumber(state, i));
303
0
                break;
304
0
            case LUA_TSTRING:
305
0
                printf("string `%s'", lua_tostring(state, i));
306
0
                break;
307
0
            case LUA_TTABLE:
308
0
                printf("table `%s'", lua_tostring(state, i));
309
0
                break;
310
0
            default:
311
0
                printf("other %s", lua_typename(state, type));
312
0
                break;
313
314
0
        }
315
0
        printf("\n");
316
0
    }
317
0
}
318
319
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
320
0
{
321
0
    lua_pushlstring(luastate, (char *)input, input_len);
322
0
    return 1;
323
0
}
324
325
int LuaPushInteger(lua_State *luastate, lua_Integer n)
326
0
{
327
0
    lua_pushinteger(luastate, n);
328
0
    return 1;
329
0
}