@@ -63,7 +63,6 @@ CLuaMain::CLuaMain ( CLuaManager* pLuaManager,
6363
6464 m_bEnableOOP = bEnableOOP;
6565
66- m_iPackageRef = -1 ;
6766
6867 CPerfStatLuaMemory::GetSingleton ()->OnLuaMainCreate ( this );
6968 CPerfStatLuaTiming::GetSingleton ()->OnLuaMainCreate ( this );
@@ -127,33 +126,13 @@ void CLuaMain::InitSecurity ( void )
127126{
128127 lua_register ( m_luaVM, " dofile" , CLuaUtilDefs::DisabledFunction );
129128 lua_register ( m_luaVM, " loadfile" , CLuaUtilDefs::DisabledFunction );
129+ lua_register ( m_luaVM, " require" , CLuaUtilDefs::DisabledFunction );
130+ lua_register ( m_luaVM, " loadlib" , CLuaUtilDefs::DisabledFunction );
130131 lua_register ( m_luaVM, " getfenv" , CLuaUtilDefs::DisabledFunction );
131132 lua_register ( m_luaVM, " newproxy" , CLuaUtilDefs::DisabledFunction );
132133}
133134
134135
135- void CLuaMain::InitPackageSecurity (void )
136- {
137- // Grab our original package library
138- lua_getglobal (m_luaVM, " package" ); // stack: [tbl_package]
139-
140- // Create a new package library, with only whitelisted functions
141- lua_newtable (m_luaVM); // stack: [tbl_package,tbl_new]
142- std::vector<const char *> szWhitelist = { " loaded" , " seeall" };
143- for (auto it = szWhitelist.begin (); it != szWhitelist.end (); it++)
144- {
145- lua_pushstring (m_luaVM, *it); // stack: [tbl_package,tbl_new,"loaded"]
146- lua_pushstring (m_luaVM, *it); // stack: [tbl_package,tbl_new,"loaded","loaded"]
147- lua_gettable (m_luaVM, -4 ); // stack: [tbl_package,tbl_new,"loaded",package.loaded]
148- lua_settable (m_luaVM, -3 ); // stack: [tbl_package,tbl_new]
149- }
150- lua_setglobal (m_luaVM, " package" ); // stack: [tbl_package]
151-
152- // Keep the original package library safe in the registry
153- m_iPackageRef = luaL_ref (m_luaVM, LUA_REGISTRYINDEX); // stack: []
154- }
155-
156-
157136void CLuaMain::InitClasses ( lua_State* luaVM )
158137{
159138 lua_initclasses ( luaVM );
@@ -206,27 +185,13 @@ void CLuaMain::InitVM ( void )
206185 lua_sethook ( m_luaVM, InstructionCountHook, LUA_MASKCOUNT, HOOK_INSTRUCTION_COUNT );
207186
208187 // Load LUA libraries
209- const luaL_Reg lualibs[] = {
210- { " " , luaopen_base },
211- { LUA_LOADLIBNAME, luaopen_package },
212- { LUA_TABLIBNAME, luaopen_table },
213- { LUA_STRLIBNAME, luaopen_string },
214- { LUA_MATHLIBNAME, luaopen_math },
215- { LUA_DBLIBNAME, luaopen_debug },
216- { " utf8" , luaopen_utf8 },
217- { NULL , NULL }
218- };
219-
220- const luaL_Reg *lib = lualibs;
221- for (; lib->func ; lib++) {
222- lua_pushcfunction (m_luaVM, lib->func );
223- lua_pushstring (m_luaVM, lib->name );
224- lua_call (m_luaVM, 1 , 0 );
225- }
226-
227- // Initialize our customized 'package' library with restricted functions
228- InitPackageSecurity ();
229-
188+ luaopen_base ( m_luaVM );
189+ luaopen_math ( m_luaVM );
190+ luaopen_string ( m_luaVM );
191+ luaopen_table ( m_luaVM );
192+ luaopen_debug ( m_luaVM );
193+ luaopen_utf8 ( m_luaVM );
194+
230195 // Initialize security restrictions. Very important to prevent lua trojans and viruses!
231196 InitSecurity ();
232197
@@ -721,39 +686,3 @@ int CLuaMain::OnUndump( const char* p, size_t n )
721686 }
722687 return 1 ;
723688}
724-
725-
726- // /////////////////////////////////////////////////////////////
727- //
728- // CLuaMain::SetPackagePaths
729- //
730- // Sets module directories for pure lua and C lua modules
731- //
732- // /////////////////////////////////////////////////////////////
733- bool CLuaMain::SetPackagePaths (SString strPath, SString strCPath)
734- {
735- if (!m_luaVM || m_iPackageRef < 0 )
736- return false ;
737-
738- // Setup our wildcards for searching
739- strPath = PathJoin (strPath, " ?.lua" ) + " ;" + PathJoin (strPath, " ?/init.lua" );
740-
741- #ifdef WIN32
742- strCPath = PathJoin (strCPath, " ?.dll" );
743- #else
744- strCPath = PathJoin (strCPath, " ?.so" );
745- #endif
746-
747- // Overwrite our path variable function from the registry (saved earlier)
748- lua_rawgeti (m_luaVM, LUA_REGISTRYINDEX, m_iPackageRef); // stack: [tbl_hiddenPackage]
749- lua_pushstring (m_luaVM, " path" ); // stack: [tbl_hiddenPackage,"path"]
750- lua_pushstring (m_luaVM, strPath.c_str ()); // stack: [tbl_hiddenPackage,"path","C:/someresource/?.lua"]
751- lua_settable (m_luaVM, -3 ); // stack: [tbl_hiddenPackage]
752-
753- lua_pushstring (m_luaVM, " cpath" ); // stack: [tbl_hiddenPackage,"cpath"]
754- lua_pushstring (m_luaVM, strCPath.c_str ()); // stack: [tbl_hiddenPackage,"cpath","C:/somemodules/?.dll"]
755- lua_settable (m_luaVM, -3 ); // stack: [tbl_hiddenPackage]
756- lua_pop (m_luaVM, -1 ); // stack: []
757-
758- return true ;
759- }
0 commit comments