简体   繁体   中英

Safe Python Environment in Linux

Is it possible to create an environment to safely run arbitrary Python scripts under Linux? Those scripts are supposed to be received from untrusted people and may be too large to check them manually.

A very brute-force solution is to create a virtual machine and restore its initial state after every launch of an untrusted script. (Too expensive.)

I wonder if it's possible to restrict Python from accessing the file system and interacting with other programs and so on.

Consider using a chroot jail. Not only is this very secure, well-supported and tested but it also applies to external applications you run from python.

There are 4 things you may try:

  • As you already mentioned, using a virtual machine or some other form of virtualisation (perhaps solaris zones are lightweight enough?). If the script breaks the OS there then you don't care.
  • Using chroot, which puts a shell session into a virtual root directory, separate from the main OS root directory.
  • Using systrace. Think of this as a firewall for system calls.
  • Using a "jail", which builds upon systrace, giving each jail it's own process table etc.

Systrace has been compromised recently, so be aware of that.

You could run jython and use the sandboxing mechanism from the JVM. The sandboxing in the JVM is very strong very well understood and more or less well documented. It will take some time to define exactly what you want to allow and what you dnt want to allow, but you should be able to get a very strong security from that ...

On the other side, jython is not 100% compatible with cPython ...

难道你不能只是作为一个除了该目录中的脚本之外的任何东西都无法访问的用户运行吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM