2021-10-29 21:25:12 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static const luaL_Reg lualibs[] = {
|
|
|
|
{"", luaopen_base},
|
|
|
|
{LUA_COLIBNAME, luaopen_coroutine},
|
|
|
|
{LUA_TABLIBNAME, luaopen_table},
|
|
|
|
{LUA_OSLIBNAME, luaopen_os},
|
|
|
|
{LUA_STRLIBNAME, luaopen_string},
|
|
|
|
{LUA_MATHLIBNAME, luaopen_math},
|
|
|
|
{LUA_DBLIBNAME, luaopen_debug},
|
|
|
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
|
|
|
{LUA_BITLIBNAME, luaopen_bit32},
|
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
2021-11-29 16:14:06 +00:00
|
|
|
void luaL_openlibs(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
const luaL_Reg* lib = lualibs;
|
|
|
|
for (; lib->func; lib++)
|
|
|
|
{
|
2021-11-19 16:10:07 +00:00
|
|
|
lua_pushcfunction(L, lib->func, NULL);
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_pushstring(L, lib->name);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:14:06 +00:00
|
|
|
void luaL_sandbox(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
// set all libraries to read-only
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, LUA_GLOBALSINDEX) != 0)
|
|
|
|
{
|
|
|
|
if (lua_istable(L, -1))
|
|
|
|
lua_setreadonly(L, -1, true);
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set all builtin metatables to read-only
|
|
|
|
lua_pushliteral(L, "");
|
|
|
|
lua_getmetatable(L, -1);
|
|
|
|
lua_setreadonly(L, -1, true);
|
2021-11-30 16:14:28 +00:00
|
|
|
lua_pop(L, 2);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
// set globals to readonly and activate safeenv since the env is immutable
|
|
|
|
lua_setreadonly(L, LUA_GLOBALSINDEX, true);
|
|
|
|
lua_setsafeenv(L, LUA_GLOBALSINDEX, true);
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:14:06 +00:00
|
|
|
void luaL_sandboxthread(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
// create new global table that proxies reads to original table
|
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
lua_setreadonly(L, -1, true);
|
|
|
|
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
// we can set safeenv now although it's important to set it to false if code is loaded twice into the thread
|
|
|
|
lua_replace(L, LUA_GLOBALSINDEX);
|
|
|
|
lua_setsafeenv(L, LUA_GLOBALSINDEX, true);
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:53:37 +00:00
|
|
|
static void* l_alloc(void* ud, void* ptr, size_t osize, size_t nsize)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
(void)ud;
|
|
|
|
(void)osize;
|
|
|
|
if (nsize == 0)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return realloc(ptr, nsize);
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:14:06 +00:00
|
|
|
lua_State* luaL_newstate(void)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
return lua_newstate(l_alloc, NULL);
|
|
|
|
}
|