Run python script with reduced permissions

Question:

I have to run some student-made code that they made as homework, I’m running this on my own computer (Mac OS), and I’m not entirely certain they won’t accidentally “rm -rf /” my machine. Is there an easy way to run their python scripts with reduced permissions, so that e.g. they can access only a certain directory? Is PyPy the way to go?

Asked By: noio

||

Answers:

You could use a chroot environment. In this setup, the script can’t access any files outside the chroot jail.

Answered By: Gareth Latty

Create a new user account “student”. Run your students’ scripts as “student”. Worst case, the script will destroy this special user account. If this happens, just delete user “student” and start over.

Answered By: Jan

Nowadays, a simple way would be to run student code inside a docker container. You just mount the volume with the code and use an image with python.

e.g. assuming /tmp/student_code contains student submissions:

docker run -it --mount type=bind,source=/tmp/student_code,target=/student_code python /bin/bash

you are dropped into a new bash session and can run python /student_code/script1.py

Answered By: mjgalindo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.