From 5ea12787f87f3ea60935b7a1f5b24be80cb2dec6 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 1 Dec 2025 21:12:42 -0600 Subject: minor net changes, prevent buffer indexing evil metatables --- src/lua.c | 5 +++++ src/lua.h | 1 + src/net.c | 4 +++- src/net/lua.c | 6 +++--- src/thread.c | 6 ++++-- 5 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lua.c b/src/lua.c index 587b291..bcec1ed 100644 --- a/src/lua.c +++ b/src/lua.c @@ -483,3 +483,8 @@ int lua_assign_upvalues(lua_State* L, int fidx){ return 0; } + +int luaI_errtraceback(lua_State* L){ + luaL_traceback(L, L, lua_tostring(L, -1), 1); + return 1; +} diff --git a/src/lua.h b/src/lua.h index cfff148..ce3d0e4 100644 --- a/src/lua.h +++ b/src/lua.h @@ -43,6 +43,7 @@ void luaI_newstream(lua_State* L, stream_read_function, stream_free_function, vo int luaI_nothing(lua_State*); int env_table(lua_State* L, int provide_table); void luaI_jointable(lua_State* L); +int luaI_errtraceback(lua_State* L); //generic macro that takes other macros (see below) #define _tset_b(L, Tidx, K, V, F)\ diff --git a/src/net.c b/src/net.c index abab731..c9fdc08 100644 --- a/src/net.c +++ b/src/net.c @@ -843,6 +843,8 @@ void* handle_client(void *_arg){ luaI_tsetv(L, res_idx, "header", header_idx); + lua_pushcfunction(L, luaI_errtraceback); + int errtraceback_idx = lua_gettop(L); //get all function that kinda match parray_t* owo = (parray_t*)v; for(int i = 0; i != owo->len; i++){ @@ -877,7 +879,7 @@ void* handle_client(void *_arg){ lua_pushvalue(L, req_idx); //push info about the request //call the function - if(lua_pcall(L, 2, 0, 0) != 0){ + if(lua_pcall(L, 2, 0, errtraceback_idx) != 0){ fprintf(stderr, "(net thread) %s\n", lua_tostring(L, -1)); //send an error message if send has not been called if(client_fd >= 0) net_error(client_fd, 500); diff --git a/src/net/lua.c b/src/net/lua.c index 829c2f7..f005185 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -210,6 +210,9 @@ int l_sendfile(lua_State* L){ if(!lua_isnil(L, -1)) filename = (char*)lua_tostring(L, -1); } + luaI_assert(L, !access(path, F_OK) /*file not found*/); + luaI_assert(L, !access(path, R_OK) /*missing permissions*/); + lua_pushvalue(L, res_idx); lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); @@ -221,9 +224,6 @@ int l_sendfile(lua_State* L){ lua_gettable(L, -2); int header = lua_gettop(L); - luaI_assert(L, !access(path, F_OK) /*file not found*/); - luaI_assert(L, !access(path, R_OK) /*missing permissions*/); - lua_pushstring(L, "Content-Type"); lua_gettable(L, header); diff --git a/src/thread.c b/src/thread.c index f166c3f..a5f5cc2 100644 --- a/src/thread.c +++ b/src/thread.c @@ -391,8 +391,10 @@ int _buffer_mod(lua_State* L){ luaI_deepcopy(L, buffer->L, STRIP_GC | SKIP_LOCALS); lua_getmetatable(L, idx); - idx = lua_gettop(L); - luaI_tsetnil(L, idx, "__gc"); + if(lua_type(L, -1) == LUA_TTABLE){ + idx = lua_gettop(L); + luaI_tsetnil(L, idx, "__gc"); + } } pthread_mutex_unlock(&*buffer->lock); -- cgit v1.2.3