小编Wil*_*ill的帖子

如何使用sigsegv捕获内存读写?

我如何欺骗linux认为内存读/写成功?我正在编写一个C++库,以便所有读/写都被重定向并透明地处理给最终用户.无论何时写入或读取变量,库都需要捕获该请求并将其发送到硬件模拟,硬件模拟将处理来自那里的数据.

请注意,我的库依赖于以下平台:

Linux ubuntu 3.16.0-39-generic#53~14.04.1-Ubuntu SMP x86_64 GNU/Linux

gcc(Ubuntu 4.8.2-19ubuntu1)4.8.2

当前方法:捕获SIGSEGV并增加REG_RIP

我目前的方法涉及使用mmap()和关闭访问来获取内存区域mprotect().我有一个SIGSEGV处理程序来获取包含内存地址的信息,将读/写导出到别处,然后增加上下文REG_RIP.

void handle_sigsegv(int code, siginfo_t *info, void *ctx)
{
    void *addr = info->si_addr;
    ucontext_t *u = (ucontext_t *)ctx;
    int err = u->uc_mcontext.gregs[REG_ERR];
    bool is_write = (err & 0x2);
    // send data read/write to simulation...
    // then continue execution of program by incrementing RIP
    u->uc_mcontext.gregs[REG_RIP] += 6;
}
Run Code Online (Sandbox Code Playgroud)

这适用于非常简单的情况,例如:

int *num_ptr = (int *)nullptr;
*num_ptr = 10;                          // write segfault
Run Code Online (Sandbox Code Playgroud)

但是对于任何稍微复杂一点的东西,我收到一个SIGABRT:

30729非法指令(核心转储)./ $ target

在SIGSEGV处理程序中使用mprotect()

如果我不增加REG_RIP, …

c++ linux memory ubuntu kernel

8
推荐指数
1
解决办法
1090
查看次数

标签 统计

c++ ×1

kernel ×1

linux ×1

memory ×1

ubuntu ×1