Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
s_security.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "cpp_api/s_base.h"
8
9
10#define CHECK_SECURE_PATH_INTERNAL(L, path, write_required, ptr) \
11 if (!ScriptApiSecurity::checkPath(L, path, write_required, ptr)) { \
12 throw LuaError(std::string("Mod security: Blocked attempted ") + \
13 (write_required ? "write to " : "read from ") + path); \
14 }
15
16#define CHECK_SECURE_PATH(L, path, write_required) \
17 if (ScriptApiSecurity::isSecure(L)) { \
18 CHECK_SECURE_PATH_INTERNAL(L, path, write_required, nullptr); \
19 }
20
21#define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \
22 if (ScriptApiSecurity::isSecure(L)) { \
23 CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \
24 }
25
26
27class ScriptApiSecurity : virtual public ScriptApiBase
28{
29public:
30 // Sets up security on the ScriptApi's Lua state
31 void initializeSecurity();
32#if CHECK_CLIENT_BUILD()
34#else
35 inline void initializeSecurityClient() { assert(0); }
36#endif
37
38 // Checks if the Lua state has been secured
39 static bool isSecure(lua_State *L);
40 // Leaves the untampered globals (table) on top of the stack
41 static void getGlobalsBackup(lua_State *L);
42
44 static bool safeLoadString(lua_State *L, std::string_view code, const char *chunk_name);
47 static bool safeLoadFile(lua_State *L, const char *path, const char *display_name = nullptr);
48
55 static std::string getCurrentModName(lua_State *L);
59 static bool checkWhitelisted(lua_State *L, const std::string &setting);
60
63 static bool checkPath(lua_State *L, const char *path, bool write_required,
64 bool *write_allowed = nullptr);
65
66protected:
67 // To be implemented by descendants:
68
75 virtual bool modNamesAreTrusted() { return false; }
76
85 virtual bool checkPathInternal(const std::string &abs_path, bool write_required,
86 bool *write_allowed) = 0;
87
88 // Ready-made implementation of `checkPathInternal` suitable for server-related uses
89 static bool checkPathWithGamedef(lua_State *L, const std::string &abs_path,
90 bool write_required, bool *write_allowed);
91
92private:
93 int getThread(lua_State *L);
94 // sets the enviroment to the table thats on top of the stack
95 void setLuaEnv(lua_State *L, int thread);
96 // creates an empty Lua environment
97 void createEmptyEnv(lua_State *L);
98
99 bool m_secure = false;
100
101 // Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
102 // (sl stands for Secure Lua)
103
104 static int sl_g_dofile(lua_State *L);
105 static int sl_g_load(lua_State *L);
106 static int sl_g_loadfile(lua_State *L);
107 static int sl_g_loadstring(lua_State *L);
108 static int sl_g_require(lua_State *L);
109
110 static int sl_io_open(lua_State *L);
111 static int sl_io_input(lua_State *L);
112 static int sl_io_output(lua_State *L);
113 static int sl_io_lines(lua_State *L);
114
115 static int sl_os_rename(lua_State *L);
116 static int sl_os_remove(lua_State *L);
117 static int sl_os_setlocale(lua_State *L);
118};
Definition s_base.h:64
Definition s_security.h:28
static int sl_os_remove(lua_State *L)
Definition s_security.cpp:943
static int sl_os_setlocale(lua_State *L)
Definition s_security.cpp:956
void initializeSecurityClient()
Definition s_security.h:35
void initializeSecurity()
Definition s_security.cpp:62
static int sl_g_loadstring(lua_State *L)
Definition s_security.cpp:822
static std::string getCurrentModName(lua_State *L)
Returns the currently running mod, only during init time.
Definition s_security.cpp:513
bool m_secure
Definition s_security.h:99
static bool safeLoadString(lua_State *L, std::string_view code, const char *chunk_name)
Loads a string as Lua code safely (doesn't allow bytecode).
Definition s_security.cpp:433
int getThread(lua_State *L)
Definition s_security.cpp:383
static int sl_g_require(lua_State *L)
Definition s_security.cpp:845
static int sl_os_rename(lua_State *L)
Definition s_security.cpp:925
static int sl_io_open(lua_State *L)
Definition s_security.cpp:852
static int sl_g_loadfile(lua_State *L)
Definition s_security.cpp:779
virtual bool checkPathInternal(const std::string &abs_path, bool write_required, bool *write_allowed)=0
Should check if the given path may be accessed.
static bool checkPathWithGamedef(lua_State *L, const std::string &abs_path, bool write_required, bool *write_allowed)
Definition s_security.cpp:607
static int sl_io_input(lua_State *L)
Definition s_security.cpp:880
static int sl_io_lines(lua_State *L)
Definition s_security.cpp:908
static int sl_g_dofile(lua_State *L)
Definition s_security.cpp:725
static bool checkPath(lua_State *L, const char *path, bool write_required, bool *write_allowed=nullptr)
Checks if mods are allowed to read (and optionally write) to the path.
Definition s_security.cpp:563
void createEmptyEnv(lua_State *L)
Definition s_security.cpp:394
static bool isSecure(lua_State *L)
Definition s_security.cpp:414
static void getGlobalsBackup(lua_State *L)
Definition s_security.cpp:422
static bool safeLoadFile(lua_State *L, const char *path, const char *display_name=nullptr)
Loads a file as Lua code safely (doesn't allow bytecode).
Definition s_security.cpp:444
static int sl_io_output(lua_State *L)
Definition s_security.cpp:894
static int sl_g_load(lua_State *L)
Definition s_security.cpp:740
virtual bool modNamesAreTrusted()
Specify if the mod names during init time(!) can be trusted.
Definition s_security.h:75
static bool checkWhitelisted(lua_State *L, const std::string &setting)
Check if mod is whitelisted in the given setting.
Definition s_security.cpp:556
void setLuaEnv(lua_State *L, int thread)
Definition s_security.cpp:401