18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

fix(scripting): sandbox the scenery Lua scripts like the Lua screens

The scenery scripts (lua <file> in the .scn) ran in a state with all of
LuaJIT's libraries (io, os, ffi, debug, require, load*), loaded with
luaL_dofile from any path, '..' and absolute ones included, and their
errors in event handlers went through an unprotected lua_call.

- scripting/luasandbox.{h,cpp}: what the Lua states share: the safe
  library set (moved from lua_program::open_libraries), loading a text-only
  chunk (moved from load_chunk), reading a script from a folder through the
  screens' path check (regular file, at most 16 MB), running it under pcall,
  and a clock-only os table (time, clock, difftime, date with the C89
  conversions; MSVC's strftime ends the program on an unknown one).
- lua.cpp: the scenery state gets the safe set and the clock; interpret()
  takes the scenery folder and the file and logs a refused or failing script
  with its file name; event handlers run under pcall and are logged with the
  event name; the scriptapi functions check their string argument
  (luaL_checkstring) instead of making a std::string from NULL; a memcell
  table without str updates the text to "" instead of from NULL.
- luaprogram.cpp: calls the shared helpers, no change for the screens.
- eu07.ini lua.fulllibraries (default no) gives trusted sceneries all
  libraries back. Documented in script_screens_doc.md.
- tests/unit/test_lua_sandbox.cpp: 8 cases for the shared sandbox.
This commit is contained in:
jerrrrycho
2026-11-13 15:23:01 +01:00
parent 39e8bcc59d
commit acda6562cd
12 changed files with 520 additions and 55 deletions

View File

@@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/.
#include "scripting/scriptscreenprogram.h"
#include "scripting/scriptscreendrawing.h"
#include "scripting/scriptscreenkernels.h"
#include "scripting/luasandbox.h"
#include "utilities/Logs.h"
#include "utilities/dictionary.h"
@@ -411,12 +412,7 @@ int api_read_file(lua_State *L)
// loads a script as text: precompiled code is refused. leaves the chunk, or an error message, on the stack
bool load_chunk(lua_State *L, std::string_view const Code, std::string const &Name)
{
if (false == Code.empty() && Code.front() == '\x1b')
{
lua_pushfstring(L, "%s: precompiled scripts aren't loaded", Name.c_str());
return false;
}
return 0 == luaL_loadbufferx(L, Code.data(), Code.size(), ("@" + Name).c_str(), "t");
return lua_sandbox::load_text_chunk(L, Code, Name);
}
// eu07.include(path): runs another script below the script's folder and returns what it returns
@@ -724,30 +720,10 @@ void push_state(lua_State *L, dictionary_source const &Input)
}
}
void open_library(lua_State *L, lua_CFunction Open, char const *Name)
{
lua_pushcfunction(L, Open);
lua_pushstring(L, Name);
lua_call(L, 1, 0);
}
void lua_program::open_libraries()
{
open_library(m_state, luaopen_base, "");
open_library(m_state, luaopen_table, LUA_TABLIBNAME);
open_library(m_state, luaopen_string, LUA_STRLIBNAME);
open_library(m_state, luaopen_math, LUA_MATHLIBNAME);
open_library(m_state, luaopen_bit, LUA_BITLIBNAME);
// what reaches files or loads code
for (auto const *name : {"dofile", "loadfile", "load", "loadstring", "module", "require"})
{
lua_pushnil(m_state);
lua_setglobal(m_state, name);
}
lua_getglobal(m_state, LUA_STRLIBNAME);
lua_pushnil(m_state);
lua_setfield(m_state, -2, "dump");
lua_pop(m_state, 1);
// base, table, string, math and bit, without what reaches files or loads code
lua_sandbox::open_safe_libraries(m_state);
}
void lua_program::register_api()