mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 22:55:16 +01:00
fix(scripting): close the small holes in the script screen sandbox
- Lua: once the instruction budget of a call is spent, the count hook runs at every instruction and raises again, so pcall, nested pcall and coroutine.resume can't swallow the limit; xpcall is rebuilt on pcall so its handler runs after the error left f, while the hook works - Lua: the metatables of eu07.image/font/sprite are locked (__metatable = false); __gc destroys only an object of its own type, once - Lua: read_png checks the size with stbi_info before decoding - Lua: string.rep makes no string over 1 MiB - Lua and WASM: at most 64 commands per call, names up to 64 bytes, 32 log lines per call cut at 1024 bytes; the drops are logged once - fonts: a glyph wider than 4096 is skipped; more than 4096 glyphs, 8192 columns or an image side over 4096 refuse the font; cut_glyph reads only the columns inside the image - the script path from the .mmd (cab screens, destination signs, make: textures) must stay inside the game folder; the files a screen reads, its script and its module must be regular files of 16 MiB at most Tests for each in test_script_screen_host.cpp, test_script_screen_drawing.cpp and test_script_screens.cpp; the test stubs can capture the log.
This commit is contained in:
@@ -25,8 +25,6 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
@@ -117,7 +115,10 @@ std::int32_t to_int32(std::size_t const Value)
|
||||
class wasm_program final : public program
|
||||
{
|
||||
public:
|
||||
wasm_program(std::string const &Script, script_screen_kind const Kind) : kind{Kind}, script{Script}, folder{substr_path(Script)} {}
|
||||
wasm_program(std::string const &Script, script_screen_kind const Kind)
|
||||
: kind{Kind}, script{Script}, folder{substr_path(Script)}, output{std::format("WASM screen \"{}\"", Script)}
|
||||
{
|
||||
}
|
||||
~wasm_program() override
|
||||
{
|
||||
if (exec_env != nullptr)
|
||||
@@ -142,6 +143,7 @@ class wasm_program final : public program
|
||||
input_view const *input{nullptr};
|
||||
render_call *call{nullptr};
|
||||
bool commands_ignored{false}; // a screen outside the cab tried to send a command, and it was logged
|
||||
output_limits output;
|
||||
|
||||
private:
|
||||
bool call_i32(wasm_function_inst_t Function, std::string_view Name, std::int32_t &Result);
|
||||
@@ -300,17 +302,23 @@ void native_command(wasm_exec_env_t Env, char const *Name, std::uint32_t const L
|
||||
}
|
||||
return;
|
||||
}
|
||||
screen->call->commands.emplace_back(std::string{Name, Length}, Param1, Param2, screen->call->vehicle);
|
||||
if (screen->output.take_command(*screen->call, std::string_view{Name, Length}))
|
||||
{
|
||||
screen->call->commands.emplace_back(std::string{Name, Length}, Param1, Param2, screen->call->vehicle);
|
||||
}
|
||||
}
|
||||
|
||||
void native_log(wasm_exec_env_t Env, char const *Text, std::uint32_t const Length)
|
||||
{
|
||||
auto const *screen{program_of(Env)};
|
||||
auto *screen{program_of(Env)};
|
||||
if (screen == nullptr || Text == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
WriteLog(std::format("WASM screen \"{}\": {}", screen->script, std::string_view{Text, Length}));
|
||||
if (auto const line{screen->output.take_log(std::string_view{Text, Length})}; line.has_value())
|
||||
{
|
||||
WriteLog(*line);
|
||||
}
|
||||
}
|
||||
|
||||
// reads a file from the screen's folder into the buffer. returns the size of the whole file, which may be more
|
||||
@@ -436,6 +444,7 @@ bool wasm_program::instantiate(wasm_module_t const Module)
|
||||
bool wasm_program::call_i32(wasm_function_inst_t Function, std::string_view const Name, std::int32_t &Result)
|
||||
{
|
||||
wasm_runtime_set_instruction_count_limit(exec_env, static_cast<int>(instruction_limit));
|
||||
output.start_call();
|
||||
std::array<std::uint32_t, 1> cells{};
|
||||
if (false == wasm_runtime_call_wasm(exec_env, Function, 0, cells.data()))
|
||||
{
|
||||
@@ -579,13 +588,13 @@ wasm_module_t wasm_runtime_host::load_module(std::string const &Script)
|
||||
}
|
||||
// a failure is kept as well, so the module isn't tried again for every screen using it
|
||||
auto &entry{m_modules[Script]};
|
||||
std::ifstream file{Script, std::ios::binary};
|
||||
if (false == file.is_open())
|
||||
auto const file{read_regular_file(Script)};
|
||||
if (false == file.has_value())
|
||||
{
|
||||
ErrorLog(std::format("WASM screen \"{}\": file not found", Script));
|
||||
ErrorLog(std::format("WASM screen \"{}\": file not found, or larger than {} bytes", Script, max_screen_file));
|
||||
return nullptr;
|
||||
}
|
||||
entry.bytes.assign(std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{});
|
||||
entry.bytes.assign(file->begin(), file->end());
|
||||
if (entry.bytes.empty() || entry.bytes.size() > std::numeric_limits<std::uint32_t>::max())
|
||||
{
|
||||
ErrorLog(std::format("WASM screen \"{}\": invalid module size", Script));
|
||||
|
||||
Reference in New Issue
Block a user