Secure way to run other people code (sandbox) on my server?

后端 未结 9 1517
一个人的身影
一个人的身影 2020-12-01 06:37

I want to make a web service that run other people code locally... Naturally, I want to limit their code access to certain \"sandbox\" directory, and that they wont be able

9条回答
  •  青春惊慌失措
    2020-12-01 07:30

    To limit CPU and memory, you want to set limits for groups of processes (POSIX resource limits only apply to individual processes). You can do this using cgroups.

    For example, to limit memory start by mounting the memory cgroups filesystem:

    # mount cgroup -t cgroup -o memory /cgroups/memory

    Then, create a new sub-directory for each group, e.g.

    # mkdir /cgroups/memory/my-users
    

    Put the processes you want constrained (process with PID "1234" here) into this group:

    # cd /cgroups/memory/my-users
    # echo 1234 >> tasks
    

    Set the total memory limit for the group:

    # echo 1000000 > memory.limit_in_bytes

    If processes in the group fork child processes, they will also be in the group.

    The above group sets the resident memory limit (i.e. constrained processes will start to swap rather than using more memory). Other cgroups let you constrain other things, such as CPU time.

    You could either put your server process into the group (so that the whole system with all its users fall under the limits) or get the server to put each new session into a new group.

提交回复
热议问题