在服务器上安全执行用户提交的Python代码

发布于 2024-08-11 08:50:39 字数 359 浏览 7 评论 0原文

我正在考虑启动一个项目,该项目涉及执行用户通过 HTML 表单输入的 python 代码。我知道这可能是致命的(exec),但我至少在

我向 Python Challenge 的开发人员发送了一封电子邮件,我被告知他们正在使用他们的解决方案他们自己提出了自己的想法,他们只透露自己正在使用“操作系统提供的安全功能”,并且“如果您知道如何使用操作系统[Linux],它可以提供您所需的大部分安全性”。

有人知道如何安全可靠地执行此操作吗?我考虑过为每次提交生成一个新的虚拟机,但这会产生太多的开销,并且几乎不可能有效地实现。

I am looking into starting a project which involves executing python code that the user enters via a HTML form. I know this can be potentially lethal (exec), but I have seen it done successfully in at least one instance.

I sent an email off to the developers of the Python Challenge and I was told they are using a solution they came up with themselves, and they only let on that they are using "security features provided by the operating system" and that "the operating system [Linux] provides most of the security you need if you know how to use it."

Would anyone know how a safe and secure way to go about doing this? I thought about spawning a new VM for every submission, but that would have way too much overhead and be pert-near impossible to implement efficiently.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘑菇王子 2024-08-18 08:50:39

在现代 Linux 上,除了 chroot(2) 之外,您还可以使用 clone(2) 而不是 fork(2) 来进一步限制进程。有几个有趣的clone(2)标志:

CLONE_NEWIPC (new namespace for semaphores, shared memory, message queues)
CLONE_NEWNET (new network namespace - nice one)
CLONE_NEWNS (new set of mountpoints)
CLONE_NEWPID (new set of process identifiers)
CLONE_NEWUTS (new hostname, domainname, etc)

以前这个功能是在OpenVZ中实现的,然后合并到上游,所以不再需要修补内核。

On a modern Linux in addition to chroot(2) you can restrict process further by using clone(2) instead of fork(2). There are several interesting clone(2) flags:

CLONE_NEWIPC (new namespace for semaphores, shared memory, message queues)
CLONE_NEWNET (new network namespace - nice one)
CLONE_NEWNS (new set of mountpoints)
CLONE_NEWPID (new set of process identifiers)
CLONE_NEWUTS (new hostname, domainname, etc)

Previously this functionality was implemented in OpenVZ and merged then upstream, so there is no need for patched kernel anymore.

念三年u 2024-08-18 08:50:39

http://codepad.org/about 已经成功实现了这样一个系统(作为公共代码粘贴/运行服务) !)

codepad.org 是一个在线编译器/解释器,也是一个简单的协作工具。它是一个为您执行代码的pastebin。 [...]

工作原理

代码执行由基于 geordi 的主管处理。策略是在 ptrace 下运行所有​​内容,不允许或忽略许多系统调用。编译器和最终的可执行文件都在 chroot 监狱中执行,具有严格的资源限制。主管是用 Haskell 编写的。

[...]

当您的应用程序是远程代码执行时,您必须预料到安全问题。我不只是依赖 chroot 和 ptrace 管理程序,而是采取了一些额外的预防措施:

  • 主管进程在虚拟机上运行,​​这些虚拟机受防火墙保护,因此无法建立传出连接。

  • 运行虚拟机的计算机也受到严格的防火墙保护,并定期从其源映像恢复。

http://codepad.org/about has implemented such a system successfully (as a public code pasting/running service!)

codepad.org is an online compiler/interpreter, and a simple collaboration tool. It's a pastebin that executes code for you. [...]

How it works

Code execution is handled by a supervisor based on geordi. The strategy is to run everything under ptrace, with many system calls disallowed or ignored. Compilers and final executables are both executed in a chroot jail, with strict resource limits. The supervisor is written in Haskell.

[...]

When your app is remote code execution, you have to expect security problems. Rather than rely on just the chroot and ptrace supervisor, I've taken some additional precautions:

  • The supervisor processes run on virtual machines, which are firewalled such that they are incapable of making outgoing connections.

  • The machines that run the virtual machines are also heavily firewalled, and restored from their source images periodically.

冷夜 2024-08-18 08:50:39

如果您以用户 nobody(在 Linux 上)身份运行脚本,它几乎无法写入任何内容,也无法读取权限设置正确的数据。但它仍然可能导致 DoS 攻击,例如:

  • 填满 /tmp
  • 吃掉所有 RAM
  • 吃掉所有 CPU

此外,可以打开外部网络连接,等等。您可能可以通过内核限制锁定所有这些,但您肯定会忘记一些东西。

因此,我认为无法访问网络或真实硬盘驱动器的虚拟机将是唯一(合理)安全的途径。也许 Python 挑战赛的开发人员使用 KVM ,原则上,“由操作系统”。

为了提高效率,您可以在同一虚拟机中运行所有提交。这可以为您节省大量开销,并且在最坏的情况下,它们只会互相妨碍,但不会妨碍您的服务器。

If you run the script as user nobody (on Linux), it can write practically nowhere and read no data that has its permissions set up properly. But it could still cause a DoS attack by, for example:

  • filling up /tmp
  • eating all RAM
  • eating all CPU

Furthermore, outside network connections can be opened, etcetera etcetera. You can probably lock all these down with kernel limits, but you are bound to forget something.

So I think that a virtual machine with no access to the network or the real hard drive would be the only (reasonably) safe route. Perhaps the developers of the Python Challenge use KVM which is, in principle, "provided by the operating system".

For efficiency, you could run all submissions in the same VM. That saves you much overhead, and in the worst-case scenario they only hamper each other, but not your server.

把梦留给海 2024-08-18 08:50:39

使用 chroot (Wikipedia) 可能是解决方案的一部分,例如与 ulimit 和其他一些常见(或自定义)工具。

Using chroot (Wikipedia) may be part of the solution, e.g. combined with ulimit and some other common (or custom) tools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文