Ptrace为execve捕获许多陷阱

时间:2017-10-29 23:47:50

标签: c system-calls ptrace

我正在使用ptrace来拦截系统调用。一切似乎工作得很好,除了我拦截了16个执行调用的事实(8个系统调用,8个系统调用)。

我看过没有它的工作示例,但我尝试使用标记PTRACE_O_TRACESYSGOOD

其他answers to ptrace problems表示我应该只看到一个前/后+一个信号,但他们没有使用PTRACE_O_TRACESYSGOOD

我的输出如下:

Intercepted rt_sigprocmask[14]
Syscall returned with value 0
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value -2
Intercepted execve[59]
Syscall returned with value 0
Tracer: Received signal: 5
Intercepted brk[12]
...

输出的其余部分与strace输出匹配。

每个"截获"并且" Syscall返回"对应一个waitid()电话。一个极简主义的示例代码来重现这个:

#include <sys/types.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/ptrace.h>
#include <sys/reg.h>     /* For constants ORIG_EAX, etc */
#include <string.h>
#include <sys/wait.h>
#include <sys/syscall.h>    /* For SYS_write, etc */
#include <unistd.h>
#include <stdio.h>

int main(){
  pid_t pid = fork();

  // Child.
  if(pid == 0){
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);

    // Wait for parent to be ready.
    raise(SIGSTOP);
    execlp("pwd", "pwd", NULL);
    return 0;
  }
  // Tracer.
  else{
    struct user_regs_struct regs;
    bool isPre = true;
    int status;
    // Wait for child to stop itself.
    waitpid(pid, &status, 0);
    ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACESYSGOOD);

    while(true){
      ptrace(PTRACE_SYSCALL, pid, 0, 0);
      pid  = waitpid(pid, &status, 0);

      // Check if tracee has exited.
      if (WIFEXITED(status)){
    return 0;
      }

      // This is a stop caused by a system call exit-pre/exit-post.
      if(WIFSTOPPED(status) && WSTOPSIG(status) == (SIGTRAP |0x80) ){
        ptrace(PTRACE_GETREGS, pid, NULL, &regs);

        if(isPre){
          printf("Intercepted syscall: %llu\n", regs.orig_rax);
          isPre = ! isPre;
        }else{
          printf("Done with system call!\n");
          isPre = ! isPre;
        }
      }else{
        printf("Tracer: Received signal: %d\n", WSTOPSIG(status));
      }
    }
  }

  return 0;
}

我担心我会误导execvePTRACE_O_TRACESYSGOOD。 我在Lubuntu 16.04上使用内核版本4.10.0-37-generic运行它。

edit:修复了系统调用的返回值。

1 个答案:

答案 0 :(得分:1)

没什么不对的。对execlp的一次调用通常会导致多次调用execve,每次调用(最后一次除外)都会返回ENOENT作为错误代码。

尽管事实上execlpexecvp经常记录在Unix和Linux手册的第2部分(系统调用)中,但它们仍然作为用户态函数实现。他们查看$PATH并在每个execve组件和可执行文件名的串联上调用$PATH,直到一个execve成功或者都失败。

以下来自musl的一些来源说明了发生了什么:

if (strchr(file, '/'))
    return execve(file, argv, envp);

if (!path) path = "/usr/local/bin:/bin:/usr/bin";
...
for(p=path; ; p=z) {
    char b[l+k+1];
    z = strchr(p, ':');
    if (!z) z = p+strlen(p);
    if (z-p >= l) {
        if (!*z++) break;
        continue;
    }
    memcpy(b, p, z-p);
    b[z-p] = '/';
    memcpy(b+(z-p)+(z>p), file, k+1);
    execve(b, argv, envp);
    if (errno == EACCES) seen_eacces = 1;
    else if (errno != ENOENT) return -1;
    if (!*z++) break;
}
if (seen_eacces) errno = EACCES;
return -1;