return rc, e
end
+--- Generate a new table populated with all the safe Lua "string" functions.
+-- The intended use is to generate a now string table for each protected
+-- environment, so that no information can be passed through string, or worse,
+-- real string functions can be overwritten and thus escape the protected env.
+-- @return New "string" replacement package.
+function e2lib.safe_string_table()
+ local safefn = {
+ "byte", "char", "find", "format", "gmatch", "gsub", "len", "lower",
+ "match", "rep", "reverse", "sub", "upper"
+ }
+ -- unsafe: dump
+
+ local st = {}
+
+ for _,name in ipairs(safefn) do
+ assert(string[name])
+ st[name] = string[name]
+ end
+
+ return st
+end
+
--- Executes Lua code loaded from path.
--@param path Filename to load lua code from (string).
--@param gtable Environment (table) that is used instead of the global _G.
e = err.new("loading chroot config %q failed", path)
t = nil
- local function assign(table)
- t = table
- end
+ local g = {
+ e2chroot = function (data) t = data end,
+ env = info.env,
+ string = e2lib.safe_string_table(),
+ }
- rc, re = e2lib.dofile2(path,
- { e2chroot = assign, env = info.env, string = string })
+ rc, re = e2lib.dofile2(path, g)
if not rc then
return false, re
end
dest[index] = table
end
- rc, re = e2lib.dofile2(path, { [var] = func, env = info.env, string=string })
+ local g = {
+ [var] = func,
+ env = info.env,
+ string = e2lib.safe_string_table(),
+ }
+
+ rc, re = e2lib.dofile2(path, g)
if not rc then
return false, e:cat(re)
end
table.insert(list, t)
end
- local g = {} -- compose the environment for the config file
- g.env = info.env -- env
- g.string = string -- string
+ -- compose the environment for the config file
+ local g = {
+ env = info.env,
+ string = e2lib.safe_string_table(),
+ }
+
+ -- and some config functions
for _,typ in ipairs(types) do
- g[typ] = f[typ] -- and some config functions
+ g[typ] = f[typ]
end
rc, re = e2lib.dofile2(path, g)
local path = e2lib.join(info.root, file)
local g = {
e2env = info.env,
- string = string,
env = mergeenv,
+ string = e2lib.safe_string_table(),
}
rc, re = e2lib.dofile2(path, g)
if not rc then
e2lib.logf(3, "loading licence config %q", path)
e = err.new("loading licence config %q failed", path)
- ltable = {}
- local function assign(table)
- for k,v in pairs(table) do
- ltable[k] = v
- end
- end
+ ltable = nil
+ local g = {
+ e2licence = function(data) ltable = data end,
+ env = info.env,
+ string = e2lib.safe_string_table(),
+ }
- rc, re = e2lib.dofile2(path,
- { e2licence = assign, env = info.env, string = string })
+ rc, re = e2lib.dofile2(path, g)
if not rc then
return false, re
end
+ if type(ltable) ~= "table" then
+ return false, e:append("empty or invalid licence configuration")
+ end
+
for k,v in pairs(ltable) do
if type(k) ~= "string" then
return false, e:append("key %q is not a string", tostring(k))