c - 将 mprotect 系统调用注入(inject)跟踪进程失败并出现 EFAULT

标签 c linux ptrace mprotect

我正在将 mprotect 调用注入(inject)跟踪进程:

static int inject_mprotect(pid_t child, void *addr, size_t len, int prot)
{
    // Machine code:
    //  int $0x80       (system call)
    //  int3            (trap)
    char code[] = {0xcd,0x80,0xcc,0};
    char orig_code[3];
    struct user_regs_struct regs;
    struct user_regs_struct orig_regs;

    // Take a copy of current state
    __check_ptrace(PTRACE_GETREGS, child, NULL, &orig_regs);
    getdata(child, INSTRUCTION_POINTER(regs), orig_code, 3);

    // Inject the code, update registers
    putdata(child, INSTRUCTION_POINTER(regs), code, 3);
    __check_ptrace(PTRACE_GETREGS, child, NULL, &regs);
    XAX_REGISTER(regs) = MPROTECT_SYSCALL;
    MPROTECT_ARG_START(regs) = (unsigned long)addr;
    MPROTECT_ARG_LEN(regs) = len;
    MPROTECT_ARG_PROT(regs) = prot;   
    __check_ptrace(PTRACE_SETREGS, child, NULL, &regs);

    // Snip

但是调用失败,返回 -14 (EFAULT)。我查看了 mprotect 源代码(内核 3.13),但不明白为什么我的系统调用会返回这个。

如果我跟踪注入(inject)的调用并打印出寄存器,我会看到以下内容:

SIGTRAP: eip: 0x34646ef8d4, syscall 10, rc = -38
PARENT 10 MPROTECT(start: 0x00007f45b9611000, len: 4096, prot: 0)
EIP: 0x00000034646ef8d4 AX: 0xffffffffffffffda  BX: 0x0000000000000005  CX: 0xffffffffffffffff
 DX: 0x0000000000000000 DI: 0x00007f45b9611000  BP: 0x00007fffcb93bc20  SI: 0x0000000000001000
 R8: 0x0000000000000000 R9: 0x0000000000000000  R10: 0x0000000000000000
SIGTRAP: eip: 0x34646ef8d4, syscall 10, rc = -14 Bad address (trap after system call exit)

为了验证系统调用格式,我向 child 添加了一个mprotect 调用并转储了它的参数和寄存器:

SIGTRAP: eip: 0x34646ef927, syscall 10, rc = -38
CHILD  10 MPROTECT(start: 0x00007f45b9611000, len: 4096, prot: 0)
EIP: 0x00000034646ef927 AX: 0xffffffffffffffda  BX: 0x0000000000000005  CX: 0xffffffffffffffff
 DX: 0x0000000000000000 DI: 0x00007f45b9611000  BP: 0x00007fffcb93bc20  SI: 0x0000000000001000
 R8: 0x000000000000004e R9: 0x746f72706d206c6c  R10: 0x00007fffcb93b9a0
SIGTRAP (child return): eip: 0x34646ef927, syscall 10, rc = 0

子进程调用成功。因此,假设我正在使用相同的参数进行相同的系统调用 (10),为什么注入(inject)的调用会因 EFAULT 而失败,而来自 child 的调用是成功的?

调用之间的唯一区别是 regs.r8regs.r9regs.r10 中的一些垃圾,但是基于 this table of system calls on X86_64 I不要相信这些寄存器的内容会影响系统调用。

最佳答案

问题与this question有关: i386 和 x86_64 对系统调用使用不同的调用约定。您的示例代码使用 int 0x80,i386 变体,但 syscall_number = 10mprotect 的 64 位系统调用号。根据 this list,在 32 位环境中,系统调用 10 对应于 unlink ,它可以返回EFAULT(错误地址)。

在 64 位平台上,以一致的方式使用 32 位或 64 位变体可以解决问题。

关于c - 将 mprotect 系统调用注入(inject)跟踪进程失败并出现 EFAULT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969524/

相关文章:

c - 进程 A 中的 waitpid() 在 B coredump 之前无法在进程 B 中的 pthread_create() 创建的线程中捕获 SIGTRAP

java - 大型数组中元素的并行求和

c++ - 如何在Linux上构建wxWidgets示例

linux - PTrace: linux/user.h: 没有那个文件或目录

linux - 在 Bash 中将一个目录中的所有文件连接成一个文件,按修改日期排序

linux - 使用 SSH 更新 WordPress (Centos)

android - android 支持 PTRACE_SINGLESTEP 吗?

堆栈大小可以动态更改吗?如何更改?

c - 二维数组在循环内工作但在循环外不工作

c - 释放二维动态数组时堆损坏