From 9b006b2c8533bae1e528bd123be209453787b9b7 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 16 Nov 2025 12:01:48 +0000 Subject: [PATCH] plugin/hook: disallow multiple hooks per function (#12320) this was never safe. After recent changes, it's become even less so. Just disallow it. ref #11992 --- src/plugins/HookSystem.cpp | 10 ++++++++++ src/plugins/HookSystem.hpp | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/plugins/HookSystem.cpp b/src/plugins/HookSystem.cpp index c5def6a5..031b1def 100644 --- a/src/plugins/HookSystem.cpp +++ b/src/plugins/HookSystem.cpp @@ -144,6 +144,12 @@ bool CFunctionHook::hook() { return false; #endif + if (g_pFunctionHookSystem->m_activeHooks.contains(rc(m_source))) { + // TODO: return actual error codes... + Debug::log(ERR, "[functionhook] failed, function is already hooked"); + return false; + } + // jmp rel32 // offset for relative addr: 1 static constexpr uint8_t RELATIVE_JMP_ADDRESS[] = {0xE9, 0x00, 0x00, 0x00, 0x00}; @@ -231,6 +237,8 @@ bool CFunctionHook::hook() { m_active = true; m_hookLen = ORIGSIZE; + g_pFunctionHookSystem->m_activeHooks.emplace(rc(m_source)); + return true; } @@ -243,6 +251,8 @@ bool CFunctionHook::unhook() { if (!m_active) return false; + g_pFunctionHookSystem->m_activeHooks.erase(rc(m_source)); + // allow write to src mprotect(sc(m_source) - rc(m_source) % sysconf(_SC_PAGE_SIZE), sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC); diff --git a/src/plugins/HookSystem.hpp b/src/plugins/HookSystem.hpp index cf098dd1..3431e8c8 100644 --- a/src/plugins/HookSystem.hpp +++ b/src/plugins/HookSystem.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "../helpers/memory/Memory.hpp" #define HANDLE void* @@ -70,7 +71,8 @@ class CHookSystem { uint64_t used = 0; }; - std::vector m_pages; + std::vector m_pages; + std::unordered_set m_activeHooks; friend class CFunctionHook; };