sandbox.lua/README.md

185 lines
5.5 KiB
Markdown
Raw Permalink Normal View History

2013-09-03 15:13:39 +00:00
sandbox.lua
===========
A pure-lua solution for running untrusted Lua code.
2014-04-28 11:56:14 +00:00
The default behavior is restricting access to "dangerous" functions in Lua, such as `os.execute`.
It's possible to provide extra functions via the `options.env` parameter.
Infinite loops are prevented via the `debug` library.
Supported Lua versions:
======================
All the features of sandbox.lua work in the following Lua environments:
* PUC-Rio Lua 5.1 **allows execution of bytecode**, which is a huge limitation (see the bytecode section below)
* PUC-Rio Lua 5.2, 5.3, 5.4 have total support.
* LuaJIT is not protected against infinite loops (see the notes in `options.quota` below)
2013-09-03 15:13:39 +00:00
Usage
=====
2014-04-28 11:56:14 +00:00
Require the module like this:
``` lua
local sandbox = require 'sandbox'
```
Then you can use `sandbox.run` and `sandbox.protect`
2014-04-28 11:56:14 +00:00
### sandbox.run(code, options, ...)
`sandbox.run(code, options, ...)` sandboxes and executes `code` with the given `options` and extra params.
`code` must be a string with Lua code inside.
`options` is described below.
Any extra parameters will just be passed to the sandboxed function when executed, and available on the top-level scope via the `...` varargs parameters.
In other words, `sandbox.run(c, o, ...)` is equivalent to `sandbox.protect(c, o)(...)`.
Notice that if `code` throws an error, it is *NOT* captured by `sandbox.run`. Use `pcall` if you want your app to be immune to errors, like this:
``` lua
local ok, result = pcall(sandbox.run, 'error("this just throws an error")')
```
### sandbox.protect(code, options)
`sandbox.protect("lua code")` (or `sandbox("lua code")`) produces a sandboxed function, without executing it.
The resulting sandboxed function works as regular functions as long as they don't access any insecure features:
2014-04-28 11:56:14 +00:00
```lua
local sandboxed_f = sandbox(function() return 'hey' end)
local msg = sandboxed_f() -- msg is now 'hey'
```
2013-09-03 15:13:39 +00:00
2014-04-28 11:56:14 +00:00
Sandboxed options can not access unsafe Lua modules. (See the [source code](https://github.com/kikito/sandbox.lua/blob/master/sandbox.lua#L35) for a list)
2013-09-05 22:40:43 +00:00
2014-04-28 11:56:14 +00:00
When a sandboxed function tries to access an unsafe module, an error is produced.
2013-09-05 22:40:43 +00:00
2014-04-28 11:56:14 +00:00
```lua
local sf = sandbox.protect([[
2014-04-28 11:56:14 +00:00
os.execute('rm -rf /') -- this will throw an error, no damage done
end
]])
2013-09-05 22:40:43 +00:00
2014-04-28 11:56:14 +00:00
sf() -- error: os.execute not found
```
2013-09-03 15:13:39 +00:00
Sandboxed code will eventually throw an error if it contains infinite loops (note: this feature is not available in LuaJIT):
2013-09-13 11:20:24 +00:00
2014-04-28 11:56:14 +00:00
```lua
local sf = sandbox.protect([[
2014-04-28 11:56:14 +00:00
while true do end
]])
2014-04-28 11:56:14 +00:00
sf() -- error: quota exceeded
```
### Bytecode
It is possible to exit a sandbox using specially-crafted Lua bytecode. References:
* http://apocrypha.numin.it/talks/lua_bytecode_exploitation.pdf
* https://github.com/erezto/lua-sandbox-escape
* https://gist.github.com/corsix/6575486
Because of this, the sandbox deactivates bytecode in all the versions of Lua where it is possible:
* PUC-Rio Lua 5.2, 5.3, 5.4
* LuaJIT
In other words, _all except PUC-Rio Lua 5.1_.
** The sandbox can be exploited in PUC-Rio Lua 5.1 via bytecode **
The only reason we keep Lua 5.1 in the list of supported versions of Lua is because
sandboxing can help against users attempting to delete a file by mistake. _It does not provide
protection against malicious users_.
As a result we _strongly recommend updating to a more recent version when possible_.
2014-04-28 11:56:14 +00:00
### options.quota
Note: This feature is not available in LuaJIT
2014-04-28 11:56:14 +00:00
`sandbox.lua` prevents infinite loops from halting the program by hooking the `debug` library to the sandboxed function, and "counting instructions". When
the instructions reach a certain limit, an error is produced.
This limit can be tweaked via the `quota` option. But default, it is 500000.
2013-09-13 11:20:24 +00:00
2013-09-05 22:40:43 +00:00
It is not possible to exhaust the machine with infinite loops; the following will throw an error after invoking 500000 instructions:
2014-04-28 11:56:14 +00:00
``` lua
sandbox.run('while true do end') -- raise errors after 500000 instructions
sandbox.run('while true do end', {quota=10000}) -- raise error after 10000 instructions
```
2013-09-05 22:40:43 +00:00
If the quota is low enough, sandboxed code with too many calculations might fail:
2013-09-05 22:40:43 +00:00
2014-04-28 11:56:14 +00:00
``` lua
local code = [[
2014-04-28 11:56:14 +00:00
local count = 1
for i=1, 400 do count = count + 1 end
return count
]]
2013-09-05 22:40:43 +00:00
sandbox.run(code, {quota=100}) -- raises error before the code ends
2014-04-28 11:56:14 +00:00
```
2013-09-13 11:20:24 +00:00
If you want to turn off the quota completely, pass `quota=false` instead.
2014-04-28 11:56:14 +00:00
### options.env
2013-09-05 22:40:43 +00:00
Use the `env` option to inject additional variables to the environment in which the sandboxed code is executed.
2013-09-03 15:13:39 +00:00
2014-04-28 11:56:14 +00:00
local msg = sandbox.run('return foo', {env = {foo = 'This is a global var on the the environment'}})
The `env` variable will be used as an "index" by the sandbox environment, but it will *not* be modified at all (changes
to the environment are thus lost). The only way to "get information out" from the sandboxed environments are:
2013-09-13 11:20:24 +00:00
Through side effects, like writing to a database. You will have to provide the side-effects functions in `env`:
2013-09-13 11:20:24 +00:00
local val = 1
local env = { write_db = function(new_val) val = new_val end }
sandbox.run('write_db(2)')
assert(val = 2)
2013-09-13 11:20:24 +00:00
Through returned values:
2013-09-13 11:20:24 +00:00
local env = { amount = 1 }
local result = sandbox.run('return amount + 1', { env = env })
assert(result = 2)
2014-04-28 11:56:14 +00:00
2013-09-03 16:07:03 +00:00
2013-09-03 15:13:39 +00:00
Installation
============
Just copy sandbox.lua wherever you need it.
Alternatively, you can use luarocks:
luarocks install kikito/sandbox
2013-09-03 15:13:39 +00:00
License
=======
This library is released under the MIT license. See MIT-LICENSE.txt for details
Specs
=====
This project uses [busted](https://github.com/Olivine-Labs/busted) for its specs. In order to run them, install it and then:
2013-09-03 15:13:39 +00:00
2014-04-28 11:58:39 +00:00
```
cd /path/to/where/the/spec/folder/is
busted spec/*
2014-04-28 11:58:39 +00:00
```