使用 gcc/g++ 时抑制系统调用

发布于 2024-10-20 08:09:14 字数 243 浏览 3 评论 0原文

我的大学 LAN 中有一个门户,人们可以在其中上传 C/C++ 编程难题的代码。我希望确保门户安全,以便人们无法通过其提交的代码进行系统调用。可能有几种解决方法,但我想知道是否可以简单地通过设置一些聪明的 gcc 标志来做到这一点。 libc 默认情况下似乎包含 ,它似乎是声明系统调用的基本文件。有没有办法告诉 gcc/g++ 在编译时“忽略”此文件,以便无法访问 unistd.h 中声明的任何函数?

I have a portal in my university LAN where people can upload code to programming puzzles in C/C++. I would like to make the portal secure so that people cannot make system calls via their submitted code. There might be several workarounds but I'd like to know if I could do it simply by setting some clever gcc flags. libc by default seems to include <unistd.h>, which appears to be the basic file where system calls are declared. Is there a way I could tell gcc/g++ to 'ignore' this file at compile time so that none of the functions declared in unistd.h can be accessed?

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

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

发布评论

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

评论(3

清风疏影 2024-10-27 08:09:14

一些特殊原因 chroot("/var/jail/empty"); setuid(65534); 还不够好(假设 65534 有合理的限制)?

Some particular reason why chroot("/var/jail/empty"); setuid(65534); isn't good enough (assuming 65534 has sensible limits)?

过气美图社 2024-10-27 08:09:14

限制对头文件的访问不会阻止您访问 libc 函数:如果您链接到 libc,它们仍然可用 - 您只是不会拥有原型(和宏)到手;但你可以自己复制它们。

不链接 libc 也无济于事:系统调用可以直接通过内联汇编器(甚至涉及跳转到数据的技巧)进行。

我认为这总体上不是一个好方法。在完全独立的虚拟沙箱中运行上传的代码(也许通过 QEMU 或类似的东西)可能是更好的方法。

Restricting access to the header file won't prevent you from accessing libc functions: they're still available if you link against libc - you just won't have the prototypes (and macros) to hand; but you can replicate them yourself.

And not linking against libc won't help either: system calls could be made directly via inline assembler (or even tricks involving jumping into data).

I don't think this is a good approach in general. Running the uploaded code in a completely self-contained virtual sandbox (via QEMU or something like that, perhaps) would probably be a better way to go.

花辞树 2024-10-27 08:09:14

-D 可以覆盖单个函数名称。例如:

gcc file.c -Dchown -Dchdir

或者您可以自己设置包含保护:

gcc file.c -D_UNISTD_H

但是智能提交者可以使用 #undef 轻松恢复它们的效果:)

-D can overwrite individual function names. For example:

gcc file.c -Dchown -Dchdir

Or you can set the include guard yourself:

gcc file.c -D_UNISTD_H

However their effects can be easily reverted with #undefs by intelligent submitters :)

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