Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Knot projects
Knot Resolver
Commits
170569f6
Commit
170569f6
authored
Apr 12, 2015
by
Marek Vavruša
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
engine: fixed sandbox for Lua 5.2+
parent
a83099ac
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
17 deletions
+29
-17
daemon/engine.c
daemon/engine.c
+4
-2
daemon/lua/sandbox.lua
daemon/lua/sandbox.lua
+25
-15
No files found.
daemon/engine.c
View file @
170569f6
...
...
@@ -171,7 +171,7 @@ static int l_sandboxcall(lua_State *L, int argc)
{
#if LUA_VERSION_NUM >= 502
lua_getglobal
(
L
,
"_SANDBOX"
);
lua_setupvalue
(
L
,
-
2
,
1
);
lua_setupvalue
(
L
,
-
(
2
+
argc
)
,
1
);
#endif
return
lua_pcall
(
L
,
argc
,
LUA_MULTRET
,
0
);
}
...
...
@@ -188,6 +188,8 @@ int engine_cmd(struct engine *engine, const char *str)
/* Check result. */
if
(
l_sandboxcall
(
engine
->
L
,
1
)
!=
0
)
{
fprintf
(
stderr
,
"%s
\n
"
,
lua_tostring
(
engine
->
L
,
-
1
));
lua_pop
(
engine
->
L
,
1
);
return
kr_error
(
EINVAL
);
}
...
...
@@ -227,7 +229,7 @@ static int engine_loadconf(struct engine *engine)
/* Evaluate */
if
(
ret
!=
0
)
{
fprintf
(
stderr
,
"
[system] error
%s
\n
"
,
lua_tostring
(
engine
->
L
,
-
1
));
fprintf
(
stderr
,
"%s
\n
"
,
lua_tostring
(
engine
->
L
,
-
1
));
lua_pop
(
engine
->
L
,
1
);
return
kr_error
(
EINVAL
);
}
...
...
daemon/lua/sandbox.lua
View file @
170569f6
...
...
@@ -13,7 +13,7 @@ setmetatable(modules, {
})
-- Make sandboxed environment
function
make_sandbox
(
defined
)
local
function
make_sandbox
(
defined
)
local
__protected
=
{
modules
=
true
,
cache
=
true
,
net
=
true
}
return
setmetatable
({},
{
__index
=
defined
,
...
...
@@ -29,6 +29,7 @@ function make_sandbox(defined)
})
end
-- Compatibility sandbox
if
setfenv
then
-- Lua 5.1 and less
_G
=
make_sandbox
(
getfenv
(
0
))
setfenv
(
0
,
_G
)
...
...
@@ -36,6 +37,29 @@ else -- Lua 5.2+
_SANDBOX
=
make_sandbox
(
_ENV
)
end
-- Interactive command evaluation
function
eval_cmd
(
line
)
-- Compatibility sandbox code loading
local
function
load_code
(
code
)
if
getfenv
then
-- Lua 5.1
return
loadstring
(
code
)
else
-- Lua 5.2+
return
load
(
code
,
nil
,
't'
,
_ENV
)
end
end
local
status
,
err
,
chunk
chunk
,
err
=
load_code
(
'table_print('
..
line
..
')'
)
if
err
then
chunk
,
err
=
load_code
(
line
)
end
if
not
err
then
chunk
()
end
if
err
then
print
(
err
)
end
end
-- Pretty printing
function
table_print
(
tt
,
indent
,
done
)
done
=
done
or
{}
...
...
@@ -57,18 +81,4 @@ function table_print (tt, indent, done)
else
io.write
(
tostring
(
tt
)
..
"
\n
"
)
end
end
-- Interactive command evaluation
function
eval_cmd
(
line
)
local
chunk
,
err
=
loadstring
(
'table_print('
..
line
..
')'
)
if
err
then
chunk
,
err
=
loadstring
(
line
)
end
if
not
err
then
status
,
err
=
pcall
(
chunk
)
end
if
err
then
print
(
err
)
end
end
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment