Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Scripting: adjust string metatable setup to fix sandbox integrity #27666

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libraries/AP_Scripting/lua/src/lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,11 @@ static void createmetatable (lua_State *L) {
*/
LUAMOD_API int luaopen_string (lua_State *L) {
luaL_newlib(L, strlib);
#if defined(ARDUPILOT_BUILD)
// metatable setup handled by Ardupilot scripting system
#else
createmetatable(L);
#endif
return 1;
}

10 changes: 10 additions & 0 deletions libraries/AP_Scripting/lua_scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ void lua_scripts::run(void) {
lua_atpanic(L, atpanic);
load_generated_bindings(L);

// set up string metatable. we set up one for all scripts that no script has
// access to, as it's impossible to set up one per-script and we don't want
// any script to be able to mess with it.
lua_pushliteral(L, ""); /* dummy string */
lua_createtable(L, 0, 1); /* table to be metatable for strings */
luaopen_string(L); /* get string library */
lua_setfield(L, -2, "__index"); /* metatable.__index = string */
lua_setmetatable(L, -2); /* set table as metatable for strings */
lua_pop(L, 1); /* pop dummy string */

#ifndef HAL_CONSOLE_DISABLED
const int loaded_mem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
DEV_PRINTF("Lua: State memory usage: %i + %i\n", inital_mem, loaded_mem - inital_mem);
Expand Down
Loading