Skip to content

Commit 8455dde

Browse files
debug.getregistry() now only retrieves whitelisted members
By default, the Lua registry is now inaccessible to .lua scripts. To expose a registry entry to .lua, you must modify CLuaUtilDefs::Debug_getregistry - ut and mt are already enabled.
1 parent 35e1d59 commit 8455dde

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Server/mods/deathmatch/logic/lua/CLuaMain.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ void CLuaMain::InitSecurity ( void )
129129
lua_register ( m_luaVM, "loadfile", CLuaUtilDefs::DisabledFunction );
130130
lua_register ( m_luaVM, "getfenv", CLuaUtilDefs::DisabledFunction );
131131
lua_register ( m_luaVM, "newproxy", CLuaUtilDefs::DisabledFunction );
132+
133+
// Wrap debug.getregistry to make it safe.
134+
lua_getglobal(m_luaVM, "debug"); // stack: [tbl_debug]
135+
lua_pushstring(m_luaVM, "getregistry"); // stack: [tbl_debug,"getregistry"]
136+
lua_pushcfunction(m_luaVM, CLuaUtilDefs::Debug_getregistry); // stack: [tbl_debug,"getregistry",CLuaUtilDefs::Debug_getregistry]
137+
lua_rawset(m_luaVM, -3); // stack: [tbl_debug]
138+
lua_pop(m_luaVM, -1); // stack: []
132139
}
133140

134141

Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ int CLuaUtilDefs::DisabledFunction ( lua_State* luaVM )
4848
return 1;
4949
}
5050

51+
52+
// Get a copy of the registry with whitelisted values
53+
int CLuaUtilDefs::Debug_getregistry(lua_State* luaVM)
54+
{
55+
// Create a new registry library, with only whitelisted sections
56+
lua_newtable(luaVM); // stack: [tbl_new]
57+
std::vector<const char*> szWhitelist = { "ud", "mt" };
58+
for (auto it = szWhitelist.begin(); it != szWhitelist.end(); it++)
59+
{
60+
lua_pushstring(luaVM, *it); // stack: [tbl_new,"ud"]
61+
lua_pushstring(luaVM, *it); // stack: [tbl_new,"ud","ud"]
62+
lua_gettable(luaVM, LUA_REGISTRYINDEX); // stack: [tbl_new,"ud",REGISTRY.ud]
63+
lua_settable(luaVM, -3); // stack: [tbl_new]
64+
}
65+
return 1;
66+
}
67+
5168
int CLuaUtilDefs::Dereference ( lua_State* luaVM )
5269
{
5370
int iPointer = 0;

Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CLuaUtilDefs : public CLuaDefs
1818

1919
// Reserved functions
2020
LUA_DECLARE ( DisabledFunction );
21+
LUA_DECLARE ( Debug_getregistry );
2122

2223
// Util functions to make scripting easier for the end user
2324
// Some of these are based on standard mIRC script funcs as a lot of people will be used to them

0 commit comments

Comments
 (0)