]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_security.cpp
Add spider
[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         };
110         static const char *io_whitelist[] = {
111                 "open",
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         };
303         static const char *os_whitelist[] = {
304                 "clock",
305                 "date",
306                 "difftime",
307                 "time"
308         };
309         static const char *debug_whitelist[] = {
310                 "getinfo",
311                 "traceback"
312         };
313 #if USE_LUAJIT
314         static const char *jit_whitelist[] = {
315                 "arch",
316                 "flush",
317                 "off",
318                 "on",
319                 "opt",
320                 "os",
321                 "status",
322                 "version",
323                 "version_num",
324         };
325 #endif
326
327         m_secure = true;
328
329         lua_State *L = getStack();
330         int thread = getThread(L);
331
332         // Backup globals to the registry
333         lua_getglobal(L, "_G");
334         lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
335
336         // create an empty environment
337         createEmptyEnv(L);
338
339         // Copy safe base functions
340         lua_getglobal(L, "_G");
341         lua_getfield(L, -2, "_G");
342         copy_safe(L, whitelist, sizeof(whitelist));
343
344         // And replace unsafe ones
345         SECURE_API(g, dofile);
346         SECURE_API(g, load);
347         SECURE_API(g, loadfile);
348         SECURE_API(g, loadstring);
349         SECURE_API(g, require);
350         lua_pop(L, 2);
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
368 #if USE_LUAJIT
369         // Copy safe jit functions, if they exist
370         lua_getglobal(L, "jit");
371         lua_newtable(L);
372         copy_safe(L, jit_whitelist, sizeof(jit_whitelist));
373         lua_setfield(L, -3, "jit");
374         lua_pop(L, 1);  // Pop old jit
375 #endif
376
377         // Set the environment to the one we created earlier
378         setLuaEnv(L, thread);
379 }
380
381 int ScriptApiSecurity::getThread(lua_State *L)
382 {
383 #if LUA_VERSION_NUM <= 501
384         int is_main = lua_pushthread(L);  // Push the main thread
385         FATAL_ERROR_IF(!is_main, "Security: ScriptApi's Lua state "
386                 "isn't the main Lua thread!");
387         return lua_gettop(L);
388 #endif
389         return 0;
390 }
391
392 void ScriptApiSecurity::createEmptyEnv(lua_State *L)
393 {
394         lua_newtable(L);  // Create new environment
395         lua_pushvalue(L, -1);
396         lua_setfield(L, -2, "_G");  // Create the _G loop
397 }
398
399 void ScriptApiSecurity::setLuaEnv(lua_State *L, int thread)
400 {
401 #if LUA_VERSION_NUM >= 502  // Lua >= 5.2
402         // Set the global environment
403         lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
404 #else  // Lua <= 5.1
405         // Set the environment of the main thread
406         FATAL_ERROR_IF(!lua_setfenv(L, thread), "Security: Unable to set "
407                 "environment of the main Lua thread!");
408         lua_pop(L, 1);  // Pop thread
409 #endif
410 }
411
412 bool ScriptApiSecurity::isSecure(lua_State *L)
413 {
414         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
415         bool secure = !lua_isnil(L, -1);
416         lua_pop(L, 1);
417         return secure;
418 }
419
420 bool ScriptApiSecurity::safeLoadString(lua_State *L, const std::string &code, const char *chunk_name)
421 {
422         if (code.size() > 0 && code[0] == LUA_SIGNATURE[0]) {
423                 lua_pushliteral(L, "Bytecode prohibited when mod security is enabled.");
424                 return false;
425         }
426         if (luaL_loadbuffer(L, code.data(), code.size(), chunk_name))
427                 return false;
428         return true;
429 }
430
431 bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path, const char *display_name)
432 {
433         FILE *fp;
434         char *chunk_name;
435         if (!display_name)
436                 display_name = path;
437         if (!path) {
438                 fp = stdin;
439                 chunk_name = const_cast<char *>("=stdin");
440         } else {
441                 fp = fopen(path, "rb");
442                 if (!fp) {
443                         lua_pushfstring(L, "%s: %s", path, strerror(errno));
444                         return false;
445                 }
446                 size_t len = strlen(display_name) + 2;
447                 chunk_name = new char[len];
448                 snprintf(chunk_name, len, "@%s", display_name);
449         }
450
451         size_t start = 0;
452         int c = std::getc(fp);
453         if (c == '#') {
454                 // Skip the first line
455                 while ((c = std::getc(fp)) != EOF && c != '\n') {}
456                 if (c == '\n')
457                         std::getc(fp);
458                 start = std::ftell(fp);
459         }
460
461         // Read the file
462         int ret = std::fseek(fp, 0, SEEK_END);
463         if (ret) {
464                 lua_pushfstring(L, "%s: %s", path, strerror(errno));
465                 if (path) {
466                         std::fclose(fp);
467                         delete [] chunk_name;
468                 }
469                 return false;
470         }
471
472         size_t size = std::ftell(fp) - start;
473         std::string code(size, '\0');
474         ret = std::fseek(fp, start, SEEK_SET);
475         if (ret) {
476                 lua_pushfstring(L, "%s: %s", path, strerror(errno));
477                 if (path) {
478                         std::fclose(fp);
479                         delete [] chunk_name;
480                 }
481                 return false;
482         }
483
484         size_t num_read = std::fread(&code[0], 1, size, fp);
485         if (path)
486                 std::fclose(fp);
487         if (num_read != size) {
488                 lua_pushliteral(L, "Error reading file to load.");
489                 if (path)
490                         delete [] chunk_name;
491                 return false;
492         }
493
494         bool result = safeLoadString(L, code, chunk_name);
495         if (path)
496                 delete [] chunk_name;
497         return result;
498 }
499
500
501 bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
502                 bool write_required, bool *write_allowed)
503 {
504         if (write_allowed)
505                 *write_allowed = false;
506
507         std::string str;  // Transient
508
509         std::string abs_path = fs::AbsolutePath(path);
510
511         if (!abs_path.empty()) {
512                 // Don't allow accessing the settings file
513                 str = fs::AbsolutePath(g_settings_path);
514                 if (str == abs_path) return false;
515         }
516
517         // If we couldn't find the absolute path (path doesn't exist) then
518         // try removing the last components until it works (to allow
519         // non-existent files/folders for mkdir).
520         std::string cur_path = path;
521         std::string removed;
522         while (abs_path.empty() && !cur_path.empty()) {
523                 std::string component;
524                 cur_path = fs::RemoveLastPathComponent(cur_path, &component);
525                 if (component == "..") {
526                         // Parent components can't be allowed or we could allow something like
527                         // /home/user/minetest/worlds/foo/noexist/../../../../../../etc/passwd.
528                         // If we have previous non-relative elements in the path we might be
529                         // able to remove them so that things like worlds/foo/noexist/../auth.txt
530                         // could be allowed, but those paths will be interpreted as nonexistent
531                         // by the operating system anyways.
532                         return false;
533                 }
534                 removed.append(component).append(removed.empty() ? "" : DIR_DELIM + removed);
535                 abs_path = fs::AbsolutePath(cur_path);
536         }
537         if (abs_path.empty())
538                 return false;
539         // Add the removed parts back so that you can't, eg, create a
540         // directory in worldmods if worldmods doesn't exist.
541         if (!removed.empty())
542                 abs_path += DIR_DELIM + removed;
543
544         // Get gamedef from registry
545         ScriptApiBase *script = ModApiBase::getScriptApiBase(L);
546         const IGameDef *gamedef = script->getGameDef();
547         if (!gamedef)
548                 return false;
549
550         // Get mod name
551         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
552         if (lua_isstring(L, -1)) {
553                 std::string mod_name = readParam<std::string>(L, -1);
554
555                 // Builtin can access anything
556                 if (mod_name == BUILTIN_MOD_NAME) {
557                         if (write_allowed) *write_allowed = true;
558                         return true;
559                 }
560
561                 // Allow paths in mod path
562                 // Don't bother if write access isn't important, since it will be handled later
563                 if (write_required || write_allowed != NULL) {
564                         const ModSpec *mod = gamedef->getModSpec(mod_name);
565                         if (mod) {
566                                 str = fs::AbsolutePath(mod->path);
567                                 if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
568                                         if (write_allowed) *write_allowed = true;
569                                         return true;
570                                 }
571                         }
572                 }
573         }
574         lua_pop(L, 1);  // Pop mod name
575
576         // Allow read-only access to all mod directories
577         if (!write_required) {
578                 const std::vector<ModSpec> &mods = gamedef->getMods();
579                 for (const ModSpec &mod : mods) {
580                         str = fs::AbsolutePath(mod.path);
581                         if (!str.empty() && fs::PathStartsWith(abs_path, str)) {
582                                 return true;
583                         }
584                 }
585         }
586
587         str = fs::AbsolutePath(gamedef->getWorldPath());
588         if (!str.empty()) {
589                 // Don't allow access to other paths in the world mod/game path.
590                 // These have to be blocked so you can't override a trusted mod
591                 // by creating a mod with the same name in a world mod directory.
592                 // We add to the absolute path of the world instead of getting
593                 // the absolute paths directly because that won't work if they
594                 // don't exist.
595                 if (fs::PathStartsWith(abs_path, str + DIR_DELIM + "worldmods") ||
596                                 fs::PathStartsWith(abs_path, str + DIR_DELIM + "game")) {
597                         return false;
598                 }
599                 // Allow all other paths in world path
600                 if (fs::PathStartsWith(abs_path, str)) {
601                         if (write_allowed) *write_allowed = true;
602                         return true;
603                 }
604         }
605
606         // Default to disallowing
607         return false;
608 }
609
610
611 int ScriptApiSecurity::sl_g_dofile(lua_State *L)
612 {
613         int nret = sl_g_loadfile(L);
614         if (nret != 1) {
615                 lua_error(L);
616                 // code after this function isn't executed
617         }
618         int top_precall = lua_gettop(L);
619         lua_call(L, 0, LUA_MULTRET);
620         // Return number of arguments returned by the function,
621         // adjusting for the function being poped.
622         return lua_gettop(L) - (top_precall - 1);
623 }
624
625
626 int ScriptApiSecurity::sl_g_load(lua_State *L)
627 {
628         size_t len;
629         const char *buf;
630         std::string code;
631         const char *chunk_name = "=(load)";
632
633         luaL_checktype(L, 1, LUA_TFUNCTION);
634         if (!lua_isnone(L, 2)) {
635                 luaL_checktype(L, 2, LUA_TSTRING);
636                 chunk_name = lua_tostring(L, 2);
637         }
638
639         while (true) {
640                 lua_pushvalue(L, 1);
641                 lua_call(L, 0, 1);
642                 int t = lua_type(L, -1);
643                 if (t == LUA_TNIL) {
644                         break;
645                 }
646
647                 if (t != LUA_TSTRING) {
648                         lua_pushnil(L);
649                         lua_pushliteral(L, "Loader didn't return a string");
650                         return 2;
651                 }
652                 buf = lua_tolstring(L, -1, &len);
653                 code += std::string(buf, len);
654                 lua_pop(L, 1); // Pop return value
655         }
656         if (!safeLoadString(L, code, chunk_name)) {
657                 lua_pushnil(L);
658                 lua_insert(L, -2);
659                 return 2;
660         }
661         return 1;
662 }
663
664
665 int ScriptApiSecurity::sl_g_loadfile(lua_State *L)
666 {
667 #ifndef SERVER
668         ScriptApiBase *script = ModApiBase::getScriptApiBase(L);
669
670         // Client implementation
671         if (script->getType() == ScriptingType::Client) {
672                 std::string path = readParam<std::string>(L, 1);
673                 const std::string *contents = script->getClient()->getModFile(path);
674                 if (!contents) {
675                         std::string error_msg = "Coudln't find script called: " + path;
676                         lua_pushnil(L);
677                         lua_pushstring(L, error_msg.c_str());
678                         return 2;
679                 }
680
681                 std::string chunk_name = "@" + path;
682                 if (!safeLoadString(L, *contents, chunk_name.c_str())) {
683                         lua_pushnil(L);
684                         lua_insert(L, -2);
685                         return 2;
686                 }
687                 return 1;
688         }
689 #endif
690
691         // Server implementation
692         const char *path = NULL;
693         if (lua_isstring(L, 1)) {
694                 path = lua_tostring(L, 1);
695                 CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL);
696         }
697
698         if (!safeLoadFile(L, path)) {
699                 lua_pushnil(L);
700                 lua_insert(L, -2);
701                 return 2;
702         }
703
704         return 1;
705 }
706
707
708 int ScriptApiSecurity::sl_g_loadstring(lua_State *L)
709 {
710         const char *chunk_name = "=(load)";
711
712         luaL_checktype(L, 1, LUA_TSTRING);
713         if (!lua_isnone(L, 2)) {
714                 luaL_checktype(L, 2, LUA_TSTRING);
715                 chunk_name = lua_tostring(L, 2);
716         }
717
718         size_t size;
719         const char *code = lua_tolstring(L, 1, &size);
720         std::string code_s(code, size);
721
722         if (!safeLoadString(L, code_s, chunk_name)) {
723                 lua_pushnil(L);
724                 lua_insert(L, -2);
725                 return 2;
726         }
727         return 1;
728 }
729
730
731 int ScriptApiSecurity::sl_g_require(lua_State *L)
732 {
733         lua_pushliteral(L, "require() is disabled when mod security is on.");
734         return lua_error(L);
735 }
736
737
738 int ScriptApiSecurity::sl_io_open(lua_State *L)
739 {
740         bool with_mode = lua_gettop(L) > 1;
741
742         luaL_checktype(L, 1, LUA_TSTRING);
743         const char *path = lua_tostring(L, 1);
744
745         bool write_requested = false;
746         if (with_mode) {
747                 luaL_checktype(L, 2, LUA_TSTRING);
748                 const char *mode = lua_tostring(L, 2);
749                 write_requested = strchr(mode, 'w') != NULL ||
750                         strchr(mode, '+') != NULL ||
751                         strchr(mode, 'a') != NULL;
752         }
753         CHECK_SECURE_PATH_INTERNAL(L, path, write_requested, NULL);
754
755         push_original(L, "io", "open");
756         lua_pushvalue(L, 1);
757         if (with_mode) {
758                 lua_pushvalue(L, 2);
759         }
760
761         lua_call(L, with_mode ? 2 : 1, 2);
762         return 2;
763 }
764
765
766 int ScriptApiSecurity::sl_io_input(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         push_original(L, "io", "input");
774         lua_pushvalue(L, 1);
775         lua_call(L, 1, 1);
776         return 1;
777 }
778
779
780 int ScriptApiSecurity::sl_io_output(lua_State *L)
781 {
782         if (lua_isstring(L, 1)) {
783                 const char *path = lua_tostring(L, 1);
784                 CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL);
785         }
786
787         push_original(L, "io", "output");
788         lua_pushvalue(L, 1);
789         lua_call(L, 1, 1);
790         return 1;
791 }
792
793
794 int ScriptApiSecurity::sl_io_lines(lua_State *L)
795 {
796         if (lua_isstring(L, 1)) {
797                 const char *path = lua_tostring(L, 1);
798                 CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL);
799         }
800
801         int top_precall = lua_gettop(L);
802         push_original(L, "io", "lines");
803         lua_pushvalue(L, 1);
804         lua_call(L, 1, LUA_MULTRET);
805         // Return number of arguments returned by the function,
806         // adjusting for the function being poped.
807         return lua_gettop(L) - top_precall;
808 }
809
810
811 int ScriptApiSecurity::sl_os_rename(lua_State *L)
812 {
813         luaL_checktype(L, 1, LUA_TSTRING);
814         const char *path1 = lua_tostring(L, 1);
815         CHECK_SECURE_PATH_INTERNAL(L, path1, true, NULL);
816
817         luaL_checktype(L, 2, LUA_TSTRING);
818         const char *path2 = lua_tostring(L, 2);
819         CHECK_SECURE_PATH_INTERNAL(L, path2, true, NULL);
820
821         push_original(L, "os", "rename");
822         lua_pushvalue(L, 1);
823         lua_pushvalue(L, 2);
824         lua_call(L, 2, 2);
825         return 2;
826 }
827
828
829 int ScriptApiSecurity::sl_os_remove(lua_State *L)
830 {
831         luaL_checktype(L, 1, LUA_TSTRING);
832         const char *path = lua_tostring(L, 1);
833         CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL);
834
835         push_original(L, "os", "remove");
836         lua_pushvalue(L, 1);
837         lua_call(L, 1, 2);
838         return 2;
839 }
840