【发布时间】:2016-08-05 19:10:45
【问题描述】:
我正在开发一个包含 ptrace 的 Linux 应用程序,以观察由 fork() 系统调用创建的另一个进程。
严格来说:我想在分叉进程(智利进程或“tracee”)中实现故障注入。
如下图所示:
跟踪器通过使用 PTRACE_GETREGS 请求从被跟踪者获取 regs (struct_user_regs) 结构。之后,tracer修改tracee的EIP值(当内核切换到tracee时,命令执行会违反所谓的控制流错误CFE)。然后 PTRAC E_CONT 请求将发送给 tracee 以继续执行。
很遗憾,修改EPI的tracee后,由于(segmentation fault),tracee没有继续执行。 如何为被跟踪 EIP 提供另一个合适的值?
这里是代码
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include<sys/user.h>
#include<sys/reg.h>
#include<stdlib.h>
#include<stdio.h>
#include <asm/ptrace-abi.h>
int main()
{
pid_t child;
int status;
int sum=0;
struct user_regs_struct regs;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
printf("hello world 1\n");
printf("hello world 2\n");
raise (SIGINT); // just to move control to the tracer
printf("hello world 3\n");
printf("hello world 4\n");
printf("hello world 5\n");
exit(EXIT_SUCCESS);
}
else {
wait(NULL);
ptrace(PTRACE_GETREGS, child,NULL, ®s);
printf("\n EIP @ 0x %#lx\n",regs.eip);
//get the tracee EIP
long int new_eip=ptrace(PTRACE_PEEKTEXT, child,regs.eip,NULL);
//chabge EIP and poke it again
new_eip += ???; // make change that let to jump to another tracee instruction address (say to print hello world 5)
ptrace(PTRACE_POKETEXT, child,regs.eip,new_eip);
ptrace(PTRACE_CONT, child, NULL, NULL);
}
return 0;
}
有什么想法吗? 感谢您的所有帮助。
【问题讨论】:
-
如果您希望任何人知道您做错了什么,您需要展示您的代码。
-
@Barmar,代码已添加:)
-
使用gdb或Qt调试器之类的调试器,无法调试tracee。
标签: linux linux-kernel signals ptrace