]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua: Don't preform operations on a not connected socket
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 27 Sep 2023 15:22:41 +0000 (17:22 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 4 Oct 2023 13:34:00 +0000 (15:34 +0200)
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

index 9e448b81646993860bc6684fd6ca0cb5fbaf4eec..34af6023ab20cdcad5bb427502b269e01ee0788c 100644 (file)
@@ -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);