JMK no matter what

Python Sandbox

In order to add Python support for Algospot Online Judge, I wrote a simple Python sandbox for running unauthorized Python programs. The sandbox is used for evaluating submissions for programming contest problems; it should accept programs written in Python, run them while restricting the program to affect the system in any way. The only thing we allow them is to access certain standard library modules, and read & write to standard i/o. The following is a brief summary of what it does:

  • Reads in the code, compile()s and eval()uates it.
  • Before executing the code, we overwrite some of the forbidden functions in __builtin__, such as open, eval, execfile, and compile. We override exit() as well, so calling exit() will not terminate the sandbox as well.
  • We override __import__() so it will check every imports the program makes.
    • The list of allowed modules are: ["string", "re", "struct", "StringIO", "cStringIO", "heapq", "array", "bisect", "_bisect", "sets", "types", "math", "cmath", "decimal", "fractions", "random", "itertools", "functools", "time", "errno", "future", "operator", "_heapq", "warnings", "binascii", "_random", "_functools"]
    • The list of allowed entries are: ["os.urandom", "sys.stdin", "sys.stdout"]
  • sys.stdin will be redirected to the input file
  • sys.stdout will be redirected to ModeratedOutputFile, which throws an exception if we write more than 16MB to the file.

The memory usage and running time of the Python runtime will be monitored by an outside process. Then, will it be possible for a malevolent Python code to affect the system in any way?

Any comments will be appreciated.

2009-06-06 01:26:11 | JM | /writings/cs/ | 11 Comments
dasony
2009-06-06 02:28:29
http://pypi.python.org/pypi/RestrictedPython/ 같은건 일부러 안쓴건가?
하긴 이런거 안써도 코딩 대회니 OS단에서만 잘 제한하면, 충분해보이는 듯.
그리고 서버 해킹 당하면 다시 깔면 되지 뭘 ㅎㅎ
dg
2009-06-07 17:29:51
OS 상에서의 샌드박스를 쓰면 어떤 언어든지 추가할 수 있을듯...
JM
2009-06-07 19:05:12
dasony/ 실은 그거 보고 비슷하게 만든거임 -_ -;;
JM
2009-06-07 19:05:37
dg/ 파이썬이나 자바같이 VM 위에서 돌아가는 경우 사용하는 라이브러리나 기타 파일 같은 것들도 파일 접근을 통제해야 하는데, 이것이 매우 어려워서.. 'ㅅ'
이희승
2009-06-08 23:16:40
POSIX ACL + SELinux 쓰면 되잖니? 생각보다 안어려우니까 함 해보셈 ㅋ
dg
2009-06-09 01:44:12
VM 위에서 돌아가더라도 OS 래벨에서 통제 하면 충분할듯... 사실 유저모드에서는 뭘 하든 상관이 없자나.. 뭔가 시스템에 영향을 끼칠만한 것들은 시스템 콜을 해야 되고.. 그나저나 C++의 경우는 지금 어떻게 하고 있는거야?
JM
2009-06-09 02:08:08
희승옹/ 호오, 이런 건 몰랐네요. 우분투에서 적용할 수 있는 방법이 있나 구글해 봐야 겠어요. 근데 지금은 채점 서버가 별도로 있는 것이 아니고 남의 서버에 빌붙어 있어서리.. 설정을 맘대로 바꾸긴 좀 힘들거든요.
dg/ 음 그야 그렇지. 사실 파일시스템 쓰는 것도 막고 싶은데, 우리가 외부에서 VM 이 하는 파일 시스템 접근과 유저 프로그램이 하는 파일 접근을 구분할 수 없다는 것이 문제인 것 같음. 예를 들어, 파이썬에서 py 파일을 pyc 로 컴파일 해서 캐시한다던지 하는 것은 디스크에 쓴다고 해도 허용해야겠지. C++ 은 지금 시스템콜만 막고 있음. 'ㅅ' 파일 출력은 표준 출력으로 제한하고, 뭐 이런 식.
dg
2009-06-09 02:46:52
음.. python으로 hello world 프로그램 짜서 strace 해보니 write는

write(1, "hello world\n", 12)

밖에 없던데.. 암튼 수거..
이희승
2009-06-09 08:51:11
페도라 고고싱? ㅋㅋㅋ
JM
2009-06-09 09:23:51
dg/ 그건 헬로 월드니까 그렇지 ㅋㅋㅋㅋㅋ 내가 IPSC 때 짠 프로그램으로 테스트를 해봤는데
http://dev.yutar.net/~jongman/e1.py 에 있고.. strace 결과는 http://dev.yutar.net/~jongman/strace.txt .. -_-;;

희승옹/ 형 레드햇 알바같아요 ㅋㅋㅋㅋㅋ
이희승
2009-06-10 08:18:08
이거 왜이라 나 정규직이야 ㅋㅋㅋㅋ
SELinux는 아무래도 레드햇에서 주도적으로 개발한거라 페도라가 짱임 ㅋ

Leave a comment

春來不似春

About

Eventstream

Pages

Guestbook

Search

Site Admin

Recent Comments