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.