1

我有一个简单的测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pthread.h>

#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/syscall.h>

void *do_work(void *args) {
    prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
    printf("OK.\n");
}

int main() {
    pthread_t t;
    int ret;
    ret = pthread_create(&t, NULL, do_work, NULL);
    if (ret != 0) {
        printf("Could not create thread, error is %d.\n", ret);
        return 1;
    }
    ret = pthread_join(t, NULL);
    if (ret != 0) {
        printf("Could not join thread, error is %d.\n", ret);
        return 2;
    }
    printf("Program done.\n");
    return 0;
}

这在 Ubuntu 16.04 中没有打印任何内容就死锁了。通过阅读有关 seccomp 的文档,我不清楚为什么会发生这种情况。为什么会这样?SIGKILL 不会杀死整个过程吗?

4

2 回答 2

1

原因在于printf功能。

如果您strace -f在您的程序上运行(没有该prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);行),您将看到创建的线程调用futex系统调用。

调用futexseccomp模式禁止,所以线程被杀死,所以pthread_join无限等待。

如果您替换printf()write(1,...),程序的行为将符合预期。

于 2016-09-12T15:10:20.633 回答
1

创建的线程与原始线程具有不同的进程ID(根据strace)。因此,只有新线程在违反 seccomp 时才会被杀死。这意味着死锁。

于 2016-10-19T14:00:54.563 回答