Как играть с ptrace на x86-64? - PullRequest
       31

Как играть с ptrace на x86-64?

7 голосов
/ 14 сентября 2011

Я следую учебному пособию здесь и немного изменил для x86-64 (в основном заменим eax на rax и т. Д.), Чтобы он компилировался:

#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;
}

Но этона самом деле не работает, как ожидалось, всегда говорит:

The child made a system call -1

Что не так в коде?

Ответы [ 2 ]

6 голосов
/ 11 мая 2013

В 64 битах = 8 * ORIG_RAX

8 = sizeof (long)

6 голосов
/ 14 сентября 2011

ptrace возвращает -1 с ошибкой EIO, потому что то, что вы пытаетесь прочитать, выровнено неправильно.Взято из 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.)

В моей 64-битной системе 4 * ORIG_RAX не выровнено по 8 байтов.Попробуйте со значениями, такими как 0 или 8, и это должно работать.

...