首页 > 解决方案 > 带有 PTRACE_O_EXITKILL 的 PTRACE_SETOPTIONS 不起作用:参数无效

问题描述

我正在学习如何使用ptrace并编写一些代码来测试它。但是,当我尝试使用PTRACE_SETOPTIONS请求设置PTRACE_O_EXITKILL选项以在跟踪进程终止时终止跟踪进程时,我遇到了问题。

作为参考,我正在使用适用于 Linux 的 Kali Linux 的 Windows 子系统,运行内核版本 4.4.0。

void child_code();
void parent_code(pid_t pid);

int main(const int argc, char *argv[])
{
    pid_t pid;
    switch (pid = fork())
    {
        case -1:
            perror("fork");
            break;
        case 0:
            child_code();
            break;
        default: //parent code
            parent_code(pid);
    }

    return 0;
}

void parent_code(pid_t pid)
{
    printf("Parent code\n");
    int status;
    if (wait(&status) == -1)
    {
        perror("parent wait one");
    }
    printf("Finished waiting\n");
    printf("PTRACE_O_EXITKILL is %x\n", PTRACE_O_EXITKILL); //0x100000
    printf("PTRACE_O_TRACEEXEC is %x\n", PTRACE_O_TRACEEXEC); //0x10
    printf("PTRACE_O_TRACEFORK is %x\n", PTRACE_O_TRACEFORK); //0x2
    printf("My PID is     %d\n", getpid());
    err_wrap(
        ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_EXITKILL), 
        0, "ptrace-set-options");
    //This is returning EINVAL error and prints "Invalid argument" and I don't know why.

}


void child_code() {
    printf("Child code\n");
    printf("Parent is:   %d\n", getppid());
    err_wrap(ptrace(PTRACE_TRACEME, 0,0,0), 0, "ptrace-traceme");
    err_wrap(raise(SIGSTOP), 0, "raise");

    unsigned int pleb = 0xffbbcde8;
    int x = 0;
    printf("Hello WoRld\n");

    printf("Pleb is:     %llx\n", pleb);
    printf("x is:        %d\n", x);
}

err_wrap 在这里定义

void err_wrap(const int ret, const int success, const char *msg)
{
    if (ret != success)
    {
        perror(msg);
    }
}

当我打电话PTRACE_SETOPTIONSPTRACE_O_EXITKILL,它说无效的论点,但我不知道为什么。当我尝试其他选项时PTRACE_O_TRACEEXEC,它工作正常。不过,我还没有尝试过所有其他选项。

我不明白为什么它不起作用。正如PTRACE_O_EXITKILL定义的那样,它不是我制作的宏/变量,我认为它ptrace会识别它,但由于某种原因它不会。我将它打印出来,它与我在ptrace源代码 ( 0x100000) 中看到的值相匹配。

任何帮助将不胜感激,谢谢。

标签: cptracesystems-programming

解决方案


推荐阅读