Ubuntu 22.04下PTRACE_TRACEME/PTRACE_ATTACH报Operation not permitted求助
PTRACE_TRACEME/PTRACE_ATTACH 提示"Operation not permitted"问题
我尝试使用PTRACE_TRACEME跟踪子进程,相关代码如下:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ptrace.h> #include <sys/wait.h> #include <sys/user.h> int main(int argc, char *argv[]) { pid_t child_pid; int status; struct user_regs_struct regs; if (argc < 2) { fprintf(stderr, "Usage: %s program [arguments]\n", argv[0]); exit(1); } child_pid = fork(); if (child_pid == 0) { /* Child process */ /* Allow parent to trace this process */ if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) { perror("ptrace3"); exit(1); } /* Replace child process with specified program */ execvp(argv[1], &argv[1]); /* If execvp returns, there was an error */ perror("execvp"); exit(1); } else if (child_pid > 0) { /* Parent process */ /* Attach to child process */ if (ptrace(PTRACE_ATTACH, child_pid, NULL, NULL) == -1) { perror("ptrace5"); exit(1); } /* Wait for child process to start */ waitpid(child_pid, &status, 0); /* Continue running child process */ if (ptrace(PTRACE_CONT, child_pid, NULL, NULL) == -1) { perror("ptrace4"); exit(1); } /* Wait for child process to stop */ waitpid(child_pid, &status, 0); /* Do something with registers */ /* Detach from child process */ if (ptrace(PTRACE_DETACH, child_pid, NULL, NULL) == -1) { perror("ptrace"); exit(1); } } else { /* Fork failed */ perror("fork"); exit(1); } return 0; }
执行后PTRACE_TRACEME报错:
ptrace3: Operation not permitted
若调整父进程中PTRACE_ATTACH与waitpid(child_pid, &status, 0);的顺序为:
/* Wait for child process to start */ waitpid(child_pid, &status, 0); /* Attach to child process */ if (ptrace(PTRACE_ATTACH, child_pid, NULL, NULL) == -1) { perror("ptrace5"); exit(1); }
则PTRACE_ATTACH报错:
ptrace5: Operation not permitted
我了解这与安全特性相关,已尝试将ptrace_scope设为0并确认SELinux已禁用,未使用Docker,系统为Ubuntu 22.04,恳请帮助解决该问题。
内容的提问来源于stack exchange,提问作者Aviel15
相关产品推荐
相关产品推荐

