Luanti 5.10.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#define CHECK_SECURE_PATH(L, path, write_required) \
16 if (ScriptApiSecurity::isSecure(L)) { \
17 CHECK_SECURE_PATH_INTERNAL(L, path, write_required, NULL); \
18 }
19#define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \
20 if (ScriptApiSecurity::isSecure(L)) { \
21 CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \
22 }
23
24
25class ScriptApiSecurity : virtual public ScriptApiBase
26{
27public:
28 // Sets up security on the ScriptApi's Lua state
29 void initializeSecurity();
31 // Checks if the Lua state has been secured
32 static bool isSecure(lua_State *L);
33 // Loads a string as Lua code safely (doesn't allow bytecode).
34 static bool safeLoadString(lua_State *L, const std::string &code, const char *chunk_name);
35 // Loads a file as Lua code safely (doesn't allow bytecode).
36 static bool safeLoadFile(lua_State *L, const char *path, const char *display_name = NULL);
37 // Check if mod is whitelisted in the given setting
38 // This additionally checks that the mod's main file scope is executing.
39 static bool checkWhitelisted(lua_State *L, const std::string &setting);
40 // Checks if mods are allowed to read (and optionally write) to the path
41 static bool checkPath(lua_State *L, const char *path, bool write_required,
42 bool *write_allowed=NULL);
43
44private:
45 int getThread(lua_State *L);
46 // sets the enviroment to the table thats on top of the stack
47 void setLuaEnv(lua_State *L, int thread);
48 // creates an empty Lua environment
49 void createEmptyEnv(lua_State *L);
50
51 // Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
52 // (sl stands for Secure Lua)
53
54 static int sl_g_dofile(lua_State *L);
55 static int sl_g_load(lua_State *L);
56 static int sl_g_loadfile(lua_State *L);
57 static int sl_g_loadstring(lua_State *L);
58 static int sl_g_require(lua_State *L);
59
60 static int sl_io_open(lua_State *L);
61 static int sl_io_input(lua_State *L);
62 static int sl_io_output(lua_State *L);
63 static int sl_io_lines(lua_State *L);
64
65 static int sl_os_rename(lua_State *L);
66 static int sl_os_remove(lua_State *L);
67 static int sl_os_setlocale(lua_State *L);
68};
Definition s_base.h:64
Definition s_security.h:26
static int sl_os_remove(lua_State *L)
Definition s_security.cpp:893
static int sl_os_setlocale(lua_State *L)
Definition s_security.cpp:906
void initializeSecurityClient()
Definition s_security.cpp:263
void initializeSecurity()
Definition s_security.cpp:62
static bool safeLoadFile(lua_State *L, const char *path, const char *display_name=NULL)
Definition s_security.cpp:434
static int sl_g_loadstring(lua_State *L)
Definition s_security.cpp:772
static bool checkPath(lua_State *L, const char *path, bool write_required, bool *write_allowed=NULL)
Definition s_security.cpp:525
int getThread(lua_State *L)
Definition s_security.cpp:378
static int sl_g_require(lua_State *L)
Definition s_security.cpp:795
static int sl_os_rename(lua_State *L)
Definition s_security.cpp:875
static int sl_io_open(lua_State *L)
Definition s_security.cpp:802
static int sl_g_loadfile(lua_State *L)
Definition s_security.cpp:729
static int sl_io_input(lua_State *L)
Definition s_security.cpp:830
static int sl_io_lines(lua_State *L)
Definition s_security.cpp:858
static int sl_g_dofile(lua_State *L)
Definition s_security.cpp:675
void createEmptyEnv(lua_State *L)
Definition s_security.cpp:389
static bool isSecure(lua_State *L)
Definition s_security.cpp:409
static bool safeLoadString(lua_State *L, const std::string &code, const char *chunk_name)
Definition s_security.cpp:423
static int sl_io_output(lua_State *L)
Definition s_security.cpp:844
static int sl_g_load(lua_State *L)
Definition s_security.cpp:690
static bool checkWhitelisted(lua_State *L, const std::string &setting)
Definition s_security.cpp:518
void setLuaEnv(lua_State *L, int thread)
Definition s_security.cpp:396