| Crates.io | sandbox-rs |
| lib.rs | sandbox-rs |
| version | 0.1.5 |
| created_at | 2025-11-25 06:06:23.9636+00 |
| updated_at | 2025-12-01 12:21:55.049217+00 |
| description | A comprehensive Rust sandbox implementation that provides process isolation, resource limiting, and syscall filtering for secure program execution. |
| homepage | https://github.com/ErickJ3/sandbox-rs |
| repository | https://github.com/ErickJ3/sandbox-rs |
| max_upload_size | |
| id | 1949238 |
| size | 304,201 |
⚠️ In active development, use with caution.
A comprehensive Rust sandbox implementation that provides process isolation, resource limiting, and syscall filtering for secure program execution.
sandbox-rs is a library and CLI tool for creating lightweight, secure sandboxes on Linux systems. It combines multiple isolation mechanisms—Linux namespaces, Seccomp BPF filtering, Cgroup v2 resource limits, and filesystem isolation—into a unified, easy-to-use interface.
Add to your Cargo.toml:
[dependencies]
sandbox-rs = "0.1"
One-shot execution:
use sandbox_rs::{SandboxBuilder, SeccompProfile};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sandbox = SandboxBuilder::new("my-sandbox")
.memory_limit_str("256M")?
.cpu_limit_percent(50)
.timeout(Duration::from_secs(30))
.seccomp_profile(SeccompProfile::IoHeavy)
.build()?;
let result = sandbox.run("/bin/echo", &["hello world"])?;
println!("Exit code: {}", result.exit_code);
println!("Memory peak: {} bytes", result.memory_peak);
println!("CPU time: {} μs", result.cpu_time_us);
Ok(())
}
Streaming output (real-time):
use sandbox_rs::{SandboxBuilder, StreamChunk};
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sandbox = SandboxBuilder::new("my-sandbox")
.memory_limit_str("256M")?
.cpu_limit_percent(50)
.timeout(Duration::from_secs(30))
.build()?;
let (result, stream) = sandbox.run_with_stream("/bin/echo", &["hello world"])?;
// Process output in real-time
for chunk in stream.into_iter() {
match chunk {
StreamChunk::Stdout(line) => println!("out: {}", line),
StreamChunk::Stderr(line) => eprintln!("err: {}", line),
StreamChunk::Exit { exit_code, signal } => {
println!("Exit code: {}", exit_code);
}
}
}
Ok(())
}
Native:
# Run program with sandbox
sandbox-ctl run --id test-run --memory 256M --cpu 50 --timeout 30 /bin/echo "hello world"
# List available seccomp profiles
sandbox-ctl profiles
# Check system requirements
sandbox-ctl check
Docker:
Run sandbox-ctl in a container without requiring root on the host machine:
# Build the Docker image
docker build -t sandbox-rs .
# Run sandbox-ctl with required permissions and cgroup access
docker run --privileged --cgroupns=host \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw \
sandbox-rs run \
--id test-run \
--memory 256M \
--cpu 50 \
--timeout 30 \
/bin/echo "hello world"
# Or use docker-compose (already configured)
docker-compose run sandbox-ctl run \
--id test-run \
--memory 256M \
--cpu 50 \
/bin/echo "hello world"
# Interactive mode
docker run -it --privileged --cgroupns=host \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw \
sandbox-rs --help
Note: The container requires:
--cgroupns=host) to manage cgroups/sys/fs/cgroup from host for resource limitingsandbox-rs is organized into modular layers:
Accepts human-readable formats:
100M - 100 megabytes1G - 1 gigabyteCPU quotas are enforced per sandbox:
cpu_limit_percent(50) → 50% of one CPU corecpu_quota(50000, 100000) → 50ms per 100ms periodFive builtin profiles control allowed syscalls:
This implementation provides defense-in-depth through multiple isolation layers. However:
cargo test
Tests are marked serial where required due to global state (root and cgroup manipulation).
See LICENSE file for details.