As part of a project I'm working on, I want to allocate some memory, write opcodes into that memory and then call it. By then, the code will jump into my allocated memory, run the opcodes and return back to the previous location using the opcode ret.
My code run on Linux and doesn't need to support Windows.
I tried to use mprotect with the flags PROT_READ | PROT_WRITE | PROT_EXEC but I get "Permission Denied" error.
I need both write and execute permission on this memory.
How can I achieve what I'm trying? Why do I get "Permission Denied"?
The allocated memory is allocated using aligned_alloc so it will be aligned to 4KB (page size).
Looks like PaX/GrSecurity work to me:
Try to use
paxctlutility to disable memory protection mechanism for your program binary.