]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: hlua: enforce proper running context for register_x functions
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 24 Feb 2023 08:43:54 +0000 (09:43 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 5 Apr 2023 06:58:16 +0000 (08:58 +0200)
register_{init, converters, fetches, action, service, cli, filter} are
meant to run exclusively from body context according to the
documentation (unlike register_task which is designed to work from both
init and runtime contexts)

A quick code inspection confirms that only register_task implements
the required precautions to make it safe out of init context.

Trying to use those register_* functions from a runtime lua task will
lead to a program crash since they all assume that they are running from
the main lua context and with no concurrent runs:

    core.register_task(function()
      core.register_init(function()
      end)
    end)

When loaded from the config, the above example would segfault.

To prevent this undefined behavior, we now report an explicit error if
the user tries to use such functions outside of init/body context.

This should be backported in every stable versions.
[prior to 2.5 lua filter API did not exist, the hlua_register_filter()
part should be skipped]

src/hlua.c

index 2ea15b995438d5b91be86e2e0592c6718e75afd4..2ad52d14c2c3284648b1046a2ab1b1c6b874ae65 100644 (file)
@@ -8607,6 +8607,11 @@ __LJMP static int hlua_register_init(lua_State *L)
 
        MAY_LJMP(check_args(L, 1, "register_init"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
+       }
+
        ref = MAY_LJMP(hlua_checkfunction(L, 1));
 
        init = calloc(1, sizeof(*init));
@@ -8974,6 +8979,11 @@ __LJMP static int hlua_register_converters(lua_State *L)
 
        MAY_LJMP(check_args(L, 2, "register_converters"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
+       }
+
        /* First argument : converter name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));
 
@@ -9053,6 +9063,11 @@ __LJMP static int hlua_register_fetches(lua_State *L)
 
        MAY_LJMP(check_args(L, 2, "register_fetches"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
+       }
+
        /* First argument : sample-fetch name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));
 
@@ -9894,6 +9909,11 @@ __LJMP static int hlua_register_action(lua_State *L)
        if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
                WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
+       }
+
        /* First argument : converter name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));
 
@@ -10060,6 +10080,11 @@ __LJMP static int hlua_register_service(lua_State *L)
 
        MAY_LJMP(check_args(L, 3, "register_service"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
+       }
+
        /* First argument : converter name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));
 
@@ -10337,6 +10362,11 @@ __LJMP static int hlua_register_cli(lua_State *L)
 
        MAY_LJMP(check_args(L, 3, "register_cli"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
+       }
+
        /* First argument : an array of maximum 5 keywords. */
        if (!lua_istable(L, 1))
                WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
@@ -11154,6 +11184,11 @@ __LJMP static int hlua_register_filter(lua_State *L)
 
        MAY_LJMP(check_args(L, 3, "register_filter"));
 
+       if (hlua_gethlua(L)) {
+               /* runtime processing */
+               WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context"));
+       }
+
        /* First argument : filter name. */
        name = MAY_LJMP(luaL_checkstring(L, 1));