-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathsandbox.lua
334 lines (293 loc) · 12.8 KB
/
sandbox.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
--------------------------------------------------------------------------
-- Modulefiles are "loaded" in a sandbox. That is when the module
-- file is evaluated, it can only run a limited set of functions.
-- This file provides for a default list of functions that can be
-- run. There is also a sandbox_registration so that sites using
-- SitePackage can register their functions. Finally there are two
-- versions of run function one for Lua 5.1 and another for Lua 5.2
-- The appropriate version is assigned to [[sandbox_run]].
--
-- @module sandbox
_G._DEBUG = false -- Required by the new lua posix
local posix = require("posix")
require("strict")
--------------------------------------------------------------------------
-- Lmod License
--------------------------------------------------------------------------
--
-- Lmod is licensed under the terms of the MIT license reproduced below.
-- This means that Lmod is free software and can be used for both academic
-- and commercial purposes at absolutely no cost.
--
-- ----------------------------------------------------------------------
--
-- Copyright (C) 2008-2018 Robert McLay
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject
-- to the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
--------------------------------------------------------------------------
require("fileOps")
require("capture")
require("modfuncs")
require("string_utils")
require("utils")
require("declare")
local lfs = require("lfs")
local dbg = require("Dbg"):dbg()
sandbox_run = false
local s_old_write = nil
local s_file_methods = nil
--------------------------------------------------------------------------
-- Table containing valid functions for modulefiles.
local sandbox_env = {
assert = assert,
loadfile = loadfile,
require = require,
ipairs = ipairs,
next = next,
pairs = pairs,
pcall = pcall,
dofile = dofile_not_supported,
loadstring = (_VERSION == "Lua 5.1") and loadstring or load,
tonumber = tonumber,
tostring = tostring,
type = type,
unpack = (_VERSION == "Lua 5.1") and unpack or table.unpack,
string = { byte = string.byte, char = string.char, find = string.find,
format = string.format, gmatch = string.gmatch, gsub = string.gsub,
len = string.len, lower = string.lower, match = string.match,
rep = string.rep, reverse = string.reverse, sub = string.sub,
upper = string.upper, split = string.split, escape = string.escape},
table = { insert = table.insert, remove = table.remove, sort = table.sort,
concat = table.concat, unpack = table.unpack, sqrt = math.sqrt,
tan = math.tan, tanh = math.tanh },
os = { clock = os.clock, difftime = os.difftime, time = os.time, date = os.date,
getenv = os.getenv, execute = os.execute, exit = os.exit },
io = { stderr = io.stderr, open = io.open, close = io.close, write = io.write,
stdout = io.stdout, popen = io.popen},
package = { cpath = package.cpath, loaded = package.loaded, loaders = package.loaders,
loadlib = package.loadlib, path = package.path, preload = package.preload,
seeall = package.seeall },
math = { floor = math.floor, ceil = math.ceil, min = math.min, max = math.max },
------------------------------------------------------------
-- lmod functions
------------------------------------------------------------
--- Load family functions ----
load = load_module,
load_any = load_any,
try_load = try_load,
try_add = try_load,
mgrload = mgrload,
unload = unload,
always_load = always_load,
depends_on = depends_on,
depends_on_any = depends_on_any,
--- Load Modify functions ---
atleast = atleast,
atmost = atmost,
between = between,
latest = latest,
--- PATH functions ---
prepend_path = prepend_path,
append_path = append_path,
remove_path = remove_path,
--- Set Environment functions ----
setenv = setenv,
pushenv = pushenv,
unsetenv = unsetenv,
--- Property functions ----
add_property = add_property,
remove_property = remove_property,
--- Set Alias/shell functions ---
set_alias = set_alias,
unset_alias = unset_alias,
set_shell_function = set_shell_function,
unset_shell_function = unset_shell_function,
--- Prereq / Conflict ---
prereq = prereq,
prereq_any = prereq_any,
conflict = conflict,
--- Family function ---
family = family,
--- Inherit function ---
inherit = inherit,
-- Whatis / Help functions
whatis = whatis,
help = help,
-- meta data foo ---
extensions = extensions,
-- Shell script foo --
source_sh = source_sh,
complete = complete,
uncomplete = uncomplete,
-- Misc --
purge = purge,
haveDynamicMPATH = haveDynamicMPATH,
LmodBreak = LmodBreak,
source_sh = source_sh,
LmodMsgRaw = LmodMsgRaw,
LmodError = LmodError,
LmodMessage = LmodMessage,
LmodVersion = LmodVersion,
LmodWarning = LmodWarning,
convertToCanonical = convertToCanonical,
hierarchyA = hierarchyA,
isPending = isPending,
isloaded = isloaded,
isAvail = isAvail,
loaded_modules = loaded_modules,
mode = mode,
moduleStackTraceBack = moduleStackTraceBack,
myConfig = myConfig,
myFileName = myFileName,
myModuleFullName = myModuleFullName,
myModuleName = myModuleName,
myModuleUsrName = myModuleUsrName,
myModuleVersion = myModuleVersion,
myShellName = myShellName,
myShellType = myShellType,
print = print,
requireFullName = requireFullName,
userInGroup = userInGroup,
userInGroups = userInGroups,
colorize = colorize,
color_banner = color_banner,
-- Normal modulefiles should not use these function(s):
LmodSystemError = LmodSystemError, -- Normal Modulefiles should use
-- LmodError instead. LmodError
-- is inactive during avail and spider.
-- This function ALWAYS produces an
-- error.
is_spider = is_spider, -- This function should not be used.
-- It is better to use
-- if (mode() == "spider") then ... end
-- This function will deprecated and will be removed
always_unload = always_unload, -- This function is exactly the same as unload()
-- This function will deprecated and will be removed
------------------------------------------------------------
-- fileOp functions
------------------------------------------------------------
pathJoin = pathJoin,
isDir = isDir,
isFile = isFile,
mkdir_recursive = mkdir_recursive,
dirname = dirname,
extname = extname,
removeExt = removeExt,
barefilename = barefilename,
splitFileName = splitFileName,
abspath = realpath,
realpath = realpath,
path_regularize = path_regularize,
------------------------------------------------------------
-- dbg functions
------------------------------------------------------------
dbg = { active = dbg.active, fini = dbg.fini, print = dbg.print,
print2D = dbg.print2D, printA = dbg.printA, printT = dbg.printT,
start = dbg.start },
------------------------------------------------------------
-- lfs functions
------------------------------------------------------------
lfs = { attributes = lfs.attributes, chdir = lfs.chdir, lock_dir = lfs.lock_dir,
currentdir = lfs.currentdir, dir = lfs.dir, lock = lfs.lock,
mkdir = lfs.mkdir, rmdir = lfs.rmdir, rmdir = lfs.rmdir,
setmode = lfs.setmode, symlinkattributes = lfs.symlinkattributes,
touch = lfs.touch, unlock = lfs.unlock,
},
------------------------------------------------------------
-- posix functions
------------------------------------------------------------
posix = { uname = posix.uname, setenv = posix.setenv,
hostid = posix.hostid, open = posix.open,
openlog = posix.openlog, closelog = posix.closelog,
syslog = posix.syslog, stat = posix.stat},
------------------------------------------------------------
-- Misc functions
------------------------------------------------------------
subprocess = subprocess,
capture = capture,
UUIDString = UUIDString,
execute = execute,
isDefined = isDefined,
isNotDefined = isNotDefined,
------------------------------------------------------------
-- Misc System Values
------------------------------------------------------------
_VERSION = _VERSION,
}
--------------------------------------------------------------------------
-- Sites should call this function if they have
-- functions inside their SitePackage.lua file for
-- any functions that they want that are callable
-- via their modulefiles.
-- @param t A table
function sandbox_registration(t)
if (type(t) ~= "table") then
LmodError{msg="e_missing_table", kind = type(t)}
end
for k,v in pairs(t) do
sandbox_env[k] = v
end
end
function sandbox_set_os_exit(func)
sandbox_env.os.exit = func
end
function turn_off_stdio()
s_file_methods = getmetatable(io.stdout).__index -- any file will do
s_old_write = s_file_methods.write
s_file_methods.write =
function(f,...)
if (f ~= io.stdout and f ~= io.stderr) then
return s_old_write(f,...)
end
return f
end
end
function turn_on_stdio()
s_file_methods.write = s_old_write
end
--------------------------------------------------------------------------
-- This function is what actually "loads" a modulefile with protection
-- against modulefiles call functions they shouldn't or syntax errors
-- of any kind.
-- @param untrusted_code A string containing lua code
local function l_run5_1(untrusted_code)
if untrusted_code:byte(1) == 27 then return nil, "binary bytecode prohibited" end
local untrusted_function, message = loadstring(untrusted_code)
if not untrusted_function then return nil, message end
setfenv(untrusted_function, sandbox_env)
return pcall(untrusted_function)
end
--------------------------------------------------------------------------
-- This function is what actually "loads" a modulefile with protection
-- against modulefiles call functions they shouldn't or syntax errors
-- of any kind. This run codes under environment [Lua 5.2] or later.
-- @param untrusted_code A string containing lua code
local function l_run5_2(untrusted_code)
local untrusted_function, message = load(untrusted_code, nil, 't', sandbox_env)
if not untrusted_function then return nil, message end
return pcall(untrusted_function)
end
--------------------------------------------------------------------------
-- Define two version: Lua 5.1 or 5.2. It is likely that
-- The 5.2 version will be good for many new versions of
-- Lua but time will only tell.
sandbox_run = (_VERSION == "Lua 5.1") and l_run5_1 or l_run5_2