运行libsandbox

我目前正在开发一个在线C / C ++ /汇编编译器,我偶然发现了一个名为libsandbox的软件。 这使我能够运行在线编写的代码,编译它并拦截系统调用。

首先,我是Linux环境中的新手。 我已下载tar.gz,解压缩,配置并安装它。 这没有任何错误,但现在我很难运行它。 我该如何在这个沙箱中运行C / C ++程序? 我必须提供.c / .cpp文件吗? 编译后的可执行文件?

这可能是一个非常愚蠢的问题。 我在互联网上搜索了如何做,并阅读了自述文件,但他们没有给我一个线索。

提前致谢!

简短而一般的答案是:使用lib 任何东西 ,你编写一个利用该库的程序 – 你将#include < anything.h >放入源代码并用-l anything开关链接。 您不应该找到任何可执行文件,除非它是测试套件或库的示例程序。

由于某种原因,我无法找到’libsandbox’,所以我的回复可能非常不准确。

沙箱仅适用于linux。 您必须首先使用库函数创建沙箱,然后告诉沙箱运行您的程序。

这个python示例展示了如何从python中完成它。 “#targeted program”行显示您将在何处指定实际应用程序的名称。

 def main(args): # sandbox configuration cookbook = { 'args': args[1:], # targeted program 'stdin': sys.stdin, # input to targeted program 'stdout': sys.stdout, # output from targeted program 'stderr': sys.stderr, # error from targeted program 'quota': dict(wallclock = 30000,# 30 sec cpu = 2000, # 2 sec memory = 8388608, # 8 MB disk = 1048576)} # 1 MB # create a sandbox instance and execute till end msb = MiniSandbox(**cookbook) msb.run() # verbose statistics sys.stderr.write("result: %(result)s\ncpu: %(cpu)dms\nmem: %(mem)dkB\n" % \ msb.probe()) return os.EX_OK 

我建议去libsandbox下载页面并获取完整的sample2.py文件,然后使用python脚本运行沙箱。 这比制作C ++或C程序更容易。

所以…

  1. 制作C或C ++程序。 不要把它链接到LIBSANDBOX。

  2. 确保安装了python。

  3. 从libsandbox页面运行示例python脚本。

  4. python脚本将为您加载libsandbox。 然后它将运行您在沙箱中构建的程序。

简单。

完全同意来自@ user1401452的回答。 关于libsandbox的更多提示,

  1. 要进行沙盒化的二进制可执行文件更好地静态链接 ,因为加载共享库涉及系统调用,如SYS_open(),默认情况下是禁止的。
  2. 编写直接调用核心沙箱库(即libsandbox)的C / C ++程序也是可行的 – 虽然比使用Pythonic包装器(即pysandbox)复杂一点。 现在可以在libsandbox的主页上获得示例python脚本的ANSI C等效(即sample2.c)。
  3. 示例程序仅演示了libsandbox的一些基本要素。 实用的沙盒解决方案通常需要具有更复杂规则的自定义沙箱策略。

免责声明:我是libsandbox的作者