18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-29 05:26:17 +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:
jerrrrycho
2026-11-13 14:25:18 +01:00
parent 39e8bcc59d
commit 1bd7187db7
14 changed files with 750 additions and 54 deletions

View File

@@ -14,11 +14,13 @@ http://mozilla.org/MPL/2.0/.
#include "scripting/scriptscreens.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace script_screens
@@ -28,6 +30,14 @@ namespace script_screens
constexpr int max_image_side{4096};
// the most instructions one call into a screen may run, so that a script stuck in a loop can't hang the worker
constexpr std::int64_t instruction_limit{200'000'000};
// what one call into a screen may send and log; the rest is dropped, which the log tells once per screen. the longest
// name of a command the engine knows is under 40 characters
constexpr std::size_t max_commands_per_call{64};
constexpr std::size_t max_command_name{64};
constexpr int max_log_lines_per_call{32};
constexpr std::size_t max_log_line{1024}; // bytes of the text, a longer one is cut
// the largest file a screen reads: its script or module, a file below its folder
constexpr std::uintmax_t max_screen_file{16 * 1024 * 1024};
struct pending_command
{
@@ -86,6 +96,31 @@ std::unique_ptr<runtime> make_wasm_runtime();
std::string utf8_text(std::string const &Text);
// a file of the screen's folder or below; nothing for a path leading outside, or no such file
std::optional<std::string> read_screen_file(std::string const &Folder, std::string_view Path);
// the file, when it is a regular file of at most max_screen_file bytes: not a folder, a device or anything larger
std::optional<std::string> read_regular_file(std::string const &Path);
// what a screen sends and logs, held to the limits of one call. Name: how the log names the screen
class output_limits
{
public:
explicit output_limits(std::string Name) : m_name{std::move(Name)} {}
// a new call into the screen
void start_call()
{
m_log_lines = 0;
}
// true when the command may go with the call; a command over the limits is dropped
bool take_command(render_call const &Call, std::string_view Name);
// the line to log, cut to max_log_line; nothing when the call logged enough
std::optional<std::string> take_log(std::string_view Text);
private:
std::string m_name;
int m_log_lines{0};
bool m_commands_dropped{false}; // logged once
bool m_logs_dropped{false}; // logged once
};
// seconds of a steady clock, the same for every screen, for animations that don't depend on how often the screen is
// refreshed
double screen_time();