Skip to content
Snippets Groups Projects
Commit 46b33a7a authored by Vladimir Davydov's avatar Vladimir Davydov
Browse files

lua: fix heap-use-after-free bug in tuple format constructor

Runtime tuple formats are reusable, which means that a tuple format
returned by runtime_tuple_format_new may not be brand new, but actually
be used by a Lua object. As a result, if we call any function that may
trigger Lua GC between runtime_tuple_format_new and tuple_format_ref,
the tuple format may be deleted, leading to a use-after-free bug. This
is what happens in lbox_tuple_format_new. Fix this issue by taking a
reference to the format before pushing a cdata object to the Lua stack
in lbox_push_tuple_format.

The issue was fixed in the master branch by commit 28ec245d ("lua:
fix heap-use-after-free bug in tuple format constructor"). This isn't
a clean cherry-pick because the code changed quite a bit.

Closes #8889

NO_DOC=bug fix
NO_TEST=difficult to reproduce, found by ASAN

(cherry picked from commit 4123061b)
parent 1fe54450
No related branches found
No related tags found
No related merge requests found
## bugfix/lua/netbox
* Fixed a heap-use-after-free bug in the function creating a tuple format Lua
object for `net.box` (gh-8889).
......@@ -247,10 +247,16 @@ lbox_tuple_format_gc(struct lua_State *L)
static int
lbox_push_tuple_format(struct lua_State *L, struct tuple_format *format)
{
/*
* Tuple formats are reusable. It means that runtime_tuple_format_new
* may return a format that is actually referenced by another Lua
* object. So we have to be extra careful not to call anything that may
* trigger Lua GC after we create a format and before we reference it.
*/
tuple_format_ref(format);
struct tuple_format **ptr = (struct tuple_format **)
luaL_pushcdata(L, CTID_STRUCT_TUPLE_FORMAT_PTR);
*ptr = format;
tuple_format_ref(format);
lua_pushcfunction(L, lbox_tuple_format_gc);
luaL_setcdatagc(L, -2);
return 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment