News for hackers. Stuff that matters.
Re:Lua in the Kernel? by pansa2 on Friday September 18, 2020 @03:29AM (#24513953)
> Scripts should not be able to crash the system, run indefinitely, or corrupt other parts of the system. To ensure that, Lunatik uses the Lua VM facilities for sandboxing the scripts so that they run in a safe execution environment [...] the number of instructions can be limited, so that infinite loops are not possible.

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.

[0] http://lua-users.org/lists/lua-l/2011-02/msg01595.html

Re:Lua in the Kernel? by DarkWiiPlayer on Friday September 18, 2020 @06:56AM (#24515202)
> 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...

Re:Lua in the Kernel? by WalterGR on Friday September 18, 2020 @04:26AM (#24514311)
I'd be surprised if it could prevent a script from running indefinitely. In particular, limiting the number of Lua bytecode instructions is insufficient

FTA: “Lua has a facility to interrupt a script after it has run a certain number of instructions.”

Re:Lua in the Kernel? by OskarS on Friday September 18, 2020 @04:56AM (#24514510)
string.find is implemented in C, not Lua. As far as Lua is concerned, that call is a single instruction.
Re:Lua in the Kernel? by dathinab on Friday September 18, 2020 @12:00PM (#24518420)
You could have a string.find C implementation which gets an additional budget parameter and depending on it might limit search or similar (and reports back how much "budged" was used)

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.

Re:Lua in the Kernel? by DarkWiiPlayer on Friday September 18, 2020 @06:57AM (#24515209)
A single instruction that can run for a long time. That's the point.
Re:Lua in the Kernel? by pctammela on Friday September 18, 2020 @09:00AM (#24516148)
ditto for eBPF when acessing the C helpers
Re:Lua in the Kernel? by raverbashing on Friday September 18, 2020 @05:01AM (#24514556)
Just add a time limit to the sandbox as well
Re:Lua in the Kernel? by lioeters on Friday September 18, 2020 @10:59AM (#24517593)
Apparently, the sandbox cannot enforce such a time limit.

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

Re:Lua in the Kernel? by smilliken on Friday September 18, 2020 @11:56AM (#24518365)
This solution is problematic because there's no one time limit that makes sense across all past and present CPUs that span orders of magnitude of performance.. your lua script would always fail on some CPU and there wouldn't be a way to fix it.
Re:Lua in the Kernel? by brmgb on Friday September 18, 2020 @04:15AM (#24514226)
> In particular, limiting the number of Lua bytecode instructions is insufficient - even a single call to `string.find` can lock up the CPU [0].

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.

Re:Lua in the Kernel? by lmm on Friday September 18, 2020 @04:34AM (#24514366)
> I am not too familiar with Lua bytecode but wouldn't "string.find" actually be compiled to multiple bytecode instructions ?

It's probably a single opcode of "invoke native-code standard-library function/intrinsic".

Re:Lua in the Kernel? by pansa2 on Friday September 18, 2020 @04:53AM (#24514488)
Yes. `string.find` is implemented in C. Invoking it (no matter how complex its arguments) requires just a few bytecode instructions.
Re:Lua in the Kernel? by brmgb on Friday September 18, 2020 @06:53AM (#24515180)
Well, then that's library dependent and not an intrinsic part of the language. So shipping Lua safely would mean having to swap or drop parts of the standard library for something a bit more robust.

Apparently, that's what Lunatik does so the work seems to already have been done.

Re:Lua in the Kernel? by anonymoushn on Friday September 18, 2020 @08:12PM (#24523230)
Appending strings together is a single bytecode instruction and this cannot really be fixed.
Re:Lua in the Kernel? by 0x76 on Friday September 18, 2020 @04:18AM (#24514245)
You can't prevent infinite loops unless you've solved the halting problem right?

They could of course just limit the run time if they really wanted to make sure scripts don't run indefinitely.

Re:Lua in the Kernel? by coldtea on Friday September 18, 2020 @05:32AM (#24514736)
>You can't prevent infinite loops unless you've solved the halting problem right?

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.

Re:Lua in the Kernel? by DarkWiiPlayer on Friday September 18, 2020 @07:00AM (#24515233)
It's more simple than that: pull the plug on the entire turing machine. Lua runs in a VM, so you can stop it from the outside, which wouldn't be possible if the code was running directly in the CPU (Without an OS underneath it, that is).
Re:Lua in the Kernel? by mikevin on Friday September 18, 2020 @03:44PM (#24521104)
You could argue this doesn't really count as preventing though. It's preventing a symptom instead of the underlying problem.
Re:Lua in the Kernel? by moring on Friday September 18, 2020 @04:42AM (#24514420)
The halting problem only says that you cannot tell if a program ever stops, i.e. takes a finite number of steps but without any a priori bounds. If you limit the number of steps to N, you can just run N steps of the program on your given input and look if it has stopped already.
Re:Lua in the Kernel? by tinus_hn on Friday September 18, 2020 @02:39PM (#24520380)
The problem only says you can’t tell if the programs stops without actually running it. But here you’re running it anyway.
Re:Lua in the Kernel? by gwd on Friday September 18, 2020 @04:33PM (#24521614)
If you run it and it stops, then it stops. If you run it and it runs forever, then it runs forever. But the point is that you want to know if it's going to run forever before you hit "forever".
Re:Lua in the Kernel? by pansa2 on Friday September 18, 2020 @05:05AM (#24514588)
> They could of course just limit the run time

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.

Re:Lua in the Kernel? by d33 on Friday September 18, 2020 @04:31AM (#24514338)
ePBF prevents infinite loops by restricting the language:

https://lwn.net/Articles/794934/