Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
s_security.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include "cpp_api/s_base.h"
23
24
25#define CHECK_SECURE_PATH_INTERNAL(L, path, write_required, ptr) \
26 if (!ScriptApiSecurity::checkPath(L, path, write_required, ptr)) { \
27 throw LuaError(std::string("Mod security: Blocked attempted ") + \
28 (write_required ? "write to " : "read from ") + path); \
29 }
30#define CHECK_SECURE_PATH(L, path, write_required) \
31 if (ScriptApiSecurity::isSecure(L)) { \
32 CHECK_SECURE_PATH_INTERNAL(L, path, write_required, NULL); \
33 }
34#define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \
35 if (ScriptApiSecurity::isSecure(L)) { \
36 CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \
37 }
38
39
40class ScriptApiSecurity : virtual public ScriptApiBase
41{
42public:
43 // Sets up security on the ScriptApi's Lua state
44 void initializeSecurity();
46 // Checks if the Lua state has been secured
47 static bool isSecure(lua_State *L);
48 // Loads a string as Lua code safely (doesn't allow bytecode).
49 static bool safeLoadString(lua_State *L, const std::string &code, const char *chunk_name);
50 // Loads a file as Lua code safely (doesn't allow bytecode).
51 static bool safeLoadFile(lua_State *L, const char *path, const char *display_name = NULL);
52 // Check if mod is whitelisted in the given setting
53 // This additionally checks that the mod's main file scope is executing.
54 static bool checkWhitelisted(lua_State *L, const std::string &setting);
55 // Checks if mods are allowed to read (and optionally write) to the path
56 static bool checkPath(lua_State *L, const char *path, bool write_required,
57 bool *write_allowed=NULL);
58
59private:
60 int getThread(lua_State *L);
61 // sets the enviroment to the table thats on top of the stack
62 void setLuaEnv(lua_State *L, int thread);
63 // creates an empty Lua environment
64 void createEmptyEnv(lua_State *L);
65
66 // Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
67 // (sl stands for Secure Lua)
68
69 static int sl_g_dofile(lua_State *L);
70 static int sl_g_load(lua_State *L);
71 static int sl_g_loadfile(lua_State *L);
72 static int sl_g_loadstring(lua_State *L);
73 static int sl_g_require(lua_State *L);
74
75 static int sl_io_open(lua_State *L);
76 static int sl_io_input(lua_State *L);
77 static int sl_io_output(lua_State *L);
78 static int sl_io_lines(lua_State *L);
79
80 static int sl_os_rename(lua_State *L);
81 static int sl_os_remove(lua_State *L);
82 static int sl_os_setlocale(lua_State *L);
83};
Definition: s_base.h:79
Definition: s_security.h:41
static int sl_os_remove(lua_State *L)
Definition: s_security.cpp:898
static int sl_os_setlocale(lua_State *L)
Definition: s_security.cpp:911
void initializeSecurityClient()
Definition: s_security.cpp:273
void initializeSecurity()
Definition: s_security.cpp:77
static bool safeLoadFile(lua_State *L, const char *path, const char *display_name=NULL)
Definition: s_security.cpp:439
static int sl_g_loadstring(lua_State *L)
Definition: s_security.cpp:777
static bool checkPath(lua_State *L, const char *path, bool write_required, bool *write_allowed=NULL)
Definition: s_security.cpp:530
int getThread(lua_State *L)
Definition: s_security.cpp:383
static int sl_g_require(lua_State *L)
Definition: s_security.cpp:800
static int sl_os_rename(lua_State *L)
Definition: s_security.cpp:880
static int sl_io_open(lua_State *L)
Definition: s_security.cpp:807
static int sl_g_loadfile(lua_State *L)
Definition: s_security.cpp:734
static int sl_io_input(lua_State *L)
Definition: s_security.cpp:835
static int sl_io_lines(lua_State *L)
Definition: s_security.cpp:863
static int sl_g_dofile(lua_State *L)
Definition: s_security.cpp:680
void createEmptyEnv(lua_State *L)
Definition: s_security.cpp:394
static bool isSecure(lua_State *L)
Definition: s_security.cpp:414
static bool safeLoadString(lua_State *L, const std::string &code, const char *chunk_name)
Definition: s_security.cpp:428
static int sl_io_output(lua_State *L)
Definition: s_security.cpp:849
static int sl_g_load(lua_State *L)
Definition: s_security.cpp:695
static bool checkWhitelisted(lua_State *L, const std::string &setting)
Definition: s_security.cpp:523
void setLuaEnv(lua_State *L, int thread)
Definition: s_security.cpp:401