aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeex <zeex@rocketmail.com>2021-12-19 14:39:14 +0600
committerGitHub <noreply@github.com>2021-12-19 14:39:14 +0600
commit8de6fdbaca68eb5f0adfbb560498481d511bdaee (patch)
tree4574b7aae5e2dda9e9f9e8b76ca219b59a12690e
parent86c102f28fb7f3b396f6879bd6ee8f8bb0199c9e (diff)
parent9d1ba393ae200bb7a4286997d4f6bddc014616b0 (diff)
downloadsubhook-8de6fdbaca68eb5f0adfbb560498481d511bdaee.zip
subhook-8de6fdbaca68eb5f0adfbb560498481d511bdaee.tar.gz
subhook-8de6fdbaca68eb5f0adfbb560498481d511bdaee.tar.bz2
Merge pull request #64 from ZNixian/page-boundary-unprotect
unix: Fix unprotecting memory across a page boundary
-rw-r--r--subhook_unix.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/subhook_unix.c b/subhook_unix.c
index 4d9c03a..697974d 100644
--- a/subhook_unix.c
+++ b/subhook_unix.c
@@ -39,14 +39,19 @@ int subhook_unprotect(void *address, size_t size) {
long pagesize;
pagesize = sysconf(_SC_PAGESIZE);
- address = (void *)((long)address & ~(pagesize - 1));
+ void *aligned_address = (void *)((long)address & ~(pagesize - 1));
- int error = mprotect(address, size, SUBHOOK_CODE_PROTECT_FLAGS);
+ // 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;
+
+ 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)address, size, 0, SUBHOOK_CODE_PROTECT_FLAGS | VM_PROT_COPY);
+ 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;