Hello I´ve got a Problem
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#define SYS_PTRACE 101
long my_ptrace(long pid)
{
long ret;
__asm__ volatile(
"mov $0x10, %%rdi\n"
"mov %0, %%rsi\n"
"xor %%rdx,%%rdx\n"
"xor %%r10, %%r10\n"
"mov $0x65, %%rax\n"
"syscall" : :"g"(pid));
__asm__ volatile("mov %%rax, %0" : "=r"(ret));
return ret;
}
int main()
{
long a = getpid();
my_ptrace(a);
printf("Hello World\n %d", a);
return 0;
}
first I move 0x10 or 16 into rdi which is the number for PTRACE_ATTACH according to https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/jb-dev/sysroot/usr/include/sys/ptrace.h
followed by moving the PID into rsi (http://man7.org/linux/man-pages/man2/ptrace.2.html) the other values are not used by this call.
But if I compile and execute the Programm strace outputs
[...]
arch_prctl(ARCH_SET_FS, 0x7f4eac1fa500) = 0
mprotect(0x7f4eac1ef000, 16384, PROT_READ) = 0
mprotect(0x55fb014bd000, 4096, PROT_READ) = 0
mprotect(0x7f4eac240000, 4096, PROT_READ) = 0
munmap(0x7f4eac1fb000, 113090) = 0
getpid() = 4328
ptrace(PTRACE_ATTACH, 4328) = -1 EPERM (Die Operation ist nicht erlaubt)
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
brk(NULL) = 0x55fb02077000
brk(0x55fb02098000) = 0x55fb02098000
write(1, "Hello World\n", 12Hello World
) = 12
write(1, " 4328", 5 4328) = 5
exit_group(0) = ?
+++ exited with 0 ++
Did I something wrong or is it not possible that a process traces itself?
Thank you for you help.
A Process is not able to attach itself but a Parent can attach his child. Forking was the solution. Thank you for your answers. Now
strace -p $PID(of the child)
returns an error
If you are using
strace -f ./Programm
strace will attach to the child before the parent is able to do it and the syscall will fail.
Here I traced the parent
[....]
rk(0x562f67a93000) = 0x562f67a93000
write(1, "My process ID : 1801\n", 21My process ID : 1801
) = 21
clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f28dc5547d0) = 1802
ptrace(PTRACE_ATTACH, 1802) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_TRAPPED, si_pid=1802, si_uid=1000, si_status=SIGSTOP, si_utime=0, si_stime=0} ---
nanosleep({tv_sec=50, tv_nsec=0}, 0x7ffc95baaf30) = 0