s_security.cpp 18 KB

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