python - 在 Python 中启用 seccomp 后如何干净退出?

标签 python seccomp

我在项目中通过 python-prctl 启用了 seccomp。我不太清楚如何彻底退出 - 结果总是被杀死。

我看到一些示例使用 ctypes 或 ffi 来尝试引用 libc,但如果我希望它们使用 WIFEXITED,它们似乎也有同样的问题。

下面的示例代码。结果总是“我们被杀了”。

def main():
  pid = os.fork()
  if not pid:
    prctl.set_seccomp(True)
    os.write(0, 'Hi\n')

#    os._exit(0)
#    _exit(0)
#    sys._exit(0)
#    return
#    ?!@#(*!  What do?

  endpid, status = os.waitpid(pid, 0)
  print 'Child forked as %d and returned with %d' % (endpid, status)
  if not os.WIFEXITED(status):
    print 'Exitted abnormally'
    if os.WIFSIGNALED:
      if os.WTERMSIG(status) == signal.SIGKILL:
        print 'We were killed to death'
  else:
    print 'Returned with %d' % (os.WEXITSTATUS(status))

快速更新,因为我忘记了 libc 的东西:

用其中任何一个定义上面的 _exit() 仍然会导致终止。

# FFI Method
ffi = cffi.FFI()
# Use _exit, which avoids atexit(), etc
ffi.cdef('void _exit(int);')
libc = ffi.dlopen(None)
_exit = libc._exit

....或....

# ctypes method
libc = cdll.LoadLibrary('libc-2.18.so')
_exit = libc._exit

最佳答案

有人帮我找到了这个答案:

In glibc up to version 2.3, the _exit() wrapper function invoked the kernel system
call of the same name.  Since glibc 2.3, the wrapper function invokes
exit_group(2), in order to terminate all of the threads in a process.

因为 _exit 包装了 exit_group 而 exit_group 不在 seccomp 过滤器中....它被杀死了。 python 执行的 strace 显示了 exit_group 调用。

关于python - 在 Python 中启用 seccomp 后如何干净退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678139/

相关文章:

python 应用程序(守护进程)和 systemd fedora

concurrency - Rust 中的进程隔离

node.js - 几天后,我无法再启动 Puppeteer,直到我重新启动服务器

docker - 关于容器的seccomp

python - 在 Linux 上使用多处理时不会出现 TKinter 窗口

python - 超越 Python 中 rx 的基础知识

python - 无法安装 jpeg,因为冲突端口处于事件状态 : libjpeg-turbo

python - 在没有 setcap cap_net_raw 的 Linux 中打开原始套接字

c - 查找 seccomp 列入白名单的系统调用

docker - Seccomp 和 Capabilities wrt Docker 环境有什么区别?