aboutsummaryrefslogtreecommitdiff
path: root/subhook_unix.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_unix.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_unix.c')
-rw-r--r--subhook_unix.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/subhook_unix.c b/subhook_unix.c
index 31f927e..3bde083 100644
--- a/subhook_unix.c
+++ b/subhook_unix.c
@@ -28,15 +28,26 @@
#include <unistd.h>
#include <sys/mman.h>
-void *subhook_unprotect(void *address, size_t size) {
+#define SUBHOOK_CODE_PROTECT_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC)
+
+int subhook_unprotect(void *address, size_t size) {
long pagesize;
pagesize = sysconf(_SC_PAGESIZE);
address = (void *)((long)address & ~(pagesize - 1));
- if (mprotect(address, size, PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
- return address;
- } else {
- return NULL;
- }
+ return mprotect(address, size, SUBHOOK_CODE_PROTECT_FLAGS);
+}
+
+void *subhook_alloc_code(size_t size) {
+ return mmap(NULL,
+ size,
+ SUBHOOK_CODE_PROTECT_FLAGS,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT,
+ -1,
+ 0);
+}
+
+int subhok_free_code(void *address, size_t size) {
+ return munmap(address, size);
}