Skip to content

Commit

Permalink
ChaosMod/LuaScripts: Block native calls during script eval by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Jan 19, 2025
1 parent bcdfd0c commit 8cdd7fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions ChaosMod/Components/LuaScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ enum class LuaNativeReturnType
Vector3
};

_LUAFUNC static sol::object LuaInvoke(const sol::environment &env, DWORD64 nativeHash, LuaNativeReturnType returnType,
const sol::variadic_args &args)
_LUAFUNC static sol::object LuaInvoke(const sol::environment &env, std::uint64_t nativeHash,
LuaNativeReturnType returnType, const sol::variadic_args &args)
{
if (nativeHash == 0x213AEB2B90CBA7AC || nativeHash == 0x5A5F40FE637EB584 || nativeHash == 0x933D6A9EEC1BACD0
|| nativeHash == 0xE80492A9AC099A93 || nativeHash == 0x8EF07E15701D61ED)
Expand Down Expand Up @@ -298,8 +298,6 @@ static const std::vector<ExposableFunc> ms_Exposables {
E("GET_HASH_KEY", GET_HASH_KEY),
E("print", [](const sol::this_environment &curEnv, const std::string &text)
{ LuaPrint(curEnv.env->get<sol::table>("EnvInfo")["ScriptName"], text); }),
E("_invoke", [](const sol::this_environment &curEnv, DWORD64 hash, LuaNativeReturnType returnType,
const sol::variadic_args &args) { return LuaInvoke(curEnv, hash, returnType, args); }),
E("WAIT", WAIT),
E("IsKeyPressed",
[](unsigned char key)
Expand Down Expand Up @@ -461,6 +459,26 @@ LuaScripts::LuaScripts()
ParseScript(fileName, path.string(), ParseScriptFlag_ScriptIsFilePath, userEffectSettings);
};

bool allowEvalNativeInvocations = DoesFeatureFlagExist("allowscriptevalnativeinvocations");

if (allowEvalNativeInvocations)
{
m_GlobalState["_invoke"] = [](const sol::this_environment &curEnv, std::uint64_t hash,
LuaNativeReturnType returnType, const sol::variadic_args &args)
{
return LuaInvoke(curEnv, hash, returnType, args);
};
}
else
{
m_GlobalState["_invoke"] = [](const sol::this_environment &curEnv, std::uint64_t hash,
LuaNativeReturnType returnType, const sol::variadic_args &args)
{
LOG("WARNING: Blocked invocation of native 0x" << std::uppercase << std::hex << hash << std::setfill(' ')
<< " during script evaluation!");
};
}

for (auto dir : ms_ScriptDirs)
{
if (!DoesFileExist(dir))
Expand All @@ -482,6 +500,15 @@ LuaScripts::LuaScripts()
parseScript(entry);
}
}

if (!allowEvalNativeInvocations)
{
m_GlobalState["_invoke"] = [](const sol::this_environment &curEnv, std::uint64_t hash,
LuaNativeReturnType returnType, const sol::variadic_args &args)
{
return LuaInvoke(curEnv, hash, returnType, args);
};
}
}

void LuaScripts::OnModPauseCleanup()
Expand Down
4 changes: 2 additions & 2 deletions ChaosMod/Memory/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ namespace Memory
<< (scanRange.StartAddr == 0 && scanRange.EndAddr == 0
? "\""
: (std::stringstream()
<< "\" within address range 0x" << std::uppercase << std::hex << std::setfill(' ')
<< scanRange.StartAddr << " to 0x" << std::uppercase << std::hex << scanRange.EndAddr)
<< "\" within address range 0x" << std::uppercase << std::hex << scanRange.StartAddr
<< std::setfill(' ') << " to 0x" << std::uppercase << std::hex << scanRange.EndAddr)
.str()));

if ((scanRange.StartAddr != 0 || scanRange.EndAddr != 0) && scanRange.StartAddr >= scanRange.EndAddr)
Expand Down

0 comments on commit 8cdd7fc

Please sign in to comment.