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