gpt4 book ai didi

c - 如何在 ptracer 中处理 SIGSTOP/SIGCONT?

转载 作者:行者123 更新时间:2023-12-02 09:38:28 26 4
gpt4 key购买 nike

所以我有一个很好的合作PTRACE_TRACEMEPTRACE 处理的子程序包装程序。
问题是如果管理员(或其他)决定 SIGSTOP子程序,我该如何处理?并拿起SIGCONT后来,当然。
答案可能是“你不能”?这是PTRACE毕竟!
我可以看到 PTRACE_LISTEN (从 Linux 3.4 开始)可以发挥作用,将子程序置于停止状态,但据记载,只有在子程序与 PTRACE_SEIZE 连接时才能工作。 .
我尝试了调用 PTRACE_LISTEN 的琐碎事情当在 PTRACE_TRACEME在 Group-stop 模式下,我得到了 Old McDonald 错误 - EIO .
我有一个想法,我可以注入(inject)代码来做 sigsuspend ,但当然这只会暂停子程序的 1 个线程。我必须向每个线程注入(inject)一个信号然后将它们全部停止?然后重启?
或者也许执行重新交付SIGSTOP已经阻止了他们?在这种情况下,我的 sigsuspend真的被触发了吗?我想如果另一个线程报告 SIGCONT 我可以直接触发它.
虽然这听起来很有趣,但我想知道我是否错过了 PTRACE 中的一个技巧。文件?也许有办法到达 PTRACE_SEIZE我缺少的兼容模式?
[更多鱼子酱]
为什么不首先使用 PTRACE_SEIZE 呢?

  • PTRACE_TRACEME 需要较少的安全权限;
  • 我们使用 seccomp 过滤掉系统调用拦截,这是重点;
  • 我们想从子exe开始执行。

  • 因此得出结论:是否有一种机制可以到达 PTRACE_SEIZE模式或模拟 SIGSTOP API 内的行为?

    最佳答案

    一个例子 PTRACE_SEIZE 下面是模拟 SIGSTOP 行为的测试程序。

    #define PTRACE_SEIZE        0x4206
    #define PTRACE_SEIZE_DEVEL 0x80000000

    static const struct timespec ts100ms = { .tv_nsec = 100000000 };
    static const struct timespec ts1s = { .tv_sec = 1 };
    static const struct timespec ts3s = { .tv_sec = 3 };

    int main(int argc, char **argv)
    {
    pid_t tracee;

    tracee = fork();
    if (tracee == 0) {
    nanosleep(&ts100ms, NULL);
    while (1) {
    printf("tracee: alive\n");
    nanosleep(&ts1s, NULL);
    }
    }

    if (argc > 1)
    kill(tracee, SIGSTOP);

    nanosleep(&ts100ms, NULL);

    ptrace(PTRACE_SEIZE, tracee, NULL,
    (void *)(unsigned long)PTRACE_SEIZE_DEVEL);
    waitid(P_PID, tracee, NULL, WSTOPPED);
    ptrace(PTRACE_CONT, tracee, NULL, NULL);
    nanosleep(&ts3s, NULL);
    printf("tracer: exiting\n");
    return 0;
    }
    当上面的代码在没有参数的情况下被调用时,tracee 被从
    运行状态并继续。当 tracer 退出时,tracee 返回到
    运行状态并不断打印。
     # ./test-seize
    tracee: alive
    tracee: alive
    tracee: alive
    tracer: exiting
    # tracee: alive
    tracee: alive
    tracee: alive
    当使用参数调用时,tracee 会从停止状态中被捕获,并且
    继续,并在跟踪器退出时返回停止状态。
        # ./test-seize
    tracee: alive
    tracee: alive
    tracee: alive
    tracer: exiting
    # ps -el|grep test-seize
    1 T 0 4720 1 0 80 0 - 941 signal ttyS0 00:00:00 test-seize
    您可以在 thread 阅读更多内容

    关于c - 如何在 ptracer 中处理 SIGSTOP/SIGCONT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63954630/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com