公平、安全、高效和多语言沙箱技术

3
我正在开发一个在线裁判系统,大约有100个不受信任的可执行文件同时运行并评估相同的输入数据。
我希望每个可执行文件都能限制CPU、内存、磁盘空间等预定义资源池的相等份额。例如,如果将资源池设置为机器CPU的3/4、3GB内存、300GB磁盘,并且正在运行2个可执行文件,则每个可执行文件将获得3/8的CPU、1.5GB内存和150GB磁盘。如果加入了另一个可执行文件,则资源将被重新调整为三个相等的部分。这是为了防止恶意或有缺陷的可执行文件从其他文件中窃取资源,同时也为每个人提供平等的资源。
理想情况下,我还希望可执行文件不受单一语言的限制(例如,让用户使用他们熟悉的语言,如C、C++、Java、Python等)。
使用整个虚拟机(VM)或类似OpenVZ的东西似乎过于复杂。是否有更轻量级的替代方案,可以为每个可执行文件使用单独的进程,同时限制其资源,禁用诸如网络访问、进程生成等功能?我寻找轻量级解决方案的原因之一是因为有大量的输入数据,我宁愿不将其复制到每个可执行文件中,而是让它们从共享内存中读取。
1个回答

0

也许为每个进程创建不同的用户ID,然后通过"ulimit"限制它们就足够了。在Debian下,bash的man页面中有关于"ulimit"的说明:

ulimit [-HSTabcdefilmnpqrstuvx [limit]]

提供对外壳和由其启动的进程可用资源的控制,适用于允许此类控制的系统。-H 和 -S 选项指定给定资源的硬限制或软限制已设置。一旦设置了硬限制,非 root 用户无法将其增加;软限制可以增加到硬限制的值。如果未指定 -H 或 -S,则设置软限制和硬限制。limit 的值可以是资源所指定单位中的数字,也可以是特殊值 hard、soft 或 unlimited,分别表示当前硬限制、当前软限制和无限制。如果省略 limit,则打印资源的当前软限制值,除非给出 -H 选项。当指定多个资源时,在值之前打印限制名称和单位。其他选项的解释如下:

          -a     All current limits are reported
          -b     The maximum socket buffer size
          -c     The maximum size of core files created
          -d     The maximum size of a process's data segment
          -e     The maximum scheduling priority ("nice")
          -f     The maximum size of files written by the shell and its children
          -i     The maximum number of pending signals
          -l     The maximum size that may be locked into memory
          -m     The **maximum resident set size** (many systems do not honor this limit)
          -n     The maximum number of open file descriptors (most systems do not allow this value to be set)
          -p     The pipe size in 512-byte blocks (this may not be set)
          -q     The maximum number of bytes in POSIX message queues
          -r     The maximum real-time scheduling priority
          -s     The maximum stack size
          -t     The maximum amount of cpu time in seconds
          -u     The maximum number of processes available to a single user
          -v     The maximum amount of virtual memory available to the shell and, on some systems, to its children
          -x     The maximum number of file locks
          -T     The **maximum number of threads**

我认为将线程限制为1,可以获得公平的CPU计算时间分配。

同时也要限制最大的RSS。


使用Docker与此相结合似乎是解决方案。 - thinkski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接