Name | edx-codejail JSON |
Version |
3.5.2
JSON |
| download |
home_page | https://github.com/openedx/codejail |
Summary | CodeJail manages execution of untrusted code in secure sandboxes. It is designed primarily for Python execution, but can be used for other languages as well. |
upload_time | 2024-11-07 17:21:40 |
maintainer | None |
docs_url | None |
author | edX |
requires_python | None |
license | Apache |
keywords |
edx
codejail
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
CodeJail
========
CodeJail manages execution of untrusted code in secure sandboxes. It is
designed primarily for Python execution, but can be used for other languages as
well.
Security is enforced with AppArmor. If your operating system doesn't support
AppArmor, then CodeJail won't protect the execution.
CodeJail is designed to be configurable, and will auto-configure itself for
Python execution if you install it properly. The configuration is designed to
be flexible: it can run in safe mode or unsafe mode. This helps support large
development groups where only some of the developers are involved enough with
secure execution to configure AppArmor on their development machines.
If CodeJail is not configured for safe execution, it will execution Python
using the same API, but will not guard against malicious code. This allows the
same code to be used on safe-configured or non-safe-configured developer's
machines.
A CodeJail sandbox consists of several pieces:
#) Sandbox environment. For a Python setup, this would be Python and
associated core packages. This is denoted throughout this document
as **<SANDENV>**. This is read-only.
#) Sandbox packages. These are additional packages needed for a given
run. For example, this might be a grader written by an instructor
to run over a student's code, or data that a student's code might
need to access. This is denoted throughout this document as
**<SANDPACK>**. This is read-only.
#) Untrusted packages. This is typically the code submitted by the
student to be tested on the server, as well as any data the code
may need to modify. This is denoted throughout this document as
**<UNTRUSTED_PACK>**. This is currently read-only, but may need to
be read-write for some applications.
#) OS packages. These are standard system libraries needed to run
Python (e.g. things in /lib). This is denoted throughout this
document as **<OSPACK>**. This is read-only, and is specified by
Ubuntu's AppArmor profile.
To run, CodeJail requires two user accounts. One account is the main
account under which the code runs, which has access to create
sandboxes. This will be referred to as **<SANDBOX_CALLER>**. The
second account is the account under which the sandbox runs. This is
typically the account 'sandbox.'
Supported Versions
------------------
This library currently is tested to work with the following versions
Python:
* 3.11
Ubuntu:
* 20.04
* 22.04
* 24.04
Installation
------------
These instructions detail how to configure your operating system so that
CodeJail can execute Python code safely. You can run CodeJail without these
steps, and you will have an unsafe CodeJail. This is fine for developers'
machines who are unconcerned with security, and simplifies the integration of
CodeJail into your project.
To secure Python execution, you'll be creating a new virtualenv. This means
you'll have two: the main virtualenv for your project, and the new one for
sandboxed Python code.
Choose a place for the new virtualenv, call it **<SANDENV>**. It will be
automatically detected and used if you put it right alongside your existing
virtualenv, but with ``-sandbox`` appended. So if your existing virtualenv is in
``/home/chris/ve/myproj``, make **<SANDENV>** be ``/home/chris/ve/myproj-sandbox``.
The user running the LMS is **<SANDBOX_CALLER>**, for example, you on
your dev machine, or ``www-data`` on a server.
Other details here that depend on your configuration:
1. Create the new virtualenv, using ``--copies`` so that there's a distinct Python executable to limit::
$ sudo python3.8 -m venv --copies <SANDENV>
By default, the virtualenv would just symlink against the system Python, and apparmor's default configuration on some operating systems may prevent confinement from being appled to that.
2. (Optional) If you have particular packages you want available to your
sandboxed code, install them by activating the sandbox virtual env, and
using pip to install them::
$ <SANDENV>/bin/pip install -r requirements/sandbox.txt
3. Add a sandbox user::
$ sudo addgroup sandbox
$ sudo adduser --disabled-login sandbox --ingroup sandbox
4. Let the web server run the sandboxed Python as sandbox. Create the file
``/etc/sudoers.d/01-sandbox``::
$ sudo visudo -f /etc/sudoers.d/01-sandbox
<SANDBOX_CALLER> ALL=(sandbox) SETENV:NOPASSWD:<SANDENV>/bin/python
<SANDBOX_CALLER> ALL=(sandbox) SETENV:NOPASSWD:/usr/bin/find
<SANDBOX_CALLER> ALL=(ALL) NOPASSWD:/usr/bin/pkill
(Note that the ``find`` binary can run arbitrary code, so this is not a safe sudoers file for non-codejail purposes.)
5. Edit an AppArmor profile. This is a text file specifying the limits on the
sandboxed Python executable. The file must be in ``/etc/apparmor.d`` and must
be named based on the executable, with slashes replaced by dots. For
example, if your sandboxed Python is at ``/home/chris/ve/myproj-sandbox/bin/python``,
then your AppArmor profile must be ``/etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python``::
$ sudo vim /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python
#include <tunables/global>
<SANDENV>/bin/python {
#include <abstractions/base>
#include <abstractions/python>
<CODEJAIL_CHECKOUT>/** mr,
<SANDENV>/** mr,
# If you have code that the sandbox must be able to access, add lines
# pointing to those directories:
/the/path/to/your/sandbox-packages/** r,
/tmp/codejail-*/ rix,
/tmp/codejail-*/** wrix,
}
Depending on your OS and AppArmor version you may need to specify a policy
ABI to ensure the restrictions are being correctly applied. Modern ubuntu
versions using AppArmor V3 should use the 3.0 ABI in order to enable
network confinment rules. A profile using the ABI 3.0 would look as
follows::
$ sudo vim /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python
abi <abi/3.0>,
#include <tunables/global>
<SANDENV>/bin/python {
#include <abstractions/base>
#include <abstractions/python>
<CODEJAIL_CHECKOUT>/** mr,
<SANDENV>/** mr,
# If you have code that the sandbox must be able to access, add lines
# pointing to those directories:
/the/path/to/your/sandbox-packages/** r,
/tmp/codejail-*/ rix,
/tmp/codejail-*/** wrix,
}
You can also look at the
``apparmor-profiles/home.sandbox.codejail_sandbox-python3.bin.python-abi3``
file which is used for testing for a full profile example.
6. Parse the profiles::
$ sudo apparmor_parser <APPARMOR_FILE>
7. Reactivate your project's main virtualenv again.
8. Disable using PAM to set rlimits::
sed -i '/pam_limits.so/d' /etc/pam.d/sudo
Using CodeJail
--------------
If your CodeJail is properly configured to use safe_exec, try these
commands at your Python terminal::
import codejail.jail_code
codejail.jail_code.configure('python', '<SANDENV>/bin/python', user='sandbox')
import codejail.safe_exec
jailed_globals = {}
codejail.safe_exec.safe_exec("output=open('/etc/passwd').read()", jailed_globals)
print(jailed_globals) # should be unreachable if codejail is working properly
This should fail with an exception.
If you need to change the packages installed into your sandbox's virtualenv,
you'll need to disable AppArmor, because your sandboxed Python doesn't have
the rights to modify the files in its site-packages directory.
1. Disable AppArmor for your sandbox::
$ sudo apt-get install apparmor-utils # if you haven't already
$ sudo aa-complain /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python
2. Install or otherwise change the packages installed::
$ pip install -r requirements/sandbox.txt
3. Re-enable AppArmor for your sandbox::
$ sudo aa-enforce /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python
Tests
-----
In order to target the sandboxed Python environment(s) you have created on your
system, you must set the following environment variables for testing::
$ export CODEJAIL_TEST_USER=<owner of sandbox (usually 'sandbox')>
$ export CODEJAIL_TEST_VENV=<SANDENV>
Run the tests with the Makefile::
$ make tests
If CodeJail is running unsafely, many of the tests will be automatically
skipped, or will fail, depending on whether CodeJail thinks it should be in
safe mode or not.
Design
------
CodeJail is general-purpose enough that it can be used in a variety of projects
to run untrusted code. It provides two layers:
* ``jail_code.py`` offers secure execution of subprocesses. It does this by
running the program in a subprocess managed by AppArmor.
* ``safe_exec.py`` offers specialized handling of Python execution, using
jail_code to provide the semantics of Python's exec statement.
CodeJail runs programs under AppArmor. AppArmor is an OS-provided feature to
limit the resources programs can access. To run Python code with limited access
to resources, we make a new virtualenv, then name that Python executable in an
AppArmor profile, and restrict resources in that profile. CodeJail will
execute the provided Python program with that executable, and AppArmor will
automatically limit the resources it can access. CodeJail also uses setrlimit
to limit the amount of CPU time and/or memory available to the process.
``CodeJail.jail_code`` takes a program to run, files to copy into its
environment, command-line arguments, and a stdin stream. It creates a
temporary directory, creates or copies the needed files, spawns a subprocess to
run the code, and returns the output and exit status of the process.
``CodeJail.safe_exec`` emulates Python's exec statement. It takes a chunk of
Python code, and runs it using jail_code, modifying the globals dictionary as a
side-effect. safe_exec does this by serializing the globals into and out of
the subprocess as JSON.
Reporting Security Issues
-------------------------
Please do not report security issues in public. Please email security@openedx.org.
Raw data
{
"_id": null,
"home_page": "https://github.com/openedx/codejail",
"name": "edx-codejail",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "edx codejail",
"author": "edX",
"author_email": "oscm@edx.org",
"download_url": "https://files.pythonhosted.org/packages/33/1b/5a2ee4383dad219803bba6c92e4d1d547e92ec165f78cb7bdb29eaf19755/edx-codejail-3.5.2.tar.gz",
"platform": null,
"description": "CodeJail\n========\n\nCodeJail manages execution of untrusted code in secure sandboxes. It is\ndesigned primarily for Python execution, but can be used for other languages as\nwell.\n\nSecurity is enforced with AppArmor. If your operating system doesn't support\nAppArmor, then CodeJail won't protect the execution.\n\nCodeJail is designed to be configurable, and will auto-configure itself for\nPython execution if you install it properly. The configuration is designed to\nbe flexible: it can run in safe mode or unsafe mode. This helps support large\ndevelopment groups where only some of the developers are involved enough with\nsecure execution to configure AppArmor on their development machines.\n\nIf CodeJail is not configured for safe execution, it will execution Python\nusing the same API, but will not guard against malicious code. This allows the\nsame code to be used on safe-configured or non-safe-configured developer's\nmachines.\n\nA CodeJail sandbox consists of several pieces:\n\n#) Sandbox environment. For a Python setup, this would be Python and\n associated core packages. This is denoted throughout this document\n as **<SANDENV>**. This is read-only.\n\n#) Sandbox packages. These are additional packages needed for a given\n run. For example, this might be a grader written by an instructor\n to run over a student's code, or data that a student's code might\n need to access. This is denoted throughout this document as\n **<SANDPACK>**. This is read-only.\n\n#) Untrusted packages. This is typically the code submitted by the\n student to be tested on the server, as well as any data the code\n may need to modify. This is denoted throughout this document as\n **<UNTRUSTED_PACK>**. This is currently read-only, but may need to\n be read-write for some applications.\n\n#) OS packages. These are standard system libraries needed to run\n Python (e.g. things in /lib). This is denoted throughout this\n document as **<OSPACK>**. This is read-only, and is specified by\n Ubuntu's AppArmor profile.\n\nTo run, CodeJail requires two user accounts. One account is the main\naccount under which the code runs, which has access to create\nsandboxes. This will be referred to as **<SANDBOX_CALLER>**. The\nsecond account is the account under which the sandbox runs. This is\ntypically the account 'sandbox.'\n\nSupported Versions\n------------------\n\nThis library currently is tested to work with the following versions\n\nPython:\n\n* 3.11\n\nUbuntu:\n\n* 20.04\n* 22.04\n* 24.04\n\nInstallation\n------------\n\nThese instructions detail how to configure your operating system so that\nCodeJail can execute Python code safely. You can run CodeJail without these\nsteps, and you will have an unsafe CodeJail. This is fine for developers'\nmachines who are unconcerned with security, and simplifies the integration of\nCodeJail into your project.\n\nTo secure Python execution, you'll be creating a new virtualenv. This means\nyou'll have two: the main virtualenv for your project, and the new one for\nsandboxed Python code.\n\nChoose a place for the new virtualenv, call it **<SANDENV>**. It will be\nautomatically detected and used if you put it right alongside your existing\nvirtualenv, but with ``-sandbox`` appended. So if your existing virtualenv is in\n``/home/chris/ve/myproj``, make **<SANDENV>** be ``/home/chris/ve/myproj-sandbox``.\n\nThe user running the LMS is **<SANDBOX_CALLER>**, for example, you on\nyour dev machine, or ``www-data`` on a server.\n\nOther details here that depend on your configuration:\n\n1. Create the new virtualenv, using ``--copies`` so that there's a distinct Python executable to limit::\n\n $ sudo python3.8 -m venv --copies <SANDENV>\n\n By default, the virtualenv would just symlink against the system Python, and apparmor's default configuration on some operating systems may prevent confinement from being appled to that.\n\n2. (Optional) If you have particular packages you want available to your\n sandboxed code, install them by activating the sandbox virtual env, and\n using pip to install them::\n\n $ <SANDENV>/bin/pip install -r requirements/sandbox.txt\n\n3. Add a sandbox user::\n\n $ sudo addgroup sandbox\n $ sudo adduser --disabled-login sandbox --ingroup sandbox\n\n4. Let the web server run the sandboxed Python as sandbox. Create the file\n ``/etc/sudoers.d/01-sandbox``::\n\n $ sudo visudo -f /etc/sudoers.d/01-sandbox\n\n <SANDBOX_CALLER> ALL=(sandbox) SETENV:NOPASSWD:<SANDENV>/bin/python\n <SANDBOX_CALLER> ALL=(sandbox) SETENV:NOPASSWD:/usr/bin/find\n <SANDBOX_CALLER> ALL=(ALL) NOPASSWD:/usr/bin/pkill\n\n (Note that the ``find`` binary can run arbitrary code, so this is not a safe sudoers file for non-codejail purposes.)\n\n5. Edit an AppArmor profile. This is a text file specifying the limits on the\n sandboxed Python executable. The file must be in ``/etc/apparmor.d`` and must\n be named based on the executable, with slashes replaced by dots. For\n example, if your sandboxed Python is at ``/home/chris/ve/myproj-sandbox/bin/python``,\n then your AppArmor profile must be ``/etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python``::\n\n $ sudo vim /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python\n\n #include <tunables/global>\n\n <SANDENV>/bin/python {\n #include <abstractions/base>\n #include <abstractions/python>\n\n <CODEJAIL_CHECKOUT>/** mr,\n <SANDENV>/** mr,\n # If you have code that the sandbox must be able to access, add lines\n # pointing to those directories:\n /the/path/to/your/sandbox-packages/** r,\n\n /tmp/codejail-*/ rix,\n /tmp/codejail-*/** wrix,\n }\n\n Depending on your OS and AppArmor version you may need to specify a policy\n ABI to ensure the restrictions are being correctly applied. Modern ubuntu\n versions using AppArmor V3 should use the 3.0 ABI in order to enable\n network confinment rules. A profile using the ABI 3.0 would look as\n follows::\n\n $ sudo vim /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python\n\n abi <abi/3.0>,\n #include <tunables/global>\n\n <SANDENV>/bin/python {\n #include <abstractions/base>\n #include <abstractions/python>\n\n <CODEJAIL_CHECKOUT>/** mr,\n <SANDENV>/** mr,\n # If you have code that the sandbox must be able to access, add lines\n # pointing to those directories:\n /the/path/to/your/sandbox-packages/** r,\n\n /tmp/codejail-*/ rix,\n /tmp/codejail-*/** wrix,\n }\n\n You can also look at the\n ``apparmor-profiles/home.sandbox.codejail_sandbox-python3.bin.python-abi3``\n file which is used for testing for a full profile example.\n\n6. Parse the profiles::\n\n $ sudo apparmor_parser <APPARMOR_FILE>\n\n7. Reactivate your project's main virtualenv again.\n\n8. Disable using PAM to set rlimits::\n\n sed -i '/pam_limits.so/d' /etc/pam.d/sudo\n\nUsing CodeJail\n--------------\n\nIf your CodeJail is properly configured to use safe_exec, try these\ncommands at your Python terminal::\n\n import codejail.jail_code\n codejail.jail_code.configure('python', '<SANDENV>/bin/python', user='sandbox')\n import codejail.safe_exec\n jailed_globals = {}\n codejail.safe_exec.safe_exec(\"output=open('/etc/passwd').read()\", jailed_globals)\n print(jailed_globals) # should be unreachable if codejail is working properly\n\nThis should fail with an exception.\n\nIf you need to change the packages installed into your sandbox's virtualenv,\nyou'll need to disable AppArmor, because your sandboxed Python doesn't have\nthe rights to modify the files in its site-packages directory.\n\n1. Disable AppArmor for your sandbox::\n\n $ sudo apt-get install apparmor-utils # if you haven't already\n $ sudo aa-complain /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python\n\n2. Install or otherwise change the packages installed::\n\n $ pip install -r requirements/sandbox.txt\n\n3. Re-enable AppArmor for your sandbox::\n\n $ sudo aa-enforce /etc/apparmor.d/home.chris.ve.myproj-sandbox.bin.python\n\n\nTests\n-----\n\nIn order to target the sandboxed Python environment(s) you have created on your\nsystem, you must set the following environment variables for testing::\n\n $ export CODEJAIL_TEST_USER=<owner of sandbox (usually 'sandbox')>\n $ export CODEJAIL_TEST_VENV=<SANDENV>\n\nRun the tests with the Makefile::\n\n $ make tests\n\nIf CodeJail is running unsafely, many of the tests will be automatically\nskipped, or will fail, depending on whether CodeJail thinks it should be in\nsafe mode or not.\n\n\nDesign\n------\n\nCodeJail is general-purpose enough that it can be used in a variety of projects\nto run untrusted code. It provides two layers:\n\n* ``jail_code.py`` offers secure execution of subprocesses. It does this by\n running the program in a subprocess managed by AppArmor.\n\n* ``safe_exec.py`` offers specialized handling of Python execution, using\n jail_code to provide the semantics of Python's exec statement.\n\nCodeJail runs programs under AppArmor. AppArmor is an OS-provided feature to\nlimit the resources programs can access. To run Python code with limited access\nto resources, we make a new virtualenv, then name that Python executable in an\nAppArmor profile, and restrict resources in that profile. CodeJail will\nexecute the provided Python program with that executable, and AppArmor will\nautomatically limit the resources it can access. CodeJail also uses setrlimit\nto limit the amount of CPU time and/or memory available to the process.\n\n``CodeJail.jail_code`` takes a program to run, files to copy into its\nenvironment, command-line arguments, and a stdin stream. It creates a\ntemporary directory, creates or copies the needed files, spawns a subprocess to\nrun the code, and returns the output and exit status of the process.\n\n``CodeJail.safe_exec`` emulates Python's exec statement. It takes a chunk of\nPython code, and runs it using jail_code, modifying the globals dictionary as a\nside-effect. safe_exec does this by serializing the globals into and out of\nthe subprocess as JSON.\n\nReporting Security Issues\n-------------------------\n\nPlease do not report security issues in public. Please email security@openedx.org.\n",
"bugtrack_url": null,
"license": "Apache",
"summary": "CodeJail manages execution of untrusted code in secure sandboxes. It is designed primarily for Python execution, but can be used for other languages as well.",
"version": "3.5.2",
"project_urls": {
"Homepage": "https://github.com/openedx/codejail"
},
"split_keywords": [
"edx",
"codejail"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4db72ab3e47b138c9eec91e38a1726feab9f3cccc932b62350bef823cb6cdad0",
"md5": "43f030d60c289cd40adef52f4a9a6858",
"sha256": "3b7781d8827d124f7fb47f288434f4a22fb249bce30e1442a8174e8703a0f6e0"
},
"downloads": -1,
"filename": "edx_codejail-3.5.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "43f030d60c289cd40adef52f4a9a6858",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 24530,
"upload_time": "2024-11-07T17:21:39",
"upload_time_iso_8601": "2024-11-07T17:21:39.601149Z",
"url": "https://files.pythonhosted.org/packages/4d/b7/2ab3e47b138c9eec91e38a1726feab9f3cccc932b62350bef823cb6cdad0/edx_codejail-3.5.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "331b5a2ee4383dad219803bba6c92e4d1d547e92ec165f78cb7bdb29eaf19755",
"md5": "aa9aa1b5ebd7a7334677d014d5bca4c5",
"sha256": "686c3dd800caf9f115e266c3e4a6e3990e5e2156213a332dc0c262a6795395d3"
},
"downloads": -1,
"filename": "edx-codejail-3.5.2.tar.gz",
"has_sig": false,
"md5_digest": "aa9aa1b5ebd7a7334677d014d5bca4c5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 28513,
"upload_time": "2024-11-07T17:21:40",
"upload_time_iso_8601": "2024-11-07T17:21:40.867664Z",
"url": "https://files.pythonhosted.org/packages/33/1b/5a2ee4383dad219803bba6c92e4d1d547e92ec165f78cb7bdb29eaf19755/edx-codejail-3.5.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-07 17:21:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "openedx",
"github_project": "codejail",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "edx-codejail"
}