upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/code_hooks.h
diff options
context:
space:
mode:
authorIlyassRS <ilyassroussi2000@gmail.com>2026-11-25 20:09:18 +0100
committerIlyassRS <ilyassroussi2000@gmail.com>2026-11-25 20:09:18 +0100
commit9fc1862fadbd2907fdcecb993eb98efd790d7c5e (patch)
tree72049267bdde81e8939f313b7b91031ba833856d /runtime/src/code_hooks.h
parentc37374180b661ac6107d06b929b5a4953328c966 (diff)
runtime: free unused thunk on zero redirects and harden code_hooks bounds
Diffstat (limited to 'runtime/src/code_hooks.h')
-rw-r--r--runtime/src/code_hooks.h34
1 files changed, 27 insertions, 7 deletions
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);
24// Executable memory within +-2 GiB of `close_to`, so a rel32 branch from the image reaches it. nullptr if none is free. 24// Executable memory within +-2 GiB of `close_to`, so a rel32 branch from the image reaches it. nullptr if none is free.
25inline uint8_t* alloc_near(const void* close_to, std::size_t size) { 25inline uint8_t* alloc_near(const void* close_to, std::size_t size) {
26 if (!close_to || size == 0) return nullptr; 26 if (!close_to || size == 0) return nullptr;
27 SYSTEM_INFO si; 27 SYSTEM_INFO si{};
28 GetSystemInfo(&si); 28 GetSystemInfo(&si);
29 const uintptr_t gran = si.dwAllocationGranularity; 29 const uintptr_t gran = si.dwAllocationGranularity ? static_cast<uintptr_t>(si.dwAllocationGranularity)
30 const uintptr_t center = reinterpret_cast<uintptr_t>(close_to) & ~(gran - 1); 30 : uintptr_t{0x10000};
31 const uintptr_t addr = reinterpret_cast<uintptr_t>(close_to);
32 const uintptr_t center = addr - (addr % gran);
31 const uintptr_t span = 0x70000000; // stay well inside the rel32 range from anywhere in a large image 33 const uintptr_t span = 0x70000000; // stay well inside the rel32 range from anywhere in a large image
32 for (uintptr_t delta = gran; delta < span; delta += gran) { 34 for (uintptr_t delta = gran; delta < span;) {
33 for (int side = 0; side < 2; ++side) { 35 for (int side = 0; side < 2; ++side) {
34 if (side == 0 && center < delta) continue; 36 if (side == 0 && center < delta) continue;
35 if (side == 1 && UINTPTR_MAX - center < delta) continue; 37 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) {
38 PAGE_EXECUTE_READWRITE)) 40 PAGE_EXECUTE_READWRITE))
39 return static_cast<uint8_t*>(p); 41 return static_cast<uint8_t*>(p);
40 } 42 }
43 if (span - delta <= gran) break;
44 delta += gran;
41 } 45 }
42 return nullptr; 46 return nullptr;
43} 47}
@@ -53,6 +57,12 @@ constexpr DWORD kThunkFunctionOffset = 0x240;
53// RUNTIME_FUNCTION, makes the page executable and registers the function table. Frees the page and returns null on 57// RUNTIME_FUNCTION, makes the page executable and registers the function table. Frees the page and returns null on
54// failure. 58// failure.
55inline uint8_t* finish_thunk(uint8_t* t, std::size_t code_size, std::initializer_list<uint8_t> unwind) { 59inline uint8_t* finish_thunk(uint8_t* t, std::size_t code_size, std::initializer_list<uint8_t> unwind) {
60 if (!t) return nullptr;
61 if (code_size == 0 || code_size > kThunkUnwindOffset ||
62 unwind.size() > kThunkFunctionOffset - kThunkUnwindOffset) {
63 VirtualFree(t, 0, MEM_RELEASE);
64 return nullptr;
65 }
56 std::size_t i = 0; 66 std::size_t i = 0;
57 for (uint8_t b : unwind) t[kThunkUnwindOffset + i++] = b; 67 for (uint8_t b : unwind) t[kThunkUnwindOffset + i++] = b;
58 auto* function = reinterpret_cast<RUNTIME_FUNCTION*>(t + kThunkFunctionOffset); 68 auto* function = reinterpret_cast<RUNTIME_FUNCTION*>(t + kThunkFunctionOffset);
@@ -151,9 +161,17 @@ inline std::size_t redirect_direct_branches(uint8_t* begin, std::size_t size, co
151 const uintptr_t next_addr = reinterpret_cast<uintptr_t>(next); 161 const uintptr_t next_addr = reinterpret_cast<uintptr_t>(next);
152 const uintptr_t dst_addr = next_addr + static_cast<uintptr_t>(static_cast<intptr_t>(rel)); 162 const uintptr_t dst_addr = next_addr + static_cast<uintptr_t>(static_cast<intptr_t>(rel));
153 if (dst_addr != reinterpret_cast<uintptr_t>(target)) continue; 163 if (dst_addr != reinterpret_cast<uintptr_t>(target)) continue;
154 const intptr_t to = static_cast<intptr_t>(reinterpret_cast<uintptr_t>(thunk) - next_addr); 164 const uintptr_t thunk_addr = reinterpret_cast<uintptr_t>(thunk);
155 if (to < INT32_MIN || to > INT32_MAX) continue; 165 int32_t rel2 = 0;
156 const int32_t rel2 = static_cast<int32_t>(to); 166 if (thunk_addr >= next_addr) {
167 const uintptr_t diff = thunk_addr - next_addr;
168 if (diff > static_cast<uintptr_t>(INT32_MAX)) continue;
169 rel2 = static_cast<int32_t>(diff);
170 } else {
171 const uintptr_t diff = next_addr - thunk_addr;
172 if (diff > static_cast<uintptr_t>(INT32_MAX) + 1u) continue;
173 rel2 = static_cast<int32_t>(-static_cast<int64_t>(diff));
174 }
157 std::memcpy(begin + i + 1, &rel2, 4); 175 std::memcpy(begin + i + 1, &rel2, 4);
158 ++n; 176 ++n;
159 i += 4; 177 i += 4;
@@ -179,6 +197,7 @@ inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeT
179 if (dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew < static_cast<LONG>(sizeof(IMAGE_DOS_HEADER))) 197 if (dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew < static_cast<LONG>(sizeof(IMAGE_DOS_HEADER)))
180 return r.error = "not a mapped PE image", r; 198 return r.error = "not a mapped PE image", r;
181 const uint64_t lfanew = static_cast<uint64_t>(dos->e_lfanew); 199 const uint64_t lfanew = static_cast<uint64_t>(dos->e_lfanew);
200 if (lfanew > UINT32_MAX - sizeof(IMAGE_NT_HEADERS64)) return r.error = "not a mapped PE image", r;
182 const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(base + lfanew); 201 const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(base + lfanew);
183 if (nt->Signature != IMAGE_NT_SIGNATURE || 202 if (nt->Signature != IMAGE_NT_SIGNATURE ||
184 nt->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64) || 203 nt->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64) ||
@@ -228,6 +247,7 @@ inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeT
228 VirtualProtect(begin, size, old, &ignored); 247 VirtualProtect(begin, size, old, &ignored);
229 FlushInstructionCache(GetCurrentProcess(), begin, size); 248 FlushInstructionCache(GetCurrentProcess(), begin, size);
230 } 249 }
250 if (r.redirected == 0) free_thunk(thunk);
231 r.ok = true; 251 r.ok = true;
232 return r; 252 return r;
233} 253}