• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 22550237931

01 Mar 2026 06:56PM UTC coverage: 64.812% (-8.9%) from 73.687%
22550237931

Pull #14920

github

web-flow
Merge e05854a6d into 90823fa90
Pull Request #14920: draft: rust based configuration file parser and loader - v4

561 of 789 new or added lines in 4 files covered. (71.1%)

23225 existing lines in 498 files now uncovered.

131605 of 203055 relevant lines covered (64.81%)

2328818.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua.c
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)
UNCOV
58
{
×
UNCOV
59
    lua_State *s = NULL;
×
UNCOV
60
    s = luaL_newstate();
×
UNCOV
61
    return s;
×
UNCOV
62
}
×
63

64
void LuaReturnState(lua_State *s)
UNCOV
65
{
×
UNCOV
66
    if (s != NULL) {
×
67
        /* clear the stack */
UNCOV
68
        while (lua_gettop(s) > 0) {
×
UNCOV
69
            lua_pop(s, 1);
×
UNCOV
70
        }
×
UNCOV
71
        lua_close(s);
×
UNCOV
72
    }
×
UNCOV
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 */
102
ThreadVars *LuaStateGetThreadVars(lua_State *luastate)
UNCOV
103
{
×
UNCOV
104
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
×
UNCOV
105
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
106
    void *tv = lua_touserdata(luastate, -1);
×
UNCOV
107
    return (ThreadVars *)tv;
×
UNCOV
108
}
×
109

110
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
UNCOV
111
{
×
UNCOV
112
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tv);
×
UNCOV
113
    lua_pushlightuserdata(luastate, (void *)tv);
×
UNCOV
114
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
115
}
×
116

117
/** \brief get packet pointer from the lua state */
118
Packet *LuaStateGetPacket(lua_State *luastate)
UNCOV
119
{
×
UNCOV
120
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
×
UNCOV
121
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
122
    void *p = lua_touserdata(luastate, -1);
×
UNCOV
123
    return (Packet *)p;
×
UNCOV
124
}
×
125

126
void LuaStateSetPacket(lua_State *luastate, Packet *p)
UNCOV
127
{
×
UNCOV
128
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_p);
×
UNCOV
129
    lua_pushlightuserdata(luastate, (void *)p);
×
UNCOV
130
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
131
}
×
132

133
/** \brief get tx pointer from the lua state */
134
void *LuaStateGetTX(lua_State *luastate)
UNCOV
135
{
×
UNCOV
136
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
×
UNCOV
137
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
138
    void *tx = lua_touserdata(luastate, -1);
×
UNCOV
139
    return tx;
×
UNCOV
140
}
×
141

142
/** \brief get tx id from the lua state */
143
uint64_t LuaStateGetTxId(lua_State *luastate)
UNCOV
144
{
×
UNCOV
145
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
×
UNCOV
146
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
147
    uint64_t tx_id = lua_tointeger(luastate, -1);
×
UNCOV
148
    return tx_id;
×
UNCOV
149
}
×
150
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
UNCOV
151
{
×
UNCOV
152
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx);
×
UNCOV
153
    lua_pushlightuserdata(luastate, (void *)txptr);
×
UNCOV
154
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
155

UNCOV
156
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_tx_id);
×
UNCOV
157
    lua_pushinteger(luastate, tx_id);
×
UNCOV
158
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
159
}
×
160

161
Flow *LuaStateGetFlow(lua_State *luastate)
UNCOV
162
{
×
UNCOV
163
    Flow *f = NULL;
×
164

UNCOV
165
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
×
UNCOV
166
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
167
    f = lua_touserdata(luastate, -1);
×
168

169
    /* need flow lock hint */
UNCOV
170
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
×
UNCOV
171
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
172

UNCOV
173
    return f;
×
UNCOV
174
}
×
175

176
void LuaStateSetFlow(lua_State *luastate, Flow *f)
UNCOV
177
{
×
178
    /* flow */
UNCOV
179
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow);
×
UNCOV
180
    lua_pushlightuserdata(luastate, (void *)f);
