views:

51

answers:

1

I'm working on a web app that can allow the user to input some code, and it will be compiled and executed, and they can see the result. I'm coding the app in PHP and I need a way to ensure that if the user inputs some harmful code (this example is PHP), it can't execute and destroy my server. Example:

<?php
shell_exec('rm -rf /');
?>

I'm thinking that permissions on the uploaded code's file might do it just fine, but I wanted some secondary input. Thanks!

+3  A: 

Do not do this unless you are a security professional. There are countless ways to destroy things that you can't possibly forsee.

The only case in which this is acceptable is if you give each user an actual user in the system with its own home directory, permission set, etc. and ensure that they can't actually touch anything that's not theirs whatsoever. And, even then, you'll still get hit with something you didn't expect.

I wouldn't trust myself to write something like this for another good 10 years, minimum, if even then. Users are never trustworthy, ever, and there's always someone smarter out there. No way am I giving out that kind of freedom.

Matchu
I think going this far is a little radical. Sure there may be issues, but there are ways to ensure that even if something does go awry, the core system in not harmed, like FreeBSD jails or something. And if I create a user that has very locked down permissions, and automatically chown the uploaded code to that user. All just ideas.
Ben
@Ben - Web-based PHP executes with the permissions of your web server. You could try to change ownership levels on the fly, but your apache (or whatever) user is the one executing the code. By allowing users to execute arbitrary code, you are vulnerable to a whole new class of exploits that web developers normally don't worry about: local user exploits. These are serious, with the worst resulting in root access. Even ignoring these, how are you going to lock out things like `mail()`? A user could use your server as a spam relay. Or even just make an infinite loop that uses all your CPU.
zombat
Alright, I guess I see the valid points here. Shows what I get for trying to be innovative :D, I could go through and put in special fixes for certain vulnerabilities, but I could never really get them all. Thanks for the help.
Ben
I actually just found a site that does this: http://codepad.org/ How do you suppose they did it?
Ben
@Ben - `<?php exec('host'); ?>` returns `Disallowed system call: SYS_pipe` - they're running PHP in CLI mode and they've (wisely) locked down the backtick operator and (theoretically) shell access. I worry for their sysadmin, though :)
danlefree
@Ben: I'm betting they're security professionals who have a very, very intricate understanding of what they're doing.
Matchu