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