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-flowvarlib.c
Line
Count
Source
1
/* Copyright (C) 2025 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
#include "suricata-common.h"
19
#include "app-layer-protos.h"
20
#include "flow-var.h"
21
#include "lauxlib.h"
22
#include "util-debug.h"
23
#include "util-lua-common.h"
24
#include "util-lua-flowvarlib.h"
25
#include "util-lua.h"
26
#include "util-var-name.h"
27
#include "detect-lua.h"
28
#include "detect-lua-extensions.h"
29
30
static const char suricata_flowvar_mt[] = "suricata:flowvar:mt";
31
32
static DetectLuaData *GetLuaData(lua_State *luastate)
33
0
{
34
0
    DetectLuaData *ld;
35
0
    lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
36
0
    lua_gettable(luastate, LUA_REGISTRYINDEX);
37
0
    ld = lua_touserdata(luastate, -1);
38
0
    return ld;
39
0
}
40
41
/**
42
 * \brief Register a flowvar.
43
 *
44
 * Ensures that a flowvar exists for the provided name, will be
45
 * created if needed.
46
 *
47
 * The flowvar ID is returned, however as this is most likely to be
48
 * used in the scripts "init" method, this ID is unlikely to be used.
49
 */
50
static int LuaFlowvarRegister(lua_State *L)
51
0
{
52
0
    DetectLuaData *ld = GetLuaData(L);
53
0
    const char *name = luaL_checkstring(L, 1);
54
0
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
55
0
    *flowvar_id = VarNameStoreRegister(name, VAR_TYPE_FLOW_VAR);
56
0
    if (*flowvar_id == 0) {
57
0
        return luaL_error(L, "failed to register flowvar");
58
0
    }
59
0
    if (ld->flowvars >= DETECT_LUA_MAX_FLOWVARS) {
60
0
        return luaL_error(L, "too many flowvars registered");
61
0
    }
62
0
    ld->flowvar[ld->flowvars++] = *flowvar_id;
63
64
0
    luaL_getmetatable(L, suricata_flowvar_mt);
65
0
    lua_setmetatable(L, -2);
66
67
0
    return 1;
68
0
}
69
70
static int LuaFlowvarGet(lua_State *L)
71
0
{
72
0
    const char *name = luaL_checkstring(L, 1);
73
0
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
74
0
    *flowvar_id = VarNameStoreLookupByName(name, VAR_TYPE_FLOW_VAR);
75
0
    if (*flowvar_id == 0) {
76
0
        return luaL_error(L, "flowvar does not exist");
77
0
    }
78
79
0
    luaL_getmetatable(L, suricata_flowvar_mt);
80
0
    lua_setmetatable(L, -2);
81
82
0
    return 1;
83
0
}
84
85
static int LuaFlowvarValue(lua_State *L)
86
0
{
87
0
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowvar_mt);
88
0
    Flow *f = LuaStateGetFlow(L);
89
0
    if (f == NULL) {
90
0
        return LuaCallbackError(L, "flow is NULL");
91
0
    }
92
0
    FlowVar *fv = FlowVarGet(f, *flowvar_id);
93
0
    if (fv == NULL) {
94
0
        lua_pushnil(L);
95
0
    } else {
96
0
        LuaPushStringBuffer(
97
0
                L, (const uint8_t *)fv->data.fv_str.value, (size_t)fv->data.fv_str.value_len);
98
0
    }
99
0
    return 1;
100
0
}
101
102
static int LuaFlowvarSet(lua_State *L)
103
0
{
104
0
    size_t slen = 0;
105
0
    const char *value = luaL_checklstring(L, 2, &slen);
106
0
    lua_Integer req = luaL_optinteger(L, 3, (lua_Integer)slen);
107
0
    if (req < 0 || ((size_t)req > slen) || (size_t)req > UINT16_MAX) {
108
0
        return luaL_error(L, "length out of range");
109
0
    }
110
0
    const size_t len = (size_t)req;
111
0
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowvar_mt);
112
0
    Flow *f = LuaStateGetFlow(L);
113
0
    if (f == NULL) {
114
0
        return luaL_error(L, "no flow");
115
0
    }
116
117
0
    uint8_t *buf = SCMalloc(len + 1);
118
0
    if (buf == NULL) {
119
0
        return luaL_error(L, "alloc failure");
120
0
    }
121
0
    memcpy(buf, value, len);
122
0
    buf[len] = '\0';
123
0
    FlowVarAddIdValue(f, *flowvar_id, buf, (uint16_t)len);
124
125
0
    return 1;
126
0
}
127
128
static const luaL_Reg flowvarlib[] = {
129
    // clang-format off
130
    { "register", LuaFlowvarRegister, },
131
    { "get", LuaFlowvarGet },
132
    { NULL, NULL, },
133
    // clang-format on
134
};
135
136
static const luaL_Reg flowvarmt[] = {
137
    // clang-format off
138
    { "value", LuaFlowvarValue, },
139
    { "set", LuaFlowvarSet, },
140
    { NULL, NULL, },
141
    // clang-format on
142
};
143
144
int LuaLoadFlowvarLib(lua_State *L)
145
0
{
146
0
    luaL_newmetatable(L, suricata_flowvar_mt);
147
0
    lua_pushvalue(L, -1);
148
0
    lua_setfield(L, -2, "__index");
149
0
    luaL_setfuncs(L, flowvarmt, 0);
150
151
0
    luaL_newlib(L, flowvarlib);
152
0
    return 1;
153
0
}