×
UNCOV
181
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
182

183
    /* flow lock status hint */
UNCOV
184
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_flow_lock_hint);
×
185
    /* locking is not required, set to 0 for backwards compatibility */
UNCOV
186
    lua_pushboolean(luastate, 0);
×
UNCOV
187
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
188
}
×
189

190
/** \brief get packet alert pointer from the lua state */
191
PacketAlert *LuaStateGetPacketAlert(lua_State *luastate)
UNCOV
192
{
×
UNCOV
193
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
×
UNCOV
194
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
195
    void *pa = lua_touserdata(luastate, -1);
×
UNCOV
196
    return (PacketAlert *)pa;
×
UNCOV
197
}
×
198

199
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
UNCOV
200
{
×
UNCOV
201
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_pa);
×
UNCOV
202
    lua_pushlightuserdata(luastate, (void *)pa);
×
UNCOV
203
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
204
}
×
205

206
/** \brief get signature pointer from the lua state */
207
Signature *LuaStateGetSignature(lua_State *luastate)
UNCOV
208
{
×
UNCOV
209
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
×
UNCOV
210
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
211
    void *s = lua_touserdata(luastate, -1);
×
UNCOV
212
    return (Signature *)s;
×
UNCOV
213
}
×
214

215
void LuaStateSetSignature(lua_State *luastate, const Signature *s)
UNCOV
216
{
×
UNCOV
217
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_s);
×
UNCOV
218
    lua_pushlightuserdata(luastate, (void *)s);
×
UNCOV
219
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
220
}
×
221

222
/** \brief get file pointer from the lua state */
223
File *LuaStateGetFile(lua_State *luastate)
UNCOV
224
{
×
UNCOV
225
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
×
UNCOV
226
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
227
    void *file = lua_touserdata(luastate, -1);
×
UNCOV
228
    return (File *)file;
×
UNCOV
229
}
×
230

231
void LuaStateSetFile(lua_State *luastate, File *file)
UNCOV
232
{
×
UNCOV
233
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_file);
×
UNCOV
234
    lua_pushlightuserdata(luastate, (void *)file);
×
UNCOV
235
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
236
}
×
237

238
/** \brief get DetectEngineThreadCtx pointer from the lua state */
239
DetectEngineThreadCtx *LuaStateGetDetCtx(lua_State *luastate)
UNCOV
240
{
×
UNCOV
241
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
×
UNCOV
242
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
243
    void *det_ctx = lua_touserdata(luastate, -1);
×
UNCOV
244
    return (DetectEngineThreadCtx *)det_ctx;
×
UNCOV
245
}
×
246

247
void LuaStateSetDetCtx(lua_State *luastate, DetectEngineThreadCtx *det_ctx)
UNCOV
248
{
×
UNCOV
249
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_det_ctx);
×
UNCOV
250
    lua_pushlightuserdata(luastate, (void *)det_ctx);
×
UNCOV
251
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
252
}
×
253

254
LuaStreamingBuffer *LuaStateGetStreamingBuffer(lua_State *luastate)
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

262
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
UNCOV
263
{
×
UNCOV
264
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_streaming_buffer);
×
UNCOV
265
    lua_pushlightuserdata(luastate, (void *)b);
×
UNCOV
266
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
267
}
×
268

269
/** \brief get packet pointer from the lua state */
270
int LuaStateGetDirection(lua_State *luastate)
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)
UNCOV
279
{
×
UNCOV
280
    lua_pushlightuserdata(luastate, (void *)&lua_ext_key_direction);
×
UNCOV
281
    lua_pushboolean(luastate, direction);
×
UNCOV
282
    lua_settable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
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)
UNCOV
320
{
×
UNCOV
321
    lua_pushlstring(luastate, (char *)input, input_len);
×
UNCOV
322
    return 1;
×
UNCOV
323
}
×
324

325
int LuaPushInteger(lua_State *luastate, lua_Integer n)
UNCOV
326
{
×
UNCOV
327
    lua_pushinteger(luastate, n);
×
UNCOV
328
    return 1;
×
UNCOV
329
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc