Created
December 1, 2019 18:44
-
-
Save OatmealDome/8c252102b258fe1fdf96718dbd218f03 to your computer and use it in GitHub Desktop.
RW/RX remap test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mach/mach.h> | |
void remap(vm_address_t orig, vm_size_t size, bool execute) | |
{ | |
// remap source to target | |
vm_address_t target = 0; | |
uint64_t mask = 0; | |
bool anywhere = true; | |
vm_address_t source = orig; | |
vm_prot_t cur_protection = 0; | |
vm_prot_t max_protection = 0; | |
kern_return_t retval = | |
vm_remap(mach_task_self(), &target, size, mask, anywhere, mach_task_self(), source, false, | |
&cur_protection, &max_protection, VM_INHERIT_DEFAULT); | |
if (retval != KERN_SUCCESS) | |
{ | |
NSLog(@"remap fail: 0x%x", retval); | |
return; | |
} | |
if (execute) | |
{ | |
retval = vm_protect(mach_task_self(), target, size, false, VM_PROT_READ | VM_PROT_EXECUTE); | |
} | |
else | |
{ | |
retval = vm_protect(mach_task_self(), target, size, false, VM_PROT_READ | VM_PROT_WRITE); | |
} | |
if (retval != KERN_SUCCESS) | |
{ | |
NSLog(@"prot fail: 0x%x", retval); | |
return; | |
} | |
NSLog(@"OK, remap at 0x%lx", target); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
vm_size_t vm_size = 4096 * 4; | |
vm_address_t vm_mem; | |
kern_return_t retval = vm_allocate(mach_task_self(), &vm_mem, vm_size, VM_FLAGS_ANYWHERE); | |
if (retval != KERN_SUCCESS) | |
{ | |
NSLog(@"vm_allocate: 0x%x", retval); | |
return -1; | |
} | |
NSLog(@"vm_mem 0x%lx", vm_mem); | |
remap(vm_mem, vm_size, false); | |
remap(vm_mem, vm_size, true); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment