]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Try to fix various Lua stack issues
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 22 Mar 2018 15:04:29 +0000 (15:04 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 22 Mar 2018 15:32:33 +0000 (15:32 +0000)
src/lua/lua_common.c
src/lua/lua_config.c
src/lua/lua_logger.c
src/lua/lua_tcp.c
src/plugins/surbl.c

index f484c02b930295cdccb3796d4bc576b7b8b269f7..a1d3f27bc20020fb4dd8018d6d9bbb5627801f7a 100644 (file)
@@ -1297,12 +1297,12 @@ rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
                        tb = lua_touserdata (L, -1);
                        msg_err_config ("error executing post load code: %v", tb);
                        g_string_free (tb, TRUE);
-                       lua_pop (L, 2);
+                       lua_settop (L, err_idx - 1);
 
                        return FALSE;
                }
 
-               lua_pop (L, 1); /* Error function */
+               lua_settop (L, err_idx - 1);
        }
 
        return TRUE;
index 6b1c9f490cc3ab3fc8ef1bca655b37577e646a0c..9a2cab5365de7bc928a0c2b5a160b3e8f681f940 100644 (file)
@@ -1041,7 +1041,6 @@ lua_watcher_callback (gpointer session_data, gpointer ud)
 
                if (tb) {
                        g_string_free (tb, TRUE);
-                       lua_pop (L, 1);
                }
        }
        else {
@@ -1118,12 +1117,10 @@ lua_watcher_callback (gpointer session_data, gpointer ud)
                                        }
                                }
                        }
-
-                       lua_pop (L, nresults);
                }
        }
 
-       lua_pop (L, 1); /* Error function */
+       lua_settop (L, err_idx - 1);
 }
 
 static void
index 65461a65572a800a1b70ee5fedadd0e83bdee8b9..2378ea59b6e297a499244f54ebfbf191868efe5b 100644 (file)
@@ -317,9 +317,11 @@ lua_logger_out_boolean (lua_State *L, gint pos, gchar *outbuf, gsize len)
 static gsize
 lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
 {
-       gint r;
+       gint r, top;
        const gchar *str = NULL;
 
+       top = lua_gettop (L);
+
        if (!lua_getmetatable (L, pos)) {
                return 0;
        }
@@ -328,7 +330,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
        lua_gettable (L, -2);
 
        if (!lua_istable (L, -1)) {
-               lua_pop (L, 2);
+               lua_settop (L, top);
+
                return 0;
        }
 
@@ -339,7 +342,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
                lua_pushvalue (L, pos);
 
                if (lua_pcall (L, 1, 1, 0) != 0) {
-                       lua_pop (L, 3);
+                       lua_settop (L, top);
+
                        return 0;
                }
 
@@ -350,7 +354,8 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
                lua_gettable (L, -2);
 
                if (!lua_isstring (L, -1)) {
-                       lua_pop (L, 3);
+                       lua_settop (L, top);
+
                        return 0;
                }
 
@@ -358,7 +363,7 @@ lua_logger_out_userdata (lua_State *L, gint pos, gchar *outbuf, gsize len)
        }
 
        r = rspamd_snprintf (outbuf, len + 1, "%s(%p)", str, lua_touserdata (L, pos));
-       lua_pop (L, 3);
+       lua_settop (L, top);
 
        return r;
 }
index dcaa3bc577cfa480ddb3b40d9ce047c3506c7311..3bfadf8cbe6da9d23219f649f027f647581535d4 100644 (file)
@@ -323,6 +323,12 @@ lua_tcp_maybe_free (struct lua_tcp_cbdata *cbd)
        }
 }
 
+#ifdef __GNUC__
+static void
+lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
+               const char *err, ...) __attribute__ ((format(printf, 3, 4)));
+#endif
+
 static void
 lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
                const char *err, ...)
@@ -330,7 +336,7 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
        va_list ap, ap_copy;
        struct lua_tcp_cbdata **pcbd;
        struct lua_tcp_handler *hdl;
-       gint cbref;
+       gint cbref, top;
 
        va_start (ap, err);
 
@@ -338,7 +344,6 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
                hdl = g_queue_peek_head (cbd->handlers);
 
                if (hdl == NULL) {
-                       va_end (ap_copy);
                        break;
                }
 
@@ -350,6 +355,7 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
                }
 
                if (cbref != -1) {
+                       top = lua_gettop (cbd->L);
                        lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbref);
 
                        /* Error message */
@@ -367,9 +373,10 @@ lua_tcp_push_error (struct lua_tcp_cbdata *cbd, gboolean is_fatal,
 
                        if (lua_pcall (cbd->L, 3, 0, 0) != 0) {
                                msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
-                               lua_pop (cbd->L, 1);
                        }
 
+                       lua_settop (cbd->L, top);
+
                        REF_RELEASE (cbd);
                }
 
@@ -391,7 +398,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
        struct rspamd_lua_text *t;
        struct lua_tcp_cbdata **pcbd;
        struct lua_tcp_handler *hdl;
-       gint cbref, arg_cnt;
+       gint cbref, arg_cnt, top;
 
        hdl = g_queue_peek_head (cbd->handlers);
 
@@ -405,6 +412,7 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
        }
 
        if (cbref != -1) {
+               top = lua_gettop (cbd->L);
                lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbref);
                /* Error */
                lua_pushnil (cbd->L);
@@ -430,9 +438,9 @@ lua_tcp_push_data (struct lua_tcp_cbdata *cbd, const guint8 *str, gsize len)
 
                if (lua_pcall (cbd->L, arg_cnt, 0, 0) != 0) {
                        msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
-                       lua_pop (cbd->L, 1);
                }
 
+               lua_settop (cbd->L, top);
                REF_RELEASE (cbd);
        }
 }
@@ -686,7 +694,9 @@ lua_tcp_handler (int fd, short what, gpointer ud)
 
                                if (cbd->connect_cb != -1) {
                                        struct lua_tcp_cbdata **pcbd;
+                                       gint top;
 
+                                       top = lua_gettop (cbd->L);
                                        lua_rawgeti (cbd->L, LUA_REGISTRYINDEX, cbd->connect_cb);
                                        pcbd = lua_newuserdata (cbd->L, sizeof (*pcbd));
                                        *pcbd = cbd;
@@ -695,9 +705,10 @@ lua_tcp_handler (int fd, short what, gpointer ud)
 
                                        if (lua_pcall (cbd->L, 1, 0, 0) != 0) {
                                                msg_info ("callback call failed: %s", lua_tostring (cbd->L, -1));
-                                               lua_pop (cbd->L, 1);
                                        }
 
+                                       lua_settop (cbd->L, top);
+
                                        REF_RELEASE (cbd);
                                }
                        }
index 6cc8b2f3c9ecf46a3cda5341281274c38b17be05..01cfb594484f206c82cf42ecb779051f6a6a509b 100644 (file)
@@ -733,7 +733,7 @@ surbl_module_parse_rule (const ucl_object_t* value, struct rspamd_config* cfg)
 
                        if (luaL_loadstring (L, tb->str) != 0) {
                                /* Reset stack */
-                               lua_settop (L, 0);
+                               lua_settop (L, err_idx - 1);
                                lua_pushcfunction (L, &rspamd_lua_traceback);
                                err_idx = lua_gettop (L);
                                /* Try with no return */