From ee687aa18d6182373e3856951313711ddec7ed92 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Wed, 27 Sep 2023 17:22:41 +0200 Subject: [PATCH] MINOR: hlua: Don't preform operations on a not connected socket There is nothing that prevent someone to create a lua socket and try to receive or to write before the connection was established ot after the shutdown was performed. The same is true when info about the socket are retrieved. It is not an issue because this will fail later. But now, we check the socket is connected or not earlier. It is more effecient but it will be also mandatory to fix issue with the lua sockets. --- src/hlua.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/hlua.c b/src/hlua.c index 9e448b8164..34af6023ab 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -2496,6 +2496,9 @@ __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua goto no_peer; csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); + if (!csk_ctx->connected) + goto connection_closed; + appctx = csk_ctx->appctx; s = appctx_strm(appctx); @@ -2737,6 +2740,12 @@ static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext } csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); + if (!csk_ctx->connected) { + xref_unlock(&socket->xref, peer); + lua_pushinteger(L, -1); + return 1; + } + appctx = csk_ctx->appctx; sc = appctx_sc(appctx); s = __sc_strm(sc); @@ -2946,6 +2955,7 @@ __LJMP static int hlua_socket_getpeername(struct lua_State *L) { struct hlua_socket *socket; struct xref *peer; + struct hlua_csk_ctx *csk_ctx; struct appctx *appctx; struct stconn *sc; const struct sockaddr_storage *dst; @@ -2968,7 +2978,14 @@ __LJMP static int hlua_socket_getpeername(struct lua_State *L) return 1; } - appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; + csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); + if (!csk_ctx->connected) { + xref_unlock(&socket->xref, peer); + lua_pushnil(L); + return 1; + } + + appctx = csk_ctx->appctx; sc = appctx_sc(appctx); dst = sc_dst(sc_opposite(sc)); if (!dst) { @@ -2989,6 +3006,7 @@ static int hlua_socket_getsockname(struct lua_State *L) struct connection *conn; struct appctx *appctx; struct xref *peer; + struct hlua_csk_ctx *csk_ctx; struct stream *s; int ret; @@ -3009,7 +3027,14 @@ static int hlua_socket_getsockname(struct lua_State *L) return 1; } - appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; + csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); + if (!csk_ctx->connected) { + xref_unlock(&socket->xref, peer); + lua_pushnil(L); + return 1; + } + + appctx = csk_ctx->appctx; s = appctx_strm(appctx); conn = sc_conn(s->scb);