我正在尝试使用环境变量将预加载库加载到ptrace子进程。但是以某种方式在创建子进程时出现错误:
int main(int argc, char **argv)
{
char *env[] = {"LD_PRELOAD=/<path-to-the-preload-library>/preload.so"};
pid_t pid = fork();
switch (pid) {
case -1: /* error */
log_fatal("%s. pid -1", strerror(errno));
break;
case 0: /* child, executing the tracee */
ptrace(PTRACE_TRACEME, 0, 0, 0);
execve(argv[1], argv + 1, env); // Fail to launch ptrace child!
//execvp(argv[1], argv + 1); // It works fine!
log_fatal("%s. child", strerror(errno));
}
waitpid(pid, 0, 0); // sync with PTRACE_TRACEME
ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_EXITKILL);
简单的预加载库代码:
$ cat preload.c
#include <stdio.h>
static void _init() __attribute__((constructor));
void _init() {
printf("I'm a constructor\n");
}
知道为什么会失败吗?
如果您告诉我们错误消息是什么,那就太好了,但是我想我可以猜测:“地址错误”?
就像向量一样,env
传递给的向量execve
需要使用NULL指针终止argv
。所以你要
char *env[] = {"LD_PRELOAD=/<path-to-the-preload-library>/preload.so", NULL};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句