I am trying to do a simple thing (just for learning), I wish to intercept clock_gettime on 64 bit linux, read the output and modify it so to return a flase date/time to the tracee (/bin/date).
What I do is:
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
if(regs.orig_rax==228){ // this is the clock_gettime syscall number in 64 bit linux
unsigned long p1=ptrace(PTRACE_PEEKDATA, pid, regs.rcx, NULL); // rcx is ARG1
printf("ARG1: 0x%lx\n",p1);
}
Now if I understood correctly (clearly not) regs.rcx should point to a timespec structure, so I should read the first long int of that structure which is the time in seconds (unixtime). But I read 0.
Also, the printf is invoked twice, once entering the syscall and the second time exiting it. So ok it's normal is 0 when entering but it should not be whene exiting. Infact strace shows it correctly:
strace 2>&1 date|grep CLOCK
clock_gettime(CLOCK_REALTIME, {tv_sec=1583960872, tv_nsec=403163000}) = 0
How can I do the same?