diff options
author | Zeex <zeex@rocketmail.com> | 2023-02-10 00:36:53 +0600 |
---|---|---|
committer | Zeex <zeex@rocketmail.com> | 2023-02-10 15:54:07 +0600 |
commit | 23bf37f44e9bf8058483c8e96afd203eac0b9a6f (patch) | |
tree | 5531a8df2ccac168cec3443f4440bf500de6e06f /subhook_unix.c | |
parent | 6cff28a750f89ad4c4af187bfacaef0d16f2daab (diff) | |
download | subhook-23bf37f44e9bf8058483c8e96afd203eac0b9a6f.zip subhook-23bf37f44e9bf8058483c8e96afd203eac0b9a6f.tar.gz subhook-23bf37f44e9bf8058483c8e96afd203eac0b9a6f.tar.bz2 |
Fix compile error and clean up in *nix-specific code
Diffstat (limited to 'subhook_unix.c')
-rw-r--r-- | subhook_unix.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/subhook_unix.c b/subhook_unix.c index 697974d..a779b3d 100644 --- a/subhook_unix.c +++ b/subhook_unix.c @@ -29,42 +29,47 @@ #include <sys/mman.h> #include "subhook.h" #ifdef SUBHOOK_APPLE -#include <mach/mach.h> + #include <mach/mach.h> #endif - #define SUBHOOK_CODE_PROTECT_FLAGS (PROT_READ | PROT_WRITE | PROT_EXEC) int subhook_unprotect(void *address, size_t size) { - long pagesize; + long page_size; + void *aligned_address; + void *end; + size_t new_size; - pagesize = sysconf(_SC_PAGESIZE); - void *aligned_address = (void *)((long)address & ~(pagesize - 1)); + page_size = sysconf(_SC_PAGESIZE); + aligned_address = (void *)((long)address & ~(page_size - 1)); - // Fix up the length - since we rounded the start address off, if a jump is right at the - // end of a page we could need to unprotect both. - void *end = address + size; - size_t new_size = end - aligned_address; + /* + * Fix up the length - since we rounded the start address off, if a jump is + * right at the end of a page we could need to unprotect both. + */ + end = address + size; + new_size = end - aligned_address; int error = mprotect(aligned_address, new_size, SUBHOOK_CODE_PROTECT_FLAGS); #ifdef SUBHOOK_APPLE - if (-1 == error) - { - /* If mprotect fails, try to use VM_PROT_COPY with vm_protect. */ - kern_return_t kret = vm_protect(mach_task_self(), (unsigned long)aligned_address, new_size, 0, SUBHOOK_CODE_PROTECT_FLAGS | VM_PROT_COPY); - if (kret != KERN_SUCCESS) - { - error = -1; - } - error = 0; - } + if (error != 0) { + /* If mprotect fails, try to use VM_PROT_COPY with vm_protect. */ + kern_return_t kret = vm_protect(mach_task_self(), + (unsigned long)aligned_address, + new_size, + 0, + SUBHOOK_CODE_PROTECT_FLAGS | VM_PROT_COPY); + error = kret == KERN_SUCCESS ? 0 : -1; + } #endif return error; } -void *subhook_alloc_code(size_t size) { +void *subhook_alloc_code(void *target_address, size_t size) { void *address; + (void)target_address; + address = mmap(NULL, size, SUBHOOK_CODE_PROTECT_FLAGS, |