2011-09-14 16 views
6

Sto seguendo il tutorial here, e modificato un poco per x86-64 (in pratica sostituire EAX a Rax, ecc) in modo che compila:Come si gioca con ptrace su x86-64?

#include <sys/ptrace.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <sys/user.h> 
#include <sys/reg.h> 
#include <unistd.h> 


int main() 
{ pid_t child; 
    long orig_eax; 
    child = fork(); 
    if(child == 0) { 
     ptrace(PTRACE_TRACEME, 0, NULL, NULL); 
     execl("/bin/ls", "ls", NULL); 
    } 
    else { 
     wait(NULL); 
     orig_eax = ptrace(PTRACE_PEEKUSER, 
          child, 4 * ORIG_RAX, 
          NULL); 
     printf("The child made a " 
       "system call %ld\n", orig_eax); 
     ptrace(PTRACE_CONT, child, NULL, NULL); 
    } 
    return 0; 
} 

Ma in realtà non funziona come previsto, si dice sempre :

The child made a system call -1 

Cosa c'è che non va nel codice?

risposta

5

ptrace restituisce -1 con errore EIO perché quello che stai cercando di leggere non è allineato correttamente. Tratto da ptrace manpage:

PTRACE_PEEKUSER 
      Reads a word at offset addr in the child's USER area, which 
      holds the registers and other information about the process (see 
      <sys/user.h>). The word is returned as the result of the 
      ptrace() call. Typically the offset must be word-aligned, 
      though this might vary by architecture. See NOTES. (data is 
      ignored.) 

Nel mio sistema a 64 bit, 4 * ORIG_RAX non è 8 byte-allineati. Prova con valori come 0 o 8 e dovrebbe funzionare.

+0

non capisco quello che vuoi dire ... –

5

In 64 bit = 8 * ORIG_RAX

8 = sizeof (long)