3. 调整限额
沙盒政策可防止 Sandboxee 调用特定系统调用,从而缩小受攻击面。不过,攻击者可能仍然能够通过无限期运行进程或耗尽 RAM 和其他资源来造成不良影响。
为了应对这一威胁,Sandboxee 默认在严格的执行限制下运行。如果这些默认限制导致您的程序无法正常执行,您可以使用 sandbox2::Limits
类通过在执行器对象上调用 limits()
来调整这些限制。
以下代码段展示了一些限制调整示例。所有可用选项均记录在 limits.h 头文件中。
// Restrict the address space size of the sandboxee to 4 GiB.
executor->limits()->set_rlimit_as(4ULL << 30);
// Kill sandboxee with SIGXFSZ if it writes more than 1 GiB to the filesystem.
executor->limits()->set_rlimit_fsize(1ULL << 30);
// Number of file descriptors which can be used by the sandboxee.
executor->limits()->set_rlimit_nofile(1ULL << 10);
// The sandboxee is not allowed to create core files.
executor->limits()->set_rlimit_core(0);
// Maximum 300s of real CPU time.
executor->limits()->set_rlimit_cpu(300);
// Maximum 120s of wall time.
executor->limits()->set_walltime_limit(absl::Seconds(120));
如需查看 sandbox2::Limits
类的使用示例,请参阅示例工具。