kyun
kyun

Reputation: 640

Suppressing system calls when using gcc/g++

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?

Upvotes: 7

Views: 470

Answers (3)

Joshua
Joshua

Reputation: 43188

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

Upvotes: 3

Matthew Slattery
Matthew Slattery

Reputation: 46988

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.

Upvotes: 3

Tugrul Ates
Tugrul Ates

Reputation: 9687

-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 :)

Upvotes: 2

Related Questions