Last active
February 11, 2019 00:27
-
-
Save dmiller423/1bac2ba090402417cfedda9122da25a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
int mprotect(void *addr, void *base, size_t len, int prot) | |
{ | |
Result rc=0; | |
static bool isMapped=false; | |
Handle hProc = envGetOwnProcessHandle(); | |
if (isMapped) { | |
rc=svcUnmapProcessCodeMemory(hProc, (u64)addr, (u64)base, len); | |
if (R_FAILED(rc)) { | |
printf(" Error, svcUnmapProcessCodeMemory() failed w. rc: %X\n", rc); | |
return (int)rc; | |
} | |
isMapped=false; | |
} | |
rc=svcMapProcessCodeMemory(hProc, (u64)addr, (u64)base, len); | |
if (R_FAILED(rc)) { | |
printf(" Error, svcMapProcessCodeMemory() failed w. rc: %X\n", rc); | |
return (int)rc; | |
} | |
else isMapped=true; | |
rc = svcSetProcessMemoryPermission(hProc, (u64)addr, len, prot); | |
if (R_FAILED(rc)) { | |
printf(" Error, svcSetProcessMemoryPermission() failed w. rc: %X\n", rc); | |
return (int)rc; | |
} | |
return 0; | |
} | |
void test() | |
{ | |
u64 vmBase=(u64)virtmemReserve(PAGE_SIZE); | |
u8 *ptext = (u8*)memalign(PAGE_SIZE, PAGE_SIZE); // single page , aligned | |
int res = 0; | |
if(0 != (res = mprotect((void*)vmBase, ptext, PAGE_SIZE, Perm_Rw))) | |
printf("mprotect Rw failed! res: %X\n",(u64)res); | |
memcpy((void*)vmBase, testcode, sizeof(testcode)); | |
if(0 != (res = mprotect((void*)vmBase, ptext, PAGE_SIZE, Perm_Rx))) | |
printf("mprotect Rx failed! res: %X\n",(u64)res); | |
else { | |
funcptr = (fp_t)vmBase; | |
printf("Test code returned: 0x%lx\n", funcptr());//This should return 0x7 (see above). | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment