I am currently working on an ELF-injector and my approach is standard: find code cave (long enough sequence of 0's), rewrite it with the instructions I want to execute and then jump back to the start of the original program to execute it as it normally would.
To actually execute code in the code cave I tried two different approaches, both of which result in sigsegv.
First one was changing entry point to the start of the code cave. The second one was "stealing" some of the first instructions from the original code and write jump to my code cave there and then after executing my injected code I would first execute stolen instructions and then jump to the instruction after the last stolen one in the original program.
I am also changing the access flags for the section, in which code cave resides.
Here are some screenshots of debugging the program in gdb:
And here are the flags for the section the code cave is in:
[19] 0x555555556058->0x555555556160 at 0x00002058: .eh_frame ALLOC LOAD READONLY CODE HAS_CONTENTS
EDIT: This is the Valgrind output, so the problem is actually with the permissions. How can I allow the code inside this section be executed?