]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: hlua: make coroutine.create() safe under haproxy (new attempt)
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 16 Nov 2026 10:24:13 +0000 (11:24 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 19 Nov 2026 14:57:32 +0000 (15:57 +0100)
We got a report from Khaled Nassar that the current coroutine.create()
wrapper implementation that was introduced in 8cd620b4 ("MINOR: hlua:
safe coroutine.create()") is still not safe. Indeed, the fact that the
freshly created coroutine could outlive the ctx that created it (and is
thus bound to it) was overlooked.

This obviously causes undefined behavior ranging from UAF to potential
memory disclosures when the coroutine which outlives its parent ctx
still continues to reference the parent hlua which is destroyed.

As of today, Lua doesn't provide a way to forcefully kill a coroutine
or thread that we created. This means we don't have control over
user created coroutines which were instanciated using coroutine.create()

This is really bug-prone, so instead of allowing potentially unsafe usages
we refuse to create threads from sub contexts. This means
coroutine.create() may only be used from the main context (body / init)
where we know for sure that the context which created the thread will
outlive it.

If user is willing to do parallel work and the current coroutine.create()
is too restrictive, core.register_task() may come into play.

It may be backported to 2.8, although we may decide to change the hard
error into a warning to prevent existing scripts breakage, especially
if coroutine.create() was used wisely by ensuring the coroutine couldn't
outlive its parent context. The warning could give time to migrate to
more suitable API such as core.register_task().

Thanks to Khaled Nassar for having reported the issue.

doc/lua-api/index.rst
src/hlua.c

index 2a53814a1a702be50342415a93b05dc5ee37e4f3..ffe638429a34c39581440ff3db98c1ca05de8e0a 100644 (file)
@@ -75,6 +75,10 @@ functions. Lua has 8 execution contexts.
 8. The **event context**: Inside a function that handles events subscribed
    through `core.event_sub()` or `Server.event_sub()`.
 
+Coroutines (sub threads) created at the user will using coroutine.create() or
+coroutine.wrap() are only supported during init or body context. See
+`core.register_task()` for recommended way of doing parallel work from Lua
+under haproxy.
 
 HAProxy Lua Hello world
 -----------------------
index 37cf03fa5ca838af5577044448cd8ce67f990163..861986b2629f042f89cf8d7416854968f671f471 100644 (file)
@@ -9062,26 +9062,25 @@ __LJMP static int hlua_set_nice(lua_State *L)
 /* safe lua coroutine.create() function:
  *
  * This is a simple wrapper for coroutine.create() that
- * ensures the current hlua state ctx is available from
- * the new subroutine state
+ * ensures the coroutine cannot be created from sub ctx,
+ * because then the coroutine could outlive its parent
+ * ctx and Lua doesn't provide a way for the parent ctx
+ * to destroy child threads.
  */
 __LJMP static int hlua_coroutine_create(lua_State *L)
 {
        lua_State *new; /* new coroutine state */
-       struct hlua **hlua_store;
        struct hlua *hlua = hlua_gethlua(L);
 
+       if (hlua) {
+               luaL_error(L, "Creating coroutine from sub context (that is, outside from init/body) is not supported. Check out core.register_task() instead.");
+               return 0; /* not reached */
+       }
+
        new = lua_newthread(L);
        if (!new)
                return 0;
 
-       hlua_store = lua_getextraspace(new);
-       /* Expose current hlua ctx on new lua thread
-        * (hlua_gethlua() will properly return the last "known"
-        *  hlua ctx instead of NULL when it is called from such coroutines)
-        */
-       *hlua_store = hlua;
-
        /* new lua thread is on the top of the stack, we
         * need to duplicate first stack argument (<f> from coroutine.create(<f>))
         * on the top of the stack to be able to use xmove() to move it on the new