You need to enable JavaScript to run this app. Ubuntu 22.04下PTRACE_TRACEME/PTRACE_ATTACH报Operation not permitted求助
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 02:17:49