Boxed is a simple sandboxing solution to Python. It works by running arbitrary Python functions as an unprivileged user. It relies on the builtin Linux Kernel security, which is very good.
The API is very simple:
from boxed import run result = run(target_func, args=args, kwargs=kwargs)
This will spawn a Python interpreted that drops its privileges and runs target_func(*args, **kwds) as the nobody user. Communication between the master and slave processes can is carried by serialized streams which can use pickle, cloudpickle, dill or JSON.
There is a possible vulnerability if the target function outputs a picklable object that produces malicious side-effects when unpickled. We don’t know how to exploit this possibility, but it is theoretically possible. It is probably not even hard to exploit using dill or cloudpickle as the serializers since both can pickle functions by bytecode, opening many doors for attack.
The possibility of damage is greatly reduced by using a JSON serializer. The downside is that both the inputs and the outputs of the target function must be serializable as JSON. Besides that, the target function itself must be a Python callable living in a public namespace: the JSON serializer just sends the function qualified name to the sandbox. In order to use the JSON serializer, just pass the serializer=’json’ argument:
>>> from boxed import run >>> from math import sqrt >>> run(sqrt, args=(4,), serializer='json') 2.0
How does it work?
The sandbox is spawned by in a different Python interpreter called python_setuid which is created on installation. This is just a copy of the regular interpreter with the Linux SETUID capability enabled. This simple technique allows the executable to change its UID, which enable it to drop its privileges early during the sandbox execution.
This might remember the infaous SUID bit. SUID executables allows an user spawn program to start its life with super user permissions and (hopefully) to drop them as soon as possible while keeping only the permissions necessary for the program to run. The classical example is a webserver. Only the super user can listen to port 80 (or the other lower ports), thus the webserver must start its life as root and quickly drop all privileges but that of communicating in the desired ports.
Linux capabilities are a fine-grained version of the SUID bit. We can grant very specific privileges to any file. The boxed library uses a Python interpreter with the SETUID capability which grant us only the capability of changing the UID of a process. Even if the process escalates its privileges to run as UID=0 (the super user), none of the other permissions are granted so it will not gain super user powers. In particular, it will not be able to read, write or execute any file that the original user who executed the sandbox did not have access to.
By default, boxed runs the sandbox as the nobody user. Upon installation, we create a copy of the interpreter called /usr/bin/python_boxed and then apply the command:
$ setcap cap_setuid+ep /usr/bin/python_boxed
Does it work on Windows, OSX, BSD, etc?
No. This technique is linux-specific. Also, it is very difficult to provide a good, lightweight, and crossplatform sandboxing solution. Currently we have no plans to implement sandboxing in other platforms.
Metadata
Release files for boxed 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| boxed-0.2.0.tar.gz | 10.5 kB | Without provenance | Details for boxed-0.2.0.tar.gz |
Release files / boxed-0.2.0.tar.gz
| Download URL | boxed-0.2.0.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5fcb4767d8cec5842f18b275ec2e4939edf500584ac031c30b1abf1385c1bf11
|
|
BLAKE2b-256 checksum How to use checksums |
5d9f34408dc0cd1ff78c336f6cb41845dcb5e35ce6274d8140d1615d6f146f9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for boxed 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.