The Python Oracle

sandbox to execute possibly unfriendly python code

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Sandbox To Execute Possibly Unfriendly Python Code
00:48 Answer 1 Score 1
01:06 Answer 2 Score 2
02:00 Answer 3 Score 2
02:43 Answer 4 Score 7
03:33 Thank you

--

Full question
https://stackoverflow.com/questions/3910...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #trustedvsuntrusted

#avk47



ANSWER 1

Score 7


If you are not specific to CPython implementation, you should consider looking at PyPy[wiki] for these purposes — this Python dialect allows transparent code sandboxing.

Otherwise, you can provide fake __builtin__ and __builtins__ in the corresponding globals/locals arguments to exec or eval.

Moreover, you can provide dictionary-like object instead of real dictionary and trace what untrusted code does with it's namespace.

Moreover, you can actually trace that code (issuing sys.settrace() inside restricted environment before any other code executed) so you can break execution if something will go bad.

If none of solutions is acceptable, use OS-level sandboxing like chroot, unionfs and standard multiprocess python module to spawn code worker in separate secured process.




ANSWER 2

Score 2


It's impossible to provide an absolute solution for this because the definition of 'bad' is pretty hard to nail down.

Is opening and writing to a file bad or good? What if that file is /dev/ram?

You can profile signatures of behavior, or you can try to block anything that might be bad, but you'll never win. Javascript is a pretty good example of this, people run arbitrary javascript code all the time on their computers -- it's supposed to be sandboxed but there's all sorts of security problems and edge conditions that crop up.

I'm not saying don't try, you'll learn a lot from the process.

Many companies have spent millions (Intel just spent billions on McAffee) trying to understand how to detect 'bad code' -- and every day machines running McAffe anti-virus get infected with viruses. Python code isn't any less dangerous than C. You can run system calls, bind to C libraries, etc.




ANSWER 3

Score 2


I would seriously consider virtualizing the environment to run this stuff, so that exploits in whatever mechanism you implement can be firewalled one more time by the configuration of the virtual machine.

Number of users and what kind of code you expect to test/run would have considerable influence on choices btw. If they aren't expected to link to files or databases, or run computationally intensive tasks, and you have very low pressure, you could be almost fine by just preventing file access entirely and imposing a time limit on the process before it gets killed and the submission flagged as too expensive or malicious.

If the code you're supposed to test might be any arbitrary Django extension or page, then you're in for a lot of work probably.




ANSWER 4

Score 1


You can try some generic sanbox such as Sydbox or Gentoo's sandbox. They are not Python-specific.

Both can be configured to restrict read/write to some directories. Sydbox can even sandbox sockets.