Lua in the Kernel?
lwn.net
lwn.net
Lua's sandbox should be able to prevent scripts from crashing or corrupting the system, but I'd be surprised if it could prevent a script from running indefinitely. In particular, limiting the number of Lua bytecode instructions is insufficient - even a single call to `string.find` can lock up the CPU [0].
> They are using the standard Lua, rather than the LuaJIT fork, Neto said, in answer to another question.
Using LuaJIT would make sandboxing significantly more difficult, because it's much more complex than regular Lua and hence more likely to have exploitable bugs. This is the reason that game consoles disallow JIT-compilation - and hence games tend to use regular Lua rather than LuaJIT.
You can disable the JIT in LuaJIT and still get better performance because it's a different implementation of the VM.
> but I'd be surprised if it could prevent a script from running indefinitely.
You'd have to patch the standard library a bit to avid things like `string.find` causing trouble, but as far as core Lua features go, yes, you can do that and all you need is to install a debug hook (Preferably from C, because performance, but you can even do this 100% from within Lua)
-----
EDIT: Here's some benchmarks I did for this a while ago https://gist.github.com/DarkWiiPlayer/f639a38df53e39df63497c...
FTA: “Lua has a facility to interrupt a script after it has run a certain number of instructions.”
Through always under the assumption that the C function is implemented correctly and adding need to do some explicit budged keeping makes it more complex and in turn more prone to errors.
From a similar thread to the one linked from the root comment:
> ..Attempts to control untrusted scripts may be in vain. E.g. string.find, string.rep and other C functions can be abused and will either run forever or allocate unlimited amounts of memory. This is true for both Lua and LuaJIT.
> The only reasonably safe way to handle untrusted Lua scripts is to isolate them in a process context and to make use of per-process quotas/limits provided by the operating system.
Improved functions sandboxing in Lua and LuaJIT - http://lua-users.org/lists/lua-l/2011-02/msg01106.html
At first glance, the second part of this sentence doesn't seem to support the initial premise. I am not too familiar with Lua bytecode but wouldn't "string.find" actually be compiled to multiple bytecode instructions ?
Unless a single bytecode instruction can lock up the cpu, doing so would most likely require either looping or recursive calls. Thus, by puting a hard limit on the number of instructions the sandbox can execute, it would end up aborting.
It's probably a single opcode of "invoke native-code standard-library function/intrinsic".
Apparently, that's what Lunatik does so the work seems to already have been done.
They could of course just limit the run time if they really wanted to make sure scripts don't run indefinitely.
Only in general arbitrary turing complete runtimes. For restricted subsets of instructions and specific implementations of runtimes you can. E.g. one could trivially disallow "jump" control flow, and limit loops to N iterations.
It seems as though this would be more robust than limiting the number of bytecode instructions. I wonder if that’s true and if so, why it’s not more commonly recommended.
https://wiki.freebsd.org/SummerOfCode2014/LuaLoader
https://lists.freebsd.org/pipermail/freebsd-current/2018-Feb...
https://www.netbsd.org/gallery/presentations/mbalmer/fosdem2...
After all, the whole "in the kernel" vs. "in userland" distinction only exists because we get hardware-accelerated sandboxing from the CPU and OS combined.
There are a few ways to limit the execution time of wasm programs:
- inject code after a set of instructions with a certain duration and in each loop that calls an external function which meters and destroys/suspends execution when a limit is reached (e.g. https://github.com/ewasm/wasm-metering)
- use a VM which counts instructions (e.g. https://github.com/perlin-network/life)
- use hardware interrupts
More random thoughts: https://esolangs.org/wiki/RarVM
I haven't heard anything about an official "typed Lua". Is there information about it somewhere?
* tl: https://github.com/teal-language/tl a compiler for Teal a typed dialect of lua. (actively maintained)
Paper: http://www.inf.puc-rio.br/~roberto/docs/pallene-sblp.pdf
Git Repo: https://github.com/pallene-lang/pallene
I am often irrationally paranoid about this sort of thing however.
Main advantage is that it allows for multiple operations to be applied atomically, as well as better error handling.
[1]: https://www.delphix.com/blog/delphix-engineering/zfs-channel...