aboutsummaryrefslogtreecommitdiff
path: root/subhook_windows.c
diff options
context:
space:
mode:
authorZeex <zeex@rocketmail.com>2020-11-02 03:18:01 +0600
committerZeex <zeex@rocketmail.com>2020-11-02 03:19:54 +0600
commit314603bc42120c0c40571eaaa9c8f022992c70a6 (patch)
treec8d60604e78b31671c9267e2a09ab50068136b7f /subhook_windows.c
parent9cc118d25ea4775c03a62860aaa604fea56de0c6 (diff)
downloadsubhook-314603bc42120c0c40571eaaa9c8f022992c70a6.zip
subhook-314603bc42120c0c40571eaaa9c8f022992c70a6.tar.gz
subhook-314603bc42120c0c40571eaaa9c8f022992c70a6.tar.bz2
Fix address overflow in trampoline
Allocate memory for storing the trampoline code via mmap() with MAP_32BIT flag to make sure that it stays withing 2GB range. Also, add missing calls to subhook_free() in the C test program (C++ calls it implicitly already via destructor).
Diffstat (limited to 'subhook_windows.c')
-rw-r--r--subhook_windows.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/subhook_windows.c b/subhook_windows.c
index b1f0be6..b934651 100644
--- a/subhook_windows.c
+++ b/subhook_windows.c
@@ -27,12 +27,25 @@
#include <stddef.h>
#include <windows.h>
-void *subhook_unprotect(void *address, size_t size) {
- DWORD old;
+#define SUBHOOK_CODE_PROTECT_FLAGS PAGE_EXECUTE_READWRITE
- if (VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, &old) != 0) {
- return address;
- } else {
- return NULL;
- }
+int subhook_unprotect(void *address, size_t size) {
+ DWORD old_flags;
+ BOOL result = VirtualProtect(address,
+ size,
+ SUBHOOK_CODE_PROTECT_FLAGS,
+ &old_flags);
+ return !result;
+}
+
+void *subhook_alloc_code(size_t size) {
+ return VirtualAlloc(NULL,
+ size,
+ MEM_COMMIT | MEM_RESERVE,
+ SUBHOOK_CODE_PROTECT_FLAGS);
+}
+
+int subhok_free_code(void *address, size_t size) {
+ (void)size;
+ return !VirtualFree(address, 0, MEM_RELEASE);
}