Expand description
A sandboxed process execution library for Linux and macOS.
This crate provides a Command API similar to std::process::Command but with
the ability to restrict process execution using platform-native sandboxing:
- Linux: Uses Landlock (requires kernel 5.13+)
- macOS: Uses the native sandbox framework
The API mirrors systemd’s execution environment options where applicable.
§Example
use std::path::Path;
use sandbox_run::{Command, Sandbox, ProtectSystem, ProtectHome};
// Create a sandbox with systemd-style options
let sandbox = Sandbox::new()
.protect_system(ProtectSystem::Strict)
.protect_home(ProtectHome::Yes)
.read_write_paths([Path::new("/tmp/myapp")])
.private_network(true)
.no_new_privileges(true)
.limit_nofile(256)
.build()
.unwrap();
// Run a command under the sandbox
let status = Command::new("myapp")
.sandbox(sandbox)
.status()
.expect("failed to execute process");§Systemd-Compatible Options
| Sandbox Method | systemd Directive |
|---|---|
protect_system() | ProtectSystem= |
protect_home() | ProtectHome= |
read_only_paths() | ReadOnlyPaths= |
read_write_paths() | ReadWritePaths= |
inaccessible_paths() | InaccessiblePaths= |
exec_paths() | ExecPaths= |
no_exec_paths() | NoExecPaths= |
private_network() | PrivateNetwork= |
no_new_privileges() | NoNewPrivileges= |
limit_nofile() | LimitNOFILE= |
limit_nproc() | LimitNPROC= |
limit_core() | LimitCORE= |
limit_fsize() | LimitFSIZE= |
§Platform Notes
§Linux
The Linux implementation uses Landlock, which is an allow-list based sandboxing
mechanism. ProtectSystem::Strict works best as it makes the entire filesystem
read-only and you can add exceptions with read_write_paths().
§macOS
The macOS implementation uses the native sandbox framework with generated profiles. All protection modes work well on macOS.
Structs§
- Command
- A sandboxed process builder, providing fine-grained control over how a new process should be spawned.
- Sandbox
- Builder for creating sandbox policies mirroring systemd.exec options.
- Sandbox
Policy - A compiled sandbox policy ready to be applied to a command.
Enums§
- Protect
Home - Protection level for home directories (systemd: ProtectHome=)
- Protect
System - Protection level for system directories (systemd: ProtectSystem=)
- Sandbox
Error
Functions§
- apply_
sandbox - Apply a compiled policy to the current process. Intended for
integrations that build their own
pre_execclosure (e.g. when running under a custom runtime that owns the child handle, liketokio::process::Command). Must be called in the child after fork and before exec. - landlock_
available - Whether the running kernel enforces Landlock (Linux 5.13+ with
Landlock enabled). Callers that fail open when sandboxing is
unavailable can use this to warn that spawned processes will run
unconfined.
Whether the running kernel enforces Landlock (Linux 5.13+ with
Landlock enabled). See
linux::landlock_available. Whether the running kernel actually enforces Landlock (Linux 5.13+ with Landlock enabled). This is a non-restricting probe: it builds a throwaway ruleset underHardRequirementcompatibility — which errors instead of silently downgrading when the feature is missing — and never callsrestrict_self, so the calling process is unaffected.