From: Aurelien DARRAGON Date: Mon, 16 Nov 2026 10:24:13 +0000 (+0100) Subject: BUG/MEDIUM: hlua: make coroutine.create() safe under haproxy (new attempt) X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/gitweb.js?a=commitdiff_plain;h=a74c9d6e8dc2009fefaec8b9a23e14f9cd7a48a6;p=haproxy.git BUG/MEDIUM: hlua: make coroutine.create() safe under haproxy (new attempt) 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. --- diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 2a53814a1..ffe638429 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -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 ----------------------- diff --git a/src/hlua.c b/src/hlua.c index 37cf03fa5..861986b26 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -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 ( from coroutine.create()) * on the top of the stack to be able to use xmove() to move it on the new