From: Andrii Serbeniuk -X (aserbeni - SOFTSERVE INC at Cisco) Date: Fri, 23 Feb 2024 12:27:58 +0000 (+0000) Subject: Pull request #4217: main: clear lua stack when registering commands in a shell X-Git-Tag: 3.1.82.0~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07b3df7df2fdbc367b49006c2b02903354f5d0f4;p=thirdparty%2Fsnort3.git Pull request #4217: main: clear lua stack when registering commands in a shell Merge in SNORT/snort3 from ~ASERBENI/snort3:lua_overflow to master Squashed commit of the following: commit d8c24607f2ad685749edcac75d94ddf3b2f458ea Author: Andrii Serbeniuk Date: Tue Feb 20 15:20:22 2024 +0200 main: clear lua stack when registering commands in a shell --- diff --git a/src/main/shell.cc b/src/main/shell.cc index fdf0a9fab..1f1c85276 100644 --- a/src/main/shell.cc +++ b/src/main/shell.cc @@ -659,9 +659,13 @@ bool Shell::configure(SnortConfig* sc, bool is_root) void Shell::install(const char* name, const luaL_Reg* reg) { if ( !strcmp(name, "snort") ) + { luaL_register(lua, "_G", reg); + lua_pop(lua, 1); + } luaL_register(lua, name, reg); + lua_pop(lua, 1); } void Shell::set_network_policy_user_id(lua_State* L, uint64_t user_id) @@ -767,3 +771,29 @@ void Shell::allowlist_update(const char* s, bool is_prefix) wlist->emplace(s); } +// ----------------------------------------------------------------------------- +// unit tests +// ----------------------------------------------------------------------------- + +#ifdef UNIT_TEST +#include "catch/snort_catch.h" + +static int test_closure(lua_State*) +{ return 0; } + +TEST_CASE("lua stack size on commands install", "[Shell]") +{ + Shell sh; + int init_stack_size = lua_gettop(sh.get_lua()); + luaL_Reg reg[2]; + reg[0].name = "test_closure"; + reg[0].func = test_closure; + reg[1].name = nullptr; + reg[1].func = nullptr; + + sh.install("test_module", reg); + int stack_size = lua_gettop(sh.get_lua()); + CHECK(stack_size == init_stack_size); +} + +#endif