diff --git a/runtime/src/code_hooks.h b/runtime/src/code_hooks.h index c87f1b2b1..636d9131e 100644 --- a/runtime/src/code_hooks.h +++ b/runtime/src/code_hooks.h @@ -24,12 +24,14 @@ using RemapFn = uint32_t (*)(uint32_t); // Executable memory within +-2 GiB of `close_to`, so a rel32 branch from the image reaches it. nullptr if none is free. inline uint8_t* alloc_near(const void* close_to, std::size_t size) { if (!close_to || size == 0) return nullptr; - SYSTEM_INFO si; + SYSTEM_INFO si{}; GetSystemInfo(&si); - const uintptr_t gran = si.dwAllocationGranularity; - const uintptr_t center = reinterpret_cast(close_to) & ~(gran - 1); + const uintptr_t gran = si.dwAllocationGranularity ? static_cast(si.dwAllocationGranularity) + : uintptr_t{0x10000}; + const uintptr_t addr = reinterpret_cast(close_to); + const uintptr_t center = addr - (addr % gran); const uintptr_t span = 0x70000000; // stay well inside the rel32 range from anywhere in a large image - for (uintptr_t delta = gran; delta < span; delta += gran) { + for (uintptr_t delta = gran; delta < span;) { for (int side = 0; side < 2; ++side) { if (side == 0 && center < delta) continue; if (side == 1 && UINTPTR_MAX - center < delta) continue; @@ -38,6 +40,8 @@ inline uint8_t* alloc_near(const void* close_to, std::size_t size) { PAGE_EXECUTE_READWRITE)) return static_cast(p); } + if (span - delta <= gran) break; + delta += gran; } return nullptr; } @@ -53,6 +57,12 @@ constexpr DWORD kThunkFunctionOffset = 0x240; // RUNTIME_FUNCTION, makes the page executable and registers the function table. Frees the page and returns null on // failure. inline uint8_t* finish_thunk(uint8_t* t, std::size_t code_size, std::initializer_list unwind) { + if (!t) return nullptr; + if (code_size == 0 || code_size > kThunkUnwindOffset || + unwind.size() > kThunkFunctionOffset - kThunkUnwindOffset) { + VirtualFree(t, 0, MEM_RELEASE); + return nullptr; + } std::size_t i = 0; for (uint8_t b : unwind) t[kThunkUnwindOffset + i++] = b; auto* function = reinterpret_cast(t + kThunkFunctionOffset); @@ -151,9 +161,17 @@ inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, co const uintptr_t next_addr = reinterpret_cast(next); const uintptr_t dst_addr = next_addr + static_cast(static_cast(rel)); if (dst_addr != reinterpret_cast(target)) continue; - const intptr_t to = static_cast(reinterpret_cast(thunk) - next_addr); - if (to < INT32_MIN || to > INT32_MAX) continue; - const int32_t rel2 = static_cast(to); + const uintptr_t thunk_addr = reinterpret_cast(thunk); + int32_t rel2 = 0; + if (thunk_addr >= next_addr) { + const uintptr_t diff = thunk_addr - next_addr; + if (diff > static_cast(INT32_MAX)) continue; + rel2 = static_cast(diff); + } else { + const uintptr_t diff = next_addr - thunk_addr; + if (diff > static_cast(INT32_MAX) + 1u) continue; + rel2 = static_cast(-static_cast(diff)); + } std::memcpy(begin + i + 1, &rel2, 4); ++n; i += 4; @@ -179,6 +197,7 @@ inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeT if (dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew < static_cast(sizeof(IMAGE_DOS_HEADER))) return r.error = "not a mapped PE image", r; const uint64_t lfanew = static_cast(dos->e_lfanew); + if (lfanew > UINT32_MAX - sizeof(IMAGE_NT_HEADERS64)) return r.error = "not a mapped PE image", r; const auto* nt = reinterpret_cast(base + lfanew); if (nt->Signature != IMAGE_NT_SIGNATURE || nt->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64) || @@ -228,6 +247,7 @@ inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeT VirtualProtect(begin, size, old, &ignored); FlushInstructionCache(GetCurrentProcess(), begin, size); } + if (r.redirected == 0) free_thunk(thunk); r.ok = true; return r; } diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp index 39efb9be1..2b76f8fa2 100644 --- a/runtime/test/code_hooks_test.cpp +++ b/runtime/test/code_hooks_test.cpp @@ -367,6 +367,33 @@ int main() { free_thunk(unwind_remap); } free_thunk(nullptr); + check(finish_thunk(nullptr, 16, {0x01, 0, 0, 0}) == nullptr, "finish_thunk refuses null thunk buffer"); + + // Zero direct callers: redirect_image_calls succeeds and frees the unused thunk and its function table. + uint8_t* img_zero = alloc_near(reinterpret_cast(&remap_lucia), 0x2000); + check(img_zero != nullptr, "synthetic PE64 image for zero-caller check allocated"); + if (img_zero) { + std::memset(img_zero, 0, 0x2000); + IMAGE_SECTION_HEADER sec_z{}; + std::memcpy(sec_z.Name, ".text", 5); + sec_z.Misc.VirtualSize = 0x100; + sec_z.VirtualAddress = 0x1000; + sec_z.SizeOfRawData = 0x100; + sec_z.Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE; + init_pe64(img_zero, 0x2000, {sec_z}); + put(img_zero + 0x1000, {0x89, 0xC8, 0xC3}); // target with no direct E8/E9 callers + uint8_t* unused_thunk = nullptr; + const HookResult rz = redirect_image_calls(img_zero, 0x1000, [&](uint8_t* target) { + unused_thunk = make_remap_thunk(target, 0, &remap_lucia, target); + return unused_thunk; + }); + DWORD64 base = 0; + check(rz.ok && rz.redirected == 0 && unused_thunk != nullptr, + "redirect_image_calls succeeds with zero redirected branches"); + check(unused_thunk && RtlLookupFunctionEntry(reinterpret_cast(unused_thunk) + 1, &base, nullptr) == nullptr, + "unused thunk and its function table are freed when zero branches were redirected"); + VirtualFree(img_zero, 0, MEM_RELEASE); + } std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); return g_failures == 0 ? 0 : 1;