I'm trying to implement CreateRemoteThread in linux using ptrace. The main problem is that the mmap and clone system call is not signal-safe, so it's dangerous to inject them directly.
But what if I can make sure there is not pending mmap and clone system calls of that process in kernel?
For example, first I attach all the threads of target process and check if the previous 2 bytes of the rip is 0x0f 0x05(syscall). If it is, then get the syscall number from orig_rax reg and check if it's 9(mmap) or 56(clone). If so, I set the address in rip to 0xcc(int3) and continue that thread. When I receive the SIGTRAP from those threads, those system calls should be finished and there should be no not pending mmap and clone system calls in that process.
Is it safe to use mmap and clone system call now?