]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4217: main: clear lua stack when registering commands in a shell
authorAndrii Serbeniuk -X (aserbeni - SOFTSERVE INC at Cisco) <aserbeni@cisco.com>
Fri, 23 Feb 2024 12:27:58 +0000 (12:27 +0000)
committerOleksii. Shumeiko -X (oshumeik - SOFTSERVE INC at Cisco) <oshumeik@cisco.com>
Fri, 23 Feb 2024 12:27:58 +0000 (12:27 +0000)
Merge in SNORT/snort3 from ~ASERBENI/snort3:lua_overflow to master

Squashed commit of the following:

commit d8c24607f2ad685749edcac75d94ddf3b2f458ea
Author: Andrii Serbeniuk <aserbeni@cisco.com>
Date:   Tue Feb 20 15:20:22 2024 +0200

    main: clear lua stack when registering commands in a shell

src/main/shell.cc

index fdf0a9fab9c23baa5fba585f9513857b50b5b953..1f1c8527646652ac17af14708da5d751e5e48c31 100644 (file)
@@ -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