Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
a5d91834
Commit
a5d91834
authored
Mar 28, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanups
parent
ff96a50c
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
18 additions
and
7 deletions
+18
-7
codejail/django_integration.py
+1
-0
codejail/jailpy.py
+8
-3
codejail/safe_exec.py
+3
-3
codejail/tests/test_jailpy.py
+2
-0
codejail/tests/test_safe_exec.py
+2
-0
codejail/util.py
+2
-1
No files found.
codejail/django_integration.py
View file @
a5d91834
...
@@ -5,6 +5,7 @@ from django.conf import settings
...
@@ -5,6 +5,7 @@ from django.conf import settings
import
codejail.jailpy
import
codejail.jailpy
class
ConfigureCodeJailMiddleware
(
object
):
class
ConfigureCodeJailMiddleware
(
object
):
"""Middleware to configure codejail on startup."""
"""Middleware to configure codejail on startup."""
...
...
codejail/jailpy.py
View file @
a5d91834
...
@@ -4,7 +4,8 @@
...
@@ -4,7 +4,8 @@
# - AppArmor.md from xserver
# - AppArmor.md from xserver
import
logging
import
logging
import
os
,
os
.
path
import
os
import
os.path
import
resource
import
resource
import
shutil
import
shutil
import
subprocess
import
subprocess
...
@@ -22,6 +23,7 @@ log = logging.getLogger(__name__)
...
@@ -22,6 +23,7 @@ log = logging.getLogger(__name__)
PYTHON_CMD
=
None
PYTHON_CMD
=
None
def
configure
(
python_bin
,
user
=
None
):
def
configure
(
python_bin
,
user
=
None
):
"""Configure the jailpy module."""
"""Configure the jailpy module."""
global
PYTHON_CMD
global
PYTHON_CMD
...
@@ -30,6 +32,7 @@ def configure(python_bin, user=None):
...
@@ -30,6 +32,7 @@ def configure(python_bin, user=None):
PYTHON_CMD
.
extend
([
'sudo'
,
'-u'
,
'sandbox'
])
PYTHON_CMD
.
extend
([
'sudo'
,
'-u'
,
'sandbox'
])
PYTHON_CMD
.
extend
([
python_bin
,
'-E'
])
PYTHON_CMD
.
extend
([
python_bin
,
'-E'
])
def
is_configured
():
def
is_configured
():
return
bool
(
PYTHON_CMD
)
return
bool
(
PYTHON_CMD
)
...
@@ -42,7 +45,9 @@ if hasattr(sys, 'real_prefix'):
...
@@ -42,7 +45,9 @@ if hasattr(sys, 'real_prefix'):
class
JailResult
(
object
):
class
JailResult
(
object
):
"""A passive object for us to return from jailpy."""
"""A passive object for us to return from jailpy."""
pass
def
__init__
(
self
):
self
.
stdout
=
self
.
stderr
=
self
.
status
=
None
def
jailpy
(
code
,
files
=
None
,
argv
=
None
,
stdin
=
None
):
def
jailpy
(
code
,
files
=
None
,
argv
=
None
,
stdin
=
None
):
"""
"""
...
@@ -104,7 +109,7 @@ def set_process_limits():
...
@@ -104,7 +109,7 @@ def set_process_limits():
resource
.
setrlimit
(
resource
.
RLIMIT_NPROC
,
(
0
,
0
))
# no subprocesses
resource
.
setrlimit
(
resource
.
RLIMIT_NPROC
,
(
0
,
0
))
# no subprocesses
resource
.
setrlimit
(
resource
.
RLIMIT_FSIZE
,
(
0
,
0
))
# no files
resource
.
setrlimit
(
resource
.
RLIMIT_FSIZE
,
(
0
,
0
))
# no files
mem
=
32
*
2
**
20
# 32 MB should be enough for anyone, right? :)
mem
=
32
*
(
2
**
20
)
# 32 MB should be enough for anyone, right? :)
resource
.
setrlimit
(
resource
.
RLIMIT_STACK
,
(
mem
,
mem
))
resource
.
setrlimit
(
resource
.
RLIMIT_STACK
,
(
mem
,
mem
))
resource
.
setrlimit
(
resource
.
RLIMIT_RSS
,
(
mem
,
mem
))
resource
.
setrlimit
(
resource
.
RLIMIT_RSS
,
(
mem
,
mem
))
resource
.
setrlimit
(
resource
.
RLIMIT_DATA
,
(
mem
,
mem
))
resource
.
setrlimit
(
resource
.
RLIMIT_DATA
,
(
mem
,
mem
))
...
...
codejail/safe_exec.py
View file @
a5d91834
...
@@ -6,9 +6,9 @@ import shutil
...
@@ -6,9 +6,9 @@ import shutil
import
sys
import
sys
import
textwrap
import
textwrap
import
jailpy
from
codejail
import
jailpy
from
codejail.util
import
temp_directory
,
change_directory
from
util
import
temp_directory
,
change_directory
def
safe_exec
(
code
,
globals_dict
,
files
=
None
,
python_path
=
None
):
def
safe_exec
(
code
,
globals_dict
,
files
=
None
,
python_path
=
None
):
"""Execute code as "exec" does, but safely.
"""Execute code as "exec" does, but safely.
...
@@ -98,7 +98,7 @@ def json_safe(d):
...
@@ -98,7 +98,7 @@ def json_safe(d):
ok_types
=
(
type
(
None
),
int
,
long
,
float
,
str
,
unicode
,
list
,
tuple
,
dict
)
ok_types
=
(
type
(
None
),
int
,
long
,
float
,
str
,
unicode
,
list
,
tuple
,
dict
)
bad_keys
=
(
"__builtins__"
,)
bad_keys
=
(
"__builtins__"
,)
jd
=
{}
jd
=
{}
for
k
,
v
in
d
.
iteritems
():
for
k
,
v
in
d
.
iteritems
():
if
not
isinstance
(
v
,
ok_types
):
if
not
isinstance
(
v
,
ok_types
):
continue
continue
if
k
in
bad_keys
:
if
k
in
bad_keys
:
...
...
codejail/tests/test_jailpy.py
View file @
a5d91834
...
@@ -9,6 +9,7 @@ from codejail.jailpy import jailpy, is_configured
...
@@ -9,6 +9,7 @@ from codejail.jailpy import jailpy, is_configured
dedent
=
textwrap
.
dedent
dedent
=
textwrap
.
dedent
class
JailPyHelpers
(
object
):
class
JailPyHelpers
(
object
):
"""Assert helpers for jailpy tests."""
"""Assert helpers for jailpy tests."""
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -112,6 +113,7 @@ class TestLimits(JailPyHelpers, unittest.TestCase):
...
@@ -112,6 +113,7 @@ class TestLimits(JailPyHelpers, unittest.TestCase):
# TODO: read network
# TODO: read network
# TODO: fork
# TODO: fork
class
TestMalware
(
JailPyHelpers
,
unittest
.
TestCase
):
class
TestMalware
(
JailPyHelpers
,
unittest
.
TestCase
):
def
test_crash_cpython
(
self
):
def
test_crash_cpython
(
self
):
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
...
...
codejail/tests/test_safe_exec.py
View file @
a5d91834
...
@@ -7,6 +7,7 @@ from nose.plugins.skip import SkipTest
...
@@ -7,6 +7,7 @@ from nose.plugins.skip import SkipTest
from
codejail.safe_exec
import
safe_exec
,
not_safe_exec
from
codejail.safe_exec
import
safe_exec
,
not_safe_exec
class
SafeExecTests
(
object
):
class
SafeExecTests
(
object
):
"""The tests for `safe_exec`, will be mixed into specific test classes below."""
"""The tests for `safe_exec`, will be mixed into specific test classes below."""
def
test_set_values
(
self
):
def
test_set_values
(
self
):
...
@@ -60,6 +61,7 @@ class TestSafeExec(SafeExecTests, unittest.TestCase):
...
@@ -60,6 +61,7 @@ class TestSafeExec(SafeExecTests, unittest.TestCase):
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
safe_exec
(
*
args
,
**
kwargs
)
safe_exec
(
*
args
,
**
kwargs
)
class
TestNotSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
class
TestNotSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
"""Run SafeExecTests, with not_safe_exec."""
"""Run SafeExecTests, with not_safe_exec."""
def
setUp
(
self
):
def
setUp
(
self
):
...
...
codejail/util.py
View file @
a5d91834
...
@@ -3,7 +3,6 @@
...
@@ -3,7 +3,6 @@
import
contextlib
import
contextlib
import
os
import
os
import
shutil
import
shutil
import
sys
import
tempfile
import
tempfile
...
@@ -19,6 +18,7 @@ class TempDirectory(object):
...
@@ -19,6 +18,7 @@ class TempDirectory(object):
# if this errors, something is genuinely wrong, so don't ignore errors.
# if this errors, something is genuinely wrong, so don't ignore errors.
shutil
.
rmtree
(
self
.
temp_dir
)
shutil
.
rmtree
(
self
.
temp_dir
)
@contextlib.contextmanager
@contextlib.contextmanager
def
temp_directory
(
delete_when_done
=
True
):
def
temp_directory
(
delete_when_done
=
True
):
"""
"""
...
@@ -40,6 +40,7 @@ class ChangeDirectory(object):
...
@@ -40,6 +40,7 @@ class ChangeDirectory(object):
def
clean_up
(
self
):
def
clean_up
(
self
):
os
.
chdir
(
self
.
old_dir
)
os
.
chdir
(
self
.
old_dir
)
@contextlib.contextmanager
@contextlib.contextmanager
def
change_directory
(
new_dir
):
def
change_directory
(
new_dir
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment