-
-
Notifications
You must be signed in to change notification settings - Fork 252
Description
Hi, this screams error:
PolyHook_2_0/sources/x64Detour.cpp
Line 721 in f4dbf7b
| auto tmpTrampoline = (uint64_t) new uint8_t[m_trampolineSz]; |
PolyHook_2_0/sources/x64Detour.cpp
Line 776 in f4dbf7b
| MemoryProtector prot(m_trampoline, m_trampolineSz, ProtFlag::R | ProtFlag::W | ProtFlag::X, *this, false); |
Similarly on x86:
PolyHook_2_0/sources/x86Detour.cpp
Line 124 in f4dbf7b
| m_trampoline = (uint64_t) new unsigned char[m_trampolineSz]; |
PolyHook_2_0/sources/x86Detour.cpp
Line 132 in f4dbf7b
| MemoryProtector prot(m_trampoline, m_trampolineSz, ProtFlag::R | ProtFlag::W | ProtFlag::X, *this, false); |
We are changing protection on memory we dont own.
This has unwanted side effects because it shares pages with other allocs, and may race with other threads or other detours that are done at the same time.
Solution to this is to replace any memory new/delete that has its protection mutated with VirtualAlloc/VirtualFree, given those require a full page at minimum, giving us absolute ownership.
At that point we dont need MemoryProtector there anymore, which also fixes the trampoline losing execute permission at the end of scope
And given these are full pages, they should be cached/reused if possible so not every trampoline requires its own page.