Exploiting Lua 5.1 on 32-bit Windows
gist.github.com
gist.github.com
I will have to study this on detail, but it seems there is little I can do on this one, besides waiting for Lua to be patched somehow.
By the way, the only thing I miss is instructions on how to patch Lua so this is not a vulnerability any more, if that's possible at all.
Good find, and thanks!
Background: Lua 5.1 has a bytecode verifier, which was designed to reject malformed (and potentially harmful) bytecode. However the author of this exploit (Peter Cawley) has found way after way to get harmful bytecode past the verifier over the past few years.
Ultimately in Lua 5.2 the verifier was removed to avoid giving a false sense of security to developers. Ironically Peter Cawley stated that would have preferred to keep the verifier and just fix the holes[2]. He has written an external verifier for 5.2[3].
My advice is, again, don't load untrusted bytecode. This also means that wherever you execute untrusted source, you must check that the first byte is not 0x1B ("\27") before passing it to loadstring() (or use the 'mode' parameter to load() in 5.2).
[1]: http://www.lua.org/bugs.html#5.1.4-1
Patch loadbuffer() et al. to make it refuse bytecode files and you're safe.