]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_security.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / cpp_api / s_security.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cpp_api/s_security.h"
21
22 #include "filesys.h"
23 #include "porting.h"
24 #include "server.h"
25 #include "client/client.h"
26 #include "settings.h"
27
28 #include <cerrno>
29 #include <string>
30 #include <iostream>
31
32
33 #define SECURE_API(lib, name) \
34         lua_pushcfunction(L, sl_##lib##_##name); \
35         lua_setfield(L, -2, #name);
36
37
38 static inline void copy_safe(lua_State *L, const char *list[], unsigned len, int from=-2, int to=-1)
39 {
40         if (from < 0) from = lua_gettop(L) + from + 1;
41         if (to   < 0) to   = lua_gettop(L) + to   + 1;
42         for (unsigned i = 0; i < (len / sizeof(list[0])); i++) {
43                 lua_getfield(L, from, list[i]);
44                 lua_setfield(L, to,   list[i]);
45         }
46 }
47
48 // Pushes the original version of a library function on the stack, from the old version
49 static inline void push_original(lua_State *L, const char *lib, const char *func)
50 {
51         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
52         lua_getfield(L, -1, lib);
53         lua_remove(L, -2);  // Remove globals_backup
54         lua_getfield(L, -1, func);
55         lua_remove(L, -2);  // Remove lib
56 }
57
58
59 void ScriptApiSecurity::initializeSecurity()
60 {
61         static const char *whitelist[] = {
62                 "assert",
63                 "core",
64                 "collectgarbage",
65                 "DIR_DELIM",
66                 "error",
67                 "getfenv",
68                 "getmetatable",
69                 "ipairs",
70                 "next",
71                 "pairs",
72                 "pcall",
73                 "print",
74                 "rawequal",
75                 "rawget",
76                 "rawset",
77                 "select",
78                 "setfenv",
79                 "setmetatable",
80                 "tonumber",
81                 "tostring",
82                 "type",
83                 "unpack",
84                 "_VERSION",
85                 "xpcall",
86                 // Completely safe libraries
87                 "coroutine",
88                 "string",
89                 "table",
90                 "math",
91         };
92         static const char *io_whitelist[] = {
93                 "open",
94                 "close",
95                 "flush",
96                 "read",
97                 "type",
98                 "write",
99         };
100         static const char *os_whitelist[] = {
101                 "clock",
102                 "date",
103                 "difftime",
104                 "getenv",
105                 "setlocale",
106                 "time",
107                 "tmpname",
108         };
109         static const char *debug_whitelist[] = {
110                 "gethook",
111                 "traceback",
112                 "getinfo",
113                 "getmetatable",
114                 "setupvalue",
115                 "setmetatable",
116                 "upvalueid",
117                 "sethook",
118                 "debug",
119                 "setlocal",
120         };
121         static const char *package_whitelist[] = {
122                 "config",
123                 "cpath",
124                 "path",
125                 "searchpath",
126         };
127 #if USE_LUAJIT
128         static const char *jit_whitelist[] = {
129                 "arch",
130                 "flush",
131                 "off",
132                 "on",
133                 "opt",
134                 "os",
135                 "status",
136                 "version",
137                 "version_num",
138         };
139 #endif
140         m_secure = true;
141
142         lua_State *L = getStack();
143
144         // Backup globals to the registry
145         lua_getglobal(L, "_G");
146         lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
147
148         // Replace the global environment with an empty one
149         int thread = getThread(L);
150         createEmptyEnv(L);
151         setLuaEnv(L, thread);
152
153         // Get old globals
154         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
155         int old_globals = lua_gettop(L);
156
157
158         // Copy safe base functions
159         lua_getglobal(L, "_G");
160         copy_safe(L, whitelist, sizeof(whitelist));
161
162         // And replace unsafe ones
163         SECURE_API(g, dofile);
164         SECURE_API(g, load);
165         SECURE_API(g, loadfile);
166         SECURE_API(g, loadstring);
167         SECURE_API(g, require);
168         lua_pop(L, 1);
169
170
171         // Copy safe IO functions
172         lua_getfield(L, old_globals, "io");
173         lua_newtable(L);
174         copy_safe(L, io_whitelist, sizeof(io_whitelist));
175
176         // And replace unsafe ones
177         //SECURE_API(io, open);
178         SECURE_API(io, input);
179         SECURE_API(io, output);
180         SECURE_API(io, lines);
181
182         lua_setglobal(L, "io");
183         lua_pop(L, 1);  // Pop old IO
184
185
186         // Copy safe OS functions
187         lua_getfield(L, old_globals, "os");
188         lua_newtable(L);
189         copy_safe(L, os_whitelist, sizeof(os_whitelist));
190
191         // And replace unsafe ones
192         SECURE_API(os, remove);
193         SECURE_API(os, rename);
194
195         lua_setglobal(L, "os");
196         lua_pop(L, 1);  // Pop old OS
197
198
199         // Copy safe debug functions
200         lua_getfield(L, old_globals, "debug");
201         lua_newtable(L);
202         copy_safe(L, debug_whitelist, sizeof(debug_whitelist));
203         lua_setglobal(L, "debug");
204         lua_pop(L, 1);  // Pop old debug
205
206
207         // Copy safe package fields
208         lua_getfield(L, old_globals, "package");
209         lua_newtable(L);
210         copy_safe(L, package_whitelist, sizeof(package_whitelist));
211         lua_setglobal(L, "package");
212         lua_pop(L, 1);  // Pop old package
213
214 #if USE_LUAJIT
215         // Copy safe jit functions, if they exist
216         lua_getfield(L, -1, "jit");
217         if (!lua_isnil(L, -1)) {
218                 lua_newtable(L);
219                 copy_safe(L, jit_whitelist, sizeof(jit_whitelist));
220                 lua_setglobal(L, "jit");
221         }
222         lua_pop(L, 1);  // Pop old jit
223 #endif
224
225         lua_pop(L, 1); // Pop globals_backup
226 }
227
228 void ScriptApiSecurity::initializeSecurityClient()
229 {
230         static const char *whitelist[] = {
231                 "assert",
232                 "core",
233                 "collectgarbage",
234                 "DIR_DELIM",
235                 "error",
236                 "getfenv",
237                 "ipairs",
238                 "next",
239                 "pairs",
240                 "pcall",
241                 "print",
242                 "rawequal",
243                 "rawget",
244                 "rawset",
245                 "select",
246                 "setfenv",
247                 // getmetatable can be used to escape the sandbox
248                 "setmetatable",
249                 "tonumber",
250                 "tostring",
251                 "type",
252                 "unpack",
253                 "_VERSION",
254                 "xpcall",
255                 // Completely safe libraries
256                 "coroutine",
257                 "string",
258                 "table",
259                 "math",
260         };
261         static const char *os_whitelist[] = {
262                 "clock",
263                 "date",
264                 "difftime",
265                 "time"
266         };
267         static const char *debug_whitelist[] = {
268                 "getinfo",
269                 "traceback"
270         };
271 #if USE_LUAJIT
272         static const char *jit_whitelist[] = {
273                 "arch",
274                 "flush",
275                 "off",
276                 "on",
277                 "opt",
278                 "os",
279                 "status",
280                 "version",
281                 "version_num",
282         };
283 #endif
284
285         m_secure = true;
286
287         lua_State *L = getStack();
288         int thread = getThread(L);
289
290         // Backup globals to the registry
291         lua_getglobal(L, "_G");
292         lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
293
294         // create an empty environment
295         createEmptyEnv(L);
296
297         // Copy safe base functions
298         lua_getglobal(L, "_G");
299         lua_getfield(L, -2, "_G");
300         copy_safe(L, whitelist, sizeof(whitelist));
301
302         // And replace unsafe ones
303         SECURE_API(g, dofile);
304         SECURE_API(g, load);
305         SECURE_API(g, loadfile);
306         SECURE_API(g, loadstring);
307         SECURE_API(g, require);
308         lua_pop(L, 2);
309
310         // Copy safe OS functions
311         lua_getglobal(L, "os");
312         lua_newtable(L);
313         copy_safe(L, os_whitelist, sizeof(os_whitelist));
314         lua_setfield(L, -3, "os");
315         lua_pop(L, 1);  // Pop old OS
316
317
318         // Copy safe debug functions
319         lua_getglobal(L, "debug");
320         lua_newtable(L);
321         copy_safe(L, debug_whitelist, sizeof(debug_whitelist));
322         lua_setfield(L, -3, "debug");
323         lua_pop(L, 1);  // Pop old debug
324         
325
326 #if USE_LUAJIT
327         // Copy safe jit functions, if they exist
328         lua_getglobal(L, "jit");
329         lua_newtable(L);
330         copy_safe(L, jit_whitelist, sizeof(jit_whitelist));
331         lua_setfield(L, -3, "jit");
332         lua_pop(L, 1);  // Pop old jit
333 #endif
334
335         // Set the environment to the one we created earlier
336         setLuaEnv(L, thread);
337 }
338
339 int ScriptApiSecurity::getThread(lua_State *L)
340 {
341 #if LUA_VERSION_NUM <= 501
342         int is_main = lua_pushthread(L);  // Push the main thread
343         FATAL_ERROR_IF(!is_main, "Security: ScriptApi's Lua state "
344                 "isn't the main Lua thread!");
345         return lua_gettop(L);
346 #endif
347         return 0;
348 }
349
350 void ScriptApiSecurity::createEmptyEnv(lua_State *L)
351 {
352         lua_newtable(L);  // Create new environment
353         lua_pushvalue(L, -1);
354         lua_setfield(L, -2, "_G");  // Create the _G loop
355 }
356
357 void ScriptApiSecurity::setLuaEnv(lua_State *L, int thread)
358 {
359 #if LUA_VERSION_NUM >= 502  // Lua >= 5.2
360         // Set the global environment
361         lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
362 #else  // Lua <= 5.1
363         // Set the environment of the main thread
364         FATAL_ERROR_IF(!lua_setfenv(L, thread), "Security: Unable to set "
365                 "environment of the main Lua thread!");
366         lua_pop(L, 1);  // Pop thread
367 #endif
368 }
369
370 bool ScriptApiSecurity::isSecure(lua_State *L)
371 {
372         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
373         bool secure = !lua_isnil(L, -1);
374         lua_pop(L, 1);
375         return secure;
376 }
377
378 bool ScriptApiSecurity::safeLoadString(lua_State *L, const std::string &code, const char *chunk_name)
379 {
380         if (code.size() > 0 && code[0] == LUA_SIGNATURE[0]) {
381                 lua_pushliteral(L, "Bytecode prohibited when mod security is enabled.");
382                 return false;
383         }
384         if (luaL_loadbuffer(L, code.data(), code.size(), chunk_name))
385                 return false;
386         return true;
387 }
388
389 bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path, const char *display_name)
390 {
391         FILE *fp;
392         char *chunk_name;
393         if (!display_name)
394                 display_name = path;
395         if (!path) {
396                 fp = stdin;
397                 chunk_name = const_cast<char *>("=stdin");
398         } else {
399                 fp = fopen(path, "rb");
400                 if (!fp) {
401                         lua_pushfstring(L, "%s: %s", path, strerror(errno));
402                         return false;
403                 }
404                 chunk_name = new char[strlen(display_name) + 2];
405                 chunk_name[0] = '@';
406                 chunk_name[1] = '\0';
407                 strcat(chunk_name, display_name);
408         }
409
410         size_t start = 0;
411         int c = std::getc(fp);
412         if (c == '#') {
413                 // Skip the first line
414                 while ((c = std::getc(fp)) != EOF && c != '\n') {}
415                 if (c == '\n')
416                         std::getc(fp);
417                 start = std::ftell(fp);
418         }
419
420         // Read the file
421         int ret = std::fseek(fp, 0, SEEK_END);
422         if (ret) {
423                 lua_pushfstring(L, "%s: %s", path, strerror(errno));
424                 if (path) {
425                         std::fclose(fp);
426                         delete [] chunk_name;
427                 }
428                 return false;
429         }
430
431         size_t size = std::ftell(fp) - start;
432         std::string code(size, '\0');
433         ret = std::fseek(fp, start, SEEK_SET);
434         if (ret) {
435                 lua_pushfstring(L, "%s: %s", path, strerror(errno));
436                 if (path) {
437                         std::fclose(fp);
438                         delete [] chunk_name;
439                 }
440                 return false;
441         }
442
443         size_t num_read = std::fread(&code[0], 1, size, fp);
444         if (path)
445                 std::fclose(fp);
446         if (num_read != size) {
447                 lua_pushliteral(L, "Error reading file to load.");
448                 if (path)
449                         delete [] chunk_name;
450                 return false;
451         }
452
453         bool result = safeLoadString(L, code, chunk_name);
454         if (path)
455                 delete [] chunk_name;
456         return result;
457 }
458
459
460 bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
461                 bool write_required, bool *write_allowed)
462 {
463         if (write_allowed)
464                 *write_allowed = false;
465
466         std::string str;  // Transient
467
468         std::string abs_path = fs::AbsolutePath(path);
469
470         if (!abs_path.empty()) {
471                 // Don't allow accessing the settings file
472                 str = fs::AbsolutePath(g_settings_path);
473                 if (str == abs_path) return false;
474         }
475
476         // If we couldn't find the absolute path (path doesn't exist) then
477         // try removing the last components until it works (to allow
478         // non-existent files/folders for mkdir).
479         std::string cur_path = path;
480         std::string removed;
481         while (abs_path.empty() && !cur_path.empty()) {
482                 std::string component;
483                 cur_path = fs::RemoveLastPathComponent(cur_path, &component);
484                 if (component == "..") {
485                         // Parent components can't be allowed or we could allow something like
486                         // /home/user/minetest/worlds/foo/noexist/../../../../../../etc/passwd.
487                         // If we have previous non-relative elements in the path we might be
488                         // able to remove them so that things like worlds/foo/noexist/../auth.txt
489                         // could be allowed, but those paths will be interpreted as nonexistent
490                         // by the operating system anyways.
491                         return false;
492                 }
493                 removed.append(component).append(removed.empty() ? "" : DIR_DELIM + removed);
494                 abs_path = fs::AbsolutePath(cur_path);
495         }
496         if (abs_path.empty())
497                 return false;
498         // Add the removed parts back so that you can't, eg, create a
499         // directory in worldmods if worldmods doesn't exist.
500         if (!removed.empty())
501                 abs_path += DIR_DELIM + removed;
502
503         // Get server from registry
504         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
505         ScriptApiBase *script;
506 #if INDIRECT_SCRIPTAPI_RIDX
507         script = (ScriptApiBase *) *(void**)(lua_touserdata(L, -1));
508 #else
509         script = (ScriptApiBase *) lua_touserdata(L, -1);
510 #endif
511         lua_pop(L, 1);
512         const IGameDef *gamedef = script->getGameDef();
513         if (!gamedef)
514                 return false;
515
516         // Get mod name
517         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
518         if (lua_isstring(L, -1)) {
519                 std::string mod_name = readParam<std::string>(L, -1);
520
521                 // Builtin can access anything
522                 if (mod_name == BUILTIN_MOD_NAME) {
523                         if (write_allowed) *write_allowed = true;
524                         return true;
525                 }
526
527                 // Allow paths in mod path
528                 // Don't bother if write access isn't important, since it will be handled later
529                 if (write_required || write_allowed != NULL) {
530                         const ModSpec *mod = gamedef->getModSpec(mod_name);
531                         if (mod) {
532                                 str = fs::AbsolutePath(mod->path);
533                                 if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
534                                         if (write_allowed) *write_allowed = true;
535                                         return true;
536                                 }
537                         }
538                 }
539         }
540         lua_pop(L, 1);  // Pop mod name
541
542         // Allow read-only access to all mod directories
543         if (!write_required) {
544                 const std::vector<ModSpec> &mods = gamedef->getMods();
545                 for (const ModSpec &mod : mods) {
546                         str = fs::AbsolutePath(mod.path);
547                         if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
548                                 return true;
549                         }
550                 }
551         }
552
553         str = fs::AbsolutePath(gamedef->getWorldPath());
554         if (!str.empty()) {
555                 // Don't allow access to other paths in the world mod/game path.
556                 // These have to be blocked so you can't override a trusted mod
557                 // by creating a mod with the same name in a world mod directory.
558                 // We add to the absolute path of the world instead of getting
559                 // the absolute paths directly because that won't work if they
560                 // don't exist.
561                 if (fs::PathStartsWith(abs_path, str + DIR_DELIM + "worldmods") ||
562                                 fs::PathStartsWith(abs_path, str + DIR_DELIM + "game")) {
563                         return false;
564                 }
565                 // Allow all other paths in world path
566                 if (fs::PathStartsWith(abs_path, str)) {
567                         if (write_allowed) *write_allowed = true;
568                         return true;
569                 }
570         }
571
572         // Default to disallowing
573         return false;
574 }
575
576
577 int ScriptApiSecurity::sl_g_dofile(lua_State *L)
578 {
579         int nret = sl_g_loadfile(L);
580         if (nret != 1) {
581                 lua_error(L);
582                 // code after this function isn't executed
583         }
584         int top_precall = lua_gettop(L);
585         lua_call(L, 0, LUA_MULTRET);
586         // Return number of arguments returned by the function,
587         // adjusting for the function being poped.
588         return lua_gettop(L) - (top_precall - 1);
589 }
590
591
592 int ScriptApiSecurity::sl_g_load(lua_State *L)
593 {
594         size_t len;
595         const char *buf;
596         std::string code;
597         const char *chunk_name = "=(load)";
598
599         luaL_checktype(L, 1, LUA_TFUNCTION);
600         if (!lua_isnone(L, 2)) {
601                 luaL_checktype(L, 2, LUA_TSTRING);
602                 chunk_name = lua_tostring(L, 2);
603         }
604
605         while (true) {
606                 lua_pushvalue(L, 1);
607                 lua_call(L, 0, 1);
608                 int t = lua_type(L, -1);
609                 if (t == LUA_TNIL) {
610                         break;
611                 }
612
613                 if (t != LUA_TSTRING) {
614                         lua_pushnil(L);
615                         lua_pushliteral(L, "Loader didn't return a string");
616                         return 2;
617                 }
618                 buf = lua_tolstring(L, -1, &len);
619                 code += std::string(buf, len);
620                 lua_pop(L, 1); // Pop return value
621         }
622         if (!safeLoadString(L, code, chunk_name)) {
623                 lua_pushnil(L);
624                 lua_insert(L, -2);
625                 return 2;
626         }
627         return 1;
628 }
629
630
631 int ScriptApiSecurity::sl_g_loadfile(lua_State *L)
632 {
633 #ifndef SERVER
634         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
635 #if INDIRECT_SCRIPTAPI_RIDX
636         ScriptApiBase *script = (ScriptApiBase *) *(void**)(lua_touserdata(L, -1));
637 #else
638         ScriptApiBase *script = (ScriptApiBase *) lua_touserdata(L, -1);
639 #endif
640         lua_pop(L, 1);
641
642         // Client implementation
643         if (script->getType() == ScriptingType::Client) {
644                 std::string path = readParam<std::string>(L, 1);
645                 const std::string *contents = script->getClient()->getModFile(path);
646                 if (!contents) {
647                         std::string error_msg = "Coudln't find script called: " + path;
648                         lua_pushnil(L);
649                         lua_pushstring(L, error_msg.c_str());
650                         return 2;
651                 }
652
653                 std::string chunk_name = "@" + path;
654                 if (!safeLoadString(L, *contents, chunk_name.c_str())) {
655                         lua_pushnil(L);
656                         lua_insert(L, -2);
657                         return 2;
658                 }
659                 return 1;
660         }
661 #endif
662
663         // Server implementation
664         const char *path = NULL;
665         if (lua_isstring(L, 1)) {
666                 path = lua_tostring(L, 1);
667                 CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL);
668         }
669
670         if (!safeLoadFile(L, path)) {
671                 lua_pushnil(L);
672                 lua_insert(L, -2);
673                 return 2;
674         }
675
676         return 1;
677 }
678
679
680 int ScriptApiSecurity::sl_g_loadstring(lua_State *L)
681 {
682         const char *chunk_name = "=(load)";
683
684         luaL_checktype(L, 1, LUA_TSTRING);
685         if (!lua_isnone(L, 2)) {
686                 luaL_checktype(L, 2, LUA_TSTRING);
687                 chunk_name = lua_tostring(L, 2);
688         }
689
690         size_t size;
691         const char *code = lua_tolstring(L, 1, &size);
692         std::string code_s(code, size);
693
694         if (!safeLoadString(L, code_s, chunk_name)) {
695                 lua_pushnil(L);
696                 lua_insert(L, -2);
697                 return 2;
698         }
699         return 1;
700 }
701
702
703 int ScriptApiSecurity::sl_g_require(lua_State *L)
704 {
705         lua_pushliteral(L, "require() is disabled when mod security is on.");
706         return lua_error(L);
707 }
708
709
710 int ScriptApiSecurity::sl_io_open(lua_State *L)
711 {
712         bool with_mode = lua_gettop(L) > 1;
713
714         luaL_checktype(L, 1, LUA_TSTRING);
715         const char *path = lua_tostring(L, 1);
716
717         bool write_requested = false;
718         if (with_mode) {
719                 luaL_checktype(L, 2, LUA_TSTRING);
720                 const char *mode = lua_tostring(L, 2);
721                 write_requested = strchr(mode, 'w') != NULL ||
722                         strchr(mode, '+') != NULL ||
723                         strchr(mode, 'a') != NULL;
724         }
725         CHECK_SECURE_PATH_INTERNAL(L, path, write_requested, NULL);
726
727         push_original(L, "io", "open");
728         lua_pushvalue(L, 1);
729         if (with_mode) {
730                 lua_pushvalue(L, 2);
731         }
732
733         lua_call(L, with_mode ? 2 : 1, 2);
734         return 2;
735 }
736
737
738 int ScriptApiSecurity::sl_io_input(lua_State *L)
739 {
740         if (lua_isstring(L, 1)) {
741                 const char *path = lua_tostring(L, 1);
742                 CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL);
743         }
744
745         push_original(L, "io", "input");
746         lua_pushvalue(L, 1);
747         lua_call(L, 1, 1);
748         return 1;
749 }
750
751
752 int ScriptApiSecurity::sl_io_output(lua_State *L)
753 {
754         if (lua_isstring(L, 1)) {
755                 const char *path = lua_tostring(L, 1);
756                 CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL);
757         }
758
759         push_original(L, "io", "output");
760         lua_pushvalue(L, 1);
761         lua_call(L, 1, 1);
762         return 1;
763 }
764
765
766 int ScriptApiSecurity::sl_io_lines(lua_State *L)
767 {
768         if (lua_isstring(L, 1)) {
769                 const char *path = lua_tostring(L, 1);
770                 CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL);
771         }
772
773         int top_precall = lua_gettop(L);
774         push_original(L, "io", "lines");
775         lua_pushvalue(L, 1);
776         lua_call(L, 1, LUA_MULTRET);
777         // Return number of arguments returned by the function,
778         // adjusting for the function being poped.
779         return lua_gettop(L) - top_precall;
780 }
781
782
783 int ScriptApiSecurity::sl_os_rename(lua_State *L)
784 {
785         luaL_checktype(L, 1, LUA_TSTRING);
786         const char *path1 = lua_tostring(L, 1);
787         CHECK_SECURE_PATH_INTERNAL(L, path1, true, NULL);
788
789         luaL_checktype(L, 2, LUA_TSTRING);
790         const char *path2 = lua_tostring(L, 2);
791         CHECK_SECURE_PATH_INTERNAL(L, path2, true, NULL);
792
793         push_original(L, "os", "rename");
794         lua_pushvalue(L, 1);
795         lua_pushvalue(L, 2);
796         lua_call(L, 2, 2);
797         return 2;
798 }
799
800
801 int ScriptApiSecurity::sl_os_remove(lua_State *L)
802 {
803         luaL_checktype(L, 1, LUA_TSTRING);
804         const char *path = lua_tostring(L, 1);
805         CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL);
806
807         push_original(L, "os", "remove");
808         lua_pushvalue(L, 1);
809         lua_call(L, 1, 2);
810         return 2;
811 }
